agents 0.22.0 → 0.23.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 (186) hide show
  1. package/README.md +20 -1
  2. package/dist/{agent-routing-CnkaHb-v.d.ts → agent-routing-DE5zmCQ8.d.ts} +1038 -394
  3. package/dist/agent-routing.d.ts +1 -1
  4. package/dist/agent-tool-types.d.ts +26 -26
  5. package/dist/{agent-tools-C0XJqWzB.d.ts → agent-tools-DtXMTDGM.d.ts} +7 -7
  6. package/dist/agent-tools.d.ts +21 -21
  7. package/dist/browser/ai.js +4 -1
  8. package/dist/browser/ai.js.map +1 -1
  9. package/dist/browser/tanstack-ai.js.map +1 -1
  10. package/dist/callable-decorator-DP__HhBA.d.ts +72 -0
  11. package/dist/callable-decorator.d.ts +18 -0
  12. package/dist/callable-decorator.js +71 -0
  13. package/dist/callable-decorator.js.map +1 -0
  14. package/dist/capability-BjSKYpzg.js.map +1 -1
  15. package/dist/capability-runner-Be_-PLR1.d.ts +459 -0
  16. package/dist/channel-Bnm4S7T2.d.ts +491 -0
  17. package/dist/channels/ai-sdk.d.ts +46 -0
  18. package/dist/channels/ai-sdk.js +120 -0
  19. package/dist/channels/ai-sdk.js.map +1 -0
  20. package/dist/channels/email.d.ts +95 -0
  21. package/dist/channels/email.js +323 -0
  22. package/dist/channels/email.js.map +1 -0
  23. package/dist/channels/index.d.ts +233 -0
  24. package/dist/channels/index.js +608 -0
  25. package/dist/channels/index.js.map +1 -0
  26. package/dist/channels/slack.d.ts +140 -0
  27. package/dist/channels/slack.js +614 -0
  28. package/dist/channels/slack.js.map +1 -0
  29. package/dist/channels/tanstack-ai.d.ts +39 -0
  30. package/dist/channels/tanstack-ai.js +17 -0
  31. package/dist/channels/tanstack-ai.js.map +1 -0
  32. package/dist/channels/telegram.d.ts +106 -0
  33. package/dist/channels/telegram.js +427 -0
  34. package/dist/channels/telegram.js.map +1 -0
  35. package/dist/channels/voice.d.ts +45 -0
  36. package/dist/channels/voice.js +122 -0
  37. package/dist/channels/voice.js.map +1 -0
  38. package/dist/chat/index.d.ts +2325 -2019
  39. package/dist/chat/index.js +888 -518
  40. package/dist/chat/index.js.map +1 -1
  41. package/dist/chat-sdk/index.d.ts +7 -7
  42. package/dist/chat-sdk/index.js +1 -1
  43. package/dist/client.d.ts +1 -1
  44. package/dist/context/index.d.ts +216 -0
  45. package/dist/context/index.js +454 -0
  46. package/dist/context/index.js.map +1 -0
  47. package/dist/{current-agent-CuMErtly.d.ts → current-agent-Da_C9a3b.d.ts} +90 -107
  48. package/dist/current-agent-DhoDkSnH.js.map +1 -1
  49. package/dist/{diagnostics-CaBjfz4J.js → diagnostics-BzvaX2UT.js} +5 -1
  50. package/dist/diagnostics-BzvaX2UT.js.map +1 -0
  51. package/dist/diagnostics-C4jcz3VK.js +360 -0
  52. package/dist/diagnostics-C4jcz3VK.js.map +1 -0
  53. package/dist/index-BB0kqhIz.d.ts +101 -0
  54. package/dist/index-BVVgDSdq.d.ts +1 -0
  55. package/dist/index-XDkuQ7zm.d.ts +89 -0
  56. package/dist/{index-DcSAZKsB.d.ts → index-YSKgfgg9.d.ts} +3 -1
  57. package/dist/index.d.ts +91 -82
  58. package/dist/index.js +3 -2
  59. package/dist/ingress-BfetZbMO.js +83 -0
  60. package/dist/ingress-BfetZbMO.js.map +1 -0
  61. package/dist/internal-CYlgHl1l.js +59 -0
  62. package/dist/internal-CYlgHl1l.js.map +1 -0
  63. package/dist/internal_context.d.ts +1 -1
  64. package/dist/lifecycle/index.d.ts +35 -17
  65. package/dist/lifecycle/index.js +1 -1
  66. package/dist/lifecycle-CMRGjZdw.js +1299 -0
  67. package/dist/lifecycle-CMRGjZdw.js.map +1 -0
  68. package/dist/mcp/client/index.d.ts +20 -20
  69. package/dist/mcp/index.d.ts +35 -35
  70. package/dist/mcp/index.js +1 -1
  71. package/dist/observability/index.d.ts +1 -1
  72. package/dist/observability/index.js +1 -1
  73. package/dist/react.d.ts +4 -4
  74. package/dist/{retries-CAvxtG9d.d.ts → retries-D9Ds-1lz.d.ts} +17 -6
  75. package/dist/retries.d.ts +8 -6
  76. package/dist/retries.js +13 -1
  77. package/dist/retries.js.map +1 -1
  78. package/dist/routing/index.d.ts +137 -0
  79. package/dist/routing/index.js +244 -0
  80. package/dist/routing/index.js.map +1 -0
  81. package/dist/sanitize-D9TujEK8.js +79 -0
  82. package/dist/sanitize-D9TujEK8.js.map +1 -0
  83. package/dist/scheduler-DD9NdYbF.js +665 -0
  84. package/dist/scheduler-DD9NdYbF.js.map +1 -0
  85. package/dist/{scheduler-DQoTGoAW.d.ts → scheduler-Dwh85ZGl.d.ts} +21 -22
  86. package/dist/schedules/index.d.ts +1 -1
  87. package/dist/schedules/index.js +1 -1
  88. package/dist/sentence-chunker-BAidJ4DA.d.ts +68 -0
  89. package/dist/serializable.d.ts +1 -1
  90. package/dist/sessions/index.d.ts +441 -0
  91. package/dist/sessions/index.js +2063 -0
  92. package/dist/sessions/index.js.map +1 -0
  93. package/dist/skills/index.d.ts +99 -0
  94. package/dist/skills/index.js +254 -5
  95. package/dist/skills/index.js.map +1 -1
  96. package/dist/{src-5W6JNKVb.js → src-DlSHshb2.js} +1460 -1110
  97. package/dist/src-DlSHshb2.js.map +1 -0
  98. package/dist/streams/index.d.ts +120 -0
  99. package/dist/streams/index.js +107 -0
  100. package/dist/streams/index.js.map +1 -0
  101. package/dist/streams-D6tJ0NN9.d.ts +370 -0
  102. package/dist/streams-DZKgAj9b.js +709 -0
  103. package/dist/streams-DZKgAj9b.js.map +1 -0
  104. package/dist/sub-routing.d.ts +12 -12
  105. package/dist/surface-bZZJqBka.js +17 -0
  106. package/dist/surface-bZZJqBka.js.map +1 -0
  107. package/dist/tasks/index.d.ts +64 -0
  108. package/dist/tasks/index.js +2 -0
  109. package/dist/tasks-BRJ5zgya.d.ts +517 -0
  110. package/dist/tasks-ylZgBjhj.js +1656 -0
  111. package/dist/tasks-ylZgBjhj.js.map +1 -0
  112. package/dist/text-segment-joiner-BtAFQSA_.js +57 -0
  113. package/dist/text-segment-joiner-BtAFQSA_.js.map +1 -0
  114. package/dist/text-stream-CpdiKrJB.js +272 -0
  115. package/dist/text-stream-CpdiKrJB.js.map +1 -0
  116. package/dist/tokens-nHAKcN6M.js +52 -0
  117. package/dist/tokens-nHAKcN6M.js.map +1 -0
  118. package/dist/tool-schema-CBjGPrsQ.js +31 -0
  119. package/dist/tool-schema-CBjGPrsQ.js.map +1 -0
  120. package/dist/types-B7LojTe4.d.ts +202 -0
  121. package/dist/types-_Faxb570.d.ts +439 -0
  122. package/dist/voice/client.d.ts +226 -0
  123. package/dist/voice/client.js +932 -0
  124. package/dist/voice/client.js.map +1 -0
  125. package/dist/voice/errors.d.ts +43 -0
  126. package/dist/voice/errors.js +41 -0
  127. package/dist/voice/errors.js.map +1 -0
  128. package/dist/voice/index.d.ts +271 -0
  129. package/dist/voice/index.js +1812 -0
  130. package/dist/voice/index.js.map +1 -0
  131. package/dist/voice/react.d.ts +167 -0
  132. package/dist/voice/react.js +234 -0
  133. package/dist/voice/react.js.map +1 -0
  134. package/dist/voice/sfu.d.ts +71 -0
  135. package/dist/voice/sfu.js +157 -0
  136. package/dist/voice/sfu.js.map +1 -0
  137. package/dist/voice/text.d.ts +6 -0
  138. package/dist/voice/text.js +2 -0
  139. package/dist/voice/types.d.ts +58 -0
  140. package/dist/voice/types.js +18 -0
  141. package/dist/voice/types.js.map +1 -0
  142. package/dist/voice/workers-ai.d.ts +136 -0
  143. package/dist/voice/workers-ai.js +568 -0
  144. package/dist/voice/workers-ai.js.map +1 -0
  145. package/dist/websockets/index.d.ts +192 -0
  146. package/dist/websockets/index.js +2 -0
  147. package/dist/websockets-DUfRHPRq.js +502 -0
  148. package/dist/websockets-DUfRHPRq.js.map +1 -0
  149. package/dist/workflow-types.d.ts +25 -25
  150. package/dist/workflows.d.ts +21 -21
  151. package/dist/workflows.js +1 -1
  152. package/docs/agent-class.md +2 -2
  153. package/docs/agent-tools.md +2 -1
  154. package/docs/channels.md +323 -0
  155. package/docs/chat-agents.md +6 -13
  156. package/docs/context.md +131 -0
  157. package/docs/index.md +15 -12
  158. package/docs/lifecycle.md +102 -55
  159. package/docs/long-running-agents.md +2 -2
  160. package/docs/mcp-servers.md +5 -1
  161. package/docs/resumable-streaming.md +1 -1
  162. package/docs/routing.md +105 -0
  163. package/docs/sessions.md +237 -871
  164. package/docs/streams.md +213 -0
  165. package/docs/sub-agents.md +184 -124
  166. package/docs/tasks.md +246 -0
  167. package/docs/voice.md +745 -0
  168. package/package.json +115 -13
  169. package/dist/capability-runner-CvHGZqUu.d.ts +0 -150
  170. package/dist/compaction-helpers-iiKMr2TQ.js +0 -340
  171. package/dist/compaction-helpers-iiKMr2TQ.js.map +0 -1
  172. package/dist/compaction-helpers-wUz6M3us.d.ts +0 -621
  173. package/dist/diagnostics-CaBjfz4J.js.map +0 -1
  174. package/dist/durable-object-lifecycle-D6nNQJJd.js +0 -862
  175. package/dist/durable-object-lifecycle-D6nNQJJd.js.map +0 -1
  176. package/dist/experimental/memory/session/index.d.ts +0 -671
  177. package/dist/experimental/memory/session/index.js +0 -2379
  178. package/dist/experimental/memory/session/index.js.map +0 -1
  179. package/dist/experimental/memory/utils/index.d.ts +0 -96
  180. package/dist/experimental/memory/utils/index.js +0 -79
  181. package/dist/experimental/memory/utils/index.js.map +0 -1
  182. package/dist/scheduler-CR9RHGos.js +0 -857
  183. package/dist/scheduler-CR9RHGos.js.map +0 -1
  184. package/dist/src-5W6JNKVb.js.map +0 -1
  185. package/dist/tool-output-truncation-CNnnGZQ3.js +0 -98
  186. package/dist/tool-output-truncation-CNnnGZQ3.js.map +0 -1
@@ -0,0 +1,136 @@
1
+ import {
2
+ a as TranscriberSessionOptions,
3
+ i as TranscriberSession,
4
+ n as TTSProvider,
5
+ r as Transcriber
6
+ } from "../types-_Faxb570.js";
7
+
8
+ //#region src/voice/workers-ai.d.ts
9
+ /** Loose type for the Workers AI binding — avoids hard dependency on @cloudflare/workers-types. */
10
+ interface AiLike {
11
+ run(
12
+ model: string,
13
+ input: Record<string, unknown>,
14
+ options?: Record<string, unknown>
15
+ ): Promise<unknown>;
16
+ }
17
+ interface WorkersAITTSOptions {
18
+ /** TTS model name. @default "@cf/deepgram/aura-1" */
19
+ model?: string;
20
+ /** TTS speaker voice. @default "asteria" */
21
+ speaker?: string;
22
+ }
23
+ /**
24
+ * Workers AI text-to-speech provider.
25
+ *
26
+ * @example
27
+ * ```ts
28
+ * class MyAgent extends VoiceAgent<Env> {
29
+ * tts = new WorkersAITTS(this.env.AI);
30
+ * }
31
+ * ```
32
+ */
33
+ declare class WorkersAITTS implements TTSProvider {
34
+ #private;
35
+ constructor(ai: AiLike, options?: WorkersAITTSOptions);
36
+ synthesize(text: string, signal?: AbortSignal): Promise<ArrayBuffer | null>;
37
+ }
38
+ interface WorkersAIFluxSTTOptions {
39
+ /** End-of-turn confidence threshold (0.5-0.9). @default 0.7 */
40
+ eotThreshold?: number;
41
+ /**
42
+ * Eager end-of-turn threshold (0.3-0.9). When set, enables
43
+ * EagerEndOfTurn and TurnResumed events for speculative processing.
44
+ */
45
+ eagerEotThreshold?: number;
46
+ /** EOT timeout in milliseconds. @default 5000 */
47
+ eotTimeoutMs?: number;
48
+ /** Keyterms to boost recognition of specialized terminology. */
49
+ keyterms?: string[];
50
+ /** Sample rate in Hz. @default 16000 */
51
+ sampleRate?: number;
52
+ }
53
+ /**
54
+ * Workers AI continuous speech-to-text provider using the Flux model.
55
+ *
56
+ * Flux is a conversational STT model with built-in end-of-turn detection.
57
+ * A single session is created per call and receives all audio continuously.
58
+ * The model detects speech boundaries and fires `onUtterance` when a
59
+ * turn is complete — no client-side silence detection needed for STT.
60
+ *
61
+ * Recommended for `withVoice` (conversational voice agents).
62
+ *
63
+ * @example
64
+ * ```ts
65
+ * import { Agent } from "agents";
66
+ * import { withVoice, WorkersAIFluxSTT, WorkersAITTS } from "agents/voice";
67
+ *
68
+ * const VoiceAgent = withVoice(Agent);
69
+ *
70
+ * class MyAgent extends VoiceAgent<Env> {
71
+ * transcriber = new WorkersAIFluxSTT(this.env.AI);
72
+ * tts = new WorkersAITTS(this.env.AI);
73
+ *
74
+ * async onTurn(transcript, context) { ... }
75
+ * }
76
+ * ```
77
+ */
78
+ declare class WorkersAIFluxSTT implements Transcriber {
79
+ #private;
80
+ constructor(ai: AiLike, options?: WorkersAIFluxSTTOptions);
81
+ createSession(options?: TranscriberSessionOptions): TranscriberSession;
82
+ }
83
+ interface WorkersAINova3STTOptions {
84
+ /** Language code. @default "en" */
85
+ language?: string;
86
+ /** Endpointing silence duration in ms. @default 300 */
87
+ endpointingMs?: number;
88
+ /** Utterance end detection timeout in ms. @default 1000 */
89
+ utteranceEndMs?: number;
90
+ /** Enable smart formatting (numbers, dates, etc.). @default true */
91
+ smartFormat?: boolean;
92
+ /** Enable punctuation. @default true */
93
+ punctuate?: boolean;
94
+ /** Keyterms to boost recognition of specialized terminology. */
95
+ keyterms?: string[];
96
+ /** Sample rate in Hz. @default 16000 */
97
+ sampleRate?: number;
98
+ }
99
+ /**
100
+ * Workers AI continuous speech-to-text provider using Nova 3.
101
+ *
102
+ * Nova 3 is a high-accuracy STT model with streaming WebSocket support.
103
+ * A single session is created per call and receives all audio continuously.
104
+ * Server-side VAD events and endpointing handle speech boundary detection.
105
+ *
106
+ * Recommended for `withVoiceInput` (dictation / voice input UIs).
107
+ *
108
+ * @example
109
+ * ```ts
110
+ * import { Agent } from "agents";
111
+ * import { withVoiceInput, WorkersAINova3STT } from "agents/voice";
112
+ *
113
+ * const InputAgent = withVoiceInput(Agent);
114
+ *
115
+ * class MyAgent extends InputAgent<Env> {
116
+ * transcriber = new WorkersAINova3STT(this.env.AI);
117
+ *
118
+ * onTranscript(text, connection) { ... }
119
+ * }
120
+ * ```
121
+ */
122
+ declare class WorkersAINova3STT implements Transcriber {
123
+ #private;
124
+ constructor(ai: AiLike, options?: WorkersAINova3STTOptions);
125
+ createSession(options?: TranscriberSessionOptions): TranscriberSession;
126
+ }
127
+ //#endregion
128
+ export {
129
+ WorkersAIFluxSTT,
130
+ WorkersAIFluxSTTOptions,
131
+ WorkersAINova3STT,
132
+ WorkersAINova3STTOptions,
133
+ WorkersAITTS,
134
+ WorkersAITTSOptions
135
+ };
136
+ //# sourceMappingURL=workers-ai.d.ts.map
@@ -0,0 +1,568 @@
1
+ import { i as _classPrivateFieldInitSpec, n as _classPrivateFieldSet2, r as _assertClassBrand, t as _classPrivateFieldGet2 } from "../classPrivateFieldGet2-DZBYAB34.js";
2
+ import { t as _classPrivateMethodInitSpec } from "../classPrivateMethodInitSpec-qMjJ6sHQ.js";
3
+ import { VoiceProviderError, logVoiceError, toVoiceError } from "./errors.js";
4
+ //#region src/voice/workers-ai.ts
5
+ /**
6
+ * Workers AI provider implementations for the voice pipeline.
7
+ *
8
+ * These are convenience classes that wrap the Workers AI binding
9
+ * (env.AI) for STT and TTS. They are not required — any object
10
+ * satisfying the provider interfaces works.
11
+ */
12
+ var _ai = /* @__PURE__ */ new WeakMap();
13
+ var _model = /* @__PURE__ */ new WeakMap();
14
+ var _speaker = /* @__PURE__ */ new WeakMap();
15
+ /**
16
+ * Workers AI text-to-speech provider.
17
+ *
18
+ * @example
19
+ * ```ts
20
+ * class MyAgent extends VoiceAgent<Env> {
21
+ * tts = new WorkersAITTS(this.env.AI);
22
+ * }
23
+ * ```
24
+ */
25
+ var WorkersAITTS = class {
26
+ constructor(ai, options) {
27
+ _classPrivateFieldInitSpec(this, _ai, void 0);
28
+ _classPrivateFieldInitSpec(this, _model, void 0);
29
+ _classPrivateFieldInitSpec(this, _speaker, void 0);
30
+ _classPrivateFieldSet2(_ai, this, ai);
31
+ _classPrivateFieldSet2(_model, this, options?.model ?? "@cf/deepgram/aura-1");
32
+ _classPrivateFieldSet2(_speaker, this, options?.speaker ?? "asteria");
33
+ }
34
+ async synthesize(text, signal) {
35
+ const response = await _classPrivateFieldGet2(_ai, this).run(_classPrivateFieldGet2(_model, this), {
36
+ text,
37
+ speaker: _classPrivateFieldGet2(_speaker, this)
38
+ }, {
39
+ returnRawResponse: true,
40
+ ...signal ? { signal } : {}
41
+ });
42
+ if (!response.ok) {
43
+ logVoiceError({
44
+ component: "WorkersAITTS",
45
+ stage: "synthesize",
46
+ message: "Workers AI TTS request failed",
47
+ error: new VoiceProviderError("Workers AI TTS request failed", { status: response.status })
48
+ });
49
+ return null;
50
+ }
51
+ return await response.arrayBuffer();
52
+ }
53
+ };
54
+ var _ai2 = /* @__PURE__ */ new WeakMap();
55
+ var _sampleRate = /* @__PURE__ */ new WeakMap();
56
+ var _eotThreshold = /* @__PURE__ */ new WeakMap();
57
+ var _eagerEotThreshold = /* @__PURE__ */ new WeakMap();
58
+ var _eotTimeoutMs = /* @__PURE__ */ new WeakMap();
59
+ var _keyterms = /* @__PURE__ */ new WeakMap();
60
+ /**
61
+ * Workers AI continuous speech-to-text provider using the Flux model.
62
+ *
63
+ * Flux is a conversational STT model with built-in end-of-turn detection.
64
+ * A single session is created per call and receives all audio continuously.
65
+ * The model detects speech boundaries and fires `onUtterance` when a
66
+ * turn is complete — no client-side silence detection needed for STT.
67
+ *
68
+ * Recommended for `withVoice` (conversational voice agents).
69
+ *
70
+ * @example
71
+ * ```ts
72
+ * import { Agent } from "agents";
73
+ * import { withVoice, WorkersAIFluxSTT, WorkersAITTS } from "agents/voice";
74
+ *
75
+ * const VoiceAgent = withVoice(Agent);
76
+ *
77
+ * class MyAgent extends VoiceAgent<Env> {
78
+ * transcriber = new WorkersAIFluxSTT(this.env.AI);
79
+ * tts = new WorkersAITTS(this.env.AI);
80
+ *
81
+ * async onTurn(transcript, context) { ... }
82
+ * }
83
+ * ```
84
+ */
85
+ var WorkersAIFluxSTT = class {
86
+ constructor(ai, options) {
87
+ _classPrivateFieldInitSpec(this, _ai2, void 0);
88
+ _classPrivateFieldInitSpec(this, _sampleRate, void 0);
89
+ _classPrivateFieldInitSpec(this, _eotThreshold, void 0);
90
+ _classPrivateFieldInitSpec(this, _eagerEotThreshold, void 0);
91
+ _classPrivateFieldInitSpec(this, _eotTimeoutMs, void 0);
92
+ _classPrivateFieldInitSpec(this, _keyterms, void 0);
93
+ _classPrivateFieldSet2(_ai2, this, ai);
94
+ _classPrivateFieldSet2(_sampleRate, this, options?.sampleRate ?? 16e3);
95
+ _classPrivateFieldSet2(_eotThreshold, this, options?.eotThreshold);
96
+ _classPrivateFieldSet2(_eagerEotThreshold, this, options?.eagerEotThreshold);
97
+ _classPrivateFieldSet2(_eotTimeoutMs, this, options?.eotTimeoutMs);
98
+ _classPrivateFieldSet2(_keyterms, this, options?.keyterms);
99
+ }
100
+ createSession(options) {
101
+ return new FluxSession(_classPrivateFieldGet2(_ai2, this), {
102
+ sampleRate: _classPrivateFieldGet2(_sampleRate, this),
103
+ eotThreshold: _classPrivateFieldGet2(_eotThreshold, this),
104
+ eagerEotThreshold: _classPrivateFieldGet2(_eagerEotThreshold, this),
105
+ eotTimeoutMs: _classPrivateFieldGet2(_eotTimeoutMs, this),
106
+ keyterms: _classPrivateFieldGet2(_keyterms, this)
107
+ }, options);
108
+ }
109
+ };
110
+ var _onInterim = /* @__PURE__ */ new WeakMap();
111
+ var _onSpeechStart = /* @__PURE__ */ new WeakMap();
112
+ var _onUtterance = /* @__PURE__ */ new WeakMap();
113
+ var _onFatalError = /* @__PURE__ */ new WeakMap();
114
+ var _ws = /* @__PURE__ */ new WeakMap();
115
+ var _connected = /* @__PURE__ */ new WeakMap();
116
+ var _closed = /* @__PURE__ */ new WeakMap();
117
+ var _fatalReported = /* @__PURE__ */ new WeakMap();
118
+ var _pendingChunks = /* @__PURE__ */ new WeakMap();
119
+ var _currentTranscript = /* @__PURE__ */ new WeakMap();
120
+ var _ready = /* @__PURE__ */ new WeakMap();
121
+ var _resolveReady = /* @__PURE__ */ new WeakMap();
122
+ var _rejectReady = /* @__PURE__ */ new WeakMap();
123
+ var _FluxSession_brand = /* @__PURE__ */ new WeakSet();
124
+ /**
125
+ * Per-call Flux transcription session. Lives for the entire call.
126
+ *
127
+ * Handles multi-turn conversations: on EndOfTurn, fires onUtterance
128
+ * and resets transcript state for the next turn. On StartOfTurn,
129
+ * clears accumulated text. The session stays alive across turns
130
+ * and is only closed on end_call or disconnect.
131
+ */
132
+ var FluxSession = class {
133
+ constructor(ai, config, options) {
134
+ _classPrivateMethodInitSpec(this, _FluxSession_brand);
135
+ _classPrivateFieldInitSpec(this, _onInterim, void 0);
136
+ _classPrivateFieldInitSpec(this, _onSpeechStart, void 0);
137
+ _classPrivateFieldInitSpec(this, _onUtterance, void 0);
138
+ _classPrivateFieldInitSpec(this, _onFatalError, void 0);
139
+ _classPrivateFieldInitSpec(this, _ws, null);
140
+ _classPrivateFieldInitSpec(this, _connected, false);
141
+ _classPrivateFieldInitSpec(this, _closed, false);
142
+ _classPrivateFieldInitSpec(this, _fatalReported, false);
143
+ _classPrivateFieldInitSpec(this, _pendingChunks, []);
144
+ _classPrivateFieldInitSpec(this, _currentTranscript, "");
145
+ _classPrivateFieldInitSpec(this, _ready, void 0);
146
+ _classPrivateFieldInitSpec(this, _resolveReady, null);
147
+ _classPrivateFieldInitSpec(this, _rejectReady, null);
148
+ _classPrivateFieldSet2(_onInterim, this, options?.onInterim);
149
+ _classPrivateFieldSet2(_onSpeechStart, this, options?.onSpeechStart);
150
+ _classPrivateFieldSet2(_onUtterance, this, options?.onUtterance);
151
+ _classPrivateFieldSet2(_onFatalError, this, options?.onFatalError);
152
+ _classPrivateFieldSet2(_ready, this, new Promise((resolve, reject) => {
153
+ _classPrivateFieldSet2(_resolveReady, this, resolve);
154
+ _classPrivateFieldSet2(_rejectReady, this, reject);
155
+ }));
156
+ _classPrivateFieldGet2(_ready, this).catch(() => {});
157
+ _assertClassBrand(_FluxSession_brand, this, _connect).call(this, ai, config);
158
+ }
159
+ waitUntilReady() {
160
+ return _classPrivateFieldGet2(_ready, this);
161
+ }
162
+ feed(chunk) {
163
+ if (_classPrivateFieldGet2(_closed, this)) return;
164
+ if (_classPrivateFieldGet2(_connected, this) && _classPrivateFieldGet2(_ws, this)) _classPrivateFieldGet2(_ws, this).send(chunk);
165
+ else _classPrivateFieldGet2(_pendingChunks, this).push(chunk);
166
+ }
167
+ close() {
168
+ if (_classPrivateFieldGet2(_closed, this)) return;
169
+ _classPrivateFieldSet2(_closed, this, true);
170
+ _classPrivateFieldSet2(_pendingChunks, this, []);
171
+ if (_classPrivateFieldGet2(_ws, this)) {
172
+ try {
173
+ _classPrivateFieldGet2(_ws, this).close();
174
+ } catch {}
175
+ _classPrivateFieldSet2(_ws, this, null);
176
+ }
177
+ _classPrivateFieldSet2(_connected, this, false);
178
+ _assertClassBrand(_FluxSession_brand, this, _resolveReadiness).call(this);
179
+ }
180
+ };
181
+ async function _connect(ai, config) {
182
+ try {
183
+ const input = {
184
+ encoding: "linear16",
185
+ sample_rate: String(config.sampleRate)
186
+ };
187
+ if (config.eotThreshold != null) input.eot_threshold = String(config.eotThreshold);
188
+ if (config.eagerEotThreshold != null) input.eager_eot_threshold = String(config.eagerEotThreshold);
189
+ if (config.eotTimeoutMs != null) input.eot_timeout_ms = String(config.eotTimeoutMs);
190
+ if (config.keyterms?.length) input.keyterm = config.keyterms;
191
+ const resp = await ai.run("@cf/deepgram/flux", input, { websocket: true });
192
+ if (_classPrivateFieldGet2(_closed, this)) {
193
+ const ws = resp.webSocket;
194
+ if (ws) {
195
+ ws.accept();
196
+ ws.close();
197
+ }
198
+ _assertClassBrand(_FluxSession_brand, this, _resolveReadiness).call(this);
199
+ return;
200
+ }
201
+ const ws = resp.webSocket;
202
+ if (!ws) {
203
+ const error = /* @__PURE__ */ new Error("Workers AI Flux STT did not return a WebSocket");
204
+ logVoiceError({
205
+ component: "FluxSTT",
206
+ stage: "connection",
207
+ message: "Flux STT WebSocket upgrade failed",
208
+ error
209
+ });
210
+ _assertClassBrand(_FluxSession_brand, this, _reportFatal).call(this, error);
211
+ _assertClassBrand(_FluxSession_brand, this, _rejectReadiness).call(this, error);
212
+ return;
213
+ }
214
+ ws.accept();
215
+ _classPrivateFieldSet2(_ws, this, ws);
216
+ _classPrivateFieldSet2(_connected, this, true);
217
+ ws.addEventListener("message", (event) => {
218
+ _assertClassBrand(_FluxSession_brand, this, _handleMessage).call(this, event);
219
+ });
220
+ ws.addEventListener("close", () => {
221
+ _classPrivateFieldSet2(_connected, this, false);
222
+ if (_classPrivateFieldGet2(_closed, this)) return;
223
+ const error = /* @__PURE__ */ new Error("Workers AI Flux STT WebSocket closed unexpectedly");
224
+ logVoiceError({
225
+ component: "FluxSTT",
226
+ stage: "websocket_close",
227
+ message: "Flux STT WebSocket closed unexpectedly",
228
+ error
229
+ });
230
+ _assertClassBrand(_FluxSession_brand, this, _reportFatal).call(this, error);
231
+ });
232
+ ws.addEventListener("error", (event) => {
233
+ const error = new Error("Workers AI Flux STT WebSocket error", { cause: event });
234
+ logVoiceError({
235
+ component: "FluxSTT",
236
+ stage: "websocket",
237
+ message: "Flux STT WebSocket error",
238
+ error
239
+ });
240
+ _classPrivateFieldSet2(_connected, this, false);
241
+ _assertClassBrand(_FluxSession_brand, this, _reportFatal).call(this, error);
242
+ });
243
+ for (const chunk of _classPrivateFieldGet2(_pendingChunks, this)) ws.send(chunk);
244
+ _classPrivateFieldSet2(_pendingChunks, this, []);
245
+ _assertClassBrand(_FluxSession_brand, this, _resolveReadiness).call(this);
246
+ } catch (error) {
247
+ const voiceError = toVoiceError(error, "Flux STT connection failed");
248
+ logVoiceError({
249
+ component: "FluxSTT",
250
+ stage: "connection",
251
+ message: "Flux STT connection failed",
252
+ error: voiceError
253
+ });
254
+ _assertClassBrand(_FluxSession_brand, this, _reportFatal).call(this, voiceError);
255
+ _assertClassBrand(_FluxSession_brand, this, _rejectReadiness).call(this, voiceError);
256
+ }
257
+ }
258
+ function _resolveReadiness() {
259
+ const resolve = _classPrivateFieldGet2(_resolveReady, this);
260
+ if (!resolve) return;
261
+ _classPrivateFieldSet2(_resolveReady, this, null);
262
+ _classPrivateFieldSet2(_rejectReady, this, null);
263
+ resolve();
264
+ }
265
+ function _rejectReadiness(reason) {
266
+ const reject = _classPrivateFieldGet2(_rejectReady, this);
267
+ if (!reject) return;
268
+ _classPrivateFieldSet2(_resolveReady, this, null);
269
+ _classPrivateFieldSet2(_rejectReady, this, null);
270
+ reject(reason);
271
+ }
272
+ function _reportFatal(error) {
273
+ if (_classPrivateFieldGet2(_closed, this) || _classPrivateFieldGet2(_fatalReported, this)) return;
274
+ _classPrivateFieldSet2(_fatalReported, this, true);
275
+ _classPrivateFieldGet2(_onFatalError, this)?.call(this, error);
276
+ }
277
+ function _handleMessage(event) {
278
+ if (_classPrivateFieldGet2(_closed, this)) return;
279
+ try {
280
+ const data = typeof event.data === "string" ? JSON.parse(event.data) : null;
281
+ if (!data || !data.event) return;
282
+ const transcript = data.transcript ?? "";
283
+ switch (data.event) {
284
+ case "StartOfTurn":
285
+ _classPrivateFieldSet2(_currentTranscript, this, "");
286
+ _classPrivateFieldGet2(_onSpeechStart, this)?.call(this, transcript || void 0);
287
+ if (transcript) {
288
+ _classPrivateFieldSet2(_currentTranscript, this, transcript);
289
+ _classPrivateFieldGet2(_onInterim, this)?.call(this, transcript);
290
+ }
291
+ break;
292
+ case "Update":
293
+ if (transcript) {
294
+ _classPrivateFieldSet2(_currentTranscript, this, transcript);
295
+ _classPrivateFieldGet2(_onInterim, this)?.call(this, transcript);
296
+ }
297
+ break;
298
+ case "EndOfTurn": {
299
+ const finalTranscript = transcript || _classPrivateFieldGet2(_currentTranscript, this);
300
+ _classPrivateFieldSet2(_currentTranscript, this, "");
301
+ if (finalTranscript) _classPrivateFieldGet2(_onUtterance, this)?.call(this, finalTranscript);
302
+ break;
303
+ }
304
+ case "EagerEndOfTurn":
305
+ if (transcript) {
306
+ _classPrivateFieldSet2(_currentTranscript, this, transcript);
307
+ _classPrivateFieldGet2(_onInterim, this)?.call(this, transcript);
308
+ }
309
+ break;
310
+ case "TurnResumed":
311
+ _classPrivateFieldSet2(_currentTranscript, this, transcript);
312
+ if (transcript) _classPrivateFieldGet2(_onInterim, this)?.call(this, transcript);
313
+ break;
314
+ }
315
+ } catch {}
316
+ }
317
+ var _ai3 = /* @__PURE__ */ new WeakMap();
318
+ var _sampleRate2 = /* @__PURE__ */ new WeakMap();
319
+ var _language = /* @__PURE__ */ new WeakMap();
320
+ var _endpointingMs = /* @__PURE__ */ new WeakMap();
321
+ var _utteranceEndMs = /* @__PURE__ */ new WeakMap();
322
+ var _smartFormat = /* @__PURE__ */ new WeakMap();
323
+ var _punctuate = /* @__PURE__ */ new WeakMap();
324
+ var _keyterms2 = /* @__PURE__ */ new WeakMap();
325
+ /**
326
+ * Workers AI continuous speech-to-text provider using Nova 3.
327
+ *
328
+ * Nova 3 is a high-accuracy STT model with streaming WebSocket support.
329
+ * A single session is created per call and receives all audio continuously.
330
+ * Server-side VAD events and endpointing handle speech boundary detection.
331
+ *
332
+ * Recommended for `withVoiceInput` (dictation / voice input UIs).
333
+ *
334
+ * @example
335
+ * ```ts
336
+ * import { Agent } from "agents";
337
+ * import { withVoiceInput, WorkersAINova3STT } from "agents/voice";
338
+ *
339
+ * const InputAgent = withVoiceInput(Agent);
340
+ *
341
+ * class MyAgent extends InputAgent<Env> {
342
+ * transcriber = new WorkersAINova3STT(this.env.AI);
343
+ *
344
+ * onTranscript(text, connection) { ... }
345
+ * }
346
+ * ```
347
+ */
348
+ var WorkersAINova3STT = class {
349
+ constructor(ai, options) {
350
+ _classPrivateFieldInitSpec(this, _ai3, void 0);
351
+ _classPrivateFieldInitSpec(this, _sampleRate2, void 0);
352
+ _classPrivateFieldInitSpec(this, _language, void 0);
353
+ _classPrivateFieldInitSpec(this, _endpointingMs, void 0);
354
+ _classPrivateFieldInitSpec(this, _utteranceEndMs, void 0);
355
+ _classPrivateFieldInitSpec(this, _smartFormat, void 0);
356
+ _classPrivateFieldInitSpec(this, _punctuate, void 0);
357
+ _classPrivateFieldInitSpec(this, _keyterms2, void 0);
358
+ _classPrivateFieldSet2(_ai3, this, ai);
359
+ _classPrivateFieldSet2(_sampleRate2, this, options?.sampleRate ?? 16e3);
360
+ _classPrivateFieldSet2(_language, this, options?.language ?? "en");
361
+ _classPrivateFieldSet2(_endpointingMs, this, options?.endpointingMs ?? 300);
362
+ _classPrivateFieldSet2(_utteranceEndMs, this, options?.utteranceEndMs ?? 1e3);
363
+ _classPrivateFieldSet2(_smartFormat, this, options?.smartFormat ?? true);
364
+ _classPrivateFieldSet2(_punctuate, this, options?.punctuate ?? true);
365
+ _classPrivateFieldSet2(_keyterms2, this, options?.keyterms);
366
+ }
367
+ createSession(options) {
368
+ return new Nova3Session(_classPrivateFieldGet2(_ai3, this), {
369
+ sampleRate: _classPrivateFieldGet2(_sampleRate2, this),
370
+ language: _classPrivateFieldGet2(_language, this),
371
+ endpointingMs: _classPrivateFieldGet2(_endpointingMs, this),
372
+ utteranceEndMs: _classPrivateFieldGet2(_utteranceEndMs, this),
373
+ smartFormat: _classPrivateFieldGet2(_smartFormat, this),
374
+ punctuate: _classPrivateFieldGet2(_punctuate, this),
375
+ keyterms: _classPrivateFieldGet2(_keyterms2, this)
376
+ }, options);
377
+ }
378
+ };
379
+ var _onInterim2 = /* @__PURE__ */ new WeakMap();
380
+ var _onUtterance2 = /* @__PURE__ */ new WeakMap();
381
+ var _onFatalError2 = /* @__PURE__ */ new WeakMap();
382
+ var _ws2 = /* @__PURE__ */ new WeakMap();
383
+ var _connected2 = /* @__PURE__ */ new WeakMap();
384
+ var _closed2 = /* @__PURE__ */ new WeakMap();
385
+ var _fatalReported2 = /* @__PURE__ */ new WeakMap();
386
+ var _pendingChunks2 = /* @__PURE__ */ new WeakMap();
387
+ var _ready2 = /* @__PURE__ */ new WeakMap();
388
+ var _resolveReady2 = /* @__PURE__ */ new WeakMap();
389
+ var _rejectReady2 = /* @__PURE__ */ new WeakMap();
390
+ var _finalizedSegments = /* @__PURE__ */ new WeakMap();
391
+ var _Nova3Session_brand = /* @__PURE__ */ new WeakSet();
392
+ /**
393
+ * Per-call Nova 3 transcription session. Lives for the entire call.
394
+ *
395
+ * Uses Nova 3's endpointing and VAD events to detect utterance
396
+ * boundaries. When a result arrives with `speech_final: true`,
397
+ * the accumulated finalized segments are emitted as an utterance.
398
+ */
399
+ var Nova3Session = class {
400
+ constructor(ai, config, options) {
401
+ _classPrivateMethodInitSpec(this, _Nova3Session_brand);
402
+ _classPrivateFieldInitSpec(this, _onInterim2, void 0);
403
+ _classPrivateFieldInitSpec(this, _onUtterance2, void 0);
404
+ _classPrivateFieldInitSpec(this, _onFatalError2, void 0);
405
+ _classPrivateFieldInitSpec(this, _ws2, null);
406
+ _classPrivateFieldInitSpec(this, _connected2, false);
407
+ _classPrivateFieldInitSpec(this, _closed2, false);
408
+ _classPrivateFieldInitSpec(this, _fatalReported2, false);
409
+ _classPrivateFieldInitSpec(this, _pendingChunks2, []);
410
+ _classPrivateFieldInitSpec(this, _ready2, void 0);
411
+ _classPrivateFieldInitSpec(this, _resolveReady2, null);
412
+ _classPrivateFieldInitSpec(this, _rejectReady2, null);
413
+ _classPrivateFieldInitSpec(this, _finalizedSegments, []);
414
+ _classPrivateFieldSet2(_onInterim2, this, options?.onInterim);
415
+ _classPrivateFieldSet2(_onUtterance2, this, options?.onUtterance);
416
+ _classPrivateFieldSet2(_onFatalError2, this, options?.onFatalError);
417
+ _classPrivateFieldSet2(_ready2, this, new Promise((resolve, reject) => {
418
+ _classPrivateFieldSet2(_resolveReady2, this, resolve);
419
+ _classPrivateFieldSet2(_rejectReady2, this, reject);
420
+ }));
421
+ _classPrivateFieldGet2(_ready2, this).catch(() => {});
422
+ _assertClassBrand(_Nova3Session_brand, this, _connect2).call(this, ai, config);
423
+ }
424
+ waitUntilReady() {
425
+ return _classPrivateFieldGet2(_ready2, this);
426
+ }
427
+ feed(chunk) {
428
+ if (_classPrivateFieldGet2(_closed2, this)) return;
429
+ if (_classPrivateFieldGet2(_connected2, this) && _classPrivateFieldGet2(_ws2, this)) _classPrivateFieldGet2(_ws2, this).send(chunk);
430
+ else _classPrivateFieldGet2(_pendingChunks2, this).push(chunk);
431
+ }
432
+ close() {
433
+ if (_classPrivateFieldGet2(_closed2, this)) return;
434
+ _classPrivateFieldSet2(_closed2, this, true);
435
+ _classPrivateFieldSet2(_pendingChunks2, this, []);
436
+ if (_classPrivateFieldGet2(_ws2, this)) {
437
+ try {
438
+ _classPrivateFieldGet2(_ws2, this).close();
439
+ } catch {}
440
+ _classPrivateFieldSet2(_ws2, this, null);
441
+ }
442
+ _classPrivateFieldSet2(_connected2, this, false);
443
+ _assertClassBrand(_Nova3Session_brand, this, _resolveReadiness2).call(this);
444
+ }
445
+ };
446
+ async function _connect2(ai, config) {
447
+ try {
448
+ const input = {
449
+ encoding: "linear16",
450
+ sample_rate: String(config.sampleRate),
451
+ language: config.language,
452
+ interim_results: "true",
453
+ vad_events: "true",
454
+ endpointing: String(config.endpointingMs),
455
+ utterance_end_ms: String(config.utteranceEndMs),
456
+ smart_format: String(config.smartFormat),
457
+ punctuate: String(config.punctuate)
458
+ };
459
+ if (config.keyterms?.length) input.keyterm = config.keyterms;
460
+ const resp = await ai.run("@cf/deepgram/nova-3", input, { websocket: true });
461
+ if (_classPrivateFieldGet2(_closed2, this)) {
462
+ const ws = resp.webSocket;
463
+ if (ws) {
464
+ ws.accept();
465
+ ws.close();
466
+ }
467
+ _assertClassBrand(_Nova3Session_brand, this, _resolveReadiness2).call(this);
468
+ return;
469
+ }
470
+ const ws = resp.webSocket;
471
+ if (!ws) {
472
+ const error = /* @__PURE__ */ new Error("Workers AI Nova-3 STT did not return a WebSocket");
473
+ logVoiceError({
474
+ component: "Nova3STT",
475
+ stage: "connection",
476
+ message: "Nova-3 STT WebSocket upgrade failed",
477
+ error
478
+ });
479
+ _assertClassBrand(_Nova3Session_brand, this, _reportFatal2).call(this, error);
480
+ _assertClassBrand(_Nova3Session_brand, this, _rejectReadiness2).call(this, error);
481
+ return;
482
+ }
483
+ ws.accept();
484
+ _classPrivateFieldSet2(_ws2, this, ws);
485
+ _classPrivateFieldSet2(_connected2, this, true);
486
+ ws.addEventListener("message", (event) => {
487
+ _assertClassBrand(_Nova3Session_brand, this, _handleMessage2).call(this, event);
488
+ });
489
+ ws.addEventListener("close", () => {
490
+ _classPrivateFieldSet2(_connected2, this, false);
491
+ if (_classPrivateFieldGet2(_closed2, this)) return;
492
+ const error = /* @__PURE__ */ new Error("Workers AI Nova-3 STT WebSocket closed unexpectedly");
493
+ logVoiceError({
494
+ component: "Nova3STT",
495
+ stage: "websocket_close",
496
+ message: "Nova-3 STT WebSocket closed unexpectedly",
497
+ error
498
+ });
499
+ _assertClassBrand(_Nova3Session_brand, this, _reportFatal2).call(this, error);
500
+ });
501
+ ws.addEventListener("error", (event) => {
502
+ const error = new Error("Workers AI Nova-3 STT WebSocket error", { cause: event });
503
+ logVoiceError({
504
+ component: "Nova3STT",
505
+ stage: "websocket",
506
+ message: "Nova-3 STT WebSocket error",
507
+ error
508
+ });
509
+ _classPrivateFieldSet2(_connected2, this, false);
510
+ _assertClassBrand(_Nova3Session_brand, this, _reportFatal2).call(this, error);
511
+ });
512
+ for (const chunk of _classPrivateFieldGet2(_pendingChunks2, this)) ws.send(chunk);
513
+ _classPrivateFieldSet2(_pendingChunks2, this, []);
514
+ _assertClassBrand(_Nova3Session_brand, this, _resolveReadiness2).call(this);
515
+ } catch (error) {
516
+ const voiceError = toVoiceError(error, "Nova-3 STT connection failed");
517
+ logVoiceError({
518
+ component: "Nova3STT",
519
+ stage: "connection",
520
+ message: "Nova-3 STT connection failed",
521
+ error: voiceError
522
+ });
523
+ _assertClassBrand(_Nova3Session_brand, this, _reportFatal2).call(this, voiceError);
524
+ _assertClassBrand(_Nova3Session_brand, this, _rejectReadiness2).call(this, voiceError);
525
+ }
526
+ }
527
+ function _resolveReadiness2() {
528
+ const resolve = _classPrivateFieldGet2(_resolveReady2, this);
529
+ if (!resolve) return;
530
+ _classPrivateFieldSet2(_resolveReady2, this, null);
531
+ _classPrivateFieldSet2(_rejectReady2, this, null);
532
+ resolve();
533
+ }
534
+ function _rejectReadiness2(reason) {
535
+ const reject = _classPrivateFieldGet2(_rejectReady2, this);
536
+ if (!reject) return;
537
+ _classPrivateFieldSet2(_resolveReady2, this, null);
538
+ _classPrivateFieldSet2(_rejectReady2, this, null);
539
+ reject(reason);
540
+ }
541
+ function _reportFatal2(error) {
542
+ if (_classPrivateFieldGet2(_closed2, this) || _classPrivateFieldGet2(_fatalReported2, this)) return;
543
+ _classPrivateFieldSet2(_fatalReported2, this, true);
544
+ _classPrivateFieldGet2(_onFatalError2, this)?.call(this, error);
545
+ }
546
+ function _handleMessage2(event) {
547
+ if (_classPrivateFieldGet2(_closed2, this)) return;
548
+ try {
549
+ const data = typeof event.data === "string" ? JSON.parse(event.data) : null;
550
+ if (!data) return;
551
+ if (data.type === "Results") {
552
+ const transcript = data.channel?.alternatives?.[0]?.transcript ?? "";
553
+ if (data.is_final && transcript) _classPrivateFieldGet2(_finalizedSegments, this).push(transcript);
554
+ if (data.speech_final) {
555
+ const fullTranscript = _classPrivateFieldGet2(_finalizedSegments, this).join(" ").trim();
556
+ _classPrivateFieldSet2(_finalizedSegments, this, []);
557
+ if (fullTranscript) _classPrivateFieldGet2(_onUtterance2, this)?.call(this, fullTranscript);
558
+ } else if (!data.is_final && transcript) {
559
+ const display = _classPrivateFieldGet2(_finalizedSegments, this).length > 0 ? _classPrivateFieldGet2(_finalizedSegments, this).join(" ") + " " + transcript : transcript;
560
+ _classPrivateFieldGet2(_onInterim2, this)?.call(this, display);
561
+ }
562
+ }
563
+ } catch {}
564
+ }
565
+ //#endregion
566
+ export { WorkersAIFluxSTT, WorkersAINova3STT, WorkersAITTS };
567
+
568
+ //# sourceMappingURL=workers-ai.js.map