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
5 changes: 5 additions & 0 deletions .changeset/cartesia-inference-timestamp-spacing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@livekit/agents': patch
---

Preserve spaces in Cartesia inference TTS aligned transcripts.
6 changes: 5 additions & 1 deletion agents/src/inference/tts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,11 @@ export class SynthesizeStream<TModel extends TTSModels> extends BaseSynthesizeSt
let aligned: TimedString[] = [];
if (serverEvent.words && serverEvent.words.length > 0) {
aligned = serverEvent.words.map((w) =>
createTimedString({ text: w.word, startTime: w.start, endTime: w.end }),
createTimedString({
text: provider === 'cartesia' ? `${w.word} ` : w.word,
startTime: w.start,
endTime: w.end,
}),
);
} else if (serverEvent.chars && serverEvent.chars.length > 0) {
aligned = serverEvent.chars.map((c) =>
Expand Down
68 changes: 68 additions & 0 deletions agents/src/inference/tts_alignment.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// SPDX-FileCopyrightText: 2026 LiveKit, Inc.
//
// SPDX-License-Identifier: Apache-2.0
import { once } from 'node:events';
import type { AddressInfo } from 'node:net';
import { expect, it } from 'vitest';
import { WebSocketServer } from 'ws';
import { SynthesizeStream } from '../tts/tts.js';
import { TTS } from './tts.js';

it('adds separators to Cartesia inference word timestamps', async () => {
const server = new WebSocketServer({ host: '127.0.0.1', port: 0 });
await once(server, 'listening');
const address = server.address() as AddressInfo;

server.on('connection', (socket) => {
socket.on('message', (raw) => {
const event = JSON.parse(raw.toString()) as { type: string };
if (event.type !== 'session.flush') return;

socket.send(
JSON.stringify({
type: 'output_alignment',
session_id: 'session-1',
words: [
{ word: 'hello', start: 0, end: 0.2 },
{ word: 'world.', start: 0.2, end: 0.4 },
],
}),
);
socket.send(
JSON.stringify({
type: 'output_audio',
session_id: 'session-1',
audio: Buffer.alloc(3200).toString('base64'),
}),
);
socket.send(JSON.stringify({ type: 'done', session_id: 'session-1' }));
});
});

const tts = new TTS({
model: 'cartesia/sonic-3',
modelOptions: { add_timestamps: true },
apiKey: 'test-key',
apiSecret: 'test-secret',
baseURL: `http://127.0.0.1:${address.port}`,
});

try {
const stream = tts.stream();
stream.pushText('hello world.');
stream.endInput();

const words: string[] = [];
for await (const event of stream) {
if (event !== SynthesizeStream.END_OF_STREAM) {
words.push(...(event.timedTranscripts ?? []).map((word) => word.text));
}
}

expect(words).toEqual(['hello ', 'world. ']);
} finally {
await tts.close();
for (const client of server.clients) client.close();
await new Promise<void>((resolve) => server.close(() => resolve()));
}
});
Loading