Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 5x 5x 2x 2x 1x 2x 2x 2x 2x 2x 2x 2x | /**
* AgentKits — STT (Speech-to-Text) Module
*
* Multi-provider speech-to-text with unified interface.
* Supports: OpenAI (Whisper), DashScope (Paraformer), Minimax, custom.
*
* Usage:
* import { createSTT } from 'agentkits/stt';
* const stt = createSTT({ provider: 'openai', apiKey: '...' });
* const text = await stt.transcribe(audioBuffer);
*/
// ── Types ──────────────────────────────────────────────────────────
export type STTProvider = 'openai' | 'dashscope' | 'minimax' | 'custom';
export interface STTConfig {
provider?: STTProvider;
model?: string;
apiKey?: string;
baseUrl?: string;
language?: string; // ISO 639-1
}
export interface TranscriptionResult {
text: string;
language?: string;
duration?: number;
segments?: Array<{
start: number;
end: number;
text: string;
}>;
}
export interface STTClient {
/** Transcribe audio buffer to text */
transcribe(audio: Buffer, options?: {
language?: string;
format?: string;
timestamps?: boolean;
}): Promise<TranscriptionResult>;
/** Current resolved config */
readonly config: Readonly<ResolvedSTTConfig>;
}
interface ResolvedSTTConfig {
provider: STTProvider;
model: string;
baseUrl: string;
language?: string;
}
// ── Provider Defaults ──────────────────────────────────────────────
const PROVIDERS: Record<STTProvider, { model: string; baseUrl: string }> = {
openai: {
model: 'whisper-1',
baseUrl: 'https://api.openai.com/v1',
},
dashscope: {
model: 'paraformer-v2',
baseUrl: 'https://dashscope.aliyuncs.com/compatible-mode/v1',
},
minimax: {
model: 'speech-01',
baseUrl: 'https://api.minimax.chat/v1',
},
custom: {
model: 'whisper-1',
baseUrl: 'http://localhost:8080',
},
};
const ENV_MAP: Record<STTProvider, string[]> = {
openai: ['OPENAI_API_KEY'],
dashscope: ['DASHSCOPE_API_KEY'],
minimax: ['MINIMAX_API_KEY'],
custom: ['AGENTKIT_STT_KEY'],
};
// ── Factory ────────────────────────────────────────────────────────
export function createSTT(userConfig: STTConfig = {}): STTClient {
const provider = userConfig.provider ?? 'openai';
const defaults = PROVIDERS[provider] ?? PROVIDERS.openai;
const resolved: ResolvedSTTConfig = {
provider,
model: userConfig.model ?? defaults.model,
baseUrl: userConfig.baseUrl ?? defaults.baseUrl,
language: userConfig.language,
};
const apiKey = userConfig.apiKey
?? (ENV_MAP[provider] ?? []).map(k => process.env[k]).find(Boolean);
return {
async transcribe(audio, options = {}) {
const language = options.language ?? resolved.language;
const format = options.format ?? 'mp3';
const formData = new FormData();
const blob = new Blob([new Uint8Array(audio)], { type: `audio/${format}` });
formData.append('file', blob, `audio.${format}`);
formData.append('model', resolved.model);
if (language) formData.append('language', language);
if (options.timestamps) {
formData.append('response_format', 'verbose_json');
formData.append('timestamp_granularities[]', 'segment');
}
const response = await fetch(`${resolved.baseUrl}/audio/transcriptions`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${apiKey}`,
},
body: formData,
});
if (!response.ok) {
const err = await response.text();
throw new Error(`STT failed (${response.status}): ${err}`);
}
const data = await response.json() as any;
return {
text: data.text ?? '',
language: data.language,
duration: data.duration,
segments: data.segments?.map((s: any) => ({
start: s.start,
end: s.end,
text: s.text,
})),
};
},
get config() {
return resolved;
},
};
}
// ── Convenience ────────────────────────────────────────────────────
export function listSTTProviders(): Array<{ id: STTProvider; model: string; region: string; languages: string }> {
return [
{ id: 'openai', model: 'whisper-1', region: 'Global', languages: '57 languages' },
{ id: 'dashscope', model: 'paraformer-v2', region: 'Global (Alibaba)', languages: 'zh, en, ja, ko, +' },
{ id: 'minimax', model: 'speech-01', region: 'Global', languages: 'zh, en' },
{ id: 'custom', model: 'configurable', region: 'Any', languages: 'Any' },
];
}
|