Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ export class VoiceClientService extends Disposable implements IVoiceClientServic
private _gptLivePassiveSpeechStarted = false;
private _gptLiveDelegationId: string | undefined;
private _gptLiveInputTranscript = '';
private _gptLiveInputContext: string | undefined;
private readonly _gptLiveDelegations = new Set<string>();
private _gptLiveOutputTranscript = '';
private _gptLiveOutputStarted = false;
Expand All @@ -194,7 +195,7 @@ export class VoiceClientService extends Disposable implements IVoiceClientServic
private _gptLiveInputStartMs: number | undefined;
private _gptLiveInputLastStartMs: number | undefined;
private _gptLiveInputEndMs: number | undefined;
private readonly _gptLiveInputs: { text: string; target: IVoiceLiveInputTarget; turnId?: string; startMs?: number; lastStartMs?: number; endMs?: number; delegated: boolean; answeredByVoice: boolean }[] = [];
private readonly _gptLiveInputs: { text: string; context?: string; voiceResponse: string; target: IVoiceLiveInputTarget; turnId?: string; startMs?: number; lastStartMs?: number; endMs?: number; delegated: boolean; answeredByVoice: boolean }[] = [];
private _gptLiveContext: IVoiceSessionContext | undefined;
private _gptLiveInstructionsSent = false;
private readonly _gptLiveRequest = this._register(new MutableDisposable<CancellationTokenSource>());
Expand Down Expand Up @@ -745,6 +746,9 @@ export class VoiceClientService extends Disposable implements IVoiceClientServic
}
}
this._gptLiveInputTarget ??= this._gptLiveInputRouter.captureTarget();
if (!this._gptLiveInputTranscript) {
this._gptLiveInputContext = this._captureGptLiveVoiceContext(this._gptLiveInputTarget);
}
const delta = asOptionalString(event.delta) ?? '';
if (typeof event.start_ms === 'number' && Number.isFinite(event.start_ms)) {
this._gptLiveInputStartMs ??= event.start_ms;
Expand Down Expand Up @@ -780,11 +784,12 @@ export class VoiceClientService extends Disposable implements IVoiceClientServic
if (!this._gptLiveOutputStarted && !this._gptLiveInputTarget?.session?.pending) {
this._finalizeGptLiveInput();
}
const input = this._gptLiveInputs.findLast(candidate => !candidate.delegated && !candidate.target.session?.pending);
if (input) {
const input = this._gptLiveInputs.at(-1);
const delta = asOptionalString(event.delta) ?? '';
if (input && !input.target.session?.pending && !this._gptLiveOutputSuppressedUntilInput) {
input.answeredByVoice = true;
input.voiceResponse += delta;
}
const delta = asOptionalString(event.delta) ?? '';
this._gptLiveOutputTranscript += delta;
if (!this._gptLiveOutputStarted) {
if (!this._gptLiveOutputInterrupted && !this._gptLiveOutputSuppressedUntilInput) {
Expand Down Expand Up @@ -842,9 +847,18 @@ export class VoiceClientService extends Disposable implements IVoiceClientServic
break;
}
input.delegated = true;
input.answeredByVoice = false;
input.voiceResponse = '';
const result = this._gptLiveInputRouter.resolve(delegationId, input.text, input.target);
if (result.toolCall) {
this._onToolCall.fire({ ...result.toolCall, ...(input.turnId ? { turnId: input.turnId } : {}) });
const toolCall = result.toolCall;
this._onToolCall.fire({
...toolCall,
args: toolCall.name === 'send_to_chat' && input.context
? { ...toolCall.args, text: localize('voice.live.conversationContext', "Voice conversation context:\n{0}\n\nUser: {1}", input.context, input.text) }
: toolCall.args,
...(input.turnId ? { turnId: input.turnId } : {}),
});
} else {
this._sendGptLiveAppend('session.commentary.append', result.clarification, delegationId);
}
Expand All @@ -860,6 +874,22 @@ export class VoiceClientService extends Disposable implements IVoiceClientServic
}
}

private _captureGptLiveVoiceContext(target: IVoiceLiveInputTarget): string | undefined {
if (!target.hasContext || target.session?.pending) {
return undefined;
}
const turns: string[] = [];
for (let index = this._gptLiveInputs.length - 1; index >= 0; index--) {
const input = this._gptLiveInputs[index];
if (!input.answeredByVoice || !input.voiceResponse || input.target.session?.pending
|| input.target.hasContext !== target.hasContext || input.target.session?.id !== target.session?.id) {
break;
}
turns.push(localize('voice.live.conversationTurn', "User: {0}\nVoice assistant: {1}", input.text, input.voiceResponse));
}
return turns.length ? turns.reverse().join('\n\n') : undefined;
}

private _finalizeGptLiveInput(): void {
if (!this._gptLiveInputTranscript) {
return;
Expand All @@ -874,6 +904,8 @@ export class VoiceClientService extends Disposable implements IVoiceClientServic
}
this._gptLiveInputs.push({
text,
context: this._gptLiveInputContext,
voiceResponse: '',
target: this._gptLiveInputTarget ?? this._gptLiveInputRouter.captureTarget(),
turnId: this._gptLiveTurnId,
startMs: this._gptLiveInputStartMs,
Expand All @@ -886,6 +918,7 @@ export class VoiceClientService extends Disposable implements IVoiceClientServic
this._gptLiveInputs.shift();
}
this._gptLiveInputTranscript = '';
this._gptLiveInputContext = undefined;
this._gptLiveInputTarget = undefined;
this._gptLiveInputStartMs = undefined;
this._gptLiveInputLastStartMs = undefined;
Expand Down Expand Up @@ -1235,6 +1268,7 @@ export class VoiceClientService extends Disposable implements IVoiceClientServic
this._usingGptLive = false;
this._gptLiveSessionInitSent = false;
this._gptLiveTurnId = undefined;
this._gptLiveInputContext = undefined;
this._gptLivePassiveTurn = false;
this._gptLivePassiveSpeechStarted = false;
this._gptLiveDelegationId = undefined;
Expand Down Expand Up @@ -1650,6 +1684,9 @@ export class VoiceClientService extends Disposable implements IVoiceClientServic

prepareForInputTargetChange(): void {
if (this._usingGptLive) {
for (const input of this._gptLiveInputs) {
input.voiceResponse = '';
}
this._gptLiveOutputSuppressedUntilInput = true;
this._updateGptLiveAudioMuted();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5070,7 +5070,7 @@ export class VoiceSessionController extends Disposable implements IVoiceSessionC
}
return {
kind: 'question',
text: formatQuestionPrompt(question, pending.allow_skip === true),
text: formatQuestionPrompt(question, pending.allow_skip === true, pending.message),
pending: { pendingId: pending.pending_id },
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,12 @@ function resolveAnswer(question: IVoicePendingQuestion, text: string, language:
: undefined;
}

function questionPrompt(question: IVoicePendingQuestion, guidance: string): string {
function questionPrompt(question: IVoicePendingQuestion, guidance: string, context?: string): string {
const options = question.options.map((option, index) => `${index + 1}. ${option.label}`).join(' ');
return [localize('voice.live.questionPrompt', "{0} {1}", question.title, options).trim(), guidance].filter(Boolean).join(' ');
const prompt = context?.trim() && context.trim() !== question.title.trim()
? localize('voice.live.questionWithContext', "{0} {1}", context.trim(), question.title)
: question.title;
Comment thread
meganrogge marked this conversation as resolved.
Resolved
return [localize('voice.live.questionPrompt', "{0} {1}", prompt, options).trim(), guidance].filter(Boolean).join(' ');
}

/** Routes only explicit, deterministic replies to captured, still-current pending occurrences, never model guesses. Ambiguous replies cannot authorize actions, and forms submit only when complete or explicitly skipped. */
Expand Down Expand Up @@ -286,8 +289,9 @@ export class VoiceLiveInputRouter {

getQuestionPrompt(sessionId: string, pendingId: string): string | undefined {
const session = this._context.sessions.find(candidate => candidate.id === sessionId && candidate.pending?.pending_id === pendingId);
const question = session?.pending?.questions?.[this._answers.get(this._key(session))?.answers.length ?? 0];
return question ? questionPrompt(question, this.getQuestionReplyGuidance()) : undefined;
const index = session ? this._answers.get(this._key(session))?.answers.length ?? 0 : 0;
const question = session?.pending?.questions?.[index];
return question ? questionPrompt(question, this.getQuestionReplyGuidance(), index === 0 ? session?.pending?.message : undefined) : undefined;
}

captureTarget(): IVoiceLiveInputTarget {
Expand Down Expand Up @@ -354,8 +358,8 @@ export class VoiceLiveInputRouter {
if (!answer) {
return {
clarification: question.allow_freeform
? localize('voice.live.chooseOrCustomAnswer', "Please say an exact choice or its number, or say other followed by a custom answer. {0}", questionPrompt(question, this.getQuestionReplyGuidance()))
: localize('voice.live.chooseAnswer', "Please say an exact choice or its number. {0}", questionPrompt(question, this.getQuestionReplyGuidance()))
? localize('voice.live.chooseOrCustomAnswer', "Please say an exact choice or its number, or say other followed by a custom answer. {0}", questionPrompt(question, this.getQuestionReplyGuidance(), answers.length === 0 ? pending.message : undefined))
: localize('voice.live.chooseAnswer', "Please say an exact choice or its number. {0}", questionPrompt(question, this.getQuestionReplyGuidance(), answers.length === 0 ? pending.message : undefined))
};
}
answers.push(answer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,14 @@

import { IVoicePendingQuestion } from './voiceClientService.js';

/**
* Render the canonical spoken form of a pending question.
*
* Mirrors `format_question_prompt` in the voice backend's `session_pending.py`,
* against the same fixtures. The client speaks the first question of a form and
* the backend speaks the rest, so a divergence is audible mid-form.
*
* Renders the wire shape rather than the carousel, because that is where the
* ordinals the user says back were assigned. Numbering from a second source
* could read out a number that selects something else.
*/
export function formatQuestionPrompt(question: IVoicePendingQuestion, allowSkip: boolean): string {
/** Render the spoken question and choices, optionally preceded by form-level context. Choice ordinals follow the displayed order in the wire payload. */
export function formatQuestionPrompt(question: IVoicePendingQuestion, allowSkip: boolean, context?: string): string {
const parts: string[] = [];
if (context?.trim()) {
parts.push(context.trim());
}
const title = question.title.trim();
if (title) {
if (title && title !== context?.trim()) {
parts.push(title);
}
if (question.options.length > 0) {
Expand Down
Loading
Loading