Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@d-id/client-sdk",
"private": false,
"version": "1.1.19",
"version": "1.1.20",
"type": "module",
"description": "d-id client sdk",
"repository": {
Expand Down
6 changes: 3 additions & 3 deletions src/api/streams/streamsApiV2.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Auth, CreateStreamV2Options, CreateStreamV2Response } from '@sdk/types';
import { Auth, CreateSessionV2Options, CreateSessionV2Response } from '@sdk/types';
import { createClient } from '../apiClient';

export function createStreamApiV2(
Expand All @@ -10,8 +10,8 @@ export function createStreamApiV2(
const client = createClient(auth, `${host}/v2/agents/${agentId}`, onError);

return {
async createStream(options: CreateStreamV2Options) {
return client.post<CreateStreamV2Response>('/streams', options);
async createStream(options: CreateSessionV2Options) {
return client.post<CreateSessionV2Response>('/sessions', options);
},
};
}
5 changes: 2 additions & 3 deletions src/services/agent-manager/connect-to-manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -525,10 +525,9 @@ describe('connect-to-manager', () => {
{
version: StreamApiVersion.V2,
transport_provider: TransportProvider.Livekit,
chat_id: 'chat-123',
},
expect.objectContaining({
chatId: 'chat-123',
expect.not.objectContaining({
chatId: expect.anything(),
})
);
});
Expand Down
45 changes: 25 additions & 20 deletions src/services/agent-manager/connect-to-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import {
ChatMode,
ChatProgressCallback,
ConnectionState,
CreateSessionV2Options,
CreateStreamOptions,
CreateStreamV2Options,
StreamEvents,
StreamType,
StreamingState,
Expand All @@ -26,10 +26,10 @@ import { Analytics } from '../analytics/mixpanel';
import { interruptTimestampTracker, latencyTimestampTracker } from '../analytics/timestamp-tracker';
import { createChat } from '../chat';

function getAgentStreamV2Options(options?: ConnectToManagerOptions): CreateStreamV2Options {
const ChatPrefix = 'cht';
function getAgentStreamV2Options(): CreateSessionV2Options {
return {
transport_provider: TransportProvider.Livekit,
chat_id: options?.chatId,
};
}

Expand All @@ -56,7 +56,7 @@ function getAgentStreamV1Options(options?: ConnectToManagerOptions): CreateStrea

function getAgentStreamOptions(agent: Agent, options?: ConnectToManagerOptions): ExtendedStreamOptions {
return isStreamsV2Agent(agent.presenter.type)
? { version: StreamApiVersion.V2, ...getAgentStreamV2Options(options) }
? { version: StreamApiVersion.V2, ...getAgentStreamV2Options() }
: { version: StreamApiVersion.V1, ...getAgentStreamV1Options(options) };
}

Expand Down Expand Up @@ -169,12 +169,12 @@ function connectToManager(
agent: Agent,
options: ConnectToManagerOptions,
analytics: Analytics
): Promise<StreamingManager<CreateStreamOptions | CreateStreamV2Options>> {
): Promise<StreamingManager<CreateStreamOptions | CreateSessionV2Options>> {
latencyTimestampTracker.reset();

return new Promise(async (resolve, reject) => {
try {
let streamingManager: StreamingManager<CreateStreamOptions | CreateStreamV2Options>;
let streamingManager: StreamingManager<CreateStreamOptions | CreateSessionV2Options>;
let shouldResolveOnComplete = false;

streamingManager = await createStreamingManager(agent, getAgentStreamOptions(agent, options), {
Expand Down Expand Up @@ -240,22 +240,27 @@ export async function initializeStreamAndChat(
agentsApi: AgentsAPI,
analytics: Analytics,
chat?: Chat
): Promise<{ chat?: Chat; streamingManager?: StreamingManager<CreateStreamOptions | CreateStreamV2Options> }> {
): Promise<{ chat?: Chat; streamingManager?: StreamingManager<CreateStreamOptions | CreateSessionV2Options> }> {
const resolveStreamAndChat = async () => {
if (isStreamsV2Agent(agent.presenter.type)) {
const chatResult = await createChat(
agent,
agentsApi,
analytics,
options.mode,
options.persistentChat,
chat
);
const streamingManager = await connectToManager(
agent,
{ ...options, chatId: chatResult.chat?.id },
analytics
);
const streamingManager = await connectToManager(agent, options, analytics);
const chatId = `${ChatPrefix}_${streamingManager.sessionId}`;
const now = new Date().toISOString();

const chatResult = {
chatMode: ChatMode.Functional,
chat: {
id: chatId,
agent_id: agent.id,
owner_id: agent.owner_id ?? '',
created: now,
modified: now,
agent_id__created_at: now,
agent_id__modified_at: now,
chat_mode: ChatMode.Functional,
messages: [],
},
};
return { chatResult, streamingManager };
} else {
const createChatPromise = createChat(
Expand Down
4 changes: 2 additions & 2 deletions src/services/streaming-manager/common.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CreateStreamOptions, CreateStreamV2Options, PayloadType, StreamType } from '@sdk/types';
import { CreateSessionV2Options, CreateStreamOptions, PayloadType, StreamType } from '@sdk/types';

export const createStreamingLogger = (debug: boolean, prefix: string) => (message: string, extra?: any) =>
debug && console.log(`[${prefix}] ${message}`, extra ?? '');
Expand All @@ -7,7 +7,7 @@ export const createStreamingLogger = (debug: boolean, prefix: string) => (messag
* Shared type for all streaming managers (LiveKit, WebRTC, etc.)
* This type represents the return value of any streaming manager implementation
*/
export type StreamingManager<T extends CreateStreamOptions | CreateStreamV2Options> = {
export type StreamingManager<T extends CreateStreamOptions | CreateSessionV2Options> = {
/**
* Method to send request to server to get clip or talk depending on payload
* @param payload The payload to send to the streaming service
Expand Down
6 changes: 3 additions & 3 deletions src/services/streaming-manager/factory.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AgentFactory, StreamingManagerOptionsFactory } from '../../test-utils/factories';
import {
CreateSessionV2Options,
CreateStreamOptions,
CreateStreamV2Options,
Providers,
StreamingManagerOptions,
TransportProvider,
Expand Down Expand Up @@ -80,9 +80,9 @@ describe('createStreamingManager', () => {
},
});

const v2StreamOptions: CreateStreamV2Options = {
const v2StreamOptions: CreateSessionV2Options = {
transport_provider: TransportProvider.Livekit,
chat_id: 'chat-123',
chat_persist: true,
};

await createStreamingManager(agent, { version: StreamApiVersion.V2, ...v2StreamOptions }, mockOptions);
Expand Down
6 changes: 3 additions & 3 deletions src/services/streaming-manager/factory.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
Agent,
CreateSessionV2Options,
CreateStreamOptions,
CreateStreamV2Options,
StreamingManagerOptions,
TransportProvider,
} from '@sdk/types';
Expand All @@ -15,13 +15,13 @@ export enum StreamApiVersion {

export type ExtendedStreamOptions =
| ({ version: StreamApiVersion.V1 } & CreateStreamOptions)
| ({ version: StreamApiVersion.V2 } & CreateStreamV2Options);
| ({ version: StreamApiVersion.V2 } & CreateSessionV2Options);
Comment thread
ariksfaradi marked this conversation as resolved.

export async function createStreamingManager(
agent: Agent,
streamOptions: ExtendedStreamOptions,
options: StreamingManagerOptions
): Promise<StreamingManager<CreateStreamOptions | CreateStreamV2Options>> {
): Promise<StreamingManager<CreateStreamOptions | CreateSessionV2Options>> {
const agentId = agent.id;

switch (streamOptions.version) {
Expand Down
39 changes: 15 additions & 24 deletions src/services/streaming-manager/livekit-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import {
AgentActivityState,
ConnectionState,
ConnectivityState,
CreateSessionV2Options,
CreateStreamOptions,
CreateStreamV2Options,
PayloadType,
StreamEvents,
StreamingManagerOptions,
Expand Down Expand Up @@ -65,29 +65,22 @@ export function handleInitError(
log('Failed to connect to LiveKit room:', error);
markInitialConnectionDone();
callbacks.onConnectionStateChange?.(ConnectionState.Fail);
callbacks.onError?.(error as Error, { streamId: '' });
callbacks.onError?.(error as Error, { sessionId: '' });
throw error;
}

export async function createLiveKitStreamingManager<T extends CreateStreamV2Options>(
export async function createLiveKitStreamingManager<T extends CreateSessionV2Options>(
agentId: string,
agent: T,
sessionOptions: CreateSessionV2Options,
options: StreamingManagerOptions
): Promise<StreamingManager<T>> {
const log = createStreamingLogger(options.debug || false, 'LiveKitStreamingManager');

const {
Room,
RoomEvent,
ConnectionState: LiveKitConnectionState,
RemoteParticipant,
RemoteTrack,
} = await importLiveKit();
const { Room, RoomEvent, ConnectionState: LiveKitConnectionState } = await importLiveKit();

const { callbacks, auth, baseURL, analytics } = options;
let room: Room | null = null;
let isConnected = false;
let videoId: string | null = null;
const streamType = StreamType.Fluent;
let isInitialConnection = true;
let sharedMediaStream: MediaStream | null = null;
Expand All @@ -98,7 +91,6 @@ export async function createLiveKitStreamingManager<T extends CreateStreamV2Opti
});

const streamApi = createStreamApiV2(auth, baseURL || didApiUrl, agentId, callbacks.onError);
let streamId: string | undefined;
let sessionId: string | undefined;

let token: string | undefined;
Expand All @@ -107,13 +99,12 @@ export async function createLiveKitStreamingManager<T extends CreateStreamV2Opti
try {
const streamResponse = await streamApi.createStream({
transport_provider: TransportProvider.Livekit,
chat_id: agent.chat_id,
chat_persist: sessionOptions.chat_persist ?? true,
});

const { session_id, session_token, session_url } = streamResponse;
callbacks.onStreamCreated?.({ stream_id: session_id, session_id, agent_id: agentId });
streamId = session_id;
sessionId = session_id;
const { id, session_token, session_url } = streamResponse;
callbacks.onStreamCreated?.({ session_id: id, stream_id: id, agent_id: agentId });
sessionId = id;
token = session_token;
url = session_url;

Expand All @@ -124,7 +115,7 @@ export async function createLiveKitStreamingManager<T extends CreateStreamV2Opti
});
}

if (!url || !token || !streamId || !sessionId) {
if (!url || !token || !sessionId) {
return Promise.reject(new Error('Failed to initialize LiveKit stream'));
}

Expand Down Expand Up @@ -263,12 +254,12 @@ export async function createLiveKitStreamingManager<T extends CreateStreamV2Opti

function handleMediaDevicesError(error: Error): void {
log('Media devices error:', error);
callbacks.onError?.(new Error(internalErrorMassage), { streamId });
callbacks.onError?.(new Error(internalErrorMassage), { sessionId });
}

function handleEncryptionError(error: Error): void {
log('Encryption error:', error);
callbacks.onError?.(new Error(internalErrorMassage), { streamId });
callbacks.onError?.(new Error(internalErrorMassage), { sessionId });
}

function handleTrackSubscriptionFailed(
Expand All @@ -290,7 +281,7 @@ export async function createLiveKitStreamingManager<T extends CreateStreamV2Opti
if (!isConnected || !room) {
log('Room is not connected for sending messages');
callbacks.onError?.(new Error(internalErrorMassage), {
streamId,
sessionId,
});
return;
}
Expand All @@ -302,7 +293,7 @@ export async function createLiveKitStreamingManager<T extends CreateStreamV2Opti
log('Message sent successfully:', message);
} catch (error) {
log('Failed to send message:', error);
callbacks.onError?.(new Error(internalErrorMassage), { streamId });
callbacks.onError?.(new Error(internalErrorMassage), { sessionId });
}
}

Expand Down Expand Up @@ -331,7 +322,7 @@ export async function createLiveKitStreamingManager<T extends CreateStreamV2Opti
sendTextMessage,

sessionId,
streamId,
streamId: sessionId,
streamType,
interruptAvailable: true,
triggersAvailable: false,
Expand Down
13 changes: 6 additions & 7 deletions src/types/stream/streams-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ export enum TransportProvider {
Livekit = 'livekit',
}

export interface CreateStreamV2Options {
export interface CreateSessionV2Options {
transport_provider: TransportProvider.Livekit;
chat_id?: string;
chat_persist?: boolean;
}

export interface CreateStreamV2Response {
agent_id: 'string';
session_id: 'string';
session_url: 'string';
session_token: 'string';
export interface CreateSessionV2Response {
id: string;
session_url: string;
session_token: string;
}