From 0c8ef19993fbbe709a8c40ea3ec48e151bff6a84 Mon Sep 17 00:00:00 2001
From: Oleksii Bieliaiev <121616408+o-bieliaiev@users.noreply.github.com>
Date: Thu, 15 Feb 2024 16:56:21 +0200
Subject: [PATCH] fix/pass ws url to socket manager (#11)
* passed wsURL to socketManager
* renamed package name
---
demo-app/app.tsx | 75 ++++++++++++++++------------
package.json | 4 +-
src/lib/connectToSocket.ts | 2 +-
src/lib/createAgentManager.ts | 16 +++---
src/types/entities/agents/manager.ts | 1 +
5 files changed, 54 insertions(+), 44 deletions(-)
diff --git a/demo-app/app.tsx b/demo-app/app.tsx
index 4dd371b..4d6e422 100644
--- a/demo-app/app.tsx
+++ b/demo-app/app.tsx
@@ -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) {
@@ -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);
@@ -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;
@@ -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");
@@ -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);
@@ -155,7 +164,9 @@ export function App() {
? 'Failed, try again'
: 'Connect'}
-
+
diff --git a/package.json b/package.json
index 6a6217d..6b35482 100644
--- a/package.json
+++ b/package.json
@@ -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": {
diff --git a/src/lib/connectToSocket.ts b/src/lib/connectToSocket.ts
index c647481..24d327b 100644
--- a/src/lib/connectToSocket.ts
+++ b/src/lib/connectToSocket.ts
@@ -80,8 +80,8 @@ async function connectWithRetries(options: Options): Promise {
export async function SocketManager(
auth: Auth,
+ host: string,
onMessage?: ChatProgressCallback,
- host: string = didSocketApiUrl
): Promise {
const messageCallbacks: ChatProgressCallback[] = onMessage ? [onMessage] : [];
const socket: WebSocket = await connectWithRetries({
diff --git a/src/lib/createAgentManager.ts b/src/lib/createAgentManager.ts
index 4d29591..1f27152 100644
--- a/src/lib/createAgentManager.ts
+++ b/src/lib/createAgentManager.ts
@@ -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) {
@@ -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,
});
}
- }
+ },
},
});
}
@@ -82,13 +79,14 @@ function initializeStreamAndChat(agent: Agent, options: AgentManagerOptions, age
*/
export async function createAgentManager(agentId: string, options: AgentManagerOptions): Promise {
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 {
@@ -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;
diff --git a/src/types/entities/agents/manager.ts b/src/types/entities/agents/manager.ts
index 2f4ce09..e2205ad 100644
--- a/src/types/entities/agents/manager.ts
+++ b/src/types/entities/agents/manager.ts
@@ -64,6 +64,7 @@ interface ManagerCallbacks {
export interface AgentManagerOptions {
callbacks: ManagerCallbacks;
baseURL?: string;
+ wsURL?: string;
debug?: boolean;
auth: Auth;
}