UNPKG

agents

Version:

A home for your AI agents

272 lines (270 loc) 8.78 kB
import { x as Connection } from "../capability-runner-BUBa6Ake.js"; import { c as Agent } from "../agent-routing-B2XLNMxq.js"; import { C as VoiceTransport, D as VoiceTurnSource, E as VoiceTurnOutcome, S as VoiceStatus, T as VoiceTurnMetrics, _ as VoiceErrorStage, a as TranscriberSessionOptions, b as VoiceRole, c as VoiceAudioFormat, d as VoiceCompletionOutcome, f as VoiceCompletionOutcomeCode, g as VoiceErrorCode, h as VoiceError, i as TranscriberSession, l as VoiceAudioInput, m as VoiceDiagnosticsOptions, n as TTSProvider, o as TranscriptMessage, p as VoiceDiagnosticEvent, r as Transcriber, s as VOICE_PROTOCOL_VERSION, t as StreamingTTSProvider, u as VoiceClientMessage, v as VoiceModelFinishReason, w as VoiceTransportCloseInfo, x as VoiceServerMessage, y as VoicePipelineMetrics } from "../types-_Faxb570.js"; import { n as TextSource, r as iterateText, t as SentenceChunker } from "../sentence-chunker-BAidJ4DA.js"; import { SFUConfig, addSFUTracks, createSFUSession, createSFUWebSocketAdapter, decodeVarint, downsample48kStereoTo16kMono, encodePayloadToProtobuf, encodeVarint, extractPayloadFromProtobuf, renegotiateSFUSession, sfuFetch, upsample16kMonoTo48kStereo } from "./sfu.js"; import { WorkersAIFluxSTT, WorkersAIFluxSTTOptions, WorkersAINova3STT, WorkersAINova3STTOptions, WorkersAITTS, WorkersAITTSOptions } from "./workers-ai.js"; //#region src/voice/voice-input.d.ts type Constructor$1<T = object> = new (...args: any[]) => T; type AgentLike$1 = Constructor$1<Pick<Agent<Cloudflare.Env>, "keepAlive">>; /** Configuration options for the voice input mixin. */ interface VoiceInputOptions { /** Optional diagnostic output. Diagnostic event names and metadata are not stable API. */ diagnostics?: VoiceDiagnosticsOptions; } /** Public surface of the voice input mixin, used as an explicit return type to satisfy TS6 declaration emit. */ interface VoiceInputMixinMembers { transcriber?: Transcriber; onTranscript(text: string, connection: Connection): void | Promise<void>; createTranscriber(connection: Connection): Transcriber | null; beforeCallStart(connection: Connection): boolean | Promise<boolean>; onCallStart(connection: Connection): void | Promise<void>; onCallEnd(connection: Connection): void | Promise<void>; onInterrupt(connection: Connection): void | Promise<void>; afterTranscribe( transcript: string, connection: Connection ): string | null | Promise<string | null>; } type VoiceInputMixinReturn<TBase extends AgentLike$1> = TBase & (new (...args: any[]) => VoiceInputMixinMembers); /** * Voice-to-text input mixin. Adds STT-only voice input to an Agent class. * * Subclasses must set a `transcriber` property (or override `createTranscriber`). * No TTS provider is needed. Override `onTranscript` to handle each * transcribed utterance. * * @param Base - The Agent class to extend (e.g. `Agent`). * @param voiceInputOptions - Optional pipeline configuration. * * @example * ```typescript * import { Agent } from "../index"; * import { withVoiceInput, WorkersAINova3STT } from "agents/voice"; * * const InputAgent = withVoiceInput(Agent); * * class MyAgent extends InputAgent<Env> { * transcriber = new WorkersAINova3STT(this.env.AI); * * onTranscript(text, connection) { * console.log("User said:", text); * } * } * ``` */ declare function withVoiceInput<TBase extends AgentLike$1>( Base: TBase, voiceInputOptions?: VoiceInputOptions ): VoiceInputMixinReturn<TBase>; //#endregion //#region src/voice/index.d.ts /** Context passed to the `onTurn()` hook. */ interface VoiceTurnContext { connection: Connection; /** Completed conversation history before the current transcript. */ messages: Array<{ role: VoiceRole; content: string; }>; signal: AbortSignal; } /** Configuration options for the voice mixin. Passed to `withVoice()`. */ interface VoiceAgentOptions { /** Max conversation history messages loaded for context. @default 20 */ historyLimit?: number; /** Audio format used for binary audio payloads sent to the client. @default "mp3" */ audioFormat?: VoiceAudioFormat; /** * Sample rate (Hz) of raw PCM audio payloads sent to the client. * Declared in the `audio_config` message so the client can play `pcm16` * at the provider's native rate (e.g. 24000 for Gemini TTS). * Encoded formats (mp3/wav/opus) carry their own rate and ignore this. * @default 16000 */ sampleRate?: number; /** Max conversation messages to keep in SQLite. Oldest are pruned. @default 1000 */ maxMessageCount?: number; /** Optional diagnostic output. Diagnostic event names and metadata are not stable API. */ diagnostics?: VoiceDiagnosticsOptions; } type Constructor<T = object> = new (...args: any[]) => T; type AgentLike = Constructor< Pick<Agent<Cloudflare.Env>, "sql" | "getConnections" | "keepAlive"> >; /** Public surface of the voice mixin, used as an explicit return type to satisfy TS6 declaration emit. */ interface VoiceAgentMixinMembers { transcriber?: Transcriber; tts?: (TTSProvider & Partial<StreamingTTSProvider>) | undefined; onTurn(transcript: string, context: VoiceTurnContext): Promise<TextSource>; createTranscriber(connection: Connection): Transcriber | null; beforeCallStart(connection: Connection): boolean | Promise<boolean>; onCallStart(connection: Connection): void | Promise<void>; onCallEnd(connection: Connection): void | Promise<void>; onInterrupt(connection: Connection): void | Promise<void>; afterTranscribe( transcript: string, connection: Connection ): string | null | Promise<string | null>; beforeSynthesize( text: string, connection: Connection ): string | null | Promise<string | null>; afterSynthesize( audio: ArrayBuffer | null, text: string, connection: Connection ): ArrayBuffer | null | Promise<ArrayBuffer | null>; saveMessage(role: "user" | "assistant", text: string): void; getConversationHistory(limit?: number): Array<{ role: VoiceRole; content: string; }>; forceEndCall(connection: Connection): void; speak(connection: Connection, text: string): Promise<void>; speakAll(text: string): Promise<void>; } type VoiceAgentMixinReturn<TBase extends AgentLike> = TBase & (new (...args: any[]) => VoiceAgentMixinMembers); /** * Voice pipeline mixin. Adds the full voice pipeline to an Agent class. * * Subclasses must set a `transcriber` property (or override `createTranscriber`) * and a `tts` provider property. The transcriber session is per-call — created * at start_call and closed at end_call. The model handles turn detection. * * @param Base - The Agent class to extend (e.g. `Agent`). * @param voiceOptions - Optional pipeline configuration. * * @example * ```typescript * import { Agent } from "../index"; * import { withVoice, WorkersAIFluxSTT, WorkersAITTS } from "agents/voice"; * * const VoiceAgent = withVoice(Agent); * * class MyAgent extends VoiceAgent<Env> { * transcriber = new WorkersAIFluxSTT(this.env.AI); * tts = new WorkersAITTS(this.env.AI); * * async onTurn(transcript, context) { * return "Hello! I heard you say: " + transcript; * } * } * ``` */ declare function withVoice<TBase extends AgentLike>( Base: TBase, voiceOptions?: VoiceAgentOptions ): VoiceAgentMixinReturn<TBase>; //#endregion export { type SFUConfig, SentenceChunker, type StreamingTTSProvider, type TTSProvider, type TextSource, type Transcriber, type TranscriberSession, type TranscriberSessionOptions, type TranscriptMessage, VOICE_PROTOCOL_VERSION, VoiceAgentMixinMembers, VoiceAgentOptions, type VoiceAudioFormat, type VoiceAudioInput, type VoiceClientMessage, type VoiceCompletionOutcome, type VoiceCompletionOutcomeCode, type VoiceDiagnosticEvent, type VoiceDiagnosticsOptions, type VoiceError, type VoiceErrorCode, type VoiceErrorStage, type VoiceInputOptions, type VoiceModelFinishReason, type VoicePipelineMetrics, type VoiceRole, type VoiceServerMessage, type VoiceStatus, type VoiceTransport, type VoiceTransportCloseInfo, VoiceTurnContext, type VoiceTurnMetrics, type VoiceTurnOutcome, type VoiceTurnSource, WorkersAIFluxSTT, type WorkersAIFluxSTTOptions, WorkersAINova3STT, type WorkersAINova3STTOptions, WorkersAITTS, type WorkersAITTSOptions, addSFUTracks, createSFUSession, createSFUWebSocketAdapter, decodeVarint, downsample48kStereoTo16kMono, encodePayloadToProtobuf, encodeVarint, extractPayloadFromProtobuf, iterateText, renegotiateSFUSession, sfuFetch, upsample16kMonoTo48kStereo, withVoice, withVoiceInput }; //# sourceMappingURL=index.d.ts.map