@effect-uai/google 0.18.0 → 0.19.0-dev-20261110065930
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/codec.d.mts +28 -7
- package/dist/codec.d.mts.map +1 -1
- package/dist/codec.mjs +52 -10
- package/dist/codec.mjs.map +1 -1
- package/dist/codec.test.d.mts +1 -0
- package/dist/codec.test.mjs +61 -0
- package/dist/codec.test.mjs.map +1 -0
- package/dist/geminiTools.test.mjs +1 -0
- package/dist/geminiTools.test.mjs.map +1 -1
- package/dist/realtimeSession.mjs +2 -2
- package/dist/realtimeSession.mjs.map +1 -1
- package/dist/realtimeSession.test.mjs +67 -36
- package/dist/realtimeSession.test.mjs.map +1 -1
- package/package.json +4 -4
- package/src/codec.test.ts +54 -0
- package/src/codec.ts +90 -21
- package/src/geminiTools.test.ts +1 -0
- package/src/realtimeSession.test.ts +5 -5
- package/src/realtimeSession.ts +2 -2
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"realtimeSession.mjs","names":[],"sources":["../src/realtimeSession.ts"],"sourcesContent":["/**\n * Gemini Live speech-to-speech over `BidiGenerateContent`.\n *\n * Auth is a query parameter, so this needs no `ws` package and no headers:\n * the platform `WebSocket` reaches it from Node, Bun, Deno and the browser.\n *\n * Two shapes differ from OpenAI Realtime and drive most of this file. There\n * are no response ids on the wire, so a turn id is minted here and every event\n * of that turn carries it; and there is no truncate op, so a\n * `PlaybackPosition` has nothing to send.\n */\nimport {\n Cause,\n Duration,\n Effect,\n Match,\n Pull,\n Queue,\n Redacted,\n Ref,\n Result,\n Schema,\n type Scope,\n Stream,\n} from \"effect\"\nimport { Base64 } from \"effect/encoding\"\nimport * as Socket from \"effect/socket/Socket\"\nimport * as AiError from \"@effect-uai/core/AiError\"\nimport type { AudioFormat } from \"@effect-uai/core/Audio\"\nimport * as Capabilities from \"@effect-uai/core/Capabilities\"\nimport type { HistoryItem, Usage } from \"@effect-uai/core/Items\"\nimport {\n type CommonSessionRequest,\n RealtimeEvent,\n type RealtimeInput,\n} from \"@effect-uai/core/Realtime\"\nimport type { RealtimeSessionHandle } from \"@effect-uai/core/RealtimeSession\"\nimport * as WebSocketSession from \"@effect-uai/core/WebSocketSession\"\nimport { parsedResponse, toolDescriptorsToTools } from \"./codec.js\"\nimport type { GeminiLiveModel } from \"./models.js\"\n\nconst PROVIDER = \"gemini-live\"\n\nconst DEFAULT_BASE_URL = \"wss://generativelanguage.googleapis.com\"\nconst RPC = \"ws/google.ai.generativelanguage.v1beta.GenerativeService.BidiGenerateContent\"\n\nexport type Config = {\n readonly apiKey: Redacted.Redacted\n readonly baseUrl?: string\n /** Build the socket yourself, for a proxy or an in-memory transport. */\n readonly webSocket?: Socket.WebSocketConstructor[\"Service\"]\n}\n\n/**\n * Gemini-typed session request. Everything past `CommonSessionRequest` is\n * provider-shaped. The configuration is immutable for the connection: changing\n * any of it means opening a new session.\n */\nexport type GeminiLiveRequest = Omit<CommonSessionRequest, \"model\"> & {\n readonly model: GeminiLiveModel\n /** Replaces the plain `turnDetection` switch when you need the knobs. */\n readonly vad?: {\n readonly startSensitivity?: \"high\" | \"low\"\n readonly endSensitivity?: \"high\" | \"low\"\n readonly prefixPaddingMs?: number\n readonly silenceDurationMs?: number\n }\n /** `\"none\"` turns barge-in off. Defaults to interrupting. */\n readonly activityHandling?: \"interrupt\" | \"none\"\n /**\n * When a tool result reaches the model, for the models that keep generating\n * while a tool runs. Defaults to `\"when-idle\"`: the server's own default is\n * to interrupt, which cuts off whatever the model was saying while it\n * waited, including the \"let me look that up\" it just spoke. `\"interrupt\"`\n * is right when the result cannot wait, `\"silent\"` when it is context the\n * model should have rather than something to announce.\n */\n readonly toolScheduling?: \"when-idle\" | \"interrupt\" | \"silent\"\n readonly turnCoverage?: \"activity\" | \"all\" | \"audioActivityAndAllVideo\"\n /** 3.1 only. Higher levels delay the first audio. */\n readonly thinkingLevel?: \"minimal\" | \"low\" | \"medium\" | \"high\"\n readonly mediaResolution?: \"low\" | \"medium\" | \"high\"\n readonly languageCode?: string\n readonly temperature?: number\n readonly maxOutputTokens?: number\n /** What turns an otherwise time-boxed session into an unbounded one. */\n readonly contextCompression?: {\n readonly triggerTokens?: number\n readonly targetTokens?: number\n }\n /** Grounding. Cannot be combined with `tools`; the pair is a 400. */\n readonly googleSearch?: boolean\n /** 2.5 only. */\n readonly proactiveAudio?: boolean\n /** 2.5 only. */\n readonly affectiveDialog?: boolean\n}\n\n// ---------------------------------------------------------------------------\n// AudioFormat -> wire\n// ---------------------------------------------------------------------------\n\n/** Output is 24 kHz whatever the input rate, and cannot be configured. */\nconst OUTPUT_RATE = 24000\n\nconst unsupportedFormat = (format: AudioFormat, field: string, expected: string) =>\n new AiError.Unsupported({\n provider: PROVIDER,\n capability: field,\n reason: `Gemini Live accepts ${expected}. Got ${JSON.stringify(format)}.`,\n })\n\n/** Any rate is accepted on input; 16 kHz is what the model works in. */\nconst inputRate = (format: AudioFormat): Effect.Effect<number, AiError.AiError> =>\n Match.value(format).pipe(\n Match.when({ container: \"raw\", encoding: \"pcm_s16le\", channels: 1 }, (f) =>\n Effect.succeed(f.sampleRate),\n ),\n Match.orElse((f) =>\n Effect.fail(unsupportedFormat(f, \"inputFormat\", \"mono pcm_s16le at any rate\")),\n ),\n )\n\nconst checkOutputFormat = (format: AudioFormat): Effect.Effect<void, AiError.AiError> =>\n Match.value(format).pipe(\n Match.when(\n { container: \"raw\", encoding: \"pcm_s16le\", sampleRate: OUTPUT_RATE, channels: 1 },\n () => Effect.void,\n ),\n Match.orElse((f) =>\n Effect.fail(unsupportedFormat(f, \"outputFormat\", `mono pcm_s16le at ${OUTPUT_RATE}`)),\n ),\n )\n\n// ---------------------------------------------------------------------------\n// Client frames\n// ---------------------------------------------------------------------------\n\nconst SENSITIVITY = { high: \"HIGH\", low: \"LOW\" } as const\n\nconst TURN_COVERAGE = {\n activity: \"TURN_INCLUDES_ONLY_ACTIVITY\",\n all: \"TURN_INCLUDES_ALL_INPUT\",\n audioActivityAndAllVideo: \"TURN_INCLUDES_AUDIO_ACTIVITY_AND_ALL_VIDEO\",\n} as const\n\nconst realtimeInputConfig = (request: GeminiLiveRequest) => ({\n automaticActivityDetection: {\n ...(request.turnDetection === \"manual\" && { disabled: true }),\n ...(request.vad?.startSensitivity !== undefined && {\n startOfSpeechSensitivity: `START_SENSITIVITY_${SENSITIVITY[request.vad.startSensitivity]}`,\n }),\n ...(request.vad?.endSensitivity !== undefined && {\n endOfSpeechSensitivity: `END_SENSITIVITY_${SENSITIVITY[request.vad.endSensitivity]}`,\n }),\n ...(request.vad?.prefixPaddingMs !== undefined && {\n prefixPaddingMs: request.vad.prefixPaddingMs,\n }),\n ...(request.vad?.silenceDurationMs !== undefined && {\n silenceDurationMs: request.vad.silenceDurationMs,\n }),\n },\n ...(request.activityHandling !== undefined && {\n activityHandling:\n request.activityHandling === \"none\" ? \"NO_INTERRUPTION\" : \"START_OF_ACTIVITY_INTERRUPTS\",\n }),\n ...(request.turnCoverage !== undefined && { turnCoverage: TURN_COVERAGE[request.turnCoverage] }),\n})\n\nconst generationConfig = (request: GeminiLiveRequest) => ({\n responseModalities: [\"AUDIO\"],\n ...(request.voiceId !== undefined && {\n speechConfig: {\n voiceConfig: { prebuiltVoiceConfig: { voiceName: request.voiceId } },\n ...(request.languageCode !== undefined && { languageCode: request.languageCode }),\n },\n }),\n ...(request.temperature !== undefined && { temperature: request.temperature }),\n ...(request.maxOutputTokens !== undefined && { maxOutputTokens: request.maxOutputTokens }),\n ...(request.mediaResolution !== undefined && {\n mediaResolution: `MEDIA_RESOLUTION_${request.mediaResolution.toUpperCase()}`,\n }),\n ...(request.thinkingLevel !== undefined && {\n thinkingConfig: { thinkingLevel: request.thinkingLevel.toUpperCase() },\n }),\n ...(request.affectiveDialog === true && { enableAffectiveDialog: true }),\n})\n\nconst toolsOf = (request: GeminiLiveRequest) =>\n request.googleSearch === true\n ? [{ googleSearch: {} }]\n : toolDescriptorsToTools(request.tools ?? [])\n\nconst setupFrame = (request: GeminiLiveRequest): string => {\n const tools = toolsOf(request)\n return JSON.stringify({\n setup: {\n model: `models/${request.model}`,\n generationConfig: generationConfig(request),\n ...(request.instructions !== undefined && {\n systemInstruction: { parts: [{ text: request.instructions }] },\n }),\n ...(tools.length > 0 && { tools }),\n realtimeInputConfig: realtimeInputConfig(request),\n ...(request.transcribeInput !== false && { inputAudioTranscription: {} }),\n // The only way to text on a native-audio model.\n outputAudioTranscription: {},\n // Always on, so a handle exists before the ten-minute socket ends.\n sessionResumption: request.resume === undefined ? {} : { handle: request.resume },\n ...(request.contextCompression !== undefined && {\n contextWindowCompression: {\n slidingWindow: {\n ...(request.contextCompression.targetTokens !== undefined && {\n targetTokens: request.contextCompression.targetTokens,\n }),\n },\n ...(request.contextCompression.triggerTokens !== undefined && {\n triggerTokens: request.contextCompression.triggerTokens,\n }),\n },\n }),\n ...(request.proactiveAudio === true && { proactivity: { proactiveAudio: true } }),\n // Seeded history arrives as `clientContent` right after the handshake.\n ...((request.history?.length ?? 0) > 0 && {\n historyConfig: { initialHistoryInClientContent: true },\n }),\n },\n })\n}\n\nconst messageText = (item: HistoryItem): string =>\n item.type !== \"message\"\n ? \"\"\n : item.content\n .map((block) =>\n block.type === \"input_text\" || block.type === \"output_text\" ? block.text : \"\",\n )\n .join(\"\")\n\n/**\n * Text-only seed, which is all `history` promises across providers. Anything\n * with no text of its own, a tool call or its output, has no place in the seed\n * and is reported once per kind rather than vanishing.\n */\nconst historyFrame = (\n history: ReadonlyArray<HistoryItem>,\n): Effect.Effect<string, AiError.AiError> =>\n Effect.gen(function* () {\n const kinds = new Set(history.filter((i) => messageText(i).length === 0).map((i) => i.type))\n yield* Effect.forEach(\n kinds,\n (kind) =>\n Capabilities.warnDropped({\n provider: PROVIDER,\n capability: \"history\",\n field: \"history\",\n value: kind,\n reason: `Gemini Live seeds a session with text turns only, so \\`${kind}\\` items are left out.`,\n }),\n { discard: true },\n )\n return JSON.stringify({\n clientContent: {\n turns: history\n .filter((item) => messageText(item).length > 0)\n .map((item) => ({\n role: item.type === \"message\" && item.role === \"assistant\" ? \"model\" : \"user\",\n parts: [{ text: messageText(item) }],\n })),\n turnComplete: true,\n },\n })\n })\n\n// ---------------------------------------------------------------------------\n// Server frames\n//\n// Gemini has no discriminator field: one message carries exactly one of these\n// keys, and `serverContent` can carry several of its own at once.\n// ---------------------------------------------------------------------------\n\nconst Part = Schema.Struct({\n text: Schema.optional(Schema.String),\n thought: Schema.optional(Schema.Boolean),\n inlineData: Schema.optional(\n Schema.Struct({\n mimeType: Schema.optional(Schema.String),\n data: Schema.optional(Schema.String),\n }),\n ),\n})\n\nconst Transcription = Schema.Struct({ text: Schema.optional(Schema.String) })\n\nconst ServerContent = Schema.Struct({\n modelTurn: Schema.optional(Schema.Struct({ parts: Schema.optional(Schema.Array(Part)) })),\n turnComplete: Schema.optional(Schema.Boolean),\n generationComplete: Schema.optional(Schema.Boolean),\n interrupted: Schema.optional(Schema.Boolean),\n inputTranscription: Schema.optional(Transcription),\n interimInputTranscription: Schema.optional(Transcription),\n outputTranscription: Schema.optional(Transcription),\n})\n\nconst UsageMetadata = Schema.Struct({\n promptTokenCount: Schema.optional(Schema.Number),\n responseTokenCount: Schema.optional(Schema.Number),\n totalTokenCount: Schema.optional(Schema.Number),\n})\n\nconst FunctionCall = Schema.Struct({\n id: Schema.optional(Schema.String),\n name: Schema.String,\n args: Schema.optional(Schema.Unknown),\n})\n\nconst ServerMessage = Schema.Struct({\n setupComplete: Schema.optional(Schema.Unknown),\n serverContent: Schema.optional(ServerContent),\n toolCall: Schema.optional(\n Schema.Struct({ functionCalls: Schema.optional(Schema.Array(FunctionCall)) }),\n ),\n toolCallCancellation: Schema.optional(\n Schema.Struct({ ids: Schema.optional(Schema.Array(Schema.String)) }),\n ),\n goAway: Schema.optional(Schema.Struct({ timeLeft: Schema.optional(Schema.String) })),\n sessionResumptionUpdate: Schema.optional(\n Schema.Struct({\n newHandle: Schema.optional(Schema.String),\n resumable: Schema.optional(Schema.Boolean),\n }),\n ),\n usageMetadata: Schema.optional(UsageMetadata),\n})\n\ntype ServerMessage = typeof ServerMessage.Type\n\n// ---------------------------------------------------------------------------\n// Session state\n// ---------------------------------------------------------------------------\n\ntype SessionState = {\n /** The turn now generating, if any. Minted here: the wire has no id. */\n readonly responseId: string | undefined\n readonly turns: number\n /** Call id to tool name, since `functionResponse` must repeat the name. */\n readonly callNames: Readonly<Record<string, string>>\n /** Latest `usageMetadata`, reported with the turn it lands in. */\n readonly usage: Usage | undefined\n /** The server has announced the close, so the next one is the lifetime cap. */\n readonly goingAway: boolean\n}\n\nconst initialState: SessionState = {\n responseId: undefined,\n turns: 0,\n callNames: {},\n usage: undefined,\n goingAway: false,\n}\n\n/**\n * The id every event of the current turn carries, minting one and announcing\n * the turn if this is its first event.\n */\nconst startTurn = (\n state: Ref.Ref<SessionState>,\n): Effect.Effect<readonly [string, ReadonlyArray<RealtimeEvent>]> =>\n Ref.modify(state, (s) => {\n const responseId = s.responseId ?? `turn_${s.turns + 1}`\n return s.responseId !== undefined\n ? [[responseId, []], s]\n : [\n [responseId, [RealtimeEvent.ResponseStarted({ responseId })]],\n { ...s, responseId, turns: s.turns + 1 },\n ]\n })\n\nconst usageOf = (metadata: typeof UsageMetadata.Type): Usage => ({\n ...(metadata.promptTokenCount !== undefined && { input_tokens: metadata.promptTokenCount }),\n ...(metadata.responseTokenCount !== undefined && { output_tokens: metadata.responseTokenCount }),\n ...(metadata.totalTokenCount !== undefined && { total_tokens: metadata.totalTokenCount }),\n})\n\nconst decodeAudio = (b64: string): Effect.Effect<Uint8Array, AiError.AiError> =>\n Result.match(Base64.decode(b64), {\n onSuccess: Effect.succeed,\n onFailure: (cause) =>\n Effect.fail(\n new AiError.GenerationFailed({\n provider: PROVIDER,\n raw: { message: \"audio part base64 decode failed\", cause },\n }),\n ),\n })\n\n/** `timeLeft` is a proto duration, so `\"60s\"` rather than a number. */\nconst parseTimeLeft = (timeLeft: string | undefined): Duration.Duration | undefined => {\n if (timeLeft === undefined) return undefined\n const seconds = Number.parseFloat(timeLeft.replace(/s$/, \"\"))\n return Number.isFinite(seconds) ? Duration.seconds(seconds) : undefined\n}\n\n/** Thought parts are the model reasoning, not something to speak or show. */\nconst partToEvents = (\n responseId: string,\n part: typeof Part.Type,\n): Effect.Effect<ReadonlyArray<RealtimeEvent>, AiError.AiError> =>\n Match.value(part).pipe(\n Match.when({ thought: true }, () => Effect.succeed([])),\n Match.when({ inlineData: { data: Match.string } }, (p) =>\n Effect.map(decodeAudio(p.inlineData.data), (bytes) => [\n RealtimeEvent.AudioDelta({ responseId, bytes }),\n ]),\n ),\n Match.when({ text: Match.string }, (p) =>\n Effect.succeed([RealtimeEvent.OutputTranscriptDelta({ responseId, text: p.text })]),\n ),\n Match.orElse(() => Effect.succeed([])),\n )\n\nconst inputTranscripts = (content: typeof ServerContent.Type): ReadonlyArray<RealtimeEvent> => [\n ...(content.interimInputTranscription?.text === undefined\n ? []\n : [\n RealtimeEvent.InputTranscript({\n text: content.interimInputTranscription.text,\n final: false,\n }),\n ]),\n ...(content.inputTranscription?.text === undefined\n ? []\n : [RealtimeEvent.InputTranscript({ text: content.inputTranscription.text, final: true })]),\n]\n\n/**\n * One `serverContent` to its events. Several fields can arrive together, and\n * 3.1 packs several parts into one message, so this answers with a batch.\n */\nconst contentToEvents = (\n state: Ref.Ref<SessionState>,\n content: typeof ServerContent.Type,\n): Effect.Effect<ReadonlyArray<RealtimeEvent>, AiError.AiError> =>\n Effect.gen(function* () {\n // The user's own words belong to no turn, so they need none started.\n const input = inputTranscripts(content)\n const parts = content.modelTurn?.parts ?? []\n const spokenText = content.outputTranscription?.text\n const generating = parts.length > 0 || spokenText !== undefined\n const ending = content.interrupted === true || content.turnComplete === true\n if (!generating && !ending) return input\n\n const [responseId, started] = generating\n ? yield* startTurn(state)\n : [(yield* Ref.get(state)).responseId, [] as ReadonlyArray<RealtimeEvent>]\n // A terminal flag for a turn that already ended is nothing to report.\n if (responseId === undefined) return input\n\n const spoken = yield* Effect.forEach(parts, (part) => partToEvents(responseId, part))\n const transcript =\n spokenText === undefined\n ? []\n : [RealtimeEvent.OutputTranscriptDelta({ responseId, text: spokenText })]\n const body = [...input, ...started, ...spoken.flat(), ...transcript]\n if (!ending) return body\n\n const usage = (yield* Ref.get(state)).usage\n yield* Ref.update(state, (s) => ({ ...s, responseId: undefined, usage: undefined }))\n return [\n ...body,\n // Playback must stop before the turn is reported as over.\n ...(content.interrupted === true ? [RealtimeEvent.Interrupted({ responseId })] : []),\n RealtimeEvent.ResponseDone({\n responseId,\n reason: content.interrupted === true ? \"interrupted\" : \"complete\",\n ...(usage !== undefined && { usage }),\n }),\n ]\n })\n\nconst toolCallEvents = (\n state: Ref.Ref<SessionState>,\n calls: ReadonlyArray<typeof FunctionCall.Type>,\n): Effect.Effect<ReadonlyArray<RealtimeEvent>> =>\n Effect.gen(function* () {\n const [responseId, started] = yield* startTurn(state)\n yield* Ref.update(state, (s) => ({\n ...s,\n callNames: calls.reduce(\n (names, call) => ({ ...names, [call.id ?? call.name]: call.name }),\n s.callNames,\n ),\n }))\n return [\n ...started,\n ...calls.map((call) =>\n RealtimeEvent.ToolCall({\n responseId,\n call: {\n type: \"function_call\" as const,\n call_id: call.id ?? call.name,\n name: call.name,\n arguments: JSON.stringify(call.args ?? {}),\n providerData: undefined,\n },\n }),\n ),\n ]\n })\n\nconst toEvents = (\n state: Ref.Ref<SessionState>,\n frame: ServerMessage,\n): Effect.Effect<ReadonlyArray<RealtimeEvent>, AiError.AiError> =>\n Effect.gen(function* () {\n if (frame.usageMetadata !== undefined) {\n const usage = usageOf(frame.usageMetadata)\n yield* Ref.update(state, (s) => ({ ...s, usage }))\n }\n if (frame.serverContent !== undefined) return yield* contentToEvents(state, frame.serverContent)\n if (frame.toolCall !== undefined) {\n return yield* toolCallEvents(state, frame.toolCall.functionCalls ?? [])\n }\n const callIds = frame.toolCallCancellation?.ids ?? []\n if (callIds.length > 0) return [RealtimeEvent.ToolCallCancelled({ callIds })]\n if (frame.goAway !== undefined) {\n const timeLeft = parseTimeLeft(frame.goAway.timeLeft)\n yield* Ref.update(state, (s) => ({ ...s, goingAway: true }))\n return [RealtimeEvent.SessionEnding({ ...(timeLeft !== undefined && { timeLeft }) })]\n }\n // An unresumable update carries an empty handle; it must not replace one.\n const handle = frame.sessionResumptionUpdate?.newHandle\n return frame.sessionResumptionUpdate?.resumable === true &&\n handle !== undefined &&\n handle !== \"\"\n ? [RealtimeEvent.ResumptionHandle({ handle })]\n : []\n })\n\n// ---------------------------------------------------------------------------\n// Input handling\n// ---------------------------------------------------------------------------\n\nconst droppedActivity = (field: string) =>\n Capabilities.warnDropped({\n provider: PROVIDER,\n capability: \"manualTurns\",\n field,\n reason: \"The session uses automatic activity detection; boundaries are the server's.\",\n })\n\nconst videoPart = (\n frame: Extract<RealtimeInput, { readonly _tag: \"VideoFrame\" }>[\"frame\"],\n): Effect.Effect<{ readonly data: string; readonly mimeType: string }, AiError.AiError> =>\n Match.value(frame).pipe(\n Match.tag(\"base64\", (s) => Effect.succeed({ data: s.base64, mimeType: s.mimeType })),\n Match.tag(\"bytes\", (s) =>\n Effect.succeed({ data: Base64.encode(s.bytes), mimeType: s.mimeType }),\n ),\n Match.tag(\"url\", () =>\n Effect.fail(\n new AiError.Unsupported({\n provider: PROVIDER,\n capability: \"videoInput\",\n reason: \"A live video frame must carry its bytes; a URL would need the Files API.\",\n }),\n ),\n ),\n Match.exhaustive,\n )\n\n/** Wire spelling of `toolScheduling`. */\ntype ToolScheduling = \"WHEN_IDLE\" | \"INTERRUPT\" | \"SILENT\"\n\nconst schedulingOf = (request: GeminiLiveRequest): ToolScheduling =>\n Match.value(request.toolScheduling ?? \"when-idle\").pipe(\n Match.when(\"when-idle\", () => \"WHEN_IDLE\" as const),\n Match.when(\"interrupt\", () => \"INTERRUPT\" as const),\n Match.when(\"silent\", () => \"SILENT\" as const),\n Match.exhaustive,\n )\n\nconst handleInput = (\n send: (frame: string) => Effect.Effect<void, AiError.AiError>,\n state: Ref.Ref<SessionState>,\n options: {\n readonly manual: boolean\n readonly rate: number\n readonly scheduling: ToolScheduling\n },\n input: RealtimeInput,\n): Effect.Effect<void, AiError.AiError> =>\n Match.value(input).pipe(\n Match.tag(\"Audio\", (i) =>\n send(\n JSON.stringify({\n realtimeInput: {\n audio: {\n data: Base64.encode(i.bytes),\n mimeType: `audio/pcm;rate=${options.rate}`,\n },\n },\n }),\n ),\n ),\n Match.tag(\"Text\", (i) =>\n i.role === \"system\"\n ? Effect.fail(\n new AiError.Unsupported({\n provider: PROVIDER,\n capability: \"systemTurn\",\n reason: \"Gemini Live fixes the system instruction at setup for the connection.\",\n }),\n )\n : send(JSON.stringify({ realtimeInput: { text: i.text } })),\n ),\n Match.tag(\"ToolResult\", (i) =>\n Effect.gen(function* () {\n const name = (yield* Ref.get(state)).callNames[i.output.call_id]\n if (name === undefined) {\n return yield* Effect.fail(\n new AiError.InvalidRequest({\n provider: PROVIDER,\n raw: `no tool call ${i.output.call_id} in this session`,\n }),\n )\n }\n // Answering resumes generation on its own; there is no turn to ask\n // for. `scheduling` says when, and matters on a model that kept\n // generating while the tool ran.\n yield* send(\n JSON.stringify({\n toolResponse: {\n functionResponses: [\n {\n id: i.output.call_id,\n name,\n response: parsedResponse(i.output.output),\n scheduling: options.scheduling,\n },\n ],\n },\n }),\n )\n }),\n ),\n Match.tag(\"Interrupt\", () =>\n Effect.fail(\n new AiError.Unsupported({\n provider: PROVIDER,\n capability: \"interrupt\",\n reason:\n 'Gemini Live has no cancel op: the server interrupts when it hears speech. Pass activityHandling: \"none\" to turn that off.',\n }),\n ),\n ),\n Match.tag(\"VideoFrame\", (i) =>\n Effect.flatMap(videoPart(i.frame), (video) =>\n send(JSON.stringify({ realtimeInput: { video } })),\n ),\n ),\n Match.tag(\"ActivityStart\", () =>\n options.manual\n ? send(JSON.stringify({ realtimeInput: { activityStart: {} } }))\n : droppedActivity(\"ActivityStart\"),\n ),\n Match.tag(\"ActivityEnd\", () =>\n options.manual\n ? send(JSON.stringify({ realtimeInput: { activityEnd: {} } }))\n : droppedActivity(\"ActivityEnd\"),\n ),\n // Interruption already drops what was never generated, but audio the\n // server did send stays in history whether or not it was heard.\n Match.tag(\"PlaybackPosition\", () =>\n Capabilities.warnDropped({\n provider: PROVIDER,\n capability: \"truncate\",\n field: \"PlaybackPosition\",\n reason: \"Gemini Live has no truncate op, so the unheard tail stays in context.\",\n }),\n ),\n Match.exhaustive,\n )\n\n// ---------------------------------------------------------------------------\n// Connection\n// ---------------------------------------------------------------------------\n\nexport const buildWsUrl = (cfg: Config): string => {\n const url = new URL(cfg.baseUrl ?? DEFAULT_BASE_URL)\n url.protocol = url.protocol === \"http:\" ? \"ws:\" : \"wss:\"\n url.pathname = `${url.pathname.replace(/\\/$/, \"\")}/${RPC}`\n url.searchParams.set(\"key\", Redacted.value(cfg.apiKey))\n return url.toString()\n}\n\nconst withSocket =\n (cfg: Config) =>\n <A, E, R>(\n effect: Effect.Effect<A, E, R | Socket.WebSocketConstructor>,\n ): Effect.Effect<A, E, Exclude<R, Socket.WebSocketConstructor>> =>\n cfg.webSocket === undefined\n ? Effect.provide(effect, Socket.layerWebSocketConstructorGlobal)\n : Effect.provideService(effect, Socket.WebSocketConstructor, cfg.webSocket)\n\n/** Drop frames until `setupComplete`; a close before it is a failed handshake. */\nconst awaitSetupComplete = (\n session: WebSocketSession.WebSocketSession<ServerMessage>,\n): Effect.Effect<void, AiError.AiError> =>\n Queue.take(session.frames).pipe(\n Effect.catchCause((cause) =>\n Pull.isDoneCause(cause)\n ? Effect.fail(\n new AiError.Unavailable({\n provider: PROVIDER,\n raw: \"the socket closed during the handshake\",\n }),\n )\n : Effect.failCause(cause as Cause.Cause<AiError.AiError>),\n ),\n Effect.flatMap((frame) =>\n frame.setupComplete === undefined ? awaitSetupComplete(session) : Effect.void,\n ),\n )\n\nexport const openSession =\n (cfg: Config) =>\n (\n request: GeminiLiveRequest,\n ): Effect.Effect<RealtimeSessionHandle, AiError.AiError, Scope.Scope> =>\n Effect.gen(function* () {\n if (request.googleSearch === true && (request.tools?.length ?? 0) > 0) {\n return yield* Effect.fail(\n new AiError.InvalidRequest({\n provider: PROVIDER,\n raw: \"Gemini rejects googleSearch alongside function declarations; pick one per session.\",\n }),\n )\n }\n const rate = yield* inputRate(request.inputFormat)\n const scheduling = schedulingOf(request)\n yield* checkOutputFormat(request.outputFormat)\n\n const session = yield* WebSocketSession.open({\n url: buildWsUrl(cfg),\n provider: PROVIDER,\n schema: ServerMessage,\n openTimeout: Duration.seconds(30),\n }).pipe(withSocket(cfg))\n\n // The client speaks first here, and nothing else may be sent until the\n // server has accepted the configuration.\n yield* session.send(setupFrame(request))\n yield* awaitSetupComplete(session)\n\n const history = request.history ?? []\n if (history.length > 0) {\n const seed = yield* historyFrame(history)\n yield* session.send(seed)\n }\n\n const state = yield* Ref.make(initialState)\n const out = yield* Queue.bounded<RealtimeEvent, AiError.AiError | Cause.Done>(128)\n\n const pump = Stream.fromQueue(session.frames).pipe(\n Stream.mapEffect((frame) => toEvents(state, frame)),\n Stream.runForEach((events) => Queue.offerAll(out, events)),\n )\n\n // A close never synthesizes a `ResponseDone`; a turn still in flight\n // ends the stream as an incomplete turn instead.\n const finish = (cause: Cause.Cause<AiError.AiError> | undefined) =>\n Effect.gen(function* () {\n const { goingAway, responseId } = yield* Ref.get(state)\n if (responseId !== undefined) {\n return yield* Queue.fail(\n out,\n new AiError.IncompleteTurn({ raw: cause ?? \"the socket closed mid turn\" }),\n )\n }\n // `goAway` was the server announcing this close, so the session hit\n // its cap. Reconnecting works; retrying this session does not.\n if (goingAway) {\n return yield* Queue.fail(\n out,\n new AiError.SessionExpired({\n provider: PROVIDER,\n raw: cause ?? \"the socket closed after the server announced it was going away\",\n }),\n )\n }\n return yield* cause !== undefined && Cause.hasFails(cause)\n ? Queue.failCause(out, cause)\n : Queue.end(out)\n })\n\n yield* pump.pipe(\n Effect.matchCauseEffect({ onFailure: finish, onSuccess: () => finish(undefined) }),\n Effect.forkScoped,\n )\n\n const manual = request.turnDetection === \"manual\"\n return {\n send: (input) => handleInput(session.send, state, { manual, rate, scheduling }, input),\n events: Stream.fromQueue(out),\n }\n })\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAyCA,MAAM,WAAW;AAEjB,MAAM,mBAAmB;AACzB,MAAM,MAAM;;AA2DZ,MAAM,cAAc;AAEpB,MAAM,qBAAqB,QAAqB,OAAe,aAC7D,IAAI,QAAQ,YAAY;CACtB,UAAU;CACV,YAAY;CACZ,QAAQ,uBAAuB,SAAS,QAAQ,KAAK,UAAU,MAAM,EAAE;AACzE,CAAC;;AAGH,MAAM,aAAa,WACjB,MAAM,MAAM,MAAM,CAAC,CAAC,KAClB,MAAM,KAAK;CAAE,WAAW;CAAO,UAAU;CAAa,UAAU;AAAE,IAAI,MACpE,OAAO,QAAQ,EAAE,UAAU,CAC7B,GACA,MAAM,QAAQ,MACZ,OAAO,KAAK,kBAAkB,GAAG,eAAe,4BAA4B,CAAC,CAC/E,CACF;AAEF,MAAM,qBAAqB,WACzB,MAAM,MAAM,MAAM,CAAC,CAAC,KAClB,MAAM,KACJ;CAAE,WAAW;CAAO,UAAU;CAAa,YAAY;CAAa,UAAU;AAAE,SAC1E,OAAO,IACf,GACA,MAAM,QAAQ,MACZ,OAAO,KAAK,kBAAkB,GAAG,gBAAgB,qBAAqB,aAAa,CAAC,CACtF,CACF;AAMF,MAAM,cAAc;CAAE,MAAM;CAAQ,KAAK;AAAM;AAE/C,MAAM,gBAAgB;CACpB,UAAU;CACV,KAAK;CACL,0BAA0B;AAC5B;AAEA,MAAM,uBAAuB,aAAgC;CAC3D,4BAA4B;EAC1B,GAAI,QAAQ,kBAAkB,YAAY,EAAE,UAAU,KAAK;EAC3D,GAAI,QAAQ,KAAK,qBAAqB,KAAA,KAAa,EACjD,0BAA0B,qBAAqB,YAAY,QAAQ,IAAI,oBACzE;EACA,GAAI,QAAQ,KAAK,mBAAmB,KAAA,KAAa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AU,QAAQ,WAAW,CAAC;CACpC,IAAI,QAAQ,SAAS,GAAG;EACtB,MAAM,OAAO,OAAO,aAAa,OAAO;EACxC,OAAO,QAAQ,KAAK,IAAI;CAC1B;CAEA,MAAM,QAAQ,OAAO,IAAI,KAAK,YAAY;CAC1C,MAAM,MAAM,OAAO,MAAM,QAAqD,GAAG;CAEjF,MAAM,OAAO,OAAO,UAAU,QAAQ,MAAM,CAAC,CAAC,KAC5C,OAAO,WAAW,UAAU,SAAS,OAAO,KAAK,CAAC,GAClD,OAAO,YAAY,WAAW,MAAM,SAAS,KAAK,MAAM,CAAC,CAC3D;CAIA,MAAM,UAAU,UACd,OAAO,IAAI,aAAa;EACtB,MAAM,EAAE,WAAW,eAAe,OAAO,IAAI,IAAI,KAAK;EACtD,IAAI,eAAe,KAAA,GACjB,OAAO,OAAO,MAAM,KAClB,KACA,IAAI,QAAQ,eAAe,EAAE,KAAK,SAAS,6BAA6B,CAAC,CAC3E;EAIF,IAAI,WACF,OAAO,OAAO,MAAM,KAClB,KACA,IAAI,QAAQ,eAAe;GACzB,UAAU;GACV,KAAK,SAAS;EAChB,CAAC,CACH;EAEF,OAAO,OAAO,UAAU,KAAA,KAAa,MAAM,SAAS,KAAK,IACrD,MAAM,UAAU,KAAK,KAAK,IAC1B,MAAM,IAAI,GAAG;CACnB,CAAC;CAEH,OAAO,KAAK,KACV,OAAO,iBAAiB;EAAE,WAAW;EAAQ,iBAAiB,OAAO,KAAA,CAAS;CAAE,CAAC,GACjF,OAAO,UACT;CAEA,MAAM,SAAS,QAAQ,kBAAkB;CACzC,OAAO;EACL,OAAO,UAAU,YAAY,QAAQ,MAAM,OAAO;GAAE;GAAQ;GAAM;EAAW,GAAG,KAAK;EACrF,QAAQ,OAAO,UAAU,GAAG;CAC9B;AACF,CAAC"}
|
|
1
|
+
{"version":3,"file":"realtimeSession.mjs","names":[],"sources":["../src/realtimeSession.ts"],"sourcesContent":["/**\n * Gemini Live speech-to-speech over `BidiGenerateContent`.\n *\n * Auth is a query parameter, so this needs no `ws` package and no headers:\n * the platform `WebSocket` reaches it from Node, Bun, Deno and the browser.\n *\n * Two shapes differ from OpenAI Realtime and drive most of this file. There\n * are no response ids on the wire, so a turn id is minted here and every event\n * of that turn carries it; and there is no truncate op, so a\n * `PlaybackPosition` has nothing to send.\n */\nimport {\n Cause,\n Duration,\n Effect,\n Match,\n Pull,\n Queue,\n Redacted,\n Ref,\n Result,\n Schema,\n type Scope,\n Stream,\n} from \"effect\"\nimport { Base64 } from \"effect/encoding\"\nimport * as Socket from \"effect/socket/Socket\"\nimport * as AiError from \"@effect-uai/core/AiError\"\nimport type { AudioFormat } from \"@effect-uai/core/Audio\"\nimport * as Capabilities from \"@effect-uai/core/Capabilities\"\nimport type { HistoryItem, Usage } from \"@effect-uai/core/Items\"\nimport {\n type CommonSessionRequest,\n RealtimeEvent,\n type RealtimeInput,\n} from \"@effect-uai/core/Realtime\"\nimport type { RealtimeSessionHandle } from \"@effect-uai/core/RealtimeSession\"\nimport * as WebSocketSession from \"@effect-uai/core/WebSocketSession\"\nimport { functionResponseBody, toolDescriptorsToTools } from \"./codec.js\"\nimport type { GeminiLiveModel } from \"./models.js\"\n\nconst PROVIDER = \"gemini-live\"\n\nconst DEFAULT_BASE_URL = \"wss://generativelanguage.googleapis.com\"\nconst RPC = \"ws/google.ai.generativelanguage.v1beta.GenerativeService.BidiGenerateContent\"\n\nexport type Config = {\n readonly apiKey: Redacted.Redacted\n readonly baseUrl?: string\n /** Build the socket yourself, for a proxy or an in-memory transport. */\n readonly webSocket?: Socket.WebSocketConstructor[\"Service\"]\n}\n\n/**\n * Gemini-typed session request. Everything past `CommonSessionRequest` is\n * provider-shaped. The configuration is immutable for the connection: changing\n * any of it means opening a new session.\n */\nexport type GeminiLiveRequest = Omit<CommonSessionRequest, \"model\"> & {\n readonly model: GeminiLiveModel\n /** Replaces the plain `turnDetection` switch when you need the knobs. */\n readonly vad?: {\n readonly startSensitivity?: \"high\" | \"low\"\n readonly endSensitivity?: \"high\" | \"low\"\n readonly prefixPaddingMs?: number\n readonly silenceDurationMs?: number\n }\n /** `\"none\"` turns barge-in off. Defaults to interrupting. */\n readonly activityHandling?: \"interrupt\" | \"none\"\n /**\n * When a tool result reaches the model, for the models that keep generating\n * while a tool runs. Defaults to `\"when-idle\"`: the server's own default is\n * to interrupt, which cuts off whatever the model was saying while it\n * waited, including the \"let me look that up\" it just spoke. `\"interrupt\"`\n * is right when the result cannot wait, `\"silent\"` when it is context the\n * model should have rather than something to announce.\n */\n readonly toolScheduling?: \"when-idle\" | \"interrupt\" | \"silent\"\n readonly turnCoverage?: \"activity\" | \"all\" | \"audioActivityAndAllVideo\"\n /** 3.1 only. Higher levels delay the first audio. */\n readonly thinkingLevel?: \"minimal\" | \"low\" | \"medium\" | \"high\"\n readonly mediaResolution?: \"low\" | \"medium\" | \"high\"\n readonly languageCode?: string\n readonly temperature?: number\n readonly maxOutputTokens?: number\n /** What turns an otherwise time-boxed session into an unbounded one. */\n readonly contextCompression?: {\n readonly triggerTokens?: number\n readonly targetTokens?: number\n }\n /** Grounding. Cannot be combined with `tools`; the pair is a 400. */\n readonly googleSearch?: boolean\n /** 2.5 only. */\n readonly proactiveAudio?: boolean\n /** 2.5 only. */\n readonly affectiveDialog?: boolean\n}\n\n// ---------------------------------------------------------------------------\n// AudioFormat -> wire\n// ---------------------------------------------------------------------------\n\n/** Output is 24 kHz whatever the input rate, and cannot be configured. */\nconst OUTPUT_RATE = 24000\n\nconst unsupportedFormat = (format: AudioFormat, field: string, expected: string) =>\n new AiError.Unsupported({\n provider: PROVIDER,\n capability: field,\n reason: `Gemini Live accepts ${expected}. Got ${JSON.stringify(format)}.`,\n })\n\n/** Any rate is accepted on input; 16 kHz is what the model works in. */\nconst inputRate = (format: AudioFormat): Effect.Effect<number, AiError.AiError> =>\n Match.value(format).pipe(\n Match.when({ container: \"raw\", encoding: \"pcm_s16le\", channels: 1 }, (f) =>\n Effect.succeed(f.sampleRate),\n ),\n Match.orElse((f) =>\n Effect.fail(unsupportedFormat(f, \"inputFormat\", \"mono pcm_s16le at any rate\")),\n ),\n )\n\nconst checkOutputFormat = (format: AudioFormat): Effect.Effect<void, AiError.AiError> =>\n Match.value(format).pipe(\n Match.when(\n { container: \"raw\", encoding: \"pcm_s16le\", sampleRate: OUTPUT_RATE, channels: 1 },\n () => Effect.void,\n ),\n Match.orElse((f) =>\n Effect.fail(unsupportedFormat(f, \"outputFormat\", `mono pcm_s16le at ${OUTPUT_RATE}`)),\n ),\n )\n\n// ---------------------------------------------------------------------------\n// Client frames\n// ---------------------------------------------------------------------------\n\nconst SENSITIVITY = { high: \"HIGH\", low: \"LOW\" } as const\n\nconst TURN_COVERAGE = {\n activity: \"TURN_INCLUDES_ONLY_ACTIVITY\",\n all: \"TURN_INCLUDES_ALL_INPUT\",\n audioActivityAndAllVideo: \"TURN_INCLUDES_AUDIO_ACTIVITY_AND_ALL_VIDEO\",\n} as const\n\nconst realtimeInputConfig = (request: GeminiLiveRequest) => ({\n automaticActivityDetection: {\n ...(request.turnDetection === \"manual\" && { disabled: true }),\n ...(request.vad?.startSensitivity !== undefined && {\n startOfSpeechSensitivity: `START_SENSITIVITY_${SENSITIVITY[request.vad.startSensitivity]}`,\n }),\n ...(request.vad?.endSensitivity !== undefined && {\n endOfSpeechSensitivity: `END_SENSITIVITY_${SENSITIVITY[request.vad.endSensitivity]}`,\n }),\n ...(request.vad?.prefixPaddingMs !== undefined && {\n prefixPaddingMs: request.vad.prefixPaddingMs,\n }),\n ...(request.vad?.silenceDurationMs !== undefined && {\n silenceDurationMs: request.vad.silenceDurationMs,\n }),\n },\n ...(request.activityHandling !== undefined && {\n activityHandling:\n request.activityHandling === \"none\" ? \"NO_INTERRUPTION\" : \"START_OF_ACTIVITY_INTERRUPTS\",\n }),\n ...(request.turnCoverage !== undefined && { turnCoverage: TURN_COVERAGE[request.turnCoverage] }),\n})\n\nconst generationConfig = (request: GeminiLiveRequest) => ({\n responseModalities: [\"AUDIO\"],\n ...(request.voiceId !== undefined && {\n speechConfig: {\n voiceConfig: { prebuiltVoiceConfig: { voiceName: request.voiceId } },\n ...(request.languageCode !== undefined && { languageCode: request.languageCode }),\n },\n }),\n ...(request.temperature !== undefined && { temperature: request.temperature }),\n ...(request.maxOutputTokens !== undefined && { maxOutputTokens: request.maxOutputTokens }),\n ...(request.mediaResolution !== undefined && {\n mediaResolution: `MEDIA_RESOLUTION_${request.mediaResolution.toUpperCase()}`,\n }),\n ...(request.thinkingLevel !== undefined && {\n thinkingConfig: { thinkingLevel: request.thinkingLevel.toUpperCase() },\n }),\n ...(request.affectiveDialog === true && { enableAffectiveDialog: true }),\n})\n\nconst toolsOf = (request: GeminiLiveRequest) =>\n request.googleSearch === true\n ? [{ googleSearch: {} }]\n : toolDescriptorsToTools(request.tools ?? [])\n\nconst setupFrame = (request: GeminiLiveRequest): string => {\n const tools = toolsOf(request)\n return JSON.stringify({\n setup: {\n model: `models/${request.model}`,\n generationConfig: generationConfig(request),\n ...(request.instructions !== undefined && {\n systemInstruction: { parts: [{ text: request.instructions }] },\n }),\n ...(tools.length > 0 && { tools }),\n realtimeInputConfig: realtimeInputConfig(request),\n ...(request.transcribeInput !== false && { inputAudioTranscription: {} }),\n // The only way to text on a native-audio model.\n outputAudioTranscription: {},\n // Always on, so a handle exists before the ten-minute socket ends.\n sessionResumption: request.resume === undefined ? {} : { handle: request.resume },\n ...(request.contextCompression !== undefined && {\n contextWindowCompression: {\n slidingWindow: {\n ...(request.contextCompression.targetTokens !== undefined && {\n targetTokens: request.contextCompression.targetTokens,\n }),\n },\n ...(request.contextCompression.triggerTokens !== undefined && {\n triggerTokens: request.contextCompression.triggerTokens,\n }),\n },\n }),\n ...(request.proactiveAudio === true && { proactivity: { proactiveAudio: true } }),\n // Seeded history arrives as `clientContent` right after the handshake.\n ...((request.history?.length ?? 0) > 0 && {\n historyConfig: { initialHistoryInClientContent: true },\n }),\n },\n })\n}\n\nconst messageText = (item: HistoryItem): string =>\n item.type !== \"message\"\n ? \"\"\n : item.content\n .map((block) =>\n block.type === \"input_text\" || block.type === \"output_text\" ? block.text : \"\",\n )\n .join(\"\")\n\n/**\n * Text-only seed, which is all `history` promises across providers. Anything\n * with no text of its own, a tool call or its output, has no place in the seed\n * and is reported once per kind rather than vanishing.\n */\nconst historyFrame = (\n history: ReadonlyArray<HistoryItem>,\n): Effect.Effect<string, AiError.AiError> =>\n Effect.gen(function* () {\n const kinds = new Set(history.filter((i) => messageText(i).length === 0).map((i) => i.type))\n yield* Effect.forEach(\n kinds,\n (kind) =>\n Capabilities.warnDropped({\n provider: PROVIDER,\n capability: \"history\",\n field: \"history\",\n value: kind,\n reason: `Gemini Live seeds a session with text turns only, so \\`${kind}\\` items are left out.`,\n }),\n { discard: true },\n )\n return JSON.stringify({\n clientContent: {\n turns: history\n .filter((item) => messageText(item).length > 0)\n .map((item) => ({\n role: item.type === \"message\" && item.role === \"assistant\" ? \"model\" : \"user\",\n parts: [{ text: messageText(item) }],\n })),\n turnComplete: true,\n },\n })\n })\n\n// ---------------------------------------------------------------------------\n// Server frames\n//\n// Gemini has no discriminator field: one message carries exactly one of these\n// keys, and `serverContent` can carry several of its own at once.\n// ---------------------------------------------------------------------------\n\nconst Part = Schema.Struct({\n text: Schema.optional(Schema.String),\n thought: Schema.optional(Schema.Boolean),\n inlineData: Schema.optional(\n Schema.Struct({\n mimeType: Schema.optional(Schema.String),\n data: Schema.optional(Schema.String),\n }),\n ),\n})\n\nconst Transcription = Schema.Struct({ text: Schema.optional(Schema.String) })\n\nconst ServerContent = Schema.Struct({\n modelTurn: Schema.optional(Schema.Struct({ parts: Schema.optional(Schema.Array(Part)) })),\n turnComplete: Schema.optional(Schema.Boolean),\n generationComplete: Schema.optional(Schema.Boolean),\n interrupted: Schema.optional(Schema.Boolean),\n inputTranscription: Schema.optional(Transcription),\n interimInputTranscription: Schema.optional(Transcription),\n outputTranscription: Schema.optional(Transcription),\n})\n\nconst UsageMetadata = Schema.Struct({\n promptTokenCount: Schema.optional(Schema.Number),\n responseTokenCount: Schema.optional(Schema.Number),\n totalTokenCount: Schema.optional(Schema.Number),\n})\n\nconst FunctionCall = Schema.Struct({\n id: Schema.optional(Schema.String),\n name: Schema.String,\n args: Schema.optional(Schema.Unknown),\n})\n\nconst ServerMessage = Schema.Struct({\n setupComplete: Schema.optional(Schema.Unknown),\n serverContent: Schema.optional(ServerContent),\n toolCall: Schema.optional(\n Schema.Struct({ functionCalls: Schema.optional(Schema.Array(FunctionCall)) }),\n ),\n toolCallCancellation: Schema.optional(\n Schema.Struct({ ids: Schema.optional(Schema.Array(Schema.String)) }),\n ),\n goAway: Schema.optional(Schema.Struct({ timeLeft: Schema.optional(Schema.String) })),\n sessionResumptionUpdate: Schema.optional(\n Schema.Struct({\n newHandle: Schema.optional(Schema.String),\n resumable: Schema.optional(Schema.Boolean),\n }),\n ),\n usageMetadata: Schema.optional(UsageMetadata),\n})\n\ntype ServerMessage = typeof ServerMessage.Type\n\n// ---------------------------------------------------------------------------\n// Session state\n// ---------------------------------------------------------------------------\n\ntype SessionState = {\n /** The turn now generating, if any. Minted here: the wire has no id. */\n readonly responseId: string | undefined\n readonly turns: number\n /** Call id to tool name, since `functionResponse` must repeat the name. */\n readonly callNames: Readonly<Record<string, string>>\n /** Latest `usageMetadata`, reported with the turn it lands in. */\n readonly usage: Usage | undefined\n /** The server has announced the close, so the next one is the lifetime cap. */\n readonly goingAway: boolean\n}\n\nconst initialState: SessionState = {\n responseId: undefined,\n turns: 0,\n callNames: {},\n usage: undefined,\n goingAway: false,\n}\n\n/**\n * The id every event of the current turn carries, minting one and announcing\n * the turn if this is its first event.\n */\nconst startTurn = (\n state: Ref.Ref<SessionState>,\n): Effect.Effect<readonly [string, ReadonlyArray<RealtimeEvent>]> =>\n Ref.modify(state, (s) => {\n const responseId = s.responseId ?? `turn_${s.turns + 1}`\n return s.responseId !== undefined\n ? [[responseId, []], s]\n : [\n [responseId, [RealtimeEvent.ResponseStarted({ responseId })]],\n { ...s, responseId, turns: s.turns + 1 },\n ]\n })\n\nconst usageOf = (metadata: typeof UsageMetadata.Type): Usage => ({\n ...(metadata.promptTokenCount !== undefined && { input_tokens: metadata.promptTokenCount }),\n ...(metadata.responseTokenCount !== undefined && { output_tokens: metadata.responseTokenCount }),\n ...(metadata.totalTokenCount !== undefined && { total_tokens: metadata.totalTokenCount }),\n})\n\nconst decodeAudio = (b64: string): Effect.Effect<Uint8Array, AiError.AiError> =>\n Result.match(Base64.decode(b64), {\n onSuccess: Effect.succeed,\n onFailure: (cause) =>\n Effect.fail(\n new AiError.GenerationFailed({\n provider: PROVIDER,\n raw: { message: \"audio part base64 decode failed\", cause },\n }),\n ),\n })\n\n/** `timeLeft` is a proto duration, so `\"60s\"` rather than a number. */\nconst parseTimeLeft = (timeLeft: string | undefined): Duration.Duration | undefined => {\n if (timeLeft === undefined) return undefined\n const seconds = Number.parseFloat(timeLeft.replace(/s$/, \"\"))\n return Number.isFinite(seconds) ? Duration.seconds(seconds) : undefined\n}\n\n/** Thought parts are the model reasoning, not something to speak or show. */\nconst partToEvents = (\n responseId: string,\n part: typeof Part.Type,\n): Effect.Effect<ReadonlyArray<RealtimeEvent>, AiError.AiError> =>\n Match.value(part).pipe(\n Match.when({ thought: true }, () => Effect.succeed([])),\n Match.when({ inlineData: { data: Match.string } }, (p) =>\n Effect.map(decodeAudio(p.inlineData.data), (bytes) => [\n RealtimeEvent.AudioDelta({ responseId, bytes }),\n ]),\n ),\n Match.when({ text: Match.string }, (p) =>\n Effect.succeed([RealtimeEvent.OutputTranscriptDelta({ responseId, text: p.text })]),\n ),\n Match.orElse(() => Effect.succeed([])),\n )\n\nconst inputTranscripts = (content: typeof ServerContent.Type): ReadonlyArray<RealtimeEvent> => [\n ...(content.interimInputTranscription?.text === undefined\n ? []\n : [\n RealtimeEvent.InputTranscript({\n text: content.interimInputTranscription.text,\n final: false,\n }),\n ]),\n ...(content.inputTranscription?.text === undefined\n ? []\n : [RealtimeEvent.InputTranscript({ text: content.inputTranscription.text, final: true })]),\n]\n\n/**\n * One `serverContent` to its events. Several fields can arrive together, and\n * 3.1 packs several parts into one message, so this answers with a batch.\n */\nconst contentToEvents = (\n state: Ref.Ref<SessionState>,\n content: typeof ServerContent.Type,\n): Effect.Effect<ReadonlyArray<RealtimeEvent>, AiError.AiError> =>\n Effect.gen(function* () {\n // The user's own words belong to no turn, so they need none started.\n const input = inputTranscripts(content)\n const parts = content.modelTurn?.parts ?? []\n const spokenText = content.outputTranscription?.text\n const generating = parts.length > 0 || spokenText !== undefined\n const ending = content.interrupted === true || content.turnComplete === true\n if (!generating && !ending) return input\n\n const [responseId, started] = generating\n ? yield* startTurn(state)\n : [(yield* Ref.get(state)).responseId, [] as ReadonlyArray<RealtimeEvent>]\n // A terminal flag for a turn that already ended is nothing to report.\n if (responseId === undefined) return input\n\n const spoken = yield* Effect.forEach(parts, (part) => partToEvents(responseId, part))\n const transcript =\n spokenText === undefined\n ? []\n : [RealtimeEvent.OutputTranscriptDelta({ responseId, text: spokenText })]\n const body = [...input, ...started, ...spoken.flat(), ...transcript]\n if (!ending) return body\n\n const usage = (yield* Ref.get(state)).usage\n yield* Ref.update(state, (s) => ({ ...s, responseId: undefined, usage: undefined }))\n return [\n ...body,\n // Playback must stop before the turn is reported as over.\n ...(content.interrupted === true ? [RealtimeEvent.Interrupted({ responseId })] : []),\n RealtimeEvent.ResponseDone({\n responseId,\n reason: content.interrupted === true ? \"interrupted\" : \"complete\",\n ...(usage !== undefined && { usage }),\n }),\n ]\n })\n\nconst toolCallEvents = (\n state: Ref.Ref<SessionState>,\n calls: ReadonlyArray<typeof FunctionCall.Type>,\n): Effect.Effect<ReadonlyArray<RealtimeEvent>> =>\n Effect.gen(function* () {\n const [responseId, started] = yield* startTurn(state)\n yield* Ref.update(state, (s) => ({\n ...s,\n callNames: calls.reduce(\n (names, call) => ({ ...names, [call.id ?? call.name]: call.name }),\n s.callNames,\n ),\n }))\n return [\n ...started,\n ...calls.map((call) =>\n RealtimeEvent.ToolCall({\n responseId,\n call: {\n type: \"function_call\" as const,\n call_id: call.id ?? call.name,\n name: call.name,\n arguments: JSON.stringify(call.args ?? {}),\n providerData: undefined,\n },\n }),\n ),\n ]\n })\n\nconst toEvents = (\n state: Ref.Ref<SessionState>,\n frame: ServerMessage,\n): Effect.Effect<ReadonlyArray<RealtimeEvent>, AiError.AiError> =>\n Effect.gen(function* () {\n if (frame.usageMetadata !== undefined) {\n const usage = usageOf(frame.usageMetadata)\n yield* Ref.update(state, (s) => ({ ...s, usage }))\n }\n if (frame.serverContent !== undefined) return yield* contentToEvents(state, frame.serverContent)\n if (frame.toolCall !== undefined) {\n return yield* toolCallEvents(state, frame.toolCall.functionCalls ?? [])\n }\n const callIds = frame.toolCallCancellation?.ids ?? []\n if (callIds.length > 0) return [RealtimeEvent.ToolCallCancelled({ callIds })]\n if (frame.goAway !== undefined) {\n const timeLeft = parseTimeLeft(frame.goAway.timeLeft)\n yield* Ref.update(state, (s) => ({ ...s, goingAway: true }))\n return [RealtimeEvent.SessionEnding({ ...(timeLeft !== undefined && { timeLeft }) })]\n }\n // An unresumable update carries an empty handle; it must not replace one.\n const handle = frame.sessionResumptionUpdate?.newHandle\n return frame.sessionResumptionUpdate?.resumable === true &&\n handle !== undefined &&\n handle !== \"\"\n ? [RealtimeEvent.ResumptionHandle({ handle })]\n : []\n })\n\n// ---------------------------------------------------------------------------\n// Input handling\n// ---------------------------------------------------------------------------\n\nconst droppedActivity = (field: string) =>\n Capabilities.warnDropped({\n provider: PROVIDER,\n capability: \"manualTurns\",\n field,\n reason: \"The session uses automatic activity detection; boundaries are the server's.\",\n })\n\nconst videoPart = (\n frame: Extract<RealtimeInput, { readonly _tag: \"VideoFrame\" }>[\"frame\"],\n): Effect.Effect<{ readonly data: string; readonly mimeType: string }, AiError.AiError> =>\n Match.value(frame).pipe(\n Match.tag(\"base64\", (s) => Effect.succeed({ data: s.base64, mimeType: s.mimeType })),\n Match.tag(\"bytes\", (s) =>\n Effect.succeed({ data: Base64.encode(s.bytes), mimeType: s.mimeType }),\n ),\n Match.tag(\"url\", () =>\n Effect.fail(\n new AiError.Unsupported({\n provider: PROVIDER,\n capability: \"videoInput\",\n reason: \"A live video frame must carry its bytes; a URL would need the Files API.\",\n }),\n ),\n ),\n Match.exhaustive,\n )\n\n/** Wire spelling of `toolScheduling`. */\ntype ToolScheduling = \"WHEN_IDLE\" | \"INTERRUPT\" | \"SILENT\"\n\nconst schedulingOf = (request: GeminiLiveRequest): ToolScheduling =>\n Match.value(request.toolScheduling ?? \"when-idle\").pipe(\n Match.when(\"when-idle\", () => \"WHEN_IDLE\" as const),\n Match.when(\"interrupt\", () => \"INTERRUPT\" as const),\n Match.when(\"silent\", () => \"SILENT\" as const),\n Match.exhaustive,\n )\n\nconst handleInput = (\n send: (frame: string) => Effect.Effect<void, AiError.AiError>,\n state: Ref.Ref<SessionState>,\n options: {\n readonly manual: boolean\n readonly rate: number\n readonly scheduling: ToolScheduling\n },\n input: RealtimeInput,\n): Effect.Effect<void, AiError.AiError> =>\n Match.value(input).pipe(\n Match.tag(\"Audio\", (i) =>\n send(\n JSON.stringify({\n realtimeInput: {\n audio: {\n data: Base64.encode(i.bytes),\n mimeType: `audio/pcm;rate=${options.rate}`,\n },\n },\n }),\n ),\n ),\n Match.tag(\"Text\", (i) =>\n i.role === \"system\"\n ? Effect.fail(\n new AiError.Unsupported({\n provider: PROVIDER,\n capability: \"systemTurn\",\n reason: \"Gemini Live fixes the system instruction at setup for the connection.\",\n }),\n )\n : send(JSON.stringify({ realtimeInput: { text: i.text } })),\n ),\n Match.tag(\"ToolResult\", (i) =>\n Effect.gen(function* () {\n const name = (yield* Ref.get(state)).callNames[i.output.call_id]\n if (name === undefined) {\n return yield* Effect.fail(\n new AiError.InvalidRequest({\n provider: PROVIDER,\n raw: `no tool call ${i.output.call_id} in this session`,\n }),\n )\n }\n // Answering resumes generation on its own; there is no turn to ask\n // for. `scheduling` says when, and matters on a model that kept\n // generating while the tool ran.\n yield* send(\n JSON.stringify({\n toolResponse: {\n functionResponses: [\n {\n id: i.output.call_id,\n name,\n ...functionResponseBody(i.output),\n scheduling: options.scheduling,\n },\n ],\n },\n }),\n )\n }),\n ),\n Match.tag(\"Interrupt\", () =>\n Effect.fail(\n new AiError.Unsupported({\n provider: PROVIDER,\n capability: \"interrupt\",\n reason:\n 'Gemini Live has no cancel op: the server interrupts when it hears speech. Pass activityHandling: \"none\" to turn that off.',\n }),\n ),\n ),\n Match.tag(\"VideoFrame\", (i) =>\n Effect.flatMap(videoPart(i.frame), (video) =>\n send(JSON.stringify({ realtimeInput: { video } })),\n ),\n ),\n Match.tag(\"ActivityStart\", () =>\n options.manual\n ? send(JSON.stringify({ realtimeInput: { activityStart: {} } }))\n : droppedActivity(\"ActivityStart\"),\n ),\n Match.tag(\"ActivityEnd\", () =>\n options.manual\n ? send(JSON.stringify({ realtimeInput: { activityEnd: {} } }))\n : droppedActivity(\"ActivityEnd\"),\n ),\n // Interruption already drops what was never generated, but audio the\n // server did send stays in history whether or not it was heard.\n Match.tag(\"PlaybackPosition\", () =>\n Capabilities.warnDropped({\n provider: PROVIDER,\n capability: \"truncate\",\n field: \"PlaybackPosition\",\n reason: \"Gemini Live has no truncate op, so the unheard tail stays in context.\",\n }),\n ),\n Match.exhaustive,\n )\n\n// ---------------------------------------------------------------------------\n// Connection\n// ---------------------------------------------------------------------------\n\nexport const buildWsUrl = (cfg: Config): string => {\n const url = new URL(cfg.baseUrl ?? DEFAULT_BASE_URL)\n url.protocol = url.protocol === \"http:\" ? \"ws:\" : \"wss:\"\n url.pathname = `${url.pathname.replace(/\\/$/, \"\")}/${RPC}`\n url.searchParams.set(\"key\", Redacted.value(cfg.apiKey))\n return url.toString()\n}\n\nconst withSocket =\n (cfg: Config) =>\n <A, E, R>(\n effect: Effect.Effect<A, E, R | Socket.WebSocketConstructor>,\n ): Effect.Effect<A, E, Exclude<R, Socket.WebSocketConstructor>> =>\n cfg.webSocket === undefined\n ? Effect.provide(effect, Socket.layerWebSocketConstructorGlobal)\n : Effect.provideService(effect, Socket.WebSocketConstructor, cfg.webSocket)\n\n/** Drop frames until `setupComplete`; a close before it is a failed handshake. */\nconst awaitSetupComplete = (\n session: WebSocketSession.WebSocketSession<ServerMessage>,\n): Effect.Effect<void, AiError.AiError> =>\n Queue.take(session.frames).pipe(\n Effect.catchCause((cause) =>\n Pull.isDoneCause(cause)\n ? Effect.fail(\n new AiError.Unavailable({\n provider: PROVIDER,\n raw: \"the socket closed during the handshake\",\n }),\n )\n : Effect.failCause(cause as Cause.Cause<AiError.AiError>),\n ),\n Effect.flatMap((frame) =>\n frame.setupComplete === undefined ? awaitSetupComplete(session) : Effect.void,\n ),\n )\n\nexport const openSession =\n (cfg: Config) =>\n (\n request: GeminiLiveRequest,\n ): Effect.Effect<RealtimeSessionHandle, AiError.AiError, Scope.Scope> =>\n Effect.gen(function* () {\n if (request.googleSearch === true && (request.tools?.length ?? 0) > 0) {\n return yield* Effect.fail(\n new AiError.InvalidRequest({\n provider: PROVIDER,\n raw: \"Gemini rejects googleSearch alongside function declarations; pick one per session.\",\n }),\n )\n }\n const rate = yield* inputRate(request.inputFormat)\n const scheduling = schedulingOf(request)\n yield* checkOutputFormat(request.outputFormat)\n\n const session = yield* WebSocketSession.open({\n url: buildWsUrl(cfg),\n provider: PROVIDER,\n schema: ServerMessage,\n openTimeout: Duration.seconds(30),\n }).pipe(withSocket(cfg))\n\n // The client speaks first here, and nothing else may be sent until the\n // server has accepted the configuration.\n yield* session.send(setupFrame(request))\n yield* awaitSetupComplete(session)\n\n const history = request.history ?? []\n if (history.length > 0) {\n const seed = yield* historyFrame(history)\n yield* session.send(seed)\n }\n\n const state = yield* Ref.make(initialState)\n const out = yield* Queue.bounded<RealtimeEvent, AiError.AiError | Cause.Done>(128)\n\n const pump = Stream.fromQueue(session.frames).pipe(\n Stream.mapEffect((frame) => toEvents(state, frame)),\n Stream.runForEach((events) => Queue.offerAll(out, events)),\n )\n\n // A close never synthesizes a `ResponseDone`; a turn still in flight\n // ends the stream as an incomplete turn instead.\n const finish = (cause: Cause.Cause<AiError.AiError> | undefined) =>\n Effect.gen(function* () {\n const { goingAway, responseId } = yield* Ref.get(state)\n if (responseId !== undefined) {\n return yield* Queue.fail(\n out,\n new AiError.IncompleteTurn({ raw: cause ?? \"the socket closed mid turn\" }),\n )\n }\n // `goAway` was the server announcing this close, so the session hit\n // its cap. Reconnecting works; retrying this session does not.\n if (goingAway) {\n return yield* Queue.fail(\n out,\n new AiError.SessionExpired({\n provider: PROVIDER,\n raw: cause ?? \"the socket closed after the server announced it was going away\",\n }),\n )\n }\n return yield* cause !== undefined && Cause.hasFails(cause)\n ? Queue.failCause(out, cause)\n : Queue.end(out)\n })\n\n yield* pump.pipe(\n Effect.matchCauseEffect({ onFailure: finish, onSuccess: () => finish(undefined) }),\n Effect.forkScoped,\n )\n\n const manual = request.turnDetection === \"manual\"\n return {\n send: (input) => handleInput(session.send, state, { manual, rate, scheduling }, input),\n events: Stream.fromQueue(out),\n }\n })\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAyCA,MAAM,WAAW;AAEjB,MAAM,mBAAmB;AACzB,MAAM,MAAM;;AA2DZ,MAAM,cAAc;AAEpB,MAAM,qBAAqB,QAAqB,OAAe,aAC7D,IAAI,QAAQ,YAAY;CACtB,UAAU;CACV,YAAY;CACZ,QAAQ,uBAAuB,SAAS,QAAQ,KAAK,UAAU,MAAM,EAAE;AACzE,CAAC;;AAGH,MAAM,aAAa,WACjB,MAAM,MAAM,MAAM,CAAC,CAAC,KAClB,MAAM,KAAK;CAAE,WAAW;CAAO,UAAU;CAAa,UAAU;AAAE,IAAI,MACpE,OAAO,QAAQ,EAAE,UAAU,CAC7B,GACA,MAAM,QAAQ,MACZ,OAAO,KAAK,kBAAkB,GAAG,eAAe,4BAA4B,CAAC,CAC/E,CACF;AAEF,MAAM,qBAAqB,WACzB,MAAM,MAAM,MAAM,CAAC,CAAC,KAClB,MAAM,KACJ;CAAE,WAAW;CAAO,UAAU;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M,SAAS,KAAK,IACrD,MAAM,UAAU,KAAK,KAAK,IAC1B,MAAM,IAAI,GAAG;CACnB,CAAC;CAEH,OAAO,KAAK,KACV,OAAO,iBAAiB;EAAE,WAAW;EAAQ,iBAAiB,OAAO,KAAA,CAAS;CAAE,CAAC,GACjF,OAAO,UACT;CAEA,MAAM,SAAS,QAAQ,kBAAkB;CACzC,OAAO;EACL,OAAO,UAAU,YAAY,QAAQ,MAAM,OAAO;GAAE;GAAQ;GAAM;EAAW,GAAG,KAAK;EACrF,QAAQ,OAAO,UAAU,GAAG;CAC9B;AACF,CAAC"}
|
|
@@ -8,6 +8,7 @@ import * as Cause$1 from "effect/Cause";
|
|
|
8
8
|
import * as Duration$1 from "effect/Duration";
|
|
9
9
|
import * as Effect$1 from "effect/Effect";
|
|
10
10
|
import * as Exit from "effect/Exit";
|
|
11
|
+
import "effect/Fiber";
|
|
11
12
|
import { constVoid, flow, pipe as pipe$1 } from "effect/Function";
|
|
12
13
|
import * as Layer$1 from "effect/Layer";
|
|
13
14
|
import * as Schedule from "effect/Schedule";
|
|
@@ -15,30 +16,31 @@ import * as Scope$1 from "effect/Scope";
|
|
|
15
16
|
import * as TestClock from "effect/testing/TestClock";
|
|
16
17
|
import * as TestConsole from "effect/testing/TestConsole";
|
|
17
18
|
import * as FakeWebSocket from "@effect-uai/core/testing/FakeWebSocket";
|
|
18
|
-
//#region ../../../node_modules/.pnpm/@effect+vitest@4.
|
|
19
|
+
//#region ../../../node_modules/.pnpm/@effect+vitest@4.1.2_effect@4.1.2_vitest@5.0.3_@types+node@26.2.0_vite@7.3.2_@types+nod_a2fffe0deec250ef9d6207b86a9a1ab8/node_modules/@effect/vitest/dist/internal/internal.js
|
|
19
20
|
/**
|
|
20
21
|
* @since 4.0.0
|
|
21
22
|
*/
|
|
22
23
|
const getCurrentSuite = TestRunner.getCurrentSuite;
|
|
23
|
-
const runPromise =
|
|
24
|
-
const exit = yield* Effect$1.exit(effect);
|
|
24
|
+
const runPromise = (effect, ctx) => Effect$1.runPromise(Effect$1.uninterruptibleMask((restore) => Effect$1.gen(function* () {
|
|
25
|
+
const exit = yield* Effect$1.exit(restore(effect));
|
|
25
26
|
if (Exit.isFailure(exit)) {
|
|
26
27
|
const errors = Cause$1.prettyErrors(exit.cause);
|
|
27
28
|
for (let i = 0; i < errors.length; i++) yield* Effect$1.logError(errors[i]);
|
|
28
29
|
}
|
|
29
30
|
return yield* exit;
|
|
30
|
-
}
|
|
31
|
+
})), { signal: ctx?.signal });
|
|
32
|
+
const settleOnAbort = (ctx, promise) => {
|
|
33
|
+
const onAbort = () => ctx.onTestFinished(() => promise.then(constVoid, constVoid));
|
|
34
|
+
ctx.signal.addEventListener("abort", onAbort, { once: true });
|
|
35
|
+
const cleanup = () => ctx.signal.removeEventListener("abort", onAbort);
|
|
36
|
+
promise.then(cleanup, cleanup);
|
|
37
|
+
if (ctx.signal.aborted) onAbort();
|
|
38
|
+
return promise;
|
|
39
|
+
};
|
|
31
40
|
/** @internal */
|
|
32
41
|
const runTest = (ctx) => (effect) => {
|
|
33
42
|
const promise = runPromise(effect, ctx);
|
|
34
|
-
|
|
35
|
-
const onAbort = () => ctx.onTestFinished(() => promise.then(constVoid, constVoid));
|
|
36
|
-
ctx.signal.addEventListener("abort", onAbort, { once: true });
|
|
37
|
-
const cleanup = () => ctx.signal.removeEventListener("abort", onAbort);
|
|
38
|
-
promise.then(cleanup, cleanup);
|
|
39
|
-
if (ctx.signal.aborted) onAbort();
|
|
40
|
-
}
|
|
41
|
-
return promise;
|
|
43
|
+
return ctx ? settleOnAbort(ctx, promise) : promise;
|
|
42
44
|
};
|
|
43
45
|
const TestEnv = /*#__PURE__*/ Layer$1.mergeAll(TestConsole.layer, /*#__PURE__*/ TestClock.layer());
|
|
44
46
|
/** @internal */
|
|
@@ -74,6 +76,9 @@ const collectTasks = (tasks, acc = []) => {
|
|
|
74
76
|
}
|
|
75
77
|
return acc;
|
|
76
78
|
};
|
|
79
|
+
const runsMoreThanOnce = (task) => {
|
|
80
|
+
return ((typeof task.retry === "number" ? task.retry : task.retry?.count) ?? 0) > 0 || (task.repeats ?? 0) > 0;
|
|
81
|
+
};
|
|
77
82
|
const registerProp = (it, name, arbitraries, property, timeout) => {
|
|
78
83
|
const arbitrary = makeArbitrary(arbitraries);
|
|
79
84
|
it(name, testOptions(timeout), withFixturesOf(() => {}, (ctx) => runCheck(ctx, arbitrary, (values) => property(values, ctx), checkOptions(timeout))));
|
|
@@ -93,11 +98,35 @@ const makeTester = (mapEffect, it) => {
|
|
|
93
98
|
prop
|
|
94
99
|
});
|
|
95
100
|
};
|
|
96
|
-
const makeProp = (it) => (name, arbitraries, self, timeout) => registerProp(it, name, arbitraries, (values, ctx) =>
|
|
101
|
+
const makeProp = (it) => (name, arbitraries, self, timeout) => registerProp(it, name, arbitraries, (values, ctx) => {
|
|
102
|
+
const result = self(values, ctx);
|
|
103
|
+
if (result !== void 0 && typeof result !== "boolean") throw new TypeError("it.prop callbacks must return boolean or void. Use it.effect.prop for Effect callbacks.");
|
|
104
|
+
return result !== false;
|
|
105
|
+
}, timeout);
|
|
97
106
|
const makeLayer = (it) => (layer_, options) => (...args) => {
|
|
98
107
|
const excludeTestServices = options?.excludeTestServices ?? false;
|
|
99
108
|
const withTestEnv = excludeTestServices ? layer_ : Layer$1.provideMerge(layer_, TestEnv);
|
|
100
109
|
const memoMap = options?.memoMap ?? Effect$1.runSync(Layer$1.makeMemoMap);
|
|
110
|
+
const buildPerTest = options?.buildPerTest ?? false;
|
|
111
|
+
const suiteOptions = options?.concurrent === void 0 ? {} : { concurrent: options.concurrent };
|
|
112
|
+
const makeBlockMethods = (provide) => makeItProxy(it, {
|
|
113
|
+
effect: makeTester((effect) => provide(Effect$1.scoped(effect)), it),
|
|
114
|
+
prop: makeProp(it),
|
|
115
|
+
flakyTest,
|
|
116
|
+
layer(nestedLayer, nestedOptions) {
|
|
117
|
+
return makeLayer(it)(Layer$1.provideMerge(nestedLayer, withTestEnv), {
|
|
118
|
+
...nestedOptions,
|
|
119
|
+
memoMap: Layer$1.forkMemoMapUnsafe(memoMap),
|
|
120
|
+
excludeTestServices,
|
|
121
|
+
buildPerTest: buildPerTest || nestedOptions?.buildPerTest === true
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
if (buildPerTest) {
|
|
126
|
+
const methods = makeBlockMethods((effect) => Effect$1.scopedWith((scope) => Layer$1.buildWithMemoMap(withTestEnv, Layer$1.forkMemoMapUnsafe(memoMap), scope).pipe(Effect$1.orDie, Effect$1.flatMap((context) => Effect$1.provide(effect, context)))));
|
|
127
|
+
if (args.length === 1) return args[0](methods);
|
|
128
|
+
return describe(args[0], suiteOptions, () => args[1](methods));
|
|
129
|
+
}
|
|
101
130
|
const scope = Effect$1.runSync(Scope$1.make());
|
|
102
131
|
const contextEffect = Layer$1.buildWithMemoMap(withTestEnv, memoMap, scope).pipe(Effect$1.orDie, Effect$1.cached, Effect$1.runSync);
|
|
103
132
|
let closed = false;
|
|
@@ -106,18 +135,7 @@ const makeLayer = (it) => (layer_, options) => (...args) => {
|
|
|
106
135
|
closed = true;
|
|
107
136
|
return runPromise(Scope$1.close(scope, Exit.void));
|
|
108
137
|
};
|
|
109
|
-
const methods =
|
|
110
|
-
effect: makeTester((effect) => Effect$1.flatMap(contextEffect, (context) => effect.pipe(Effect$1.scoped, Effect$1.provide(context))), it),
|
|
111
|
-
prop: makeProp(it),
|
|
112
|
-
flakyTest,
|
|
113
|
-
layer(nestedLayer, options) {
|
|
114
|
-
return makeLayer(it)(Layer$1.provideMerge(nestedLayer, withTestEnv), {
|
|
115
|
-
...options,
|
|
116
|
-
memoMap: Layer$1.forkMemoMapUnsafe(memoMap),
|
|
117
|
-
excludeTestServices
|
|
118
|
-
});
|
|
119
|
-
}
|
|
120
|
-
});
|
|
138
|
+
const methods = makeBlockMethods((effect) => Effect$1.flatMap(contextEffect, (context) => Effect$1.provide(effect, context)));
|
|
121
139
|
if (args.length === 1) {
|
|
122
140
|
const currentSuite = getCurrentSuite();
|
|
123
141
|
const previousTasks = new Set(currentSuite.tasks);
|
|
@@ -128,10 +146,11 @@ const makeLayer = (it) => (layer_, options) => (...args) => {
|
|
|
128
146
|
return;
|
|
129
147
|
}
|
|
130
148
|
const blockTaskSet = new Set(blockTasks);
|
|
149
|
+
const releaseAfterLastTest = !blockTasks.some(runsMoreThanOnce);
|
|
131
150
|
let remaining = blockTasks.length;
|
|
132
151
|
beforeEach(({ onTestFinished, signal, task }) => {
|
|
133
152
|
if (!blockTaskSet.has(task)) return;
|
|
134
|
-
onTestFinished(() => {
|
|
153
|
+
if (releaseAfterLastTest) onTestFinished(() => {
|
|
135
154
|
remaining--;
|
|
136
155
|
if (remaining === 0) return closeScope();
|
|
137
156
|
});
|
|
@@ -140,7 +159,6 @@ const makeLayer = (it) => (layer_, options) => (...args) => {
|
|
|
140
159
|
afterAll(() => closeScope(), hookTimeout(options?.timeout));
|
|
141
160
|
return;
|
|
142
161
|
}
|
|
143
|
-
const suiteOptions = options?.concurrent === void 0 ? {} : { concurrent: options.concurrent };
|
|
144
162
|
return describe(args[0], suiteOptions, () => {
|
|
145
163
|
beforeAll(() => runPromise(Effect$1.asVoid(contextEffect)), hookTimeout(options?.timeout));
|
|
146
164
|
afterAll(() => closeScope(), hookTimeout(options?.timeout));
|
|
@@ -162,11 +180,12 @@ const it$1 = /*#__PURE__*/ makeMethods(it$2);
|
|
|
162
180
|
/** @internal */
|
|
163
181
|
const { effect, layer, live, prop } = it$1;
|
|
164
182
|
//#endregion
|
|
165
|
-
//#region ../../../node_modules/.pnpm/@effect+vitest@4.
|
|
166
|
-
/**
|
|
167
|
-
* @since 4.0.0
|
|
168
|
-
*/
|
|
183
|
+
//#region ../../../node_modules/.pnpm/@effect+vitest@4.1.2_effect@4.1.2_vitest@5.0.3_@types+node@26.2.0_vite@7.3.2_@types+nod_a2fffe0deec250ef9d6207b86a9a1ab8/node_modules/@effect/vitest/dist/index.js
|
|
169
184
|
/**
|
|
185
|
+
* Vitest test API extended with Effect test helpers.
|
|
186
|
+
*
|
|
187
|
+
* @stability unstable
|
|
188
|
+
* @category testing
|
|
170
189
|
* @since 4.0.0
|
|
171
190
|
*/
|
|
172
191
|
const it = it$1;
|
|
@@ -326,7 +345,10 @@ describe("Gemini live session tools", () => {
|
|
|
326
345
|
yield* handle.send(RealtimeInput.ToolResult({ output: {
|
|
327
346
|
type: "function_call_output",
|
|
328
347
|
call_id: "fc_1",
|
|
329
|
-
output:
|
|
348
|
+
output: [{
|
|
349
|
+
type: "input_json",
|
|
350
|
+
value: { celsius: 32 }
|
|
351
|
+
}],
|
|
330
352
|
providerData: void 0
|
|
331
353
|
} }));
|
|
332
354
|
yield* server.push({ serverContent: { turnComplete: true } });
|
|
@@ -338,7 +360,7 @@ describe("Gemini live session tools", () => {
|
|
|
338
360
|
globalExpect(response.toolResponse.functionResponses).toEqual([{
|
|
339
361
|
id: "fc_1",
|
|
340
362
|
name: "get_weather",
|
|
341
|
-
response: { celsius: 32 },
|
|
363
|
+
response: { output: { celsius: 32 } },
|
|
342
364
|
scheduling: "WHEN_IDLE"
|
|
343
365
|
}]);
|
|
344
366
|
}).pipe(Effect.scoped));
|
|
@@ -354,7 +376,10 @@ describe("Gemini live session tools", () => {
|
|
|
354
376
|
yield* handle.send(RealtimeInput.ToolResult({ output: {
|
|
355
377
|
type: "function_call_output",
|
|
356
378
|
call_id: "fc_1",
|
|
357
|
-
output:
|
|
379
|
+
output: [{
|
|
380
|
+
type: "input_json",
|
|
381
|
+
value: {}
|
|
382
|
+
}],
|
|
358
383
|
providerData: void 0
|
|
359
384
|
} }));
|
|
360
385
|
}), { request: {
|
|
@@ -373,7 +398,10 @@ describe("Gemini live session tools", () => {
|
|
|
373
398
|
const failure = yield* Effect.exit(handle.send(RealtimeInput.ToolResult({ output: {
|
|
374
399
|
type: "function_call_output",
|
|
375
400
|
call_id: "fc_unknown",
|
|
376
|
-
output:
|
|
401
|
+
output: [{
|
|
402
|
+
type: "input_json",
|
|
403
|
+
value: {}
|
|
404
|
+
}],
|
|
377
405
|
providerData: void 0
|
|
378
406
|
} })));
|
|
379
407
|
globalExpect(failure._tag).toBe("Failure");
|
|
@@ -453,7 +481,10 @@ describe("Gemini live session history", () => {
|
|
|
453
481
|
{
|
|
454
482
|
type: "function_call_output",
|
|
455
483
|
call_id: "fc_1",
|
|
456
|
-
output:
|
|
484
|
+
output: [{
|
|
485
|
+
type: "input_json",
|
|
486
|
+
value: { now: "12:00" }
|
|
487
|
+
}],
|
|
457
488
|
providerData: void 0
|
|
458
489
|
}
|
|
459
490
|
]
|