Skip to content

Commit 2492eb5

Browse files
authored
Merge pull request #105 from anam-org/fix/remove-duplicate-stream
fix: remove duplicate stream
2 parents e6c3da0 + 2dfe884 commit 2492eb5

File tree

3 files changed

+49
-26
lines changed

3 files changed

+49
-26
lines changed

src/AnamClient.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
DEFAULT_ANAM_METRICS_BASE_URL,
55
ErrorCode,
66
setErrorMetricsBaseUrl,
7+
setCurrentSessionInfo,
78
} from './lib/ClientError';
89
import {
910
CoreApiRestClient,
@@ -34,6 +35,7 @@ export default class AnamClient {
3435
private inputAudioState: InputAudioState = { isMuted: false };
3536

3637
private sessionId: string | null = null;
38+
private organizationId: string | null = null;
3739

3840
private streamingClient: StreamingClient | null = null;
3941
private apiClient: CoreApiRestClient;
@@ -111,6 +113,9 @@ export default class AnamClient {
111113
// Validate persona configuration based on session token
112114
if (sessionToken) {
113115
const decodedToken = this.decodeJwt(sessionToken);
116+
this.organizationId = decodedToken.accountId;
117+
setCurrentSessionInfo(this.sessionId, this.organizationId);
118+
114119
const tokenType = decodedToken.type?.toLowerCase();
115120

116121
if (tokenType === 'legacy') {
@@ -218,11 +223,15 @@ export default class AnamClient {
218223
'Failed to initialize streaming client',
219224
ErrorCode.CLIENT_ERROR_CODE_SERVER_ERROR,
220225
500,
221-
{ cause: error instanceof Error ? error.message : String(error) },
226+
{
227+
cause: error instanceof Error ? error.message : String(error),
228+
sessionId,
229+
},
222230
);
223231
}
224232

225233
this.sessionId = sessionId;
234+
setCurrentSessionInfo(this.sessionId, this.organizationId);
226235
return sessionId;
227236
}
228237

@@ -290,6 +299,16 @@ export default class AnamClient {
290299
videoElementId: string,
291300
audioElementId: string,
292301
userProvidedAudioStream?: MediaStream,
302+
): Promise<void> {
303+
console.warn(
304+
'AnamClient: streamToVideoAndAudioElements is deprecated. Please use streamToVideoElement instead.',
305+
);
306+
await this.streamToVideoElement(videoElementId, userProvidedAudioStream);
307+
}
308+
309+
public async streamToVideoElement(
310+
videoElementId: string,
311+
userProvidedAudioStream?: MediaStream,
293312
): Promise<void> {
294313
if (this.clientOptions?.disableInputAudio && userProvidedAudioStream) {
295314
console.warn(
@@ -309,6 +328,7 @@ export default class AnamClient {
309328
500,
310329
{
311330
cause: error instanceof Error ? error.message : String(error),
331+
sessionId: this.sessionId,
312332
},
313333
);
314334
}
@@ -321,10 +341,7 @@ export default class AnamClient {
321341
throw new Error('Failed to stream: streaming client is not available');
322342
}
323343

324-
this.streamingClient.setMediaStreamTargetsById(
325-
videoElementId,
326-
audioElementId,
327-
);
344+
this.streamingClient.setMediaStreamTargetById(videoElementId);
328345
this.streamingClient.startConnection();
329346
}
330347

@@ -356,6 +373,7 @@ export default class AnamClient {
356373
this.streamingClient.stopConnection();
357374
this.streamingClient = null;
358375
this.sessionId = null;
376+
setCurrentSessionInfo(null, this.organizationId);
359377
this._isStreaming = false;
360378
}
361379
}

src/lib/ClientError.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ export const DEFAULT_ANAM_API_VERSION = '/v1';
1717
let anamCurrentBaseUrl = DEFAULT_ANAM_METRICS_BASE_URL;
1818
let anamCurrentApiVersion = DEFAULT_ANAM_API_VERSION;
1919

20+
let currentSessionId: string | null = null;
21+
let currentOrganizationId: string | null = null;
22+
2023
export const setErrorMetricsBaseUrl = (
2124
baseUrl: string,
2225
apiVersion: string = DEFAULT_ANAM_API_VERSION,
@@ -25,12 +28,33 @@ export const setErrorMetricsBaseUrl = (
2528
anamCurrentApiVersion = apiVersion;
2629
};
2730

31+
export const setCurrentSessionInfo = (
32+
sessionId: string | null,
33+
organizationId: string | null,
34+
) => {
35+
currentSessionId = sessionId;
36+
currentOrganizationId = organizationId;
37+
};
38+
2839
export const sendErrorMetric = async (
2940
name: string,
3041
value: string,
3142
tags?: Record<string, string | number>,
3243
) => {
3344
try {
45+
const metricTags: Record<string, string | number> = {
46+
...CLIENT_METADATA,
47+
...tags,
48+
};
49+
50+
// Add session and organization IDs if available
51+
if (currentSessionId) {
52+
metricTags.sessionId = currentSessionId;
53+
}
54+
if (currentOrganizationId) {
55+
metricTags.organizationId = currentOrganizationId;
56+
}
57+
3458
await fetch(
3559
`${anamCurrentBaseUrl}${anamCurrentApiVersion}/metrics/client`,
3660
{
@@ -41,10 +65,7 @@ export const sendErrorMetric = async (
4165
body: JSON.stringify({
4266
name,
4367
value,
44-
tags: {
45-
...CLIENT_METADATA,
46-
...tags,
47-
},
68+
tags: metricTags,
4869
}),
4970
},
5071
);

src/modules/StreamingClient.ts

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ export class StreamingClient {
3434
private dataChannel: RTCDataChannel | null = null;
3535
private videoElement: HTMLVideoElement | null = null;
3636
private videoStream: MediaStream | null = null;
37-
private audioElement: HTMLAudioElement | null = null;
3837
private audioStream: MediaStream | null = null;
3938
private inputAudioState: InputAudioState = { isMuted: false };
4039
private audioDeviceId: string | undefined;
@@ -155,10 +154,7 @@ export class StreamingClient {
155154
}
156155
}
157156

158-
public setMediaStreamTargetsById(
159-
videoElementId: string,
160-
audioElementId: string,
161-
) {
157+
public setMediaStreamTargetById(videoElementId: string) {
162158
// set up streaming targets
163159
if (videoElementId) {
164160
const videoElement = document.getElementById(videoElementId);
@@ -169,15 +165,6 @@ export class StreamingClient {
169165
}
170166
this.videoElement = videoElement as HTMLVideoElement;
171167
}
172-
if (audioElementId) {
173-
const audioElement = document.getElementById(audioElementId);
174-
if (!audioElement) {
175-
throw new Error(
176-
`StreamingClient: audio element with id ${audioElementId} not found`,
177-
);
178-
}
179-
this.audioElement = audioElement as HTMLAudioElement;
180-
}
181168
}
182169

183170
public startConnection() {
@@ -400,9 +387,6 @@ export class StreamingClient {
400387
AnamEvent.AUDIO_STREAM_STARTED,
401388
this.audioStream,
402389
);
403-
if (this.audioElement) {
404-
this.audioElement.srcObject = this.audioStream;
405-
}
406390
}
407391
}
408392
/**

0 commit comments

Comments
 (0)