@inworld/web-core
Version:
149 lines (148 loc) • 4.92 kB
JavaScript
import { SCENE_HAS_INVALID_FORMAT, STOP_DURATION_NATURAL_NUMBER, STOP_TICKS_NATURAL_NUMBER, } from '../common/errors.js';
import { GrpcAudioPlayback } from '../components/sound/grpc_audio.playback.js';
import { GrpcAudioRecorder } from '../components/sound/grpc_audio.recorder.js';
import { GrpcWebRtcLoopbackBiDiSession } from '../components/sound/grpc_web_rtc_loopback_bidi.session.js';
import { SessionContinuation, } from '../entities/continuation/session_continuation.entity.js';
import { isNaturalNumber } from '../guard/number.js';
import { sceneHasValidFormat } from '../guard/scene.js';
import { ConnectionService } from '../services/connection.service.js';
import { InworldConnectionService } from '../services/inworld_connection.service.js';
export class InworldClient {
constructor() {
this.scene = '';
this.config = {};
}
setUser(user) {
this.user = user;
return this;
}
setClient(client) {
this.client = {
id: client.id,
};
return this;
}
setGenerateSessionToken(generateSessionToken) {
this.generateSessionToken = generateSessionToken;
return this;
}
setConfiguration(config) {
this.config = config;
const { capabilities } = this.config;
if ((capabilities === null || capabilities === void 0 ? void 0 : capabilities.logs) !== undefined) {
console.warn('logs capability is deprecated. Please use logsDebug, logsInfo, logsWarning instead');
}
return this;
}
setScene(name) {
this.scene = name;
return this;
}
setOnDisconnect(fn) {
this.onDisconnect = fn;
return this;
}
setOnError(fn) {
this.onError = fn;
return this;
}
setOnWarning(fn) {
this.onWarning = fn;
return this;
}
setOnMessage(fn) {
this.onMessage = fn;
return this;
}
setOnReady(fn) {
this.onReady = fn;
return this;
}
setOnHistoryChange(fn) {
this.onHistoryChange = fn;
return this;
}
setOnInterruption(fn) {
this.onInterruption = fn;
return this;
}
setOnPhoneme(fn) {
this.onPhoneme = fn;
return this;
}
setOnAfterPlaying(fn) {
this.onAfterPlaying = fn;
return this;
}
setOnBeforePlaying(fn) {
this.onBeforePlaying = fn;
return this;
}
setOnStopPlaying(fn) {
this.onStopPlaying = fn;
return this;
}
setExtension(extension) {
this.extension = extension;
return this;
}
setSessionContinuation(sessionContinuation) {
this.sessionContinuation = new SessionContinuation(sessionContinuation);
return this;
}
build() {
this.validate();
const webRtcLoopbackBiDiSession = new GrpcWebRtcLoopbackBiDiSession();
const grpcAudioRecorder = new GrpcAudioRecorder();
const grpcAudioPlayer = new GrpcAudioPlayback({
audioPlaybackConfig: this.config.audioPlayback,
onAfterPlaying: this.onAfterPlaying,
onBeforePlaying: this.onBeforePlaying,
onStopPlaying: this.onStopPlaying,
onPhoneme: this.onPhoneme,
});
const connection = new ConnectionService({
config: this.config,
grpcAudioPlayer,
webRtcLoopbackBiDiSession,
name: this.scene,
user: this.user,
client: this.client,
onError: this.onError,
onReady: this.onReady,
onMessage: this.onMessage,
onWarning: this.onWarning,
onHistoryChange: this.onHistoryChange,
onDisconnect: this.onDisconnect,
onInterruption: this.onInterruption,
generateSessionToken: this.generateSessionToken,
sessionContinuation: this.sessionContinuation,
extension: this.extension,
});
const inworldConnection = new InworldConnectionService({
connection,
grpcAudioPlayer,
grpcAudioRecorder,
webRtcLoopbackBiDiSession,
});
inworldConnection.clearState();
return inworldConnection;
}
validate() {
if (!this.scene) {
throw Error('Scene name is required');
}
if (!sceneHasValidFormat(this.scene)) {
throw Error(SCENE_HAS_INVALID_FORMAT);
}
const { audioPlayback } = this.config;
if (audioPlayback === null || audioPlayback === void 0 ? void 0 : audioPlayback.stop) {
if (!isNaturalNumber(audioPlayback.stop.duration)) {
throw Error(STOP_DURATION_NATURAL_NUMBER);
}
if (!isNaturalNumber(audioPlayback.stop.ticks)) {
throw Error(STOP_TICKS_NATURAL_NUMBER);
}
}
}
}