@effect-uai/inworld 0.17.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
@@ -19,17 +19,26 @@
19
19
  * Multi-context (`contextId`) is not surfaced here — one logical
20
20
  * utterance per call.
21
21
  */
22
- import { Cause, Effect, Queue, Redacted, Schema, Stream } from "effect"
23
- import * as Socket from "effect/unstable/socket/Socket"
22
+ import { Effect, Option, Predicate, Redacted, Schema, Stream } from "effect"
23
+ import * as Socket from "effect/socket/Socket"
24
24
  import * as AiError from "@effect-uai/core/AiError"
25
25
  import type { AudioChunk, AudioFormat } from "@effect-uai/core/Audio"
26
26
  import * as JSONL from "@effect-uai/core/JSONL"
27
27
  import type { CustomPronunciation } from "@effect-uai/core/SpeechSynthesizer"
28
+ import * as WebSocketSession from "@effect-uai/core/WebSocketSession"
28
29
  import { audioConfigFor, decodeAudioContent, defaultFormat } from "./codec.js"
29
30
  import type { InworldDeliveryMode, InworldTtsModel, InworldVoiceId } from "./models.js"
30
31
  import { authedWsConstructor } from "./wsAuth.js"
31
32
 
32
- 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
+ }
33
42
 
34
43
  /**
35
44
  * Incremental-text-in request. Mirrors the sync request minus `text` (which
@@ -110,21 +119,25 @@ const ServerFrame = Schema.Struct({
110
119
  })
111
120
  const decodeServerFrame = Schema.decodeUnknownEffect(ServerFrame)
112
121
 
113
- const handleServerFrame = (queue: Queue.Queue<AudioChunk, Cause.Done>) => (raw: string) =>
122
+ /** One raw text frame to at most one audio chunk; error frames are logged, not raised. */
123
+ const frameToChunk = (raw: string): Effect.Effect<AudioChunk | undefined> =>
114
124
  Effect.gen(function* () {
115
125
  const json = yield* JSONL.parseSafe(raw)
116
- if (json === undefined) return
126
+ if (json === undefined) return undefined
117
127
  const decoded = yield* decodeServerFrame(json).pipe(Effect.option)
118
- if (decoded._tag === "None") return
128
+ if (Option.isNone(decoded)) return undefined
119
129
  const frame = decoded.value
120
130
  if (frame.error !== undefined) {
121
131
  yield* Effect.logWarning("[inworld-tts] server error frame", { error: frame.error })
122
- return
132
+ return undefined
123
133
  }
124
134
  const audio = frame.result?.audioChunk?.audioContent
125
- if (audio === undefined || audio === "") return
135
+ if (audio === undefined || audio === "") return undefined
126
136
  const bytes = yield* decodeAudioContent(audio).pipe(Effect.option)
127
- if (bytes._tag === "Some") yield* Queue.offer(queue, { bytes: bytes.value })
137
+ return Option.match(bytes, {
138
+ onNone: () => undefined,
139
+ onSome: (b): AudioChunk => ({ bytes: b }),
140
+ })
128
141
  })
129
142
 
130
143
  // ---------------------------------------------------------------------------
@@ -153,32 +166,35 @@ export const streamSynthesis =
153
166
  )
154
167
  }
155
168
  const create = yield* createFrame(request)
156
- const socket = yield* Socket.makeWebSocket(buildWsUrl(cfg), {
157
- // Effect's Socket treats all close codes as errors by default —
158
- // whitelist standard clean-close codes (1000 / 1001 / 1005).
159
- closeCodeIsError: (code) => code !== 1000 && code !== 1001 && code !== 1005,
160
- }).pipe(Effect.provideService(Socket.WebSocketConstructor, authedWsConstructor(cfg.apiKey)))
161
- const queue = yield* Queue.bounded<AudioChunk, Cause.Done>(64)
162
- const write = yield* socket.writer
163
-
164
- // Writer: BOS `create` → drain text as `send_text` → `close_context`.
165
- // The reader fiber surfaces the remaining audio drained by the server
166
- // post-close and shuts the queue when the WS upstream closes.
167
- yield* Effect.gen(function* () {
168
- yield* write(create)
169
- yield* Stream.runForEach(textIn, (text) =>
170
- text.length === 0 ? Effect.void : write(sendTextFrame(text)),
171
- )
172
- yield* write(closeContextFrame)
173
- }).pipe(Effect.ignore, Effect.forkScoped)
174
-
175
- // `Queue.end` flushes pending chunks then fails the next take with
176
- // `Done` — clean stream end. `Queue.shutdown` would CLEAR queued items
177
- // and interrupt pending takes (wrong for graceful teardown).
178
- yield* socket
179
- .runString(handleServerFrame(queue))
180
- .pipe(Effect.ensuring(Queue.end(queue)), Effect.forkScoped)
181
-
182
- return Stream.fromQueue(queue)
169
+ const socket = yield* Socket.makeWebSocket(buildWsUrl(cfg)).pipe(
170
+ Effect.provideService(
171
+ Socket.WebSocketConstructor,
172
+ cfg.webSocket ?? authedWsConstructor(cfg.apiKey),
173
+ ),
174
+ )
175
+ const { write } = yield* socket.writer
176
+ const asAiError = WebSocketSession.toAiError("inworld")
177
+
178
+ const chunks = Stream.fromPull(Socket.readerString(socket)).pipe(
179
+ Stream.scoped,
180
+ Stream.catchIf(WebSocketSession.isCleanClose, () => Stream.empty),
181
+ Stream.mapError(asAiError),
182
+ Stream.mapEffect(frameToChunk),
183
+ Stream.filter(Predicate.isNotUndefined),
184
+ )
185
+
186
+ const outgoing = Stream.concat(
187
+ Stream.make(create),
188
+ Stream.concat(
189
+ textIn.pipe(
190
+ Stream.filter((text) => text.length > 0),
191
+ Stream.map(sendTextFrame),
192
+ ),
193
+ Stream.make(closeContextFrame),
194
+ ),
195
+ ).pipe(Stream.mapEffect((frame) => Effect.mapError(write(frame), asAiError)))
196
+
197
+ // The server drains remaining audio after `close_context`, then closes.
198
+ return Stream.merge(chunks, Stream.drain(outgoing), { haltStrategy: "left" })
183
199
  }),
184
200
  )
package/src/wsAuth.ts CHANGED
@@ -9,7 +9,7 @@
9
9
  * we use the `ws` peer dep (Node/Bun only).
10
10
  */
11
11
  import { Redacted } from "effect"
12
- import type * as Socket from "effect/unstable/socket/Socket"
12
+ import type * as Socket from "effect/socket/Socket"
13
13
  import { WebSocket as WSWebSocket } from "ws"
14
14
  import { authHeader } from "./codec.js"
15
15