@effect-uai/inworld 0.16.0 → 0.18.0

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.
Files changed (48) hide show
  1. package/dist/InworldRealtimeSynthesizer.d.mts +1 -1
  2. package/dist/InworldRealtimeSynthesizer.mjs +2 -2
  3. package/dist/InworldRealtimeSynthesizer.mjs.map +1 -1
  4. package/dist/InworldRealtimeTranscriber.d.mts +1 -1
  5. package/dist/InworldRealtimeTranscriber.mjs +2 -2
  6. package/dist/InworldRealtimeTranscriber.mjs.map +1 -1
  7. package/dist/InworldSynthesizer.d.mts +1 -1
  8. package/dist/InworldSynthesizer.mjs +2 -2
  9. package/dist/InworldSynthesizer.mjs.map +1 -1
  10. package/dist/InworldTranscriber.d.mts +1 -1
  11. package/dist/InworldTranscriber.d.mts.map +1 -1
  12. package/dist/InworldTranscriber.mjs +5 -4
  13. package/dist/InworldTranscriber.mjs.map +1 -1
  14. package/dist/codec-BU4azW_k.d.mts.map +1 -1
  15. package/dist/codec.mjs +4 -3
  16. package/dist/codec.mjs.map +1 -1
  17. package/dist/dist-Cu2gIMHV.mjs +17453 -0
  18. package/dist/dist-Cu2gIMHV.mjs.map +1 -0
  19. package/dist/dist-DHer1cql.mjs +1533 -0
  20. package/dist/dist-DHer1cql.mjs.map +1 -0
  21. package/dist/realtimeStt.d.mts +6 -0
  22. package/dist/realtimeStt.d.mts.map +1 -1
  23. package/dist/realtimeStt.mjs +18 -19
  24. package/dist/realtimeStt.mjs.map +1 -1
  25. package/dist/realtimeStt.test.d.mts +1 -0
  26. package/dist/realtimeStt.test.mjs +83 -0
  27. package/dist/realtimeStt.test.mjs.map +1 -0
  28. package/dist/realtimeTts.d.mts +6 -0
  29. package/dist/realtimeTts.d.mts.map +1 -1
  30. package/dist/realtimeTts.mjs +18 -17
  31. package/dist/realtimeTts.mjs.map +1 -1
  32. package/dist/realtimeTts.test.d.mts +1 -0
  33. package/dist/realtimeTts.test.mjs +70 -0
  34. package/dist/realtimeTts.test.mjs.map +1 -0
  35. package/dist/{rolldown-runtime-D7D4PA-g.mjs → rolldown-runtime-EAsCxrDo.mjs} +2 -1
  36. package/dist/wsAuth.d.mts +1 -1
  37. package/dist/wsAuth.mjs.map +1 -1
  38. package/package.json +4 -4
  39. package/src/InworldRealtimeSynthesizer.ts +1 -1
  40. package/src/InworldRealtimeTranscriber.ts +1 -1
  41. package/src/InworldSynthesizer.ts +1 -1
  42. package/src/InworldTranscriber.ts +4 -3
  43. package/src/codec.ts +3 -2
  44. package/src/realtimeStt.test.ts +82 -0
  45. package/src/realtimeStt.ts +46 -36
  46. package/src/realtimeTts.test.ts +74 -0
  47. package/src/realtimeTts.ts +52 -36
  48. package/src/wsAuth.ts +1 -1
@@ -1,9 +1,10 @@
1
1
  import { audioConfigFor, decodeAudioContent, defaultFormat } from "./codec.mjs";
2
2
  import { authedWsConstructor } from "./wsAuth.mjs";
3
- import { Effect, Queue, Schema, Stream } from "effect";
3
+ import { Effect, Option, Predicate, Schema, Stream } from "effect";
4
4
  import * as AiError from "@effect-uai/core/AiError";
5
5
  import * as JSONL from "@effect-uai/core/JSONL";
6
- import * as Socket from "effect/unstable/socket/Socket";
6
+ import * as Socket from "effect/socket/Socket";
7
+ import * as WebSocketSession from "@effect-uai/core/WebSocketSession";
7
8
  //#region src/realtimeTts.ts
8
9
  /**
9
10
  * Inworld Realtime TTS — `wss://api.inworld.ai/tts/v1/voice:streamBidirectional`.
@@ -61,20 +62,24 @@ const ServerFrame = Schema.Struct({
61
62
  error: Schema.optional(Schema.Unknown)
62
63
  });
63
64
  const decodeServerFrame = Schema.decodeUnknownEffect(ServerFrame);
64
- const handleServerFrame = (queue) => (raw) => Effect.gen(function* () {
65
+ /** One raw text frame to at most one audio chunk; error frames are logged, not raised. */
66
+ const frameToChunk = (raw) => Effect.gen(function* () {
65
67
  const json = yield* JSONL.parseSafe(raw);
66
- if (json === void 0) return;
68
+ if (json === void 0) return void 0;
67
69
  const decoded = yield* decodeServerFrame(json).pipe(Effect.option);
68
- if (decoded._tag === "None") return;
70
+ if (Option.isNone(decoded)) return void 0;
69
71
  const frame = decoded.value;
70
72
  if (frame.error !== void 0) {
71
73
  yield* Effect.logWarning("[inworld-tts] server error frame", { error: frame.error });
72
74
  return;
73
75
  }
74
76
  const audio = frame.result?.audioChunk?.audioContent;
75
- if (audio === void 0 || audio === "") return;
77
+ if (audio === void 0 || audio === "") return void 0;
76
78
  const bytes = yield* decodeAudioContent(audio).pipe(Effect.option);
77
- if (bytes._tag === "Some") yield* Queue.offer(queue, { bytes: bytes.value });
79
+ return Option.match(bytes, {
80
+ onNone: () => void 0,
81
+ onSome: (b) => ({ bytes: b })
82
+ });
78
83
  });
79
84
  const streamSynthesis = (cfg) => (textIn, request) => Stream.unwrap(Effect.gen(function* () {
80
85
  if (request.pronunciations !== void 0 && request.pronunciations.length > 0) return Stream.fail(new AiError.Unsupported({
@@ -83,16 +88,12 @@ const streamSynthesis = (cfg) => (textIn, request) => Stream.unwrap(Effect.gen(f
83
88
  reason: "Inworld applies inline /ipa/ pronunciations on sync `synthesize` only; the incremental WS path streams text in chunks with no place to rewrite. Use `synthesize` for pronunciation overrides."
84
89
  }));
85
90
  const create = yield* createFrame(request);
86
- const socket = yield* Socket.makeWebSocket(buildWsUrl(cfg), { closeCodeIsError: (code) => code !== 1e3 && code !== 1001 && code !== 1005 }).pipe(Effect.provideService(Socket.WebSocketConstructor, authedWsConstructor(cfg.apiKey)));
87
- const queue = yield* Queue.bounded(64);
88
- const write = yield* socket.writer;
89
- yield* Effect.gen(function* () {
90
- yield* write(create);
91
- yield* Stream.runForEach(textIn, (text) => text.length === 0 ? Effect.void : write(sendTextFrame(text)));
92
- yield* write(closeContextFrame);
93
- }).pipe(Effect.ignore, Effect.forkScoped);
94
- yield* socket.runString(handleServerFrame(queue)).pipe(Effect.ensuring(Queue.end(queue)), Effect.forkScoped);
95
- return Stream.fromQueue(queue);
91
+ const socket = yield* Socket.makeWebSocket(buildWsUrl(cfg)).pipe(Effect.provideService(Socket.WebSocketConstructor, cfg.webSocket ?? authedWsConstructor(cfg.apiKey)));
92
+ const { write } = yield* socket.writer;
93
+ const asAiError = WebSocketSession.toAiError("inworld");
94
+ const chunks = Stream.fromPull(Socket.readerString(socket)).pipe(Stream.scoped, Stream.catchIf(WebSocketSession.isCleanClose, () => Stream.empty), Stream.mapError(asAiError), Stream.mapEffect(frameToChunk), Stream.filter(Predicate.isNotUndefined));
95
+ const outgoing = Stream.concat(Stream.make(create), Stream.concat(textIn.pipe(Stream.filter((text) => text.length > 0), Stream.map(sendTextFrame)), Stream.make(closeContextFrame))).pipe(Stream.mapEffect((frame) => Effect.mapError(write(frame), asAiError)));
96
+ return Stream.merge(chunks, Stream.drain(outgoing), { haltStrategy: "left" });
96
97
  }));
97
98
  //#endregion
98
99
  export { buildWsUrl, streamSynthesis };
@@ -1 +1 @@
1
- {"version":3,"file":"realtimeTts.mjs","names":[],"sources":["../src/realtimeTts.ts"],"sourcesContent":["/**\n * Inworld Realtime TTS — `wss://api.inworld.ai/tts/v1/voice:streamBidirectional`.\n *\n * Auth: `Authorization: Basic <API_KEY>` on the WS upgrade header (matches\n * Inworld's own JS sample). The docs mention a `?Authorization=…` query\n * variant but the server rejects it in practice. Setting headers needs the\n * `ws` peer dep (Node/Bun); see `./wsAuth.ts`.\n *\n * Wire shape (single-context per call):\n * client → server:\n * `{ \"create\": { voiceId, modelId, audioConfig, ... } }`\n * `{ \"send_text\": { \"text\": \"...\" } }` (repeated)\n * `{ \"close_context\": {} }` (text stream end)\n * server → client:\n * `{ \"result\": { \"contextCreated\": {...} } }` (handshake ack)\n * `{ \"result\": { \"audioChunk\": { \"audioContent\": \"<b64>\" } } }` (×N)\n * `{ \"result\": { \"contextClosed\": {...} } }` (final)\n *\n * Multi-context (`contextId`) is not surfaced here — one logical\n * utterance per call.\n */\nimport { Cause, Effect, Queue, Redacted, Schema, Stream } from \"effect\"\nimport * as Socket from \"effect/unstable/socket/Socket\"\nimport * as AiError from \"@effect-uai/core/AiError\"\nimport type { AudioChunk, AudioFormat } from \"@effect-uai/core/Audio\"\nimport * as JSONL from \"@effect-uai/core/JSONL\"\nimport type { CustomPronunciation } from \"@effect-uai/core/SpeechSynthesizer\"\nimport { audioConfigFor, decodeAudioContent, defaultFormat } from \"./codec.js\"\nimport type { InworldDeliveryMode, InworldTtsModel, InworldVoiceId } from \"./models.js\"\nimport { authedWsConstructor } from \"./wsAuth.js\"\n\nexport type Config = { readonly apiKey: Redacted.Redacted; readonly baseUrl?: string }\n\n/**\n * Incremental-text-in request. Mirrors the sync request minus `text` (which\n * comes from the input stream).\n */\nexport type StreamSynthesizeRequest = {\n readonly model: InworldTtsModel\n readonly voiceId: InworldVoiceId\n readonly outputFormat?: AudioFormat\n readonly languageCode?: string\n readonly temperature?: number\n readonly deliveryMode?: InworldDeliveryMode\n readonly applyTextNormalization?: \"ON\" | \"OFF\"\n readonly speed?: number\n /** Carried from the Common request so the WS path can reject inline\n * pronunciations (no place to rewrite into a chunked text stream). */\n readonly pronunciations?: ReadonlyArray<CustomPronunciation>\n}\n\n// ---------------------------------------------------------------------------\n// URL + frame builders\n// ---------------------------------------------------------------------------\n\nexport const buildWsUrl = (cfg: Config) => {\n const wsBase = (cfg.baseUrl ?? \"https://api.inworld.ai\").replace(/^http/, \"ws\")\n return `${wsBase}/tts/v1/voice:streamBidirectional`\n}\n\n// TTS WS uses snake_case outbound (per Inworld's own JS sample). The\n// camelCase helper from `codec.ts` is built for the REST/JSON sync paths;\n// rewire the field names here for the WS path. Server responses come back\n// camelCase regardless — see `ResultBody` below.\nconst audioConfigSnake = (request: StreamSynthesizeRequest) =>\n Effect.map(audioConfigFor(request.outputFormat ?? defaultFormat, request.speed), (cfg) => ({\n audio_encoding: cfg.audioEncoding,\n ...(cfg.sampleRateHertz !== undefined && { sample_rate_hertz: cfg.sampleRateHertz }),\n ...(cfg.bitRate !== undefined && { bit_rate: cfg.bitRate }),\n ...(cfg.speakingRate !== undefined && { speaking_rate: cfg.speakingRate }),\n }))\n\nconst createFrame = (request: StreamSynthesizeRequest) =>\n Effect.map(audioConfigSnake(request), (audio_config) =>\n JSON.stringify({\n create: {\n voice_id: request.voiceId,\n model_id: request.model,\n audio_config,\n ...(request.languageCode !== undefined && { language: request.languageCode }),\n ...(request.deliveryMode !== undefined && { delivery_mode: request.deliveryMode }),\n ...(request.temperature !== undefined && { temperature: request.temperature }),\n ...(request.applyTextNormalization !== undefined && {\n apply_text_normalization: request.applyTextNormalization,\n }),\n },\n }),\n )\n\n// `flush_context: {}` inside `send_text` nudges the server to flush\n// pending audio promptly — matches the low-latency sample.\nconst sendTextFrame = (text: string) => JSON.stringify({ send_text: { text, flush_context: {} } })\nconst closeContextFrame = JSON.stringify({ close_context: {} })\n\n// ---------------------------------------------------------------------------\n// Wire schema (server → client)\n// ---------------------------------------------------------------------------\n\nconst ResultBody = Schema.Struct({\n audioChunk: Schema.optional(Schema.Struct({ audioContent: Schema.String })),\n contextCreated: Schema.optional(Schema.Unknown),\n flushCompleted: Schema.optional(Schema.Unknown),\n contextClosed: Schema.optional(Schema.Unknown),\n status: Schema.optional(Schema.Unknown),\n})\n\nconst ServerFrame = Schema.Struct({\n result: Schema.optional(ResultBody),\n error: Schema.optional(Schema.Unknown),\n})\nconst decodeServerFrame = Schema.decodeUnknownEffect(ServerFrame)\n\nconst handleServerFrame = (queue: Queue.Queue<AudioChunk, Cause.Done>) => (raw: string) =>\n Effect.gen(function* () {\n const json = yield* JSONL.parseSafe(raw)\n if (json === undefined) return\n const decoded = yield* decodeServerFrame(json).pipe(Effect.option)\n if (decoded._tag === \"None\") return\n const frame = decoded.value\n if (frame.error !== undefined) {\n yield* Effect.logWarning(\"[inworld-tts] server error frame\", { error: frame.error })\n return\n }\n const audio = frame.result?.audioChunk?.audioContent\n if (audio === undefined || audio === \"\") return\n const bytes = yield* decodeAudioContent(audio).pipe(Effect.option)\n if (bytes._tag === \"Some\") yield* Queue.offer(queue, { bytes: bytes.value })\n })\n\n// ---------------------------------------------------------------------------\n// Stream<string> → Stream<AudioChunk>\n// ---------------------------------------------------------------------------\n\nexport const streamSynthesis =\n (cfg: Config) =>\n <E, R>(\n textIn: Stream.Stream<string, E, R>,\n request: StreamSynthesizeRequest,\n ): Stream.Stream<AudioChunk, AiError.AiError | E, R> =>\n Stream.unwrap(\n Effect.gen(function* () {\n // Inline pronunciations are load-bearing (bucket 1) but there is no\n // single text to rewrite `/ipa/` into on a chunked stream. Reject\n // rather than silently drop; use sync `synthesize` for overrides.\n if (request.pronunciations !== undefined && request.pronunciations.length > 0) {\n return Stream.fail(\n new AiError.Unsupported({\n provider: \"inworld\",\n capability: \"pronunciations\",\n reason:\n \"Inworld applies inline /ipa/ pronunciations on sync `synthesize` only; the incremental WS path streams text in chunks with no place to rewrite. Use `synthesize` for pronunciation overrides.\",\n }),\n )\n }\n const create = yield* createFrame(request)\n const socket = yield* Socket.makeWebSocket(buildWsUrl(cfg), {\n // Effect's Socket treats all close codes as errors by default —\n // whitelist standard clean-close codes (1000 / 1001 / 1005).\n closeCodeIsError: (code) => code !== 1000 && code !== 1001 && code !== 1005,\n }).pipe(Effect.provideService(Socket.WebSocketConstructor, authedWsConstructor(cfg.apiKey)))\n const queue = yield* Queue.bounded<AudioChunk, Cause.Done>(64)\n const write = yield* socket.writer\n\n // Writer: BOS `create` → drain text as `send_text` → `close_context`.\n // The reader fiber surfaces the remaining audio drained by the server\n // post-close and shuts the queue when the WS upstream closes.\n yield* Effect.gen(function* () {\n yield* write(create)\n yield* Stream.runForEach(textIn, (text) =>\n text.length === 0 ? Effect.void : write(sendTextFrame(text)),\n )\n yield* write(closeContextFrame)\n }).pipe(Effect.ignore, Effect.forkScoped)\n\n // `Queue.end` flushes pending chunks then fails the next take with\n // `Done` — clean stream end. `Queue.shutdown` would CLEAR queued items\n // and interrupt pending takes (wrong for graceful teardown).\n yield* socket\n .runString(handleServerFrame(queue))\n .pipe(Effect.ensuring(Queue.end(queue)), Effect.forkScoped)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ACzE,CAAC,CAAC,CAAC,KAAK,OAAO,eAAe,OAAO,sBAAsB,oBAAoB,IAAI,MAAM,CAAC,CAAC;CAC3F,MAAM,QAAQ,OAAO,MAAM,QAAgC,EAAE;CAC7D,MAAM,QAAQ,OAAO,OAAO;CAK5B,OAAO,OAAO,IAAI,aAAa;EAC7B,OAAO,MAAM,MAAM;EACnB,OAAO,OAAO,WAAW,SAAS,SAChC,KAAK,WAAW,IAAI,OAAO,OAAO,MAAM,cAAc,IAAI,CAAC,CAC7D;EACA,OAAO,MAAM,iBAAiB;CAChC,CAAC,CAAC,CAAC,KAAK,OAAO,QAAQ,OAAO,UAAU;CAKxC,OAAO,OACJ,UAAU,kBAAkB,KAAK,CAAC,CAAC,CACnC,KAAK,OAAO,SAAS,MAAM,IAAI,KAAK,CAAC,GAAG,OAAO,UAAU;CAE5D,OAAO,OAAO,UAAU,KAAK;AAC/B,CAAC,CACH"}
1
+ {"version":3,"file":"realtimeTts.mjs","names":[],"sources":["../src/realtimeTts.ts"],"sourcesContent":["/**\n * Inworld Realtime TTS — `wss://api.inworld.ai/tts/v1/voice:streamBidirectional`.\n *\n * Auth: `Authorization: Basic <API_KEY>` on the WS upgrade header (matches\n * Inworld's own JS sample). The docs mention a `?Authorization=…` query\n * variant but the server rejects it in practice. Setting headers needs the\n * `ws` peer dep (Node/Bun); see `./wsAuth.ts`.\n *\n * Wire shape (single-context per call):\n * client → server:\n * `{ \"create\": { voiceId, modelId, audioConfig, ... } }`\n * `{ \"send_text\": { \"text\": \"...\" } }` (repeated)\n * `{ \"close_context\": {} }` (text stream end)\n * server → client:\n * `{ \"result\": { \"contextCreated\": {...} } }` (handshake ack)\n * `{ \"result\": { \"audioChunk\": { \"audioContent\": \"<b64>\" } } }` (×N)\n * `{ \"result\": { \"contextClosed\": {...} } }` (final)\n *\n * Multi-context (`contextId`) is not surfaced here — one logical\n * utterance per call.\n */\nimport { Effect, Option, Predicate, Redacted, Schema, Stream } from \"effect\"\nimport * as Socket from \"effect/socket/Socket\"\nimport * as AiError from \"@effect-uai/core/AiError\"\nimport type { AudioChunk, AudioFormat } from \"@effect-uai/core/Audio\"\nimport * as JSONL from \"@effect-uai/core/JSONL\"\nimport type { CustomPronunciation } from \"@effect-uai/core/SpeechSynthesizer\"\nimport * as WebSocketSession from \"@effect-uai/core/WebSocketSession\"\nimport { audioConfigFor, decodeAudioContent, defaultFormat } from \"./codec.js\"\nimport type { InworldDeliveryMode, InworldTtsModel, InworldVoiceId } from \"./models.js\"\nimport { authedWsConstructor } from \"./wsAuth.js\"\n\nexport type Config = {\n readonly apiKey: Redacted.Redacted\n readonly baseUrl?: string\n /**\n * Build the socket yourself, for a proxy or an in-memory transport. The\n * default attaches the Basic auth header through the `ws` package.\n */\n readonly webSocket?: Socket.WebSocketConstructor[\"Service\"]\n}\n\n/**\n * Incremental-text-in request. Mirrors the sync request minus `text` (which\n * comes from the input stream).\n */\nexport type StreamSynthesizeRequest = {\n readonly model: InworldTtsModel\n readonly voiceId: InworldVoiceId\n readonly outputFormat?: AudioFormat\n readonly languageCode?: string\n readonly temperature?: number\n readonly deliveryMode?: InworldDeliveryMode\n readonly applyTextNormalization?: \"ON\" | \"OFF\"\n readonly speed?: number\n /** Carried from the Common request so the WS path can reject inline\n * pronunciations (no place to rewrite into a chunked text stream). */\n readonly pronunciations?: ReadonlyArray<CustomPronunciation>\n}\n\n// ---------------------------------------------------------------------------\n// URL + frame builders\n// ---------------------------------------------------------------------------\n\nexport const buildWsUrl = (cfg: Config) => {\n const wsBase = (cfg.baseUrl ?? \"https://api.inworld.ai\").replace(/^http/, \"ws\")\n return `${wsBase}/tts/v1/voice:streamBidirectional`\n}\n\n// TTS WS uses snake_case outbound (per Inworld's own JS sample). The\n// camelCase helper from `codec.ts` is built for the REST/JSON sync paths;\n// rewire the field names here for the WS path. Server responses come back\n// camelCase regardless — see `ResultBody` below.\nconst audioConfigSnake = (request: StreamSynthesizeRequest) =>\n Effect.map(audioConfigFor(request.outputFormat ?? defaultFormat, request.speed), (cfg) => ({\n audio_encoding: cfg.audioEncoding,\n ...(cfg.sampleRateHertz !== undefined && { sample_rate_hertz: cfg.sampleRateHertz }),\n ...(cfg.bitRate !== undefined && { bit_rate: cfg.bitRate }),\n ...(cfg.speakingRate !== undefined && { speaking_rate: cfg.speakingRate }),\n }))\n\nconst createFrame = (request: StreamSynthesizeRequest) =>\n Effect.map(audioConfigSnake(request), (audio_config) =>\n JSON.stringify({\n create: {\n voice_id: request.voiceId,\n model_id: request.model,\n audio_config,\n ...(request.languageCode !== undefined && { language: request.languageCode }),\n ...(request.deliveryMode !== undefined && { delivery_mode: request.deliveryMode }),\n ...(request.temperature !== undefined && { temperature: request.temperature }),\n ...(request.applyTextNormalization !== undefined && {\n apply_text_normalization: request.applyTextNormalization,\n }),\n },\n }),\n )\n\n// `flush_context: {}` inside `send_text` nudges the server to flush\n// pending audio promptly — matches the low-latency sample.\nconst sendTextFrame = (text: string) => JSON.stringify({ send_text: { text, flush_context: {} } })\nconst closeContextFrame = JSON.stringify({ close_context: {} })\n\n// ---------------------------------------------------------------------------\n// Wire schema (server → client)\n// ---------------------------------------------------------------------------\n\nconst ResultBody = Schema.Struct({\n audioChunk: Schema.optional(Schema.Struct({ audioContent: Schema.String })),\n contextCreated: Schema.optional(Schema.Unknown),\n flushCompleted: Schema.optional(Schema.Unknown),\n contextClosed: Schema.optional(Schema.Unknown),\n status: Schema.optional(Schema.Unknown),\n})\n\nconst ServerFrame = Schema.Struct({\n result: Schema.optional(ResultBody),\n error: Schema.optional(Schema.Unknown),\n})\nconst decodeServerFrame = Schema.decodeUnknownEffect(ServerFrame)\n\n/** One raw text frame to at most one audio chunk; error frames are logged, not raised. */\nconst frameToChunk = (raw: string): Effect.Effect<AudioChunk | undefined> =>\n Effect.gen(function* () {\n const json = yield* JSONL.parseSafe(raw)\n if (json === undefined) return undefined\n const decoded = yield* decodeServerFrame(json).pipe(Effect.option)\n if (Option.isNone(decoded)) return undefined\n const frame = decoded.value\n if (frame.error !== undefined) {\n yield* Effect.logWarning(\"[inworld-tts] server error frame\", { error: frame.error })\n return undefined\n }\n const audio = frame.result?.audioChunk?.audioContent\n if (audio === undefined || audio === \"\") return undefined\n const bytes = yield* decodeAudioContent(audio).pipe(Effect.option)\n return Option.match(bytes, {\n onNone: () => undefined,\n onSome: (b): AudioChunk => ({ bytes: b }),\n })\n })\n\n// ---------------------------------------------------------------------------\n// Stream<string> → Stream<AudioChunk>\n// ---------------------------------------------------------------------------\n\nexport const streamSynthesis =\n (cfg: Config) =>\n <E, R>(\n textIn: Stream.Stream<string, E, R>,\n request: StreamSynthesizeRequest,\n ): Stream.Stream<AudioChunk, AiError.AiError | E, R> =>\n Stream.unwrap(\n Effect.gen(function* () {\n // Inline pronunciations are load-bearing (bucket 1) but there is no\n // single text to rewrite `/ipa/` into on a chunked stream. Reject\n // rather than silently drop; use sync `synthesize` for overrides.\n if (request.pronunciations !== undefined && request.pronunciations.length > 0) {\n return Stream.fail(\n new AiError.Unsupported({\n provider: \"inworld\",\n capability: \"pronunciations\",\n reason:\n \"Inworld applies inline /ipa/ pronunciations on sync `synthesize` only; the incremental WS path streams text in chunks with no place to rewrite. Use `synthesize` for pronunciation overrides.\",\n }),\n )\n }\n const create = yield* createFrame(request)\n const socket = yield* Socket.makeWebSocket(buildWsUrl(cfg)).pipe(\n Effect.provideService(\n Socket.WebSocketConstructor,\n cfg.webSocket ?? authedWsConstructor(cfg.apiKey),\n ),\n )\n const { write } = yield* socket.writer\n const asAiError = WebSocketSession.toAiError(\"inworld\")\n\n const chunks = Stream.fromPull(Socket.readerString(socket)).pipe(\n Stream.scoped,\n Stream.catchIf(WebSocketSession.isCleanClose, () => Stream.empty),\n Stream.mapError(asAiError),\n Stream.mapEffect(frameToChunk),\n Stream.filter(Predicate.isNotUndefined),\n )\n\n const outgoing = Stream.concat(\n Stream.make(create),\n Stream.concat(\n textIn.pipe(\n Stream.filter((text) => text.length > 0),\n Stream.map(sendTextFrame),\n ),\n Stream.make(closeContextFrame),\n ),\n ).pipe(Stream.mapEffect((frame) => Effect.mapError(write(frame), asAiError)))\n\n // The server drains remaining audio after `close_context`, then closes.\n return Stream.merge(chunks, Stream.drain(outgoing), { haltStrategy: \"left\" })\n }),\n )\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgEA,MAAa,cAAc,QAAgB;CAEzC,OAAO,IADS,IAAI,WAAW,yBAAA,CAA0B,QAAQ,SAAS,IAC3D,EAAE;AACnB;AAMA,MAAM,oBAAoB,YACxB,OAAO,IAAI,eAAe,QAAQ,gBAAgB,eAAe,QAAQ,KAAK,IAAI,SAAS;CACzF,gBAAgB,IAAI;CACpB,GAAI,IAAI,oBAAoB,KAAA,KAAa,EAAE,mBAAmB,IAAI,gBAAgB;CAClF,GAAI,IAAI,YAAY,KAAA,KAAa,EAAE,UAAU,IAAI,QAAQ;CACzD,GAAI,IAAI,iBAAiB,KAAA,KAAa,EAAE,eAAe,IAAI,aAAa;AAC1E,EAAE;AAEJ,MAAM,eAAe,YACnB,OAAO,IAAI,iBAAiB,OAAO,IAAI,iBACrC,KAAK,UAAU,EACb,QAAQ;CACN,UAAU,QAAQ;CAClB,UAAU,QAAQ;CAClB;CACA,GAAI,QAAQ,iBAAiB,KAAA,KAAa,EAAE,UAAU,QAAQ,aAAa;CAC3E,GAAI,QAAQ,iBAAiB,KAAA,KAAa,EAAE,eAAe,QAAQ,aAAa;CAChF,GAAI,QAAQ,gBAAgB,KAAA,KAAa,EAAE,aAAa,QAAQ,YAAY;CAC5E,GAAI,QAAQ,2BAA2B,KAAA,KAAa,EAClD,0BAA0B,QAAQ,uBACpC;AACF,EACF,CAAC,CACH;AAIF,MAAM,iBAAiB,SAAiB,KAAK,UAAU,EAAE,WAAW;CAAE;CAAM,eAAe,CAAC;AAAE,EAAE,CAAC;AACjG,MAAM,oBAAoB,KAAK,UAAU,EAAE,eAAe,CAAC,EAAE,CAAC;AAM9D,MAAM,aAAa,OAAO,OAAO;CAC/B,YAAY,OA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,CAAC,CACH;CAEF,MAAM,SAAS,OAAO,YAAY,OAAO;CACzC,MAAM,SAAS,OAAO,OAAO,cAAc,WAAW,GAAG,CAAC,CAAC,CAAC,KAC1D,OAAO,eACL,OAAO,sBACP,IAAI,aAAa,oBAAoB,IAAI,MAAM,CACjD,CACF;CACA,MAAM,EAAE,UAAU,OAAO,OAAO;CAChC,MAAM,YAAY,iBAAiB,UAAU,SAAS;CAEtD,MAAM,SAAS,OAAO,SAAS,OAAO,aAAa,MAAM,CAAC,CAAC,CAAC,KAC1D,OAAO,QACP,OAAO,QAAQ,iBAAiB,oBAAoB,OAAO,KAAK,GAChE,OAAO,SAAS,SAAS,GACzB,OAAO,UAAU,YAAY,GAC7B,OAAO,OAAO,UAAU,cAAc,CACxC;CAEA,MAAM,WAAW,OAAO,OACtB,OAAO,KAAK,MAAM,GAClB,OAAO,OACL,OAAO,KACL,OAAO,QAAQ,SAAS,KAAK,SAAS,CAAC,GACvC,OAAO,IAAI,aAAa,CAC1B,GACA,OAAO,KAAK,iBAAiB,CAC/B,CACF,CAAC,CAAC,KAAK,OAAO,WAAW,UAAU,OAAO,SAAS,MAAM,KAAK,GAAG,SAAS,CAAC,CAAC;CAG5E,OAAO,OAAO,MAAM,QAAQ,OAAO,MAAM,QAAQ,GAAG,EAAE,cAAc,OAAO,CAAC;AAC9E,CAAC,CACH"}
@@ -0,0 +1 @@
1
+ export {}
@@ -0,0 +1,70 @@
1
+ import { streamSynthesis } from "./realtimeTts.mjs";
2
+ import { n as globalExpect, r as describe, t as it } from "./dist-Cu2gIMHV.mjs";
3
+ import { Effect, Exit, Fiber, Redacted, Stream } from "effect";
4
+ import { Base64 } from "effect/encoding";
5
+ import * as FakeWebSocket from "@effect-uai/core/testing/FakeWebSocket";
6
+ //#region src/realtimeTts.test.ts
7
+ const request = {
8
+ model: "inworld-tts-2",
9
+ voiceId: "Ashley"
10
+ };
11
+ const synthesizing = FakeWebSocket.make({ greeting: [
12
+ { result: { contextCreated: {} } },
13
+ { result: { audioChunk: { audioContent: Base64.encode(new Uint8Array([
14
+ 1,
15
+ 2,
16
+ 3
17
+ ])) } } },
18
+ { error: { message: "transient" } },
19
+ { result: { audioChunk: { audioContent: "" } } },
20
+ { result: { contextClosed: {} } }
21
+ ] });
22
+ /** Run a synthesis against `server`, closing it, if asked, once the frames are in. */
23
+ const synthesize = (server, textIn, close) => Effect.gen(function* () {
24
+ const run = streamSynthesis({
25
+ apiKey: Redacted.make("test-key"),
26
+ webSocket: server.connect
27
+ });
28
+ const collector = yield* Effect.forkChild(Effect.exit(Stream.runCollect(run(textIn, request))));
29
+ if (close !== void 0) {
30
+ yield* server.greeted;
31
+ yield* server.awaitSent(close.afterSent ?? 0);
32
+ yield* server.close(close.code);
33
+ }
34
+ return yield* Fiber.join(collector);
35
+ });
36
+ describe("Inworld Realtime TTS socket", () => {
37
+ it.live("creates the context before any text, and ends on a clean close", () => Effect.gen(function* () {
38
+ const server = yield* synthesizing;
39
+ const exit = yield* synthesize(server, Stream.make("Hello", "", " world"), {
40
+ code: 1e3,
41
+ afterSent: 4
42
+ });
43
+ globalExpect(exit).toEqual(Exit.succeed([{ bytes: new Uint8Array([
44
+ 1,
45
+ 2,
46
+ 3
47
+ ]) }]));
48
+ const sent = yield* server.sent;
49
+ globalExpect(sent.map((f) => Object.keys(f)[0])).toEqual([
50
+ "create",
51
+ "send_text",
52
+ "send_text",
53
+ "close_context"
54
+ ]);
55
+ }).pipe(Effect.scoped));
56
+ it.live("fails rather than ending when the connection drops", () => Effect.gen(function* () {
57
+ const server = yield* synthesizing;
58
+ const exit = yield* synthesize(server, Stream.never, { code: 1011 });
59
+ globalExpect(Exit.isFailure(exit) && JSON.stringify(exit.cause)).toContain("Unavailable");
60
+ }).pipe(Effect.scoped));
61
+ it.live("fails with the text input's own error", () => Effect.gen(function* () {
62
+ const server = yield* synthesizing;
63
+ const exit = yield* synthesize(server, Stream.fail("upstream llm failed"));
64
+ globalExpect(Exit.isFailure(exit) && JSON.stringify(exit.cause)).toContain("upstream llm failed");
65
+ }).pipe(Effect.scoped));
66
+ });
67
+ //#endregion
68
+ export {};
69
+
70
+ //# sourceMappingURL=realtimeTts.test.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"realtimeTts.test.mjs","names":[],"sources":["../src/realtimeTts.test.ts"],"sourcesContent":["import { describe, it } from \"@effect/vitest\"\nimport { Effect, Exit, Fiber, Redacted, Stream } from \"effect\"\nimport { Base64 } from \"effect/encoding\"\nimport { expect } from \"vitest\"\nimport * as FakeWebSocket from \"@effect-uai/core/testing/FakeWebSocket\"\nimport { streamSynthesis } from \"./realtimeTts.js\"\n\nconst request = { model: \"inworld-tts-2\", voiceId: \"Ashley\" } as const\n\nconst synthesizing = FakeWebSocket.make({\n greeting: [\n { result: { contextCreated: {} } },\n { result: { audioChunk: { audioContent: Base64.encode(new Uint8Array([1, 2, 3])) } } },\n { error: { message: \"transient\" } },\n { result: { audioChunk: { audioContent: \"\" } } },\n { result: { contextClosed: {} } },\n ],\n})\n\n/** Run a synthesis against `server`, closing it, if asked, once the frames are in. */\nconst synthesize = <E>(\n server: FakeWebSocket.FakeWebSocketServer,\n textIn: Stream.Stream<string, E>,\n close?: { readonly code: number; readonly afterSent?: number },\n) =>\n Effect.gen(function* () {\n const run = streamSynthesis({ apiKey: Redacted.make(\"test-key\"), webSocket: server.connect })\n const collector = yield* Effect.forkChild(Effect.exit(Stream.runCollect(run(textIn, request))))\n if (close !== undefined) {\n yield* server.greeted\n yield* server.awaitSent(close.afterSent ?? 0)\n yield* server.close(close.code)\n }\n return yield* Fiber.join(collector)\n })\n\ndescribe(\"Inworld Realtime TTS socket\", () => {\n it.live(\"creates the context before any text, and ends on a clean close\", () =>\n Effect.gen(function* () {\n const server = yield* synthesizing\n const exit = yield* synthesize(server, Stream.make(\"Hello\", \"\", \" world\"), {\n code: 1000,\n afterSent: 4,\n })\n\n expect(exit).toEqual(Exit.succeed([{ bytes: new Uint8Array([1, 2, 3]) }]))\n const sent = (yield* server.sent) as ReadonlyArray<Record<string, unknown>>\n expect(sent.map((f) => Object.keys(f)[0])).toEqual([\n \"create\",\n \"send_text\",\n \"send_text\",\n \"close_context\",\n ])\n }).pipe(Effect.scoped),\n )\n\n it.live(\"fails rather than ending when the connection drops\", () =>\n Effect.gen(function* () {\n const server = yield* synthesizing\n const exit = yield* synthesize(server, Stream.never, { code: 1011 })\n\n expect(Exit.isFailure(exit) && JSON.stringify(exit.cause)).toContain(\"Unavailable\")\n }).pipe(Effect.scoped),\n )\n\n it.live(\"fails with the text input's own error\", () =>\n Effect.gen(function* () {\n const server = yield* synthesizing\n const exit = yield* synthesize(server, Stream.fail(\"upstream llm failed\"))\n\n expect(Exit.isFailure(exit) && JSON.stringify(exit.cause)).toContain(\"upstream llm failed\")\n }).pipe(Effect.scoped),\n )\n})\n"],"mappings":";;;;;;AAOA,MAAM,UAAU;CAAE,OAAO;CAAiB,SAAS;AAAS;AAE5D,MAAM,eAAe,cAAc,KAAK,EACtC,UAAU;CACR,EAAE,QAAQ,EAAE,gBAAgB,CAAC,EAAE,EAAE;CACjC,EAAE,QAAQ,EAAE,YAAY,EAAE,cAAc,OAAO,OAAO,IAAI,WAAW;EAAC;EAAG;EAAG;CAAC,CAAC,CAAC,EAAE,EAAE,EAAE;CACrF,EAAE,OAAO,EAAE,SAAS,YAAY,EAAE;CAClC,EAAE,QAAQ,EAAE,YAAY,EAAE,cAAc,GAAG,EAAE,EAAE;CAC/C,EAAE,QAAQ,EAAE,eAAe,CAAC,EAAE,EAAE;AAClC,EACF,CAAC;;AAGD,MAAM,cACJ,QACA,QACA,UAEA,OAAO,IAAI,aAAa;CACtB,MAAM,MAAM,gBAAgB;EAAE,QAAQ,SAAS,KAAK,UAAU;EAAG,WAAW,OAAO;CAAQ,CAAC;CAC5F,MAAM,YAAY,OAAO,OAAO,UAAU,OAAO,KAAK,OAAO,WAAW,IAAI,QAAQ,OAAO,CAAC,CAAC,CAAC;CAC9F,IAAI,UAAU,KAAA,GAAW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B;AACF,CAAC"}
@@ -1,5 +1,6 @@
1
1
  //#region \0rolldown/runtime.js
2
2
  var __defProp = Object.defineProperty;
3
+ var __commonJSMin = (cb, mod) => () => (mod || (cb((mod = { exports: {} }).exports, mod), cb = null), mod.exports);
3
4
  var __exportAll = (all, no_symbols) => {
4
5
  let target = {};
5
6
  for (var name in all) __defProp(target, name, {
@@ -10,4 +11,4 @@ var __exportAll = (all, no_symbols) => {
10
11
  return target;
11
12
  };
12
13
  //#endregion
13
- export { __exportAll as t };
14
+ export { __exportAll as n, __commonJSMin as t };
package/dist/wsAuth.d.mts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Redacted } from "effect";
2
- import * as Socket from "effect/unstable/socket/Socket";
2
+ import * as Socket from "effect/socket/Socket";
3
3
  //#region src/wsAuth.d.ts
4
4
  declare const authedWsConstructor: (apiKey: Redacted.Redacted) => Socket.WebSocketConstructor["Service"];
5
5
  //#endregion
@@ -1 +1 @@
1
- {"version":3,"file":"wsAuth.mjs","names":["WSWebSocket"],"sources":["../src/wsAuth.ts"],"sourcesContent":["/**\n * Inworld WS auth helper. The Inworld realtime endpoints (`/tts/...` and\n * `/stt/...`) require `Authorization: Basic <API_KEY>` on the WS upgrade\n * — the docs document a `?Authorization=…` query-param variant, but in\n * practice the server returns `authentication is required` for that\n * path. Header auth is what Inworld's own samples use.\n *\n * Headers on a WS upgrade aren't settable via `globalThis.WebSocket`, so\n * we use the `ws` peer dep (Node/Bun only).\n */\nimport { Redacted } from \"effect\"\nimport type * as Socket from \"effect/unstable/socket/Socket\"\nimport { WebSocket as WSWebSocket } from \"ws\"\nimport { authHeader } from \"./codec.js\"\n\nexport const authedWsConstructor =\n (apiKey: Redacted.Redacted): Socket.WebSocketConstructor[\"Service\"] =>\n (url) =>\n new WSWebSocket(url, undefined, {\n headers: { Authorization: authHeader(apiKey) },\n }) as unknown as globalThis.WebSocket\n"],"mappings":";;;AAeA,MAAa,uBACV,YACA,QACC,IAAIA,UAAY,KAAK,KAAA,GAAW,EAC9B,SAAS,EAAE,eAAe,WAAW,MAAM,EAAE,EAC/C,CAAC"}
1
+ {"version":3,"file":"wsAuth.mjs","names":["WSWebSocket"],"sources":["../src/wsAuth.ts"],"sourcesContent":["/**\n * Inworld WS auth helper. The Inworld realtime endpoints (`/tts/...` and\n * `/stt/...`) require `Authorization: Basic <API_KEY>` on the WS upgrade\n * — the docs document a `?Authorization=…` query-param variant, but in\n * practice the server returns `authentication is required` for that\n * path. Header auth is what Inworld's own samples use.\n *\n * Headers on a WS upgrade aren't settable via `globalThis.WebSocket`, so\n * we use the `ws` peer dep (Node/Bun only).\n */\nimport { Redacted } from \"effect\"\nimport type * as Socket from \"effect/socket/Socket\"\nimport { WebSocket as WSWebSocket } from \"ws\"\nimport { authHeader } from \"./codec.js\"\n\nexport const authedWsConstructor =\n (apiKey: Redacted.Redacted): Socket.WebSocketConstructor[\"Service\"] =>\n (url) =>\n new WSWebSocket(url, undefined, {\n headers: { Authorization: authHeader(apiKey) },\n }) as unknown as globalThis.WebSocket\n"],"mappings":";;;AAeA,MAAa,uBACV,YACA,QACC,IAAIA,UAAY,KAAK,KAAA,GAAW,EAC9B,SAAS,EAAE,eAAe,WAAW,MAAM,EAAE,EAC/C,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@effect-uai/inworld",
3
- "version": "0.16.0",
3
+ "version": "0.18.0",
4
4
  "description": "Inworld speech-to-text and text-to-speech provider for @effect-uai/core.",
5
5
  "keywords": [
6
6
  "ai",
@@ -58,14 +58,14 @@
58
58
  },
59
59
  "devDependencies": {
60
60
  "@types/ws": "^8.5.13",
61
- "effect": "4.0.0-rc.111",
61
+ "effect": "4.0.1",
62
62
  "typescript": "^7.0.2",
63
63
  "ws": "^8.18.0",
64
- "@effect-uai/core": "0.16.0"
64
+ "@effect-uai/core": "0.18.0"
65
65
  },
66
66
  "peerDependencies": {
67
67
  "@effect-uai/core": ">=0.2.0 <1",
68
- "effect": ">=4.0.0-rc.111 <5.0.0",
68
+ "effect": ">=4.0.1 <5.0.0",
69
69
  "ws": "^8.0.0"
70
70
  },
71
71
  "peerDependenciesMeta": {
@@ -9,7 +9,7 @@
9
9
  * the WS path.
10
10
  */
11
11
  import { Effect, Layer, Stream } from "effect"
12
- import { HttpClient } from "effect/unstable/http"
12
+ import { HttpClient } from "effect/http"
13
13
  import {
14
14
  type CommonStreamSynthesizeRequest,
15
15
  type CommonSynthesizeRequest,
@@ -8,7 +8,7 @@
8
8
  * the only added surface is the WS path.
9
9
  */
10
10
  import { Effect, Layer } from "effect"
11
- import { HttpClient } from "effect/unstable/http"
11
+ import { HttpClient } from "effect/http"
12
12
  import {
13
13
  type CommonTranscribeRequest,
14
14
  SttStreaming,
@@ -11,7 +11,7 @@
11
11
  * `TtsIncrementalText` capability marker.
12
12
  */
13
13
  import { Array as Arr, Context, Effect, Layer, Redacted, Schema, Stream } from "effect"
14
- import { HttpClient, HttpClientRequest } from "effect/unstable/http"
14
+ import { HttpClient, HttpClientRequest } from "effect/http"
15
15
  import * as AiError from "@effect-uai/core/AiError"
16
16
  import type { AudioBlob, AudioChunk } from "@effect-uai/core/Audio"
17
17
  import * as JSONL from "@effect-uai/core/JSONL"
@@ -8,8 +8,9 @@
8
8
  * Streaming transcription lives at the `InworldRealtimeTranscriber`
9
9
  * subpath, which adds the WS path and the `SttStreaming` capability marker.
10
10
  */
11
- import { Context, Effect, Encoding, Layer, Match, Redacted, Schema, Stream } from "effect"
12
- import { HttpClient, HttpClientRequest } from "effect/unstable/http"
11
+ import { Context, Effect, Layer, Match, Redacted, Schema, Stream } from "effect"
12
+ import { Base64 } from "effect/encoding"
13
+ import { HttpClient, HttpClientRequest } from "effect/http"
13
14
  import * as AiError from "@effect-uai/core/AiError"
14
15
  import type { AudioSource } from "@effect-uai/core/Audio"
15
16
  import * as Capabilities from "@effect-uai/core/Capabilities"
@@ -65,7 +66,7 @@ const urlNotSupported: AiError.AiError = new AiError.InvalidRequest({
65
66
 
66
67
  const audioToBase64: (audio: AudioSource) => Effect.Effect<string, AiError.AiError> =
67
68
  Match.type<AudioSource>().pipe(
68
- Match.tag("bytes", (a) => Effect.succeed(Encoding.encodeBase64(a.bytes))),
69
+ Match.tag("bytes", (a) => Effect.succeed(Base64.encode(a.bytes))),
69
70
  Match.tag("base64", (a) => Effect.succeed(a.base64)),
70
71
  Match.tag("url", () => Effect.fail(urlNotSupported)),
71
72
  Match.exhaustive,
package/src/codec.ts CHANGED
@@ -1,4 +1,5 @@
1
- import { Effect, Encoding, Match, Redacted, Result } from "effect"
1
+ import { Effect, Match, Redacted, Result } from "effect"
2
+ import { Base64 } from "effect/encoding"
2
3
  import * as AiError from "@effect-uai/core/AiError"
3
4
  import type { AudioFormat } from "@effect-uai/core/Audio"
4
5
  import type { InworldAudioEncoding } from "./models.js"
@@ -82,7 +83,7 @@ export const defaultFormat: AudioFormat = {
82
83
  // ---------------------------------------------------------------------------
83
84
 
84
85
  export const decodeAudioContent = (b64: string): Effect.Effect<Uint8Array, AiError.AiError> =>
85
- Result.match(Encoding.decodeBase64(b64), {
86
+ Result.match(Base64.decode(b64), {
86
87
  onSuccess: Effect.succeed,
87
88
  onFailure: (cause) =>
88
89
  Effect.fail(
@@ -0,0 +1,82 @@
1
+ import { describe, it } from "@effect/vitest"
2
+ import { Effect, Exit, Fiber, Redacted, Stream } from "effect"
3
+ import { expect } from "vitest"
4
+ import * as FakeWebSocket from "@effect-uai/core/testing/FakeWebSocket"
5
+ import { streamTranscription } from "./realtimeStt.js"
6
+
7
+ const pcm = { container: "raw", encoding: "pcm_s16le", sampleRate: 16000, channels: 1 } as const
8
+
9
+ const request = { model: "inworld/inworld-stt-1", inputFormat: pcm }
10
+
11
+ const transcribing = FakeWebSocket.make({
12
+ greeting: [
13
+ { result: { speechStarted: { startTimeMs: 1500 } } },
14
+ { result: { transcription: { transcript: "hel", isFinal: false } } },
15
+ { result: { transcription: { transcript: "hello", isFinal: true } } },
16
+ ],
17
+ })
18
+
19
+ /** Run a transcription against `server`, closing it, if asked, once the frames are in. */
20
+ const transcribe = <E>(
21
+ server: FakeWebSocket.FakeWebSocketServer,
22
+ audioIn: Stream.Stream<Uint8Array, E>,
23
+ close?: { readonly code: number; readonly afterSent?: number },
24
+ ) =>
25
+ Effect.gen(function* () {
26
+ const run = streamTranscription({
27
+ apiKey: Redacted.make("test-key"),
28
+ webSocket: server.connect,
29
+ })
30
+ const collector = yield* Effect.forkChild(Effect.exit(Stream.runCollect(run(audioIn, request))))
31
+ if (close !== undefined) {
32
+ yield* server.greeted
33
+ yield* server.awaitSent(close.afterSent ?? 0)
34
+ yield* server.close(close.code)
35
+ }
36
+ return yield* Fiber.join(collector)
37
+ })
38
+
39
+ describe("Inworld Realtime STT socket", () => {
40
+ it.live("sends the config before any audio, and ends on a clean close", () =>
41
+ Effect.gen(function* () {
42
+ const server = yield* transcribing
43
+ const exit = yield* transcribe(server, Stream.make(new Uint8Array([1, 2])), {
44
+ code: 1000,
45
+ afterSent: 4,
46
+ })
47
+
48
+ expect(exit).toEqual(
49
+ Exit.succeed([
50
+ { _tag: "speech-started", atSeconds: 1.5 },
51
+ { _tag: "partial", text: "hel" },
52
+ { _tag: "final", text: "hello" },
53
+ ]),
54
+ )
55
+ const sent = (yield* server.sent) as ReadonlyArray<Record<string, unknown>>
56
+ expect(sent.map((f) => Object.keys(f)[0])).toEqual([
57
+ "transcribeConfig",
58
+ "audioChunk",
59
+ "endTurn",
60
+ "closeStream",
61
+ ])
62
+ }).pipe(Effect.scoped),
63
+ )
64
+
65
+ it.live("fails rather than ending when the connection drops", () =>
66
+ Effect.gen(function* () {
67
+ const server = yield* transcribing
68
+ const exit = yield* transcribe(server, Stream.never, { code: 1011 })
69
+
70
+ expect(Exit.isFailure(exit) && JSON.stringify(exit.cause)).toContain("Unavailable")
71
+ }).pipe(Effect.scoped),
72
+ )
73
+
74
+ it.live("fails with the audio input's own error", () =>
75
+ Effect.gen(function* () {
76
+ const server = yield* transcribing
77
+ const exit = yield* transcribe(server, Stream.fail("microphone unplugged"))
78
+
79
+ expect(Exit.isFailure(exit) && JSON.stringify(exit.cause)).toContain("microphone unplugged")
80
+ }).pipe(Effect.scoped),
81
+ )
82
+ })
@@ -18,17 +18,27 @@
18
18
  * Inworld's STT WS sends audio as base64 inside JSON (NOT binary frames),
19
19
  * matching the rest of the Inworld API style.
20
20
  */
21
- import { Cause, Effect, Encoding, Match, Queue, Redacted, Schema, Stream } from "effect"
22
- import * as Socket from "effect/unstable/socket/Socket"
21
+ import { Effect, Match, Option, Predicate, Redacted, Schema, Stream } from "effect"
22
+ import { Base64 } from "effect/encoding"
23
+ import * as Socket from "effect/socket/Socket"
23
24
  import * as AiError from "@effect-uai/core/AiError"
24
25
  import type { AudioFormat } from "@effect-uai/core/Audio"
25
26
  import * as Capabilities from "@effect-uai/core/Capabilities"
26
27
  import * as JSONL from "@effect-uai/core/JSONL"
27
28
  import type { TranscriptEvent, WordTimestamp } from "@effect-uai/core/Transcript"
28
29
  import type { CommonStreamTranscribeRequest } from "@effect-uai/core/Transcriber"
30
+ import * as WebSocketSession from "@effect-uai/core/WebSocketSession"
29
31
  import { authedWsConstructor } from "./wsAuth.js"
30
32
 
31
- export type Config = { readonly apiKey: Redacted.Redacted; readonly baseUrl?: string }
33
+ export type Config = {
34
+ readonly apiKey: Redacted.Redacted
35
+ readonly baseUrl?: string
36
+ /**
37
+ * Build the socket yourself, for a proxy or an in-memory transport. The
38
+ * default attaches the Basic auth header through the `ws` package.
39
+ */
40
+ readonly webSocket?: Socket.WebSocketConstructor["Service"]
41
+ }
32
42
 
33
43
  // ---------------------------------------------------------------------------
34
44
  // AudioFormat → `audioEncoding` slug for Inworld STT
@@ -85,7 +95,7 @@ const configFrame = (encoding: WireEncoding, request: CommonStreamTranscribeRequ
85
95
  })
86
96
 
87
97
  const audioChunkFrame = (bytes: Uint8Array) =>
88
- JSON.stringify({ audioChunk: { content: Encoding.encodeBase64(bytes) } })
98
+ JSON.stringify({ audioChunk: { content: Base64.encode(bytes) } })
89
99
 
90
100
  const endTurnFrame = JSON.stringify({ endTurn: {} })
91
101
  const closeStreamFrame = JSON.stringify({ closeStream: {} })
@@ -177,14 +187,13 @@ export const wireToEvent = (frame: typeof ServerFrame.Type): TranscriptEvent | u
177
187
  return undefined
178
188
  }
179
189
 
180
- const handleServerMessage = (queue: Queue.Queue<TranscriptEvent, Cause.Done>) => (raw: string) =>
190
+ /** One raw text frame to at most one event; unknown or malformed frames yield `undefined`. */
191
+ const frameToEvent = (raw: string): Effect.Effect<TranscriptEvent | undefined> =>
181
192
  Effect.gen(function* () {
182
193
  const json = yield* JSONL.parseSafe(raw)
183
- if (json === undefined) return
194
+ if (json === undefined) return undefined
184
195
  const decoded = yield* decodeServerFrame(json).pipe(Effect.option)
185
- if (decoded._tag === "None") return
186
- const event = wireToEvent(decoded.value)
187
- if (event !== undefined) yield* Queue.offer(queue, event)
196
+ return Option.match(decoded, { onNone: () => undefined, onSome: wireToEvent })
188
197
  })
189
198
 
190
199
  // ---------------------------------------------------------------------------
@@ -206,32 +215,33 @@ export const streamTranscription =
206
215
  reason: "Inworld STT has no free-form prompt field; bias via `biasingTerms`.",
207
216
  })
208
217
  const encoding = yield* inputFormatToWire(request.inputFormat)
209
- const socket = yield* Socket.makeWebSocket(buildWsUrl(cfg), {
210
- // Effect's Socket treats all close codes as errors by default —
211
- // whitelist standard clean-close codes (1000 / 1001 / 1005).
212
- closeCodeIsError: (code) => code !== 1000 && code !== 1001 && code !== 1005,
213
- }).pipe(Effect.provideService(Socket.WebSocketConstructor, authedWsConstructor(cfg.apiKey)))
214
- const queue = yield* Queue.bounded<TranscriptEvent, Cause.Done>(64)
215
- const write = yield* socket.writer
216
-
217
- // Writer: config → drain audio → endTurn → closeStream. Inworld
218
- // emits `result.transcription` partials in real-time via VAD;
219
- // `endTurn` is sent on input-stream end to flush any tail audio
220
- // into a final transcript, then `closeStream` for graceful close.
221
- yield* Effect.gen(function* () {
222
- yield* write(configFrame(encoding, request))
223
- yield* Stream.runForEach(audioIn, (bytes) => write(audioChunkFrame(bytes)))
224
- yield* write(endTurnFrame)
225
- yield* write(closeStreamFrame)
226
- }).pipe(Effect.ignore, Effect.forkScoped)
227
-
228
- // `Queue.end` flushes pending events then fails the next take with
229
- // `Done` — clean stream end. `Queue.shutdown` would CLEAR queued
230
- // items and interrupt pending takes (wrong for graceful teardown).
231
- yield* socket
232
- .runString(handleServerMessage(queue))
233
- .pipe(Effect.ensuring(Queue.end(queue)), Effect.forkScoped)
234
-
235
- return Stream.fromQueue(queue)
218
+ const socket = yield* Socket.makeWebSocket(buildWsUrl(cfg)).pipe(
219
+ Effect.provideService(
220
+ Socket.WebSocketConstructor,
221
+ cfg.webSocket ?? authedWsConstructor(cfg.apiKey),
222
+ ),
223
+ )
224
+ const { write } = yield* socket.writer
225
+ const asAiError = WebSocketSession.toAiError("inworld")
226
+
227
+ const events = Stream.fromPull(Socket.readerString(socket)).pipe(
228
+ Stream.scoped,
229
+ Stream.catchIf(WebSocketSession.isCleanClose, () => Stream.empty),
230
+ Stream.mapError(asAiError),
231
+ Stream.mapEffect(frameToEvent),
232
+ Stream.filter(Predicate.isNotUndefined),
233
+ )
234
+
235
+ // `endTurn` flushes tail audio into a final transcript before `closeStream`.
236
+ const outgoing = Stream.concat(
237
+ Stream.make(configFrame(encoding, request)),
238
+ Stream.concat(
239
+ Stream.map(audioIn, audioChunkFrame),
240
+ Stream.make(endTurnFrame, closeStreamFrame),
241
+ ),
242
+ ).pipe(Stream.mapEffect((frame) => Effect.mapError(write(frame), asAiError)))
243
+
244
+ // The server's close ends the transcript; the audio running out does not.
245
+ return Stream.merge(events, Stream.drain(outgoing), { haltStrategy: "left" })
236
246
  }),
237
247
  )
@@ -0,0 +1,74 @@
1
+ import { describe, it } from "@effect/vitest"
2
+ import { Effect, Exit, Fiber, Redacted, Stream } from "effect"
3
+ import { Base64 } from "effect/encoding"
4
+ import { expect } from "vitest"
5
+ import * as FakeWebSocket from "@effect-uai/core/testing/FakeWebSocket"
6
+ import { streamSynthesis } from "./realtimeTts.js"
7
+
8
+ const request = { model: "inworld-tts-2", voiceId: "Ashley" } as const
9
+
10
+ const synthesizing = FakeWebSocket.make({
11
+ greeting: [
12
+ { result: { contextCreated: {} } },
13
+ { result: { audioChunk: { audioContent: Base64.encode(new Uint8Array([1, 2, 3])) } } },
14
+ { error: { message: "transient" } },
15
+ { result: { audioChunk: { audioContent: "" } } },
16
+ { result: { contextClosed: {} } },
17
+ ],
18
+ })
19
+
20
+ /** Run a synthesis against `server`, closing it, if asked, once the frames are in. */
21
+ const synthesize = <E>(
22
+ server: FakeWebSocket.FakeWebSocketServer,
23
+ textIn: Stream.Stream<string, E>,
24
+ close?: { readonly code: number; readonly afterSent?: number },
25
+ ) =>
26
+ Effect.gen(function* () {
27
+ const run = streamSynthesis({ apiKey: Redacted.make("test-key"), webSocket: server.connect })
28
+ const collector = yield* Effect.forkChild(Effect.exit(Stream.runCollect(run(textIn, request))))
29
+ if (close !== undefined) {
30
+ yield* server.greeted
31
+ yield* server.awaitSent(close.afterSent ?? 0)
32
+ yield* server.close(close.code)
33
+ }
34
+ return yield* Fiber.join(collector)
35
+ })
36
+
37
+ describe("Inworld Realtime TTS socket", () => {
38
+ it.live("creates the context before any text, and ends on a clean close", () =>
39
+ Effect.gen(function* () {
40
+ const server = yield* synthesizing
41
+ const exit = yield* synthesize(server, Stream.make("Hello", "", " world"), {
42
+ code: 1000,
43
+ afterSent: 4,
44
+ })
45
+
46
+ expect(exit).toEqual(Exit.succeed([{ bytes: new Uint8Array([1, 2, 3]) }]))
47
+ const sent = (yield* server.sent) as ReadonlyArray<Record<string, unknown>>
48
+ expect(sent.map((f) => Object.keys(f)[0])).toEqual([
49
+ "create",
50
+ "send_text",
51
+ "send_text",
52
+ "close_context",
53
+ ])
54
+ }).pipe(Effect.scoped),
55
+ )
56
+
57
+ it.live("fails rather than ending when the connection drops", () =>
58
+ Effect.gen(function* () {
59
+ const server = yield* synthesizing
60
+ const exit = yield* synthesize(server, Stream.never, { code: 1011 })
61
+
62
+ expect(Exit.isFailure(exit) && JSON.stringify(exit.cause)).toContain("Unavailable")
63
+ }).pipe(Effect.scoped),
64
+ )
65
+
66
+ it.live("fails with the text input's own error", () =>
67
+ Effect.gen(function* () {
68
+ const server = yield* synthesizing
69
+ const exit = yield* synthesize(server, Stream.fail("upstream llm failed"))
70
+
71
+ expect(Exit.isFailure(exit) && JSON.stringify(exit.cause)).toContain("upstream llm failed")
72
+ }).pipe(Effect.scoped),
73
+ )
74
+ })