|
| 1 | +import { Effect, type Redacted, Schema } from "effect" |
| 2 | +import { HttpClientRequest } from "effect/http" |
| 3 | +import { RequestExecutor } from "../route/executor.js" |
| 4 | +import { AIError, HttpContext, InvalidProviderOutputError, InvalidRequestError } from "../schema/index.js" |
| 5 | +import { encodeJson } from "../utils/json.js" |
| 6 | + |
| 7 | +const OPENAI_BASE_URL = "https://api.openai.com/v1" |
| 8 | +const XAI_BASE_URL = "https://api.x.ai/v1" |
| 9 | + |
| 10 | +export type RealtimeTransportInput = |
| 11 | + | { readonly type: "webrtc"; readonly sdp: string; readonly dcid?: number } |
| 12 | + | { readonly type: "websocket" } |
| 13 | + |
| 14 | +export type RealtimeTransport = |
| 15 | + | { readonly type: "webrtc"; readonly sdp: string } |
| 16 | + | { readonly type: "websocket"; readonly url: string; readonly token: string; readonly expiresAt: number } |
| 17 | + |
| 18 | +export type RealtimeSession = Readonly<Record<string, unknown>> |
| 19 | + |
| 20 | +export interface RealtimeConnectInput { |
| 21 | + readonly model: string |
| 22 | + readonly apiKey: Redacted.Redacted |
| 23 | + readonly session?: RealtimeSession |
| 24 | + readonly transport: RealtimeTransportInput |
| 25 | +} |
| 26 | + |
| 27 | +const engines = [ |
| 28 | + { family: "gpt-live", id: "openai-live", provider: "openai" }, |
| 29 | + { family: "gpt-realtime", id: "openai-realtime", provider: "openai" }, |
| 30 | + { family: "grok-voice", id: "xai-realtime", provider: "xai" }, |
| 31 | +] as const |
| 32 | + |
| 33 | +// Matches the bare family name (`gpt-realtime`) and its versions (`gpt-realtime-2.1`, `gpt-realtime-mini`). |
| 34 | +export const engine = (model: string) => |
| 35 | + engines.find((item) => model === item.family || model.startsWith(`${item.family}-`)) |
| 36 | + |
| 37 | +export const connect = Effect.fn("Realtime.connect")(function* (input: RealtimeConnectInput) { |
| 38 | + const resolved = engine(input.model) |
| 39 | + if (!resolved) return yield* invalidRequest(`Unsupported voice model: ${input.model}`) |
| 40 | + const session = { ...input.session, model: input.model } |
| 41 | + |
| 42 | + switch (resolved.id) { |
| 43 | + case "openai-live": { |
| 44 | + if (input.transport.type !== "webrtc") |
| 45 | + return yield* invalidRequest("OpenAI GPT-Live only supports WebRTC transport") |
| 46 | + const data = yield* post( |
| 47 | + resolved.id, |
| 48 | + `${OPENAI_BASE_URL}/live/sessions`, |
| 49 | + input.apiKey, |
| 50 | + { session, transport: input.transport }, |
| 51 | + LiveSessionResponse, |
| 52 | + ) |
| 53 | + return { type: "webrtc" as const, sdp: data.transport.sdp } |
| 54 | + } |
| 55 | + case "openai-realtime": { |
| 56 | + if (input.transport.type === "websocket") |
| 57 | + return yield* clientSecret(resolved.id, OPENAI_BASE_URL, input.model, input.apiKey, session) |
| 58 | + const form = new FormData() |
| 59 | + form.append("sdp", input.transport.sdp) |
| 60 | + form.append("session", encodeJson(session)) |
| 61 | + const query = input.transport.dcid === undefined ? "" : `?dcid=${input.transport.dcid}` |
| 62 | + const sdp = yield* post( |
| 63 | + resolved.id, |
| 64 | + `${OPENAI_BASE_URL}/realtime/calls${query}`, |
| 65 | + input.apiKey, |
| 66 | + form, |
| 67 | + Schema.String, |
| 68 | + ) |
| 69 | + return { type: "webrtc" as const, sdp } |
| 70 | + } |
| 71 | + case "xai-realtime": { |
| 72 | + if (input.transport.type !== "websocket") |
| 73 | + return yield* invalidRequest("xAI Grok Voice only supports WebSocket transport") |
| 74 | + return yield* clientSecret(resolved.id, XAI_BASE_URL, input.model, input.apiKey) |
| 75 | + } |
| 76 | + } |
| 77 | +}) |
| 78 | + |
| 79 | +export const Realtime = { |
| 80 | + engine, |
| 81 | + connect, |
| 82 | +} as const |
| 83 | + |
| 84 | +const LiveSessionResponse = Schema.fromJsonString( |
| 85 | + Schema.Struct({ |
| 86 | + transport: Schema.Struct({ sdp: Schema.String }), |
| 87 | + }), |
| 88 | +) |
| 89 | + |
| 90 | +// xAI's ephemeral-token docs (https://docs.x.ai/developers/model-capabilities/audio/ephemeral-tokens) |
| 91 | +// document the request body (`{ expires_after: { seconds } }`) but not the response payload; we assume |
| 92 | +// it mirrors OpenAI's `{ value, expires_at }` response shape. |
| 93 | +const ClientSecretResponse = Schema.fromJsonString( |
| 94 | + Schema.Struct({ |
| 95 | + value: Schema.String, |
| 96 | + expires_at: Schema.Number, |
| 97 | + }), |
| 98 | +) |
| 99 | + |
| 100 | +const invalidRequest = (message: string) => new AIError({ reason: new InvalidRequestError({ message }) }) |
| 101 | + |
| 102 | +const post = <A>( |
| 103 | + route: string, |
| 104 | + url: string, |
| 105 | + apiKey: Redacted.Redacted, |
| 106 | + body: Record<string, unknown> | FormData, |
| 107 | + response: Schema.Codec<A, string>, |
| 108 | +) => |
| 109 | + Effect.gen(function* () { |
| 110 | + const executor = yield* RequestExecutor.Service |
| 111 | + const res = yield* executor.execute( |
| 112 | + HttpClientRequest.post(url).pipe( |
| 113 | + HttpClientRequest.bearerToken(apiKey), |
| 114 | + body instanceof FormData |
| 115 | + ? HttpClientRequest.bodyFormData(body) |
| 116 | + : HttpClientRequest.bodyText(encodeJson(body), "application/json"), |
| 117 | + ), |
| 118 | + ) |
| 119 | + const http = new HttpContext({ url: res.request.url, status: res.status, headers: res.headers }) |
| 120 | + const fail = (message: string, cause?: unknown, raw?: string) => |
| 121 | + new AIError({ reason: new InvalidProviderOutputError({ route, message, body: raw, http, cause }) }) |
| 122 | + const text = yield* res.text.pipe(Effect.mapError((cause) => fail(`Failed to read ${route} response`, cause))) |
| 123 | + return yield* Schema.decodeEffect(response)(text).pipe( |
| 124 | + Effect.mapError((cause) => fail(`${route} returned an invalid response`, cause, text)), |
| 125 | + ) |
| 126 | + }) |
| 127 | + |
| 128 | +const clientSecret = ( |
| 129 | + route: string, |
| 130 | + baseURL: string, |
| 131 | + model: string, |
| 132 | + apiKey: Redacted.Redacted, |
| 133 | + session?: Record<string, unknown>, |
| 134 | +) => |
| 135 | + post( |
| 136 | + route, |
| 137 | + `${baseURL}/realtime/client_secrets`, |
| 138 | + apiKey, |
| 139 | + { expires_after: { seconds: 60 }, session }, |
| 140 | + ClientSecretResponse, |
| 141 | + ).pipe( |
| 142 | + Effect.map( |
| 143 | + (data): RealtimeTransport => ({ |
| 144 | + type: "websocket", |
| 145 | + url: `${baseURL.replace(/^http/, "ws")}/realtime?model=${encodeURIComponent(model)}`, |
| 146 | + token: data.value, |
| 147 | + expiresAt: data.expires_at, |
| 148 | + }), |
| 149 | + ), |
| 150 | + ) |
0 commit comments