feat(vela): start mocked response flow after push-to-talk commit

This commit is contained in:
2026-04-08 21:20:17 +02:00
parent 98bcc543f5
commit 28712443cc
8 changed files with 284 additions and 25 deletions

View File

@@ -67,21 +67,23 @@ function scheduleMockedTurnStep(session, turnId, delay, callback) {
session.mockedTurnTimers.push(timer);
}
function startMockedTurn(socket, session) {
function startMockedTurn(socket, session, { transcript = MOCKED_USER_TRANSCRIPT, includeListeningState = true } = {}) {
if (session.mockedTurnInFlight) {
sendSocketError(socket, 'mocked_turn_in_flight', 'Only one mocked turn can run per session at a time.');
return;
}
clearMockedTurn(session);
session.audioChunkCount = 0;
session.mockedTurnInFlight = true;
const turnId = crypto.randomUUID();
session.activeMockedTurnId = turnId;
updateSessionState(socket, session, 'listening');
if (includeListeningState) {
updateSessionState(socket, session, 'listening');
}
scheduleMockedTurnStep(session, turnId, 75, () => {
sendSocketMessage(socket, 'transcript.final', { text: MOCKED_USER_TRANSCRIPT });
sendSocketMessage(socket, 'transcript.final', { text: transcript });
updateSessionState(socket, session, 'thinking');
});
@@ -264,11 +266,12 @@ function handleClientMessage(socket, session, rawMessage) {
break;
}
sendSocketMessage(socket, 'transcript.final', {
text: createPlaceholderFinalTranscript(session.audioChunkCount)
});
const finalTranscript = createPlaceholderFinalTranscript(session.audioChunkCount);
session.audioChunkCount = 0;
updateSessionState(socket, session, 'idle');
startMockedTurn(socket, session, {
transcript: finalTranscript,
includeListeningState: false
});
break;
case 'response.cancel':
clearMockedTurn(session);

View File

@@ -303,6 +303,26 @@ test('websocket handles valid and invalid client messages safely', async () => {
type: 'transcript.final',
payload: { text: '[mocked final] Placeholder push-to-talk transcript completed from 1 appended chunk.' }
});
assert.deepEqual(await client.nextMessage(), {
type: 'session.state',
payload: { value: 'thinking' }
});
assert.deepEqual(await client.nextMessage(), {
type: 'session.state',
payload: { value: 'speaking' }
});
assert.deepEqual(await client.nextMessage(), {
type: 'response.text.delta',
payload: { text: '[mocked assistant] ' }
});
assert.deepEqual(await client.nextMessage(), {
type: 'response.text.delta',
payload: { text: 'This is a deterministic mocked response from the gateway vertical slice.' }
});
assert.deepEqual(await client.nextMessage(), {
type: 'response.completed',
payload: {}
});
assert.deepEqual(await client.nextMessage(), {
type: 'session.state',
payload: { value: 'idle' }
@@ -358,6 +378,26 @@ test('websocket accepts a placeholder input cycle before a mocked turn on the sa
type: 'transcript.final',
payload: { text: '[mocked final] Placeholder push-to-talk transcript completed from 1 appended chunk.' }
});
assert.deepEqual(await client.nextMessage(), {
type: 'session.state',
payload: { value: 'thinking' }
});
assert.deepEqual(await client.nextMessage(), {
type: 'session.state',
payload: { value: 'speaking' }
});
assert.deepEqual(await client.nextMessage(), {
type: 'response.text.delta',
payload: { text: '[mocked assistant] ' }
});
assert.deepEqual(await client.nextMessage(), {
type: 'response.text.delta',
payload: { text: 'This is a deterministic mocked response from the gateway vertical slice.' }
});
assert.deepEqual(await client.nextMessage(), {
type: 'response.completed',
payload: {}
});
assert.deepEqual(await client.nextMessage(), {
type: 'session.state',
payload: { value: 'idle' }
@@ -404,6 +444,37 @@ test('websocket emits deterministic partials for repeated appends and a determin
type: 'transcript.final',
payload: { text: '[mocked final] Placeholder push-to-talk transcript completed from 2 appended chunks.' }
});
assert.deepEqual(await client.nextMessage(), {
type: 'session.state',
payload: { value: 'thinking' }
});
client.sendJson({ type: 'input_audio.commit', payload: {} });
assert.deepEqual(await client.nextMessage(), {
type: 'error',
payload: {
code: 'mocked_turn_in_flight',
message: 'Wait for the mocked turn to finish before committing input.',
retryable: true
}
});
assert.deepEqual(await client.nextMessage(), {
type: 'session.state',
payload: { value: 'speaking' }
});
assert.deepEqual(await client.nextMessage(), {
type: 'response.text.delta',
payload: { text: '[mocked assistant] ' }
});
assert.deepEqual(await client.nextMessage(), {
type: 'response.text.delta',
payload: { text: 'This is a deterministic mocked response from the gateway vertical slice.' }
});
assert.deepEqual(await client.nextMessage(), {
type: 'response.completed',
payload: {}
});
assert.deepEqual(await client.nextMessage(), {
type: 'session.state',
payload: { value: 'idle' }
@@ -414,7 +485,30 @@ test('websocket emits deterministic partials for repeated appends and a determin
type: 'transcript.final',
payload: { text: '[mocked final] Placeholder push-to-talk transcript completed without appended audio.' }
});
await assert.rejects(() => client.nextMessage(150), /timed out waiting for websocket message/);
assert.deepEqual(await client.nextMessage(), {
type: 'session.state',
payload: { value: 'thinking' }
});
assert.deepEqual(await client.nextMessage(), {
type: 'session.state',
payload: { value: 'speaking' }
});
assert.deepEqual(await client.nextMessage(), {
type: 'response.text.delta',
payload: { text: '[mocked assistant] ' }
});
assert.deepEqual(await client.nextMessage(), {
type: 'response.text.delta',
payload: { text: 'This is a deterministic mocked response from the gateway vertical slice.' }
});
assert.deepEqual(await client.nextMessage(), {
type: 'response.completed',
payload: {}
});
assert.deepEqual(await client.nextMessage(), {
type: 'session.state',
payload: { value: 'idle' }
});
await client.close();
} finally {
@@ -542,6 +636,53 @@ test('websocket cancel stops an active mocked turn and allows a new one without
}
});
test('websocket cancel stops a push-to-talk commit response and allows another turn', async () => {
const server = await startServer();
try {
const client = await connectWebSocket(server.port);
await client.nextMessage();
await client.nextMessage();
client.sendJson({ type: 'input_audio.append', payload: { chunk: 'chunk-1' } });
assert.deepEqual(await client.nextMessage(), {
type: 'session.state',
payload: { value: 'listening' }
});
assert.deepEqual(await client.nextMessage(), {
type: 'transcript.partial',
payload: { text: '[mocked partial] Placeholder push-to-talk transcript in progress.' }
});
client.sendJson({ type: 'input_audio.commit', payload: {} });
assert.deepEqual(await client.nextMessage(), {
type: 'transcript.final',
payload: { text: '[mocked final] Placeholder push-to-talk transcript completed from 1 appended chunk.' }
});
assert.deepEqual(await client.nextMessage(), {
type: 'session.state',
payload: { value: 'thinking' }
});
client.sendJson({ type: 'response.cancel', payload: {} });
assert.deepEqual(await client.nextMessage(), {
type: 'session.state',
payload: { value: 'idle' }
});
await assert.rejects(() => client.nextMessage(150), /timed out waiting for websocket message/);
client.sendJson({ type: 'mocked.turn.trigger', payload: {} });
assert.deepEqual(await client.nextMessage(), {
type: 'session.state',
payload: { value: 'listening' }
});
await client.close();
} finally {
await server.close();
}
});
test('websocket safely accepts cancel when no turn is active', async () => {
const server = await startServer();