Skip to content

Commit

Permalink
fix/pass ws url to socket manager (#11)
Browse files Browse the repository at this point in the history
* passed wsURL to socketManager

* renamed package name
  • Loading branch information
obieliaiev authored Feb 15, 2024
1 parent 30a8b1e commit 0c8ef19
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 44 deletions.
75 changes: 43 additions & 32 deletions demo-app/app.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
// TODO delete this app before launch
import { useEffect, useRef, useState } from 'preact/hooks';
import {
Agent,
AgentManager,
Auth,
ChatProgress,
ClipStreamOptions,
CreateStreamOptions,
StreamingManager,
StreamingState,
VideoType,
createAgentManager,
} from '../src/types/index';
import './app.css';
import { clientKey, didApiUrl, agentId } from './environment';
import { Agent, Auth, ClipStreamOptions, CreateStreamOptions, StreamingManager, StreamingState, VideoType, createAgentManager, createStreamingManager, AgentManager, ChatProgress } from '../src/types/index';
import { agentId, clientKey, didApiUrl, didSocketApiUrl } from './environment';

function getAgentStreamArgs(agent: Agent): CreateStreamOptions {
if (agent.presenter?.type === VideoType.Clip) {
Expand Down Expand Up @@ -41,7 +52,7 @@ export function App() {
// createAgentsApi(auth, 'https://api-dev.d-id.com').getById(agentId).then(setAgent);
}, [auth]);

const onConnectionStateChange = function(state) {
const onConnectionStateChange = function (state) {
console.log('state callabck', state);
if (state === 'connected') {
setStreamState(State.Connected);
Expand All @@ -56,9 +67,9 @@ export function App() {
} else if (state === 'disconnected') {
setStreamState(State.New);
}
}
};

const onVideoStateChange = function(state, stats) {
const onVideoStateChange = function (state, stats) {
setStreamState(streamState => {
if (streamState === State.Speaking) {
return state === StreamingState.Stop ? State.Connected : State.Speaking;
Expand All @@ -69,17 +80,17 @@ export function App() {
if (state === StreamingState.Stop && stats) {
console.log('Video stats', stats);
}
}
};

const onChatEvents = function(event, data) {
const onChatEvents = function (event, data) {
if (event === ChatProgress.Partial) {
setAnswer(answer => answer + data.content);
} else if (event === ChatProgress.Answer) {
setAnswer(data.content);
}
}
};

const callbacks ={
const callbacks = {
onSrcObjectReady(value) {
if (!videoRef.current) {
throw new Error("Couldn't find video ref");
Expand All @@ -89,43 +100,41 @@ export function App() {
},
onConnectionStateChange,
onVideoStateChange,
onChatEvents
}
onChatEvents,
};

async function onClick() {
if (!agentAPI) {
const agentAPI: AgentManager = await createAgentManager(agentId, {callbacks, baseURL: didApiUrl, auth} )
setAgentAPI(agentAPI)
}
else if(text) {
console.log('ws url', didSocketApiUrl)
const agentAPI: AgentManager = await createAgentManager(agentId, {
callbacks,
baseURL: didApiUrl,
auth,
wsURL: didSocketApiUrl,
});
setAgentAPI(agentAPI);
} else if (text) {
setStreamState(State.Speaking);
try {
agentAPI.speak({
type: 'text',
provider: agentAPI.agent.presenter.voice,
input: text,
})
} catch(e) {
console.error(e)
type: 'text',
provider: agentAPI.agent.presenter.voice,
input: text,
});
} catch (e) {
console.error(e);
setStreamState(State.Fail);
}

}
}

async function onChat() {
console.log("on chat")
const newMessages: any[] = [
{ role: 'user', content: text.trim(), created_at: new Date().toISOString() },
];
const response = agentAPI?.chat(
newMessages
)
console.log(response)
const newMessages: any[] = [{ role: 'user', content: text.trim(), created_at: new Date().toISOString() }];
const response = agentAPI?.chat(newMessages);
}

function terminate() {
agentAPI?.terminate()
agentAPI?.terminate();
// rtcConnection?.terminate();
setRtcConnection(null);
setStreamState(State.New);
Expand Down Expand Up @@ -155,7 +164,9 @@ export function App() {
? 'Failed, try again'
: 'Connect'}
</button>
<button onClick={onChat} disabled={streamState !== State.Connected}>Send to chat text</button>
<button onClick={onChat} disabled={streamState !== State.Connected}>
Send to chat text
</button>
<button onClick={terminate} disabled={streamState !== State.Connected}>
Close connection
</button>
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@d-id/client-sdk",
"name": "@d-id/agents-sdk",
"private": false,
"version": "1.0.19",
"version": "0.0.1",
"type": "module",
"description": "d-id client sdk",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/connectToSocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ async function connectWithRetries(options: Options): Promise<WebSocket> {

export async function SocketManager(
auth: Auth,
host: string,
onMessage?: ChatProgressCallback,
host: string = didSocketApiUrl
): Promise<SocketManager> {
const messageCallbacks: ChatProgressCallback[] = onMessage ? [onMessage] : [];
const socket: WebSocket = await connectWithRetries({
Expand Down
16 changes: 7 additions & 9 deletions src/lib/createAgentManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,17 @@ import {
AgentManagerOptions,
AgentsAPI,
Chat,
ChatProgressCallback,
ConnectionStateChangeCallback,
CreateStreamOptions,
Message,
RatingPayload,
SupportedStreamScipt,
VideoStateChangeCallback,
VideoType,
} from '$/types/index';
import { ChatProgress, StreamEvents, StreamingManager, createKnowledgeApi, createStreamingManager } from '..';
import { createAgentsApi } from './api/agents';
import { createRatingsApi } from './api/ratings';
import { SocketManager } from './connectToSocket';
import { didApiUrl } from './environment';
import { didApiUrl, didSocketApiUrl } from './environment';

export function getAgentStreamArgs(agent: Agent): CreateStreamOptions {
if (agent.presenter.type === VideoType.Clip) {
Expand Down Expand Up @@ -54,14 +51,14 @@ function initializeStreamAndChat(agent: Agent, options: AgentManagerOptions, age
},
// TODO remove when webscoket will return partial
onMessage: (event, data) => {
if(event === StreamEvents.ChatPartial) {
if (event === StreamEvents.ChatPartial) {
// Mock ws event result to remove in future
options.callbacks.onChatEvents?.(ChatProgress.Partial, {
content: data,
event: ChatProgress.Partial
event: ChatProgress.Partial,
});
}
}
},
},
});
}
Expand All @@ -82,13 +79,14 @@ function initializeStreamAndChat(agent: Agent, options: AgentManagerOptions, age
*/
export async function createAgentManager(agentId: string, options: AgentManagerOptions): Promise<AgentManager> {
const baseURL = options.baseURL || didApiUrl;
const wsURL = options.wsURL || didSocketApiUrl;
const abortController: AbortController = new AbortController();
const agentsApi = createAgentsApi(options.auth, baseURL);
const ratingsAPI = createRatingsApi(options.auth, baseURL);
const knowledgeApi = createKnowledgeApi(options.auth, baseURL);

const agent = await agentsApi.getById(agentId);
const socketManager = await SocketManager(options.auth, options.callbacks.onChatEvents);
const socketManager = await SocketManager(options.auth, wsURL, options.callbacks.onChatEvents,);
let { chat, streamingManager } = await initializeStreamAndChat(agent, options, agentsApi);

return {
Expand Down Expand Up @@ -124,7 +122,7 @@ export async function createAgentManager(agentId: string, options: AgentManagerO
return ratingsAPI.create(payload);
},
deleteRate(id: string) {
return ratingsAPI.delete(id)
return ratingsAPI.delete(id);
},
speak(payload: SupportedStreamScipt) {
let completePayload;
Expand Down
1 change: 1 addition & 0 deletions src/types/entities/agents/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ interface ManagerCallbacks {
export interface AgentManagerOptions {
callbacks: ManagerCallbacks;
baseURL?: string;
wsURL?: string;
debug?: boolean;
auth: Auth;
}
Expand Down

0 comments on commit 0c8ef19

Please sign in to comment.