@effect-uai/core 0.5.2 → 0.7.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 (175) hide show
  1. package/README.md +1 -1
  2. package/dist/{Audio-BfCTGnH3.d.mts → Audio-DPlTsNdz.d.mts} +3 -2
  3. package/dist/Audio-DPlTsNdz.d.mts.map +1 -0
  4. package/dist/{Items-BH8xUkoR.d.mts → Items-D8UaXQx_.d.mts} +15 -15
  5. package/dist/Items-D8UaXQx_.d.mts.map +1 -0
  6. package/dist/SandboxError-BvKypkJm.d.mts +158 -0
  7. package/dist/SandboxError-BvKypkJm.d.mts.map +1 -0
  8. package/dist/{Tool-87ViKCCO.d.mts → Tool-CiFXYjmR.d.mts} +11 -11
  9. package/dist/{Tool-87ViKCCO.d.mts.map → Tool-CiFXYjmR.d.mts.map} +1 -1
  10. package/dist/capabilities/Capabilities.d.mts +78 -0
  11. package/dist/capabilities/Capabilities.d.mts.map +1 -0
  12. package/dist/capabilities/Capabilities.mjs +65 -0
  13. package/dist/capabilities/Capabilities.mjs.map +1 -0
  14. package/dist/domain/Audio.d.mts +1 -1
  15. package/dist/domain/Audio.mjs.map +1 -1
  16. package/dist/domain/Items.d.mts +2 -2
  17. package/dist/domain/Items.mjs +15 -15
  18. package/dist/domain/Items.mjs.map +1 -1
  19. package/dist/domain/Music.d.mts +131 -72
  20. package/dist/domain/Music.d.mts.map +1 -1
  21. package/dist/domain/Music.mjs +36 -9
  22. package/dist/domain/Music.mjs.map +1 -1
  23. package/dist/domain/Transcript.d.mts +8 -2
  24. package/dist/domain/Transcript.d.mts.map +1 -1
  25. package/dist/domain/Transcript.mjs.map +1 -1
  26. package/dist/domain/Turn.d.mts +388 -2
  27. package/dist/domain/Turn.d.mts.map +1 -0
  28. package/dist/domain/Turn.mjs +13 -13
  29. package/dist/domain/Turn.mjs.map +1 -1
  30. package/dist/embedding-model/Embedding.d.mts +12 -2
  31. package/dist/embedding-model/Embedding.d.mts.map +1 -1
  32. package/dist/embedding-model/Embedding.mjs.map +1 -1
  33. package/dist/embedding-model/EmbeddingModel.d.mts +36 -23
  34. package/dist/embedding-model/EmbeddingModel.d.mts.map +1 -1
  35. package/dist/embedding-model/EmbeddingModel.mjs +20 -1
  36. package/dist/embedding-model/EmbeddingModel.mjs.map +1 -1
  37. package/dist/embedding-model/EmbeddingModel.test.mjs +15 -3
  38. package/dist/embedding-model/EmbeddingModel.test.mjs.map +1 -1
  39. package/dist/index.d.mts +13 -7
  40. package/dist/index.mjs +9 -3
  41. package/dist/language-model/LanguageModel.d.mts +5 -5
  42. package/dist/language-model/LanguageModel.d.mts.map +1 -1
  43. package/dist/language-model/LanguageModel.mjs.map +1 -1
  44. package/dist/loop/Loop.d.mts +37 -90
  45. package/dist/loop/Loop.d.mts.map +1 -1
  46. package/dist/loop/Loop.mjs +39 -93
  47. package/dist/loop/Loop.mjs.map +1 -1
  48. package/dist/loop/Loop.test.mjs +46 -54
  49. package/dist/loop/Loop.test.mjs.map +1 -1
  50. package/dist/music-generator/MusicGenerator.d.mts +48 -25
  51. package/dist/music-generator/MusicGenerator.d.mts.map +1 -1
  52. package/dist/music-generator/MusicGenerator.mjs +17 -9
  53. package/dist/music-generator/MusicGenerator.mjs.map +1 -1
  54. package/dist/music-generator/MusicGenerator.test.mjs +81 -56
  55. package/dist/music-generator/MusicGenerator.test.mjs.map +1 -1
  56. package/dist/sandbox/Memory.d.mts +53 -0
  57. package/dist/sandbox/Memory.d.mts.map +1 -0
  58. package/dist/sandbox/Memory.mjs +68 -0
  59. package/dist/sandbox/Memory.mjs.map +1 -0
  60. package/dist/sandbox/Sandbox.d.mts +717 -0
  61. package/dist/sandbox/Sandbox.d.mts.map +1 -0
  62. package/dist/sandbox/Sandbox.mjs +192 -0
  63. package/dist/sandbox/Sandbox.mjs.map +1 -0
  64. package/dist/sandbox/SandboxError.d.mts +2 -0
  65. package/dist/sandbox/SandboxError.mjs +89 -0
  66. package/dist/sandbox/SandboxError.mjs.map +1 -0
  67. package/dist/sandbox/SandboxImage.d.mts +28 -0
  68. package/dist/sandbox/SandboxImage.d.mts.map +1 -0
  69. package/dist/sandbox/SandboxImage.mjs +32 -0
  70. package/dist/sandbox/SandboxImage.mjs.map +1 -0
  71. package/dist/sandbox/SandboxNetwork.d.mts +34 -0
  72. package/dist/sandbox/SandboxNetwork.d.mts.map +1 -0
  73. package/dist/sandbox/SandboxNetwork.mjs +36 -0
  74. package/dist/sandbox/SandboxNetwork.mjs.map +1 -0
  75. package/dist/speech-synthesizer/SpeechSynthesizer.d.mts +84 -3
  76. package/dist/speech-synthesizer/SpeechSynthesizer.d.mts.map +1 -1
  77. package/dist/speech-synthesizer/SpeechSynthesizer.mjs +32 -2
  78. package/dist/speech-synthesizer/SpeechSynthesizer.mjs.map +1 -1
  79. package/dist/speech-synthesizer/SpeechSynthesizer.test.mjs +59 -5
  80. package/dist/speech-synthesizer/SpeechSynthesizer.test.mjs.map +1 -1
  81. package/dist/testing/MockMusicGenerator.d.mts +12 -7
  82. package/dist/testing/MockMusicGenerator.d.mts.map +1 -1
  83. package/dist/testing/MockMusicGenerator.mjs +4 -3
  84. package/dist/testing/MockMusicGenerator.mjs.map +1 -1
  85. package/dist/testing/MockProvider.d.mts +3 -3
  86. package/dist/testing/MockProvider.d.mts.map +1 -1
  87. package/dist/testing/MockProvider.mjs.map +1 -1
  88. package/dist/testing/MockSandbox.d.mts +604 -0
  89. package/dist/testing/MockSandbox.d.mts.map +1 -0
  90. package/dist/testing/MockSandbox.mjs +160 -0
  91. package/dist/testing/MockSandbox.mjs.map +1 -0
  92. package/dist/testing/MockSpeechSynthesizer.d.mts +27 -12
  93. package/dist/testing/MockSpeechSynthesizer.d.mts.map +1 -1
  94. package/dist/testing/MockSpeechSynthesizer.mjs +75 -51
  95. package/dist/testing/MockSpeechSynthesizer.mjs.map +1 -1
  96. package/dist/tool/{Resolvers.d.mts → Approval.d.mts} +27 -27
  97. package/dist/tool/Approval.d.mts.map +1 -0
  98. package/dist/tool/{Resolvers.mjs → Approval.mjs} +18 -18
  99. package/dist/tool/Approval.mjs.map +1 -0
  100. package/dist/tool/{Resolvers.test.mjs → Approval.test.mjs} +40 -40
  101. package/dist/tool/Approval.test.mjs.map +1 -0
  102. package/dist/tool/HistoryCheck.d.mts +6 -6
  103. package/dist/tool/HistoryCheck.d.mts.map +1 -1
  104. package/dist/tool/HistoryCheck.mjs +5 -5
  105. package/dist/tool/HistoryCheck.mjs.map +1 -1
  106. package/dist/tool/Tool.d.mts +2 -2
  107. package/dist/tool/Tool.mjs +4 -4
  108. package/dist/tool/Tool.mjs.map +1 -1
  109. package/dist/tool/ToolEvent.d.mts +19 -19
  110. package/dist/tool/ToolEvent.d.mts.map +1 -1
  111. package/dist/tool/ToolEvent.mjs +8 -8
  112. package/dist/tool/ToolEvent.mjs.map +1 -1
  113. package/dist/tool/{Outcome.d.mts → ToolResult.d.mts} +29 -29
  114. package/dist/tool/ToolResult.d.mts.map +1 -0
  115. package/dist/tool/ToolResult.mjs +61 -0
  116. package/dist/tool/ToolResult.mjs.map +1 -0
  117. package/dist/tool/Toolkit.d.mts +58 -26
  118. package/dist/tool/Toolkit.d.mts.map +1 -1
  119. package/dist/tool/Toolkit.mjs +62 -34
  120. package/dist/tool/Toolkit.mjs.map +1 -1
  121. package/dist/tool/Toolkit.test.mjs +13 -13
  122. package/dist/tool/Toolkit.test.mjs.map +1 -1
  123. package/dist/transcriber/Transcriber.d.mts +11 -10
  124. package/dist/transcriber/Transcriber.d.mts.map +1 -1
  125. package/dist/transcriber/Transcriber.mjs.map +1 -1
  126. package/dist/transcriber/Transcriber.test.mjs +3 -3
  127. package/dist/transcriber/Transcriber.test.mjs.map +1 -1
  128. package/package.json +35 -7
  129. package/src/capabilities/Capabilities.ts +81 -0
  130. package/src/domain/Audio.ts +2 -1
  131. package/src/domain/Items.ts +9 -9
  132. package/src/domain/Music.ts +142 -68
  133. package/src/domain/Transcript.ts +7 -1
  134. package/src/domain/Turn.ts +16 -16
  135. package/src/embedding-model/Embedding.ts +11 -0
  136. package/src/embedding-model/EmbeddingModel.test.ts +15 -4
  137. package/src/embedding-model/EmbeddingModel.ts +48 -21
  138. package/src/index.ts +8 -2
  139. package/src/language-model/LanguageModel.ts +3 -3
  140. package/src/loop/Loop.test.ts +93 -91
  141. package/src/loop/Loop.ts +72 -170
  142. package/src/music-generator/MusicGenerator.test.ts +78 -53
  143. package/src/music-generator/MusicGenerator.ts +65 -24
  144. package/src/sandbox/Memory.ts +128 -0
  145. package/src/sandbox/Sandbox.ts +639 -0
  146. package/src/sandbox/SandboxError.ts +140 -0
  147. package/src/sandbox/SandboxImage.ts +26 -0
  148. package/src/sandbox/SandboxNetwork.ts +34 -0
  149. package/src/speech-synthesizer/SpeechSynthesizer.test.ts +92 -3
  150. package/src/speech-synthesizer/SpeechSynthesizer.ts +109 -0
  151. package/src/testing/MockMusicGenerator.ts +28 -18
  152. package/src/testing/MockProvider.ts +21 -20
  153. package/src/testing/MockSandbox.ts +342 -0
  154. package/src/testing/MockSpeechSynthesizer.ts +137 -77
  155. package/src/tool/{Resolvers.test.ts → Approval.test.ts} +45 -54
  156. package/src/tool/{Resolvers.ts → Approval.ts} +25 -25
  157. package/src/tool/HistoryCheck.ts +10 -13
  158. package/src/tool/Tool.ts +13 -15
  159. package/src/tool/ToolEvent.ts +7 -7
  160. package/src/tool/{Outcome.ts → ToolResult.ts} +20 -20
  161. package/src/tool/Toolkit.test.ts +13 -13
  162. package/src/tool/Toolkit.ts +97 -73
  163. package/src/transcriber/Transcriber.test.ts +3 -3
  164. package/src/transcriber/Transcriber.ts +10 -7
  165. package/dist/Audio-BfCTGnH3.d.mts.map +0 -1
  166. package/dist/Items-BH8xUkoR.d.mts.map +0 -1
  167. package/dist/Turn-0CwCAyVe.d.mts +0 -388
  168. package/dist/Turn-0CwCAyVe.d.mts.map +0 -1
  169. package/dist/tool/Outcome.d.mts.map +0 -1
  170. package/dist/tool/Outcome.mjs +0 -61
  171. package/dist/tool/Outcome.mjs.map +0 -1
  172. package/dist/tool/Resolvers.d.mts.map +0 -1
  173. package/dist/tool/Resolvers.mjs.map +0 -1
  174. package/dist/tool/Resolvers.test.mjs.map +0 -1
  175. /package/dist/tool/{Resolvers.test.d.mts → Approval.test.d.mts} +0 -0
@@ -0,0 +1,140 @@
1
+ import { Data, Duration } from "effect"
2
+
3
+ /**
4
+ * Failed to provision a sandbox. Covers image pull failures, region
5
+ * unavailability, the provider being unreachable, etc. The provider's
6
+ * structured reason (if any) is on `reason`; the raw error from the
7
+ * underlying SDK is on `raw`.
8
+ */
9
+ export class SandboxCreateFailed extends Data.TaggedError("SandboxCreateFailed")<{
10
+ provider: string
11
+ reason?: string
12
+ raw: unknown
13
+ }> {}
14
+
15
+ /**
16
+ * The requested sandbox id does not exist (or was already destroyed).
17
+ * Distinct from `SandboxCreateFailed` — this is for `attach` / `destroy` /
18
+ * `get`-style lookups against a stale id.
19
+ */
20
+ export class SandboxNotFound extends Data.TaggedError("SandboxNotFound")<{
21
+ provider: string
22
+ id: string
23
+ }> {}
24
+
25
+ /**
26
+ * Caller tried to create a sandbox whose id is already taken. Providers
27
+ * that keyed by name (Microsandbox, Modal) surface this when a previous
28
+ * sandbox with the same name is still alive. Distinct from
29
+ * `SandboxCreateFailed` so callers can retry with `replace` semantics.
30
+ */
31
+ export class SandboxAlreadyExists extends Data.TaggedError("SandboxAlreadyExists")<{
32
+ provider: string
33
+ id: string
34
+ }> {}
35
+
36
+ /**
37
+ * A shell exec inside the sandbox failed at the transport / SDK layer —
38
+ * i.e. the SDK couldn't deliver the command to the guest, or the guest
39
+ * agent itself errored. A non-zero exit code from the guest's command
40
+ * is NOT an error; it's an `ExecResult` with `exitCode !== 0`.
41
+ */
42
+ export class SandboxExecFailed extends Data.TaggedError("SandboxExecFailed")<{
43
+ provider: string
44
+ reason?: string
45
+ raw: unknown
46
+ }> {}
47
+
48
+ /**
49
+ * A sandbox operation (create, exec, file op) exceeded its time budget.
50
+ * Carries the budget that was breached for observability.
51
+ */
52
+ export class SandboxTimeout extends Data.TaggedError("SandboxTimeout")<{
53
+ provider: string
54
+ operation: "create" | "exec" | "filesystem" | "attach"
55
+ budget?: Duration.Duration
56
+ raw?: unknown
57
+ }> {}
58
+
59
+ /**
60
+ * Outbound network request from inside the sandbox was blocked by the
61
+ * configured egress policy (host not on allowlist, CIDR mismatch, etc.).
62
+ * Surfaced when the provider's SDK reports the denial back to the host
63
+ * — many providers simply let the request fail inside the guest and
64
+ * never report this to the host, in which case this error is never
65
+ * raised by the adapter.
66
+ */
67
+ export class SandboxNetworkPolicyDenied extends Data.TaggedError("SandboxNetworkPolicyDenied")<{
68
+ provider: string
69
+ host?: string
70
+ reason?: string
71
+ raw: unknown
72
+ }> {}
73
+
74
+ /**
75
+ * Provider rejected the create / exec because account / org / project
76
+ * quota or concurrency limit was exceeded. Surfaced separately from
77
+ * `SandboxCreateFailed` so callers can back off and retry intelligently.
78
+ */
79
+ export class SandboxQuotaExceeded extends Data.TaggedError("SandboxQuotaExceeded")<{
80
+ provider: string
81
+ reason?: string
82
+ raw: unknown
83
+ }> {}
84
+
85
+ /**
86
+ * Caller's request shape is malformed for the wired provider — e.g. a
87
+ * Dockerfile passed to a provider that only accepts registry refs, a
88
+ * hostname allowlist passed to a CIDR-only provider, etc. Adapters
89
+ * should also reject these at the request-schema layer where possible;
90
+ * this is the runtime fallback.
91
+ */
92
+ export class SandboxInvalidRequest extends Data.TaggedError("SandboxInvalidRequest")<{
93
+ provider: string
94
+ param?: string
95
+ reason?: string
96
+ raw?: unknown
97
+ }> {}
98
+
99
+ /**
100
+ * Sandbox SDK could not authenticate against the provider. Subtype
101
+ * mirrors the model-side `AiError.AuthFailed` shape for consistency.
102
+ */
103
+ export type SandboxAuthSubtype = "auth" | "permission" | "billing" | "quota"
104
+
105
+ export class SandboxAuthFailed extends Data.TaggedError("SandboxAuthFailed")<{
106
+ provider: string
107
+ subtype: SandboxAuthSubtype
108
+ raw: unknown
109
+ }> {}
110
+
111
+ /**
112
+ * The wired provider does not implement the requested capability for
113
+ * this specific request. Distinct from `SandboxInvalidRequest` (the
114
+ * request shape is malformed) and from compile-time capability
115
+ * markers (`SandboxSnapshots`, `SandboxSecretInjection`, …) which gate
116
+ * blanket provider-level gaps.
117
+ *
118
+ * Reserved for request-data-dependent gaps where the provider supports
119
+ * the method in general but not for these inputs — e.g. passing
120
+ * `hosts: [...]` to a CIDR-only allowlist provider, or passing
121
+ * `dockerfile: ...` to a provider whose `SandboxCustomImage` marker
122
+ * was provided but which only accepts a flat Dockerfile string.
123
+ */
124
+ export class SandboxUnsupported extends Data.TaggedError("SandboxUnsupported")<{
125
+ provider: string
126
+ capability: string
127
+ reason?: string
128
+ }> {}
129
+
130
+ export type SandboxError =
131
+ | SandboxCreateFailed
132
+ | SandboxNotFound
133
+ | SandboxAlreadyExists
134
+ | SandboxExecFailed
135
+ | SandboxTimeout
136
+ | SandboxNetworkPolicyDenied
137
+ | SandboxQuotaExceeded
138
+ | SandboxInvalidRequest
139
+ | SandboxAuthFailed
140
+ | SandboxUnsupported
@@ -0,0 +1,26 @@
1
+ import { ImageRef, SnapshotId } from "./Sandbox.js"
2
+
3
+ /**
4
+ * Ergonomic constructors for {@link ImageRef}. `ImageRef.*` tagged
5
+ * constructors still exist (and are what pattern-matching code uses);
6
+ * these helpers are what end users reach for at the call site.
7
+ *
8
+ * @example
9
+ * ```ts
10
+ * import * as Image from "@effect-uai/core/SandboxImage"
11
+ *
12
+ * Image.auto // provider's house image
13
+ * Image.registry("python:3.12") // OCI registry ref
14
+ * Image.snapshot("ml-warm-1") // restore captured state (gated)
15
+ * Image.dockerfile("FROM ubuntu...") // build custom (gated)
16
+ * ```
17
+ */
18
+
19
+ export const auto: ImageRef = ImageRef.Default()
20
+
21
+ export const registry = (ref: string): ImageRef => ImageRef.Registry({ ref })
22
+
23
+ export const snapshot = (id: SnapshotId | string): ImageRef =>
24
+ ImageRef.Snapshot({ id: SnapshotId(id) })
25
+
26
+ export const dockerfile = (contents: string): ImageRef => ImageRef.Dockerfile({ contents })
@@ -0,0 +1,34 @@
1
+ import { NetworkPolicy } from "./Sandbox.js"
2
+
3
+ /**
4
+ * Ergonomic constructors for {@link NetworkPolicy}. The tagged
5
+ * constructors under `Sandbox.NetworkPolicy.*` still exist (and are
6
+ * what pattern-matching code uses); these helpers are what end users
7
+ * reach for at the call site.
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * import * as Network from "@effect-uai/core/SandboxNetwork"
12
+ *
13
+ * Network.open // provider defaults apply
14
+ * Network.blocked // airgapped — no egress
15
+ * Network.allowHosts("api.openai.com", "github.com")
16
+ * Network.allowCidrs("10.0.0.0/8")
17
+ * Network.allow({ hosts: [...], cidrs: [...] }) // combined allowlist
18
+ * ```
19
+ */
20
+
21
+ export const open: NetworkPolicy = NetworkPolicy.Open()
22
+
23
+ export const blocked: NetworkPolicy = NetworkPolicy.Blocked()
24
+
25
+ export const allowHosts = (...hosts: ReadonlyArray<string>): NetworkPolicy =>
26
+ NetworkPolicy.Allowlist({ hosts })
27
+
28
+ export const allowCidrs = (...cidrs: ReadonlyArray<string>): NetworkPolicy =>
29
+ NetworkPolicy.Allowlist({ cidrs })
30
+
31
+ export const allow = (opts: {
32
+ readonly hosts?: ReadonlyArray<string>
33
+ readonly cidrs?: ReadonlyArray<string>
34
+ }): NetworkPolicy => NetworkPolicy.Allowlist(opts)
@@ -1,4 +1,4 @@
1
- import { Effect, Stream } from "effect"
1
+ import { Duration, Effect, Stream } from "effect"
2
2
  import { describe, expect, expectTypeOf, it } from "vitest"
3
3
  import type * as AiError from "../domain/AiError.js"
4
4
  import type { AudioBlob, AudioChunk, AudioFormat } from "../domain/Audio.js"
@@ -14,7 +14,7 @@ const pcmFormat: AudioFormat = {
14
14
  const blob: AudioBlob = {
15
15
  format: pcmFormat,
16
16
  bytes: new Uint8Array([0xde, 0xad, 0xbe, 0xef]),
17
- durationSeconds: 0.5,
17
+ duration: Duration.millis(500),
18
18
  }
19
19
 
20
20
  const chunk = (n: number): AudioChunk => ({ bytes: new Uint8Array([n]) })
@@ -29,7 +29,7 @@ describe("SpeechSynthesizer.synthesize", () => {
29
29
  })
30
30
  const result = await Effect.runPromise(program.pipe(Effect.provide(mock.layer)))
31
31
  expect(result.bytes).toEqual(blob.bytes)
32
- expect(result.durationSeconds).toBe(0.5)
32
+ expect(result.duration).toEqual(Duration.millis(500))
33
33
  })
34
34
  })
35
35
 
@@ -106,6 +106,95 @@ describe("SpeechSynthesizer capability marker (compile-time)", () => {
106
106
  })
107
107
  })
108
108
 
109
+ describe("SpeechSynthesizer.synthesizeDialogue", () => {
110
+ const dialogueReq: SpeechSynthesizer.CommonSynthesizeDialogueRequest = {
111
+ model: "mock-tts",
112
+ turns: [
113
+ { voiceId: "voice-a", text: "Hi" },
114
+ { voiceId: "voice-b", text: "Hello" },
115
+ ],
116
+ }
117
+
118
+ it("returns the scripted AudioBlob", async () => {
119
+ const mock = MockSpeechSynthesizer.layer({ dialogueBlobs: [blob] })
120
+ const result = await Effect.runPromise(
121
+ SpeechSynthesizer.synthesizeDialogue(dialogueReq).pipe(Effect.provide(mock.layer)),
122
+ )
123
+ expect(result.bytes).toEqual(blob.bytes)
124
+ })
125
+
126
+ it("records the request on the synthesizeDialogue call channel", async () => {
127
+ const mock = MockSpeechSynthesizer.layer({ dialogueBlobs: [blob] })
128
+ const program = Effect.gen(function* () {
129
+ yield* SpeechSynthesizer.synthesizeDialogue(dialogueReq)
130
+ return yield* mock.recorder
131
+ })
132
+ const rec = await Effect.runPromise(program.pipe(Effect.provide(mock.layer)))
133
+ expect(rec.synthesizeDialogueCalls.length).toBe(1)
134
+ expect(rec.synthesizeDialogueCalls[0]!.turns.length).toBe(2)
135
+ expect(rec.synthesizeCalls.length).toBe(0)
136
+ })
137
+
138
+ it("emits scripted chunks for streamSynthesizeDialogue", async () => {
139
+ const mock = MockSpeechSynthesizer.layer({
140
+ streamSynthesizeDialogueChunks: [[chunk(7), chunk(8)]],
141
+ })
142
+ const out = await Effect.runPromise(
143
+ Stream.runCollect(SpeechSynthesizer.streamSynthesizeDialogue(dialogueReq)).pipe(
144
+ Effect.provide(mock.layer),
145
+ ),
146
+ )
147
+ expect(out.map((c) => Array.from(c.bytes))).toEqual([[7], [8]])
148
+ })
149
+ })
150
+
151
+ describe("MultiSpeakerTts capability marker (compile-time)", () => {
152
+ const dialogueReq: SpeechSynthesizer.CommonSynthesizeDialogueRequest = {
153
+ model: "mock-tts",
154
+ turns: [{ voiceId: "a", text: "Hi" }],
155
+ }
156
+
157
+ it("requires `MultiSpeakerTts` on the R channel of synthesizeDialogue", () => {
158
+ const eff = SpeechSynthesizer.synthesizeDialogue(dialogueReq)
159
+ expectTypeOf(eff).toEqualTypeOf<
160
+ Effect.Effect<
161
+ AudioBlob,
162
+ AiError.AiError,
163
+ SpeechSynthesizer.SpeechSynthesizer | SpeechSynthesizer.MultiSpeakerTts
164
+ >
165
+ >()
166
+ })
167
+
168
+ it("requires `MultiSpeakerTts` on the R channel of streamSynthesizeDialogue", () => {
169
+ const s = SpeechSynthesizer.streamSynthesizeDialogue(dialogueReq)
170
+ expectTypeOf(s).toEqualTypeOf<
171
+ Stream.Stream<
172
+ AudioChunk,
173
+ AiError.AiError,
174
+ SpeechSynthesizer.SpeechSynthesizer | SpeechSynthesizer.MultiSpeakerTts
175
+ >
176
+ >()
177
+ })
178
+
179
+ it("a layer without the marker leaves `MultiSpeakerTts` unsatisfied in R", () => {
180
+ const noMarker = MockSpeechSynthesizer.layerWithoutMultiSpeaker({})
181
+ const program = SpeechSynthesizer.synthesizeDialogue(dialogueReq).pipe(
182
+ Effect.provide(noMarker.layer),
183
+ )
184
+ expectTypeOf(program).toEqualTypeOf<
185
+ Effect.Effect<AudioBlob, AiError.AiError, SpeechSynthesizer.MultiSpeakerTts>
186
+ >()
187
+ })
188
+
189
+ it("a full layer (with marker) clears R to never", () => {
190
+ const fullMock = MockSpeechSynthesizer.layer({ dialogueBlobs: [blob] })
191
+ const program = SpeechSynthesizer.synthesizeDialogue(dialogueReq).pipe(
192
+ Effect.provide(fullMock.layer),
193
+ )
194
+ expectTypeOf(program).toEqualTypeOf<Effect.Effect<AudioBlob, AiError.AiError, never>>()
195
+ })
196
+ })
197
+
109
198
  describe("SpeechSynthesizer.streamSynthesisFrom", () => {
110
199
  const ssfReq: SpeechSynthesizer.CommonStreamSynthesizeRequest = {
111
200
  model: "mock-tts",
@@ -2,6 +2,23 @@ import { Context, Effect, Function, Stream } from "effect"
2
2
  import * as AiError from "../domain/AiError.js"
3
3
  import type { AudioBlob, AudioChunk, AudioFormat } from "../domain/Audio.js"
4
4
 
5
+ /**
6
+ * Per-phrase pronunciation override. `pronunciation` is always IPA, the
7
+ * universal phonetic interchange format. Adapters translate to the
8
+ * provider's wire form (inline `/ipa/` token, SSML `<phoneme>`, or a
9
+ * structured field), converting IPA to X-SAMPA internally where the wire
10
+ * requires it (Google, Azure).
11
+ *
12
+ * Pronunciations are load-bearing (the configured word is mispronounced
13
+ * if dropped), so a provider with no stateless IPA path fails the call
14
+ * with `AiError.Unsupported` rather than silently degrading.
15
+ */
16
+ export type CustomPronunciation = {
17
+ readonly phrase: string
18
+ /** IPA notation, e.g. `"ænˈθrɒpɪk"`. */
19
+ readonly pronunciation: string
20
+ }
21
+
5
22
  /**
6
23
  * Cross-provider synthesis request. Provider-specific extensions
7
24
  * (ElevenLabs `stability` / `similarity_boost`, Cartesia `emotion`,
@@ -23,6 +40,13 @@ export type CommonSynthesizeRequest = {
23
40
  readonly outputFormat?: AudioFormat
24
41
  readonly speed?: number
25
42
  readonly languageCode?: string
43
+ /**
44
+ * IPA phoneme overrides for specific phrases. Adapters render them to
45
+ * the provider's wire form (inline token, SSML `<phoneme>`, structured
46
+ * field). Load-bearing: a provider with no stateless IPA path fails
47
+ * the call with `AiError.Unsupported` rather than mispronouncing.
48
+ */
49
+ readonly pronunciations?: ReadonlyArray<CustomPronunciation>
26
50
  }
27
51
 
28
52
  /**
@@ -36,6 +60,31 @@ export type CommonSynthesizeRequest = {
36
60
  */
37
61
  export type CommonStreamSynthesizeRequest = Omit<CommonSynthesizeRequest, "text">
38
62
 
63
+ /**
64
+ * One turn in a multi-speaker dialogue. Just a voice and its line; the
65
+ * only in-tree dialogue endpoint (ElevenLabs `/v1/text-to-dialogue`)
66
+ * accepts `{ voice_id, text }` per turn. Per-turn styling knobs
67
+ * (Hume Octave-2 `styleDescription` / `speed`) are provider-specific
68
+ * and belong on a provider-typed turn extension, not here.
69
+ */
70
+ export type DialogueTurn = {
71
+ readonly voiceId: string
72
+ readonly text: string
73
+ }
74
+
75
+ /**
76
+ * Cross-provider dialogue request. Same return type as `synthesize` —
77
+ * one continuous `AudioBlob`. Per-turn timing metadata is not exposed
78
+ * in the common shape (only Hume returns it natively).
79
+ */
80
+ export type CommonSynthesizeDialogueRequest = {
81
+ readonly model: string
82
+ readonly turns: ReadonlyArray<DialogueTurn>
83
+ readonly outputFormat?: AudioFormat
84
+ readonly languageCode?: string
85
+ readonly pronunciations?: ReadonlyArray<CustomPronunciation>
86
+ }
87
+
39
88
  export type SpeechSynthesizerService = {
40
89
  /** One-shot. Full text in, full audio bytes out. Universally supported. */
41
90
  readonly synthesize: (
@@ -62,6 +111,26 @@ export type SpeechSynthesizerService = {
62
111
  textIn: Stream.Stream<string, E, R>,
63
112
  request: CommonStreamSynthesizeRequest,
64
113
  ) => Stream.Stream<AudioChunk, AiError.AiError | E, R>
114
+ /**
115
+ * One-shot multi-speaker dialogue. Turn array in, single `AudioBlob`
116
+ * out. Providers without native dialogue support return
117
+ * `AiError.Unsupported` and do NOT ship the `MultiSpeakerTts` marker
118
+ * — calls via the top-level helper become a compile-time error.
119
+ */
120
+ readonly synthesizeDialogue: (
121
+ request: CommonSynthesizeDialogueRequest,
122
+ ) => Effect.Effect<AudioBlob, AiError.AiError>
123
+ /**
124
+ * Chunked-streaming variant of `synthesizeDialogue`. Same input,
125
+ * audio chunks emitted as the wire delivers them. Providers that
126
+ * synthesize the whole dialogue server-side may fall back to a
127
+ * single-chunk wrap of the sync result.
128
+ *
129
+ * Gated by `MultiSpeakerTts` at the top-level helper.
130
+ */
131
+ readonly streamSynthesizeDialogue: (
132
+ request: CommonSynthesizeDialogueRequest,
133
+ ) => Stream.Stream<AudioChunk, AiError.AiError>
65
134
  }
66
135
 
67
136
  export class SpeechSynthesizer extends Context.Service<
@@ -83,6 +152,19 @@ export class TtsIncrementalText extends Context.Service<TtsIncrementalText, void
83
152
  "@betalyra/effect-uai/capability/TtsIncrementalText",
84
153
  ) {}
85
154
 
155
+ /**
156
+ * Capability marker for multi-speaker dialogue. Shipped by provider
157
+ * Layers whose `synthesizeDialogue` is wired to a native multi-speaker
158
+ * endpoint (ElevenLabs `/v1/text-to-dialogue`, Hume `/v0/tts` with
159
+ * `utterances[]`, Google Cloud TTS Gemini TTS `multiSpeakerMarkup`).
160
+ * Other providers leave the marker unregistered so the top-level
161
+ * `synthesizeDialogue` / `streamSynthesizeDialogue` helpers fail to
162
+ * satisfy R against those Layers — compile-time error.
163
+ */
164
+ export class MultiSpeakerTts extends Context.Service<MultiSpeakerTts, void>()(
165
+ "@betalyra/effect-uai/capability/MultiSpeakerTts",
166
+ ) {}
167
+
86
168
  /** One-shot synthesis. */
87
169
  export const synthesize = (
88
170
  request: CommonSynthesizeRequest,
@@ -129,3 +211,30 @@ export const streamSynthesisFrom: {
129
211
  }),
130
212
  ),
131
213
  )
214
+
215
+ /**
216
+ * One-shot multi-speaker dialogue. Requires `MultiSpeakerTts` in R —
217
+ * providers without dialogue support are a compile-time error.
218
+ */
219
+ export const synthesizeDialogue = (
220
+ request: CommonSynthesizeDialogueRequest,
221
+ ): Effect.Effect<AudioBlob, AiError.AiError, SpeechSynthesizer | MultiSpeakerTts> =>
222
+ Effect.gen(function* () {
223
+ const s = yield* SpeechSynthesizer.asEffect()
224
+ yield* MultiSpeakerTts.asEffect()
225
+ return yield* s.synthesizeDialogue(request)
226
+ })
227
+
228
+ /**
229
+ * Chunked-streaming variant. Same R requirement as `synthesizeDialogue`.
230
+ */
231
+ export const streamSynthesizeDialogue = (
232
+ request: CommonSynthesizeDialogueRequest,
233
+ ): Stream.Stream<AudioChunk, AiError.AiError, SpeechSynthesizer | MultiSpeakerTts> =>
234
+ Stream.unwrap(
235
+ Effect.gen(function* () {
236
+ const s = yield* SpeechSynthesizer.asEffect()
237
+ yield* MultiSpeakerTts.asEffect()
238
+ return s.streamSynthesizeDialogue(request)
239
+ }),
240
+ )
@@ -1,11 +1,13 @@
1
1
  import { Effect, Layer, Ref, Stream } from "effect"
2
2
  import * as AiError from "../domain/AiError.js"
3
3
  import type { AudioChunk } from "../domain/Audio.js"
4
- import type {
5
- CommonGenerateMusicRequest,
6
- CommonStreamGenerateMusicRequest,
7
- MusicResult,
8
- MusicSessionInput,
4
+ import {
5
+ audioEvent,
6
+ type CommonGenerateMusicRequest,
7
+ type CommonStreamGenerateMusicRequest,
8
+ type GenerateResult,
9
+ type MusicSessionInput,
10
+ type MusicStreamEvent,
9
11
  } from "../domain/Music.js"
10
12
  import {
11
13
  MusicGenerator,
@@ -21,11 +23,15 @@ export type MockMusicGeneratorRecorder = {
21
23
 
22
24
  export type MockMusicGeneratorScript = {
23
25
  /** One result per `generate` call, consumed in order. */
24
- readonly results?: ReadonlyArray<MusicResult>
26
+ readonly results?: ReadonlyArray<GenerateResult>
25
27
  /** One chunk-list per `streamGeneration` call, consumed in order. */
26
28
  readonly streamGenerationChunks?: ReadonlyArray<ReadonlyArray<AudioChunk>>
27
- /** One chunk-list per `streamGenerationFrom` call, consumed in order. */
28
- readonly streamGenerationFromChunks?: ReadonlyArray<ReadonlyArray<AudioChunk>>
29
+ /**
30
+ * One event-list per `streamGenerationFrom` call, consumed in order.
31
+ * If a list contains only `AudioChunk`s for ergonomics, wrap them in
32
+ * `audioEvent(chunk)` first.
33
+ */
34
+ readonly streamGenerationFromEvents?: ReadonlyArray<ReadonlyArray<MusicStreamEvent>>
29
35
  }
30
36
 
31
37
  const makeService = (
@@ -74,22 +80,23 @@ const makeService = (
74
80
  streamGenerationFrom: <E, R>(
75
81
  input: Stream.Stream<MusicSessionInput, E, R>,
76
82
  request: CommonStreamGenerateMusicRequest,
77
- ): Stream.Stream<AudioChunk, AiError.AiError | E, R> =>
83
+ ): Stream.Stream<MusicStreamEvent, AiError.AiError | E, R> =>
78
84
  Stream.unwrap(
79
85
  Effect.gen(function* () {
80
86
  yield* record.streamGenerationFrom(request)
81
87
  const i = yield* Ref.getAndUpdate(sgfCursor, (n) => n + 1)
82
- const scripted = script.streamGenerationFromChunks ?? []
88
+ const scripted = script.streamGenerationFromEvents ?? []
83
89
  if (i >= scripted.length) {
84
- const exhausted: Stream.Stream<AudioChunk, AiError.AiError | E, R> = Stream.fail(
85
- new AiError.InvalidRequest({
86
- provider: "mock",
87
- raw: `MockMusicGenerator exhausted: ${scripted.length} streamGenerationFrom lists scripted, but call ${i + 1} was made`,
88
- }),
89
- )
90
+ const exhausted: Stream.Stream<MusicStreamEvent, AiError.AiError | E, R> =
91
+ Stream.fail(
92
+ new AiError.InvalidRequest({
93
+ provider: "mock",
94
+ raw: `MockMusicGenerator exhausted: ${scripted.length} streamGenerationFrom lists scripted, but call ${i + 1} was made`,
95
+ }),
96
+ )
90
97
  return exhausted
91
98
  }
92
- // Drain the input fully before emitting scripted audio chunks,
99
+ // Drain the input fully before emitting scripted events,
93
100
  // so consumers can assert on what session messages were pushed.
94
101
  return Stream.drain(input).pipe(Stream.concat(Stream.fromIterable(scripted[i]!)))
95
102
  }),
@@ -133,7 +140,7 @@ export const layer = (
133
140
  }
134
141
 
135
142
  /**
136
- * Variant that omits the `MusicInteractiveSession` marker — simulates a
143
+ * Variant that omits the `MusicInteractiveSession` marker, simulates a
137
144
  * provider without bidirectional support (Lyria 3 sync, ElevenLabs,
138
145
  * Mureka, MiniMax, Stable Audio, Suno). Calls to
139
146
  * `streamGenerationFrom` in code under test should be a compile-time
@@ -166,3 +173,6 @@ export const layerWithoutInteractive = (
166
173
  }),
167
174
  }
168
175
  }
176
+
177
+ // Re-export the event constructor for test-script ergonomics.
178
+ export { audioEvent }
@@ -1,6 +1,6 @@
1
1
  import { Array as Arr, Duration, Effect, Layer, Match, Option, Ref, Schedule, Stream } from "effect"
2
2
  import * as AiError from "../domain/AiError.js"
3
- import { type Item, isOutputText } from "../domain/Items.js"
3
+ import { type HistoryItem, isOutputText } from "../domain/Items.js"
4
4
  import {
5
5
  LanguageModel,
6
6
  type LanguageModelService,
@@ -18,7 +18,7 @@ export type MockOptions = {
18
18
  }
19
19
 
20
20
  export type Call = {
21
- readonly history: ReadonlyArray<Item>
21
+ readonly history: ReadonlyArray<HistoryItem>
22
22
  readonly turn: Turn
23
23
  }
24
24
 
@@ -36,24 +36,25 @@ export type MockRecorder = {
36
36
  // Pure projection: Turn → ReadonlyArray<TurnEvent>
37
37
  // ---------------------------------------------------------------------------
38
38
 
39
- const itemToDeltas: (item: Item) => ReadonlyArray<TurnEvent> = Match.type<Item>().pipe(
40
- Match.discriminators("type")({
41
- message: (m): ReadonlyArray<TurnEvent> =>
42
- m.role === "assistant"
43
- ? m.content.filter(isOutputText).map((b) => TurnEvent.TextDelta({ text: b.text }))
44
- : [],
45
- function_call: (fc) => [
46
- TurnEvent.ToolCallStart({ call_id: fc.call_id, name: fc.name }),
47
- TurnEvent.ToolCallArgsDelta({ call_id: fc.call_id, delta: fc.arguments }),
48
- ],
49
- function_call_output: () => [],
50
- reasoning: (r) =>
51
- r.summary !== undefined
52
- ? [TurnEvent.ReasoningDelta({ text: r.summary, kind: "summary" as const })]
53
- : [],
54
- }),
55
- Match.exhaustive,
56
- )
39
+ const itemToDeltas: (item: HistoryItem) => ReadonlyArray<TurnEvent> =
40
+ Match.type<HistoryItem>().pipe(
41
+ Match.discriminators("type")({
42
+ message: (m): ReadonlyArray<TurnEvent> =>
43
+ m.role === "assistant"
44
+ ? m.content.filter(isOutputText).map((b) => TurnEvent.TextDelta({ text: b.text }))
45
+ : [],
46
+ function_call: (fc) => [
47
+ TurnEvent.ToolCallStart({ call_id: fc.call_id, name: fc.name }),
48
+ TurnEvent.ToolCallArgsDelta({ call_id: fc.call_id, delta: fc.arguments }),
49
+ ],
50
+ function_call_output: () => [],
51
+ reasoning: (r) =>
52
+ r.summary !== undefined
53
+ ? [TurnEvent.ReasoningDelta({ text: r.summary, kind: "summary" as const })]
54
+ : [],
55
+ }),
56
+ Match.exhaustive,
57
+ )
57
58
 
58
59
  const turnToDeltas = (turn: Turn): ReadonlyArray<TurnEvent> => [
59
60
  ...turn.items.flatMap(itemToDeltas),