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
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ describe("POST /api/runtimes/[id]/talk/realtime/relay", () => {
result: {
delegated: true,
runId: "run_1",
finalText: "The repo is a CrewCMD app.",
result: { ok: true },
},
});
Expand Down
2 changes: 1 addition & 1 deletion src/app/api/runtimes/[id]/talk/realtime/relay/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ async function runRealtimeToolCall(
callId: params.callId,
result: { result: text },
});
return { delegated: true, runId, result };
return { delegated: true, runId, result, finalText: text };
} catch (error) {
const message = error instanceof Error ? error.message : "OpenClaw realtime tool call failed";
await client.realtimeRelayToolResult({
Expand Down
18 changes: 16 additions & 2 deletions src/lib/realtime-voice-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ export interface RealtimeRelayToolCall {
args?: unknown;
}

export interface RealtimeRelayToolCallResult {
delegated?: boolean;
runId?: string;
finalText?: string;
result?: unknown;
}

export async function startRealtimeVoiceSession(
request: RealtimeVoiceSessionRequest,
): Promise<RealtimeVoiceSession> {
Expand Down Expand Up @@ -114,11 +121,17 @@ export async function sendRealtimeRelayToolResult(
});
}

export async function sendRealtimeRelayToolCall(runtimeId: string, toolCall: RealtimeRelayToolCall): Promise<void> {
await postRealtimeRelay(runtimeId, {
export async function sendRealtimeRelayToolCall(
runtimeId: string,
toolCall: RealtimeRelayToolCall,
): Promise<RealtimeRelayToolCallResult> {
const data = await postRealtimeRelay(runtimeId, {
action: "toolCall",
...toolCall,
});
return data.result && typeof data.result === "object"
? data.result as RealtimeRelayToolCallResult
: {};
}

export async function stopRealtimeRelay(runtimeId: string, relaySessionId: string): Promise<void> {
Expand All @@ -145,6 +158,7 @@ async function postRealtimeRelay(runtimeId: string, body: Record<string, unknown
if (!response.ok) {
throw new Error(await readRealtimeVoiceError(response, "Realtime relay request failed"));
}
return await response.json().catch(() => ({})) as { result?: unknown };
}

async function readRealtimeVoiceError(response: Response, fallback: string) {
Expand Down
44 changes: 30 additions & 14 deletions src/lib/realtime-voice-gateway-relay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export class RealtimeGatewayRelaySession {
private cancelRequestedForPlayback = false;
private speechFramesDuringPlayback = 0;
private outputStartedAtMs: number | null = null;
private pendingToolCalls = 0;
private readonly bargeInProfile = resolveRealtimeBargeInProfile();

constructor(
Expand Down Expand Up @@ -229,6 +230,7 @@ export class RealtimeGatewayRelaySession {
return;
case "transcript":
if (event.role && event.text) {
if (event.role === "assistant" && this.pendingToolCalls > 0) return;
this.callbacks.onTranscript?.({
role: event.role,
text: event.text,
Expand Down Expand Up @@ -299,20 +301,34 @@ export class RealtimeGatewayRelaySession {
const name = event.name;

this.callbacks.onStatus?.("processing", "Consulting OpenClaw");
void sendRealtimeRelayToolCall(this.runtimeId, {
relaySessionId,
sessionKey,
callId,
name,
args: event.args ?? {},
}).catch((error) => {
const message = error instanceof Error ? error.message : String(error);
this.callbacks.onError?.(message);
void sendRealtimeRelayToolResult(this.runtimeId, relaySessionId, callId, {
error: message,
name,
}).catch(() => {});
});
this.pendingToolCalls += 1;
void (async () => {
try {
const result = await sendRealtimeRelayToolCall(this.runtimeId, {
relaySessionId,
sessionKey,
callId,
name,
args: event.args ?? {},
});
if (result.finalText?.trim()) {
this.callbacks.onTranscript?.({
role: "assistant",
text: result.finalText.trim(),
final: true,
});
}
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
this.callbacks.onError?.(message);
void sendRealtimeRelayToolResult(this.runtimeId, relaySessionId, callId, {
error: message,
name,
}).catch(() => {});
} finally {
this.pendingToolCalls = Math.max(0, this.pendingToolCalls - 1);
}
})();
}

private cancelOutputForBargeIn(): void {
Expand Down
Loading