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
10 changes: 8 additions & 2 deletions packages/sdk/src/realtime/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ export type RealTimeClient = {
on: <K extends keyof Events>(event: K, listener: (data: Events[K]) => void) => void;
off: <K extends keyof Events>(event: K, listener: (data: Events[K]) => void) => void;
sessionId: string;
setImage: (image: Blob | File | string | null, options?: { prompt?: string; enhance?: boolean }) => Promise<void>;
setImage: (
image: Blob | File | string | null,
options?: { prompt?: string; enhance?: boolean; timeout?: number },
) => Promise<void>;
// live_avatar audio method (only available when model is live_avatar and no stream is provided)
playAudio?: (audio: Blob | File | ArrayBuffer) => Promise<void>;
};
Expand Down Expand Up @@ -167,7 +170,10 @@ export const createRealTimeClient = (opts: RealTimeClientOptions) => {
on: eventEmitter.on,
off: eventEmitter.off,
sessionId,
setImage: async (image: Blob | File | string | null, options?: { prompt?: string; enhance?: boolean }) => {
setImage: async (
image: Blob | File | string | null,
options?: { prompt?: string; enhance?: boolean; timeout?: number },
) => {
if (image === null) {
return webrtcManager.setImage(null, options);
}
Expand Down
7 changes: 5 additions & 2 deletions packages/sdk/src/realtime/webrtc-connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,15 @@ export class WebRTCConnection {
* Pass null to clear the reference image or use a placeholder.
* Optionally include a prompt to send with the image.
*/
async setImageBase64(imageBase64: string | null, options?: { prompt?: string; enhance?: boolean }): Promise<void> {
async setImageBase64(
imageBase64: string | null,
options?: { prompt?: string; enhance?: boolean; timeout?: number },
): Promise<void> {
return new Promise((resolve, reject) => {
const timeoutId = setTimeout(() => {
this.websocketMessagesEmitter.off("setImageAck", listener);
reject(new Error("Image send timed out"));
}, AVATAR_SETUP_TIMEOUT_MS);
}, options?.timeout ?? AVATAR_SETUP_TIMEOUT_MS);

const listener = (msg: SetImageAckMessage) => {
clearTimeout(timeoutId);
Expand Down
5 changes: 4 additions & 1 deletion packages/sdk/src/realtime/webrtc-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ export class WebRTCManager {
return this.connection.websocketMessagesEmitter;
}

setImage(imageBase64: string | null, options?: { prompt?: string; enhance?: boolean }): Promise<void> {
setImage(
imageBase64: string | null,
options?: { prompt?: string; enhance?: boolean; timeout?: number },
): Promise<void> {
return this.connection.setImageBase64(imageBase64, options);
}
}
63 changes: 62 additions & 1 deletion packages/sdk/tests/unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { HttpResponse, http } from "msw";
import { setupServer } from "msw/node";
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it } from "vitest";
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
import { createDecartClient, models } from "../src/index.js";

const MOCK_RESPONSE_DATA = new Uint8Array([0x00, 0x01, 0x02]).buffer;
Expand Down Expand Up @@ -943,6 +943,67 @@ describe("Lucy 14b realtime", () => {
});
});

describe("WebRTCConnection", () => {
describe("setImageBase64 timeout", () => {
beforeEach(() => {
vi.useFakeTimers();
});

afterEach(() => {
vi.useRealTimers();
});

it("uses custom timeout when provided", async () => {
const { WebRTCConnection } = await import("../src/realtime/webrtc-connection.js");
const connection = new WebRTCConnection();

const customTimeout = 5000;
let rejected = false;
let rejectionError: Error | null = null;

const promise = connection.setImageBase64("base64data", { timeout: customTimeout }).catch((err) => {
rejected = true;
rejectionError = err;
});

// Advance time to just before the custom timeout - should not have rejected yet
await vi.advanceTimersByTimeAsync(customTimeout - 1);
expect(rejected).toBe(false);

// Advance past the custom timeout - now it should reject
await vi.advanceTimersByTimeAsync(2);
await promise;

expect(rejected).toBe(true);
expect(rejectionError?.message).toBe("Image send timed out");
});

it("uses default timeout (15000ms) when not provided", async () => {
const { WebRTCConnection } = await import("../src/realtime/webrtc-connection.js");
const connection = new WebRTCConnection();

let rejected = false;
let rejectionError: Error | null = null;

const promise = connection.setImageBase64("base64data").catch((err) => {
rejected = true;
rejectionError = err;
});

// Advance to just before the default timeout (15000ms) - should not reject yet
await vi.advanceTimersByTimeAsync(14999);
expect(rejected).toBe(false);

// Now advance past the default timeout
await vi.advanceTimersByTimeAsync(2);
await promise;

expect(rejected).toBe(true);
expect(rejectionError?.message).toBe("Image send timed out");
});
});
});

describe("live_avatar Model", () => {
describe("Model Definition", () => {
it("has correct model name", () => {
Expand Down
Loading