|
6 | 6 | } from "openclaw/plugin-sdk/proxy-capture"; |
7 | 7 | import type { |
8 | 8 | RealtimeVoiceBridge, |
| 9 | + RealtimeVoiceBrowserSession, |
| 10 | + RealtimeVoiceBrowserSessionCreateRequest, |
9 | 11 | RealtimeVoiceBridgeCreateRequest, |
10 | 12 | RealtimeVoiceProviderConfig, |
11 | 13 | RealtimeVoiceProviderPlugin, |
@@ -59,6 +61,8 @@ type OpenAIRealtimeVoiceBridgeConfig = RealtimeVoiceBridgeCreateRequest & { |
59 | 61 | azureApiVersion?: string; |
60 | 62 | }; |
61 | 63 |
|
| 64 | +const OPENAI_REALTIME_DEFAULT_MODEL = "gpt-realtime-1.5"; |
| 65 | + |
62 | 66 | type RealtimeEvent = { |
63 | 67 | type: string; |
64 | 68 | delta?: string; |
@@ -117,7 +121,7 @@ function base64ToBuffer(b64: string): Buffer { |
117 | 121 | } |
118 | 122 |
|
119 | 123 | class OpenAIRealtimeVoiceBridge implements RealtimeVoiceBridge { |
120 | | - private static readonly DEFAULT_MODEL = "gpt-realtime-1.5"; |
| 124 | + private static readonly DEFAULT_MODEL = OPENAI_REALTIME_DEFAULT_MODEL; |
121 | 125 | private static readonly MAX_RECONNECT_ATTEMPTS = 5; |
122 | 126 | private static readonly BASE_RECONNECT_DELAY_MS = 1000; |
123 | 127 | private static readonly CONNECT_TIMEOUT_MS = 10_000; |
@@ -579,6 +583,77 @@ class OpenAIRealtimeVoiceBridge implements RealtimeVoiceBridge { |
579 | 583 | } |
580 | 584 | } |
581 | 585 |
|
| 586 | +function readStringField(value: unknown, key: string): string | undefined { |
| 587 | + if (!value || typeof value !== "object") { |
| 588 | + return undefined; |
| 589 | + } |
| 590 | + const raw = (value as Record<string, unknown>)[key]; |
| 591 | + return typeof raw === "string" && raw.trim() ? raw.trim() : undefined; |
| 592 | +} |
| 593 | + |
| 594 | +async function createOpenAIRealtimeBrowserSession( |
| 595 | + req: RealtimeVoiceBrowserSessionCreateRequest, |
| 596 | +): Promise<RealtimeVoiceBrowserSession> { |
| 597 | + const config = normalizeProviderConfig(req.providerConfig); |
| 598 | + const apiKey = config.apiKey || process.env.OPENAI_API_KEY; |
| 599 | + if (!apiKey) { |
| 600 | + throw new Error("OpenAI API key missing"); |
| 601 | + } |
| 602 | + if (config.azureEndpoint || config.azureDeployment) { |
| 603 | + throw new Error("OpenAI Realtime browser sessions do not support Azure endpoints yet"); |
| 604 | + } |
| 605 | + |
| 606 | + const model = req.model ?? config.model ?? OPENAI_REALTIME_DEFAULT_MODEL; |
| 607 | + const voice = (req.voice ?? config.voice ?? "alloy") as OpenAIRealtimeVoice; |
| 608 | + const session: Record<string, unknown> = { |
| 609 | + type: "realtime", |
| 610 | + model, |
| 611 | + instructions: req.instructions, |
| 612 | + audio: { |
| 613 | + output: { voice }, |
| 614 | + }, |
| 615 | + }; |
| 616 | + if (req.tools && req.tools.length > 0) { |
| 617 | + session.tools = req.tools; |
| 618 | + session.tool_choice = "auto"; |
| 619 | + } |
| 620 | + |
| 621 | + const response = await fetch("https://api.openai.com/v1/realtime/client_secrets", { |
| 622 | + method: "POST", |
| 623 | + headers: { |
| 624 | + Authorization: `Bearer ${apiKey}`, |
| 625 | + "Content-Type": "application/json", |
| 626 | + }, |
| 627 | + body: JSON.stringify({ session }), |
| 628 | + }); |
| 629 | + if (!response.ok) { |
| 630 | + const detail = await response.text().catch(() => ""); |
| 631 | + throw new Error( |
| 632 | + `OpenAI Realtime browser session failed (${response.status}): ${detail || response.statusText}`, |
| 633 | + ); |
| 634 | + } |
| 635 | + const payload = (await response.json()) as unknown; |
| 636 | + const nestedSecret = |
| 637 | + payload && typeof payload === "object" |
| 638 | + ? (payload as Record<string, unknown>).client_secret |
| 639 | + : undefined; |
| 640 | + const clientSecret = readStringField(payload, "value") ?? readStringField(nestedSecret, "value"); |
| 641 | + if (!clientSecret) { |
| 642 | + throw new Error("OpenAI Realtime browser session did not return a client secret"); |
| 643 | + } |
| 644 | + const expiresAt = |
| 645 | + payload && typeof payload === "object" |
| 646 | + ? (payload as Record<string, unknown>).expires_at |
| 647 | + : undefined; |
| 648 | + return { |
| 649 | + provider: "openai", |
| 650 | + clientSecret, |
| 651 | + model, |
| 652 | + voice, |
| 653 | + ...(typeof expiresAt === "number" ? { expiresAt } : {}), |
| 654 | + }; |
| 655 | +} |
| 656 | + |
582 | 657 | export function buildOpenAIRealtimeVoiceProvider(): RealtimeVoiceProviderPlugin { |
583 | 658 | return { |
584 | 659 | id: "openai", |
@@ -607,6 +682,7 @@ export function buildOpenAIRealtimeVoiceProvider(): RealtimeVoiceProviderPlugin |
607 | 682 | azureApiVersion: config.azureApiVersion, |
608 | 683 | }); |
609 | 684 | }, |
| 685 | + createBrowserSession: createOpenAIRealtimeBrowserSession, |
610 | 686 | }; |
611 | 687 | } |
612 | 688 |
|
|
0 commit comments