@12-apps/llm 1.1.0 → 1.2.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.
- package/README.md +50 -2
- package/dist/index.d.ts +103 -4
- package/dist/index.js +263 -2
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -19,8 +19,11 @@ interface onto the provider's official SDK and maps the answer back.
|
|
|
19
19
|
- **A support ladder**: automation, then model tiers that escalate, then a
|
|
20
20
|
person, with least-privilege tools per tier bound to the host's scope.
|
|
21
21
|
|
|
22
|
-
|
|
23
|
-
transcription
|
|
22
|
+
- **Speech to text** behind the same shape: `createTranscriber`, a registry
|
|
23
|
+
of transcription adapters, ElevenLabs' Scribe built in (see
|
|
24
|
+
[Speech to text](#speech-to-text)).
|
|
25
|
+
|
|
26
|
+
Chat and transcription ship today; an adapter declares what it can do.
|
|
24
27
|
|
|
25
28
|
## Install
|
|
26
29
|
|
|
@@ -409,6 +412,50 @@ result.escalations; // [{ from, to, reason }]
|
|
|
409
412
|
`tool_missing`, `scope_exposed`; and `state_conflict` when the state port's
|
|
410
413
|
level keeps changing while the ladder raises it.
|
|
411
414
|
|
|
415
|
+
## Speech to text
|
|
416
|
+
|
|
417
|
+
`createTranscriber` is `createModelClient`'s sibling for recordings: one
|
|
418
|
+
interface, one normalized result, an adapter per provider picked by id from a
|
|
419
|
+
registry the host can extend. ElevenLabs' speech-to-text ("Scribe") is built in,
|
|
420
|
+
over a direct `fetch` (no SDK to install).
|
|
421
|
+
|
|
422
|
+
```ts
|
|
423
|
+
import { createTranscriber } from "@12-apps/llm";
|
|
424
|
+
|
|
425
|
+
const transcriber = createTranscriber({
|
|
426
|
+
provider: "elevenlabs",
|
|
427
|
+
credentials: { kind: "apiKey", apiKey: () => readKeyFromYourVault() },
|
|
428
|
+
// model defaults to "scribe_v2"; retry takes the same hook as the chat client
|
|
429
|
+
});
|
|
430
|
+
|
|
431
|
+
const result = await transcriber.transcribe({
|
|
432
|
+
audio: bytes, // Uint8Array | Blob
|
|
433
|
+
mimeType: "audio/ogg",
|
|
434
|
+
languageHint: "pt", // optional: the provider detects the language otherwise
|
|
435
|
+
maxDurationSeconds: 120, // optional
|
|
436
|
+
durationSeconds: 37, // optional: the length, when the host already knows it
|
|
437
|
+
});
|
|
438
|
+
// { provider, model, text, language?, durationSeconds, usage: { audioSeconds } }
|
|
439
|
+
```
|
|
440
|
+
|
|
441
|
+
- **Duration limit.** A recording longer than `maxDurationSeconds` throws
|
|
442
|
+
`AudioTooLongError` (an `InvalidRequestError`, so never retried). It is
|
|
443
|
+
refused before anything is sent when the host passes `durationSeconds`, and
|
|
444
|
+
after the call when the provider reports a longer `audioSeconds`
|
|
445
|
+
(`beforeSending` says which).
|
|
446
|
+
- **Credentials:** API keys only. The adapter's calls go through the same
|
|
447
|
+
sealed edge as chat: one allowed route, no redirects, only the call's own key,
|
|
448
|
+
nothing from the platform's environment.
|
|
449
|
+
- **Errors** are the table below: 401/403 `AuthError`, 429 `RateLimitError`,
|
|
450
|
+
5xx and network failures `ProviderUnavailableError`, any other 4xx
|
|
451
|
+
`InvalidRequestError`. A 200 that carries no transcript (a webhook-shaped
|
|
452
|
+
`{ message, request_id }` body) is an `InvalidResponseError`.
|
|
453
|
+
- **Duration.** `durationSeconds` is the provider's `audio_duration_secs`, else
|
|
454
|
+
the last word's end, else the `durationSeconds` the host passed, else 0.
|
|
455
|
+
- **A second provider** is one `TranscriptionAdapter` (`id`, `credentialKinds`,
|
|
456
|
+
`transcribe(call)`) registered with `createTranscriptionRegistry(...)` and
|
|
457
|
+
passed as `registry`.
|
|
458
|
+
|
|
412
459
|
## Errors and retries
|
|
413
460
|
|
|
414
461
|
| class | when |
|
|
@@ -417,6 +464,7 @@ result.escalations; // [{ from, to, reason }]
|
|
|
417
464
|
| `RateLimitError` | 429; `retryAfterMs` comes from `retry-after-ms` or `retry-after` |
|
|
418
465
|
| `InvalidRequestError` | 400, 404, 413 or 422, a non-strict tool schema, too many breakpoints |
|
|
419
466
|
| `ProviderUnavailableError` | 5xx, 529, 408, network failures |
|
|
467
|
+
| `AudioTooLongError` | a recording over `maxDurationSeconds`; an `InvalidRequestError` |
|
|
420
468
|
| `InvalidResponseError` | an answer the adapter cannot read (tool arguments that are not JSON) |
|
|
421
469
|
| `RefusalError` | thrown only by `ensureNotRefused` |
|
|
422
470
|
| `UnsupportedError` | an unknown provider, an undeclared credential kind, a missing optional SDK |
|
package/dist/index.d.ts
CHANGED
|
@@ -407,9 +407,6 @@ declare const XAI_PROVIDER = "xai";
|
|
|
407
407
|
type XaiAdapterOptions = OpenAIAdapterOptions;
|
|
408
408
|
declare function xaiAdapter(options?: XaiAdapterOptions): ProviderAdapter;
|
|
409
409
|
|
|
410
|
-
/** Fresh instances of the built-in adapters, with their defaults. */
|
|
411
|
-
declare function builtInAdapters(): ProviderAdapter[];
|
|
412
|
-
|
|
413
410
|
/**
|
|
414
411
|
* Typed errors. Every adapter maps its SDK's failures onto these, so a host
|
|
415
412
|
* decides retry, hand-off and alerting once, whatever the provider.
|
|
@@ -448,6 +445,14 @@ declare class RefusalError extends LlmError {
|
|
|
448
445
|
declare class InvalidRequestError extends LlmError {
|
|
449
446
|
constructor(provider: string, message: string, status?: number);
|
|
450
447
|
}
|
|
448
|
+
/** The recording is longer than the host allows; it will fail again as is. */
|
|
449
|
+
declare class AudioTooLongError extends InvalidRequestError {
|
|
450
|
+
readonly durationSeconds: number;
|
|
451
|
+
readonly maxDurationSeconds: number;
|
|
452
|
+
/** True when the refusal came before anything was sent. */
|
|
453
|
+
readonly beforeSending: boolean;
|
|
454
|
+
constructor(provider: string, durationSeconds: number, maxDurationSeconds: number, beforeSending: boolean);
|
|
455
|
+
}
|
|
451
456
|
/** The provider is down, overloaded or unreachable (5xx, 529, network). */
|
|
452
457
|
declare class ProviderUnavailableError extends LlmError {
|
|
453
458
|
constructor(provider: string, message: string, status?: number);
|
|
@@ -524,6 +529,100 @@ declare function retryTransient(options?: {
|
|
|
524
529
|
}): RetryPolicy;
|
|
525
530
|
declare function createModelClient(options: ModelClientOptions): ModelClient;
|
|
526
531
|
|
|
532
|
+
/**
|
|
533
|
+
* Speech to text behind the same interchangeable-provider shape as the chat
|
|
534
|
+
* connector: `createTranscriber` picks a `TranscriptionAdapter` by provider id
|
|
535
|
+
* from a registry the host can extend, and wraps it with what every provider
|
|
536
|
+
* shares (the credential strategy and the host's retry policy). A second
|
|
537
|
+
* provider is one adapter, registered next to the built-in one.
|
|
538
|
+
*/
|
|
539
|
+
|
|
540
|
+
/** One recording to transcribe. */
|
|
541
|
+
interface TranscriptionRequest {
|
|
542
|
+
/** The recording's bytes. */
|
|
543
|
+
audio: Uint8Array | Blob;
|
|
544
|
+
/** The recording's media type, e.g. `audio/ogg`. */
|
|
545
|
+
mimeType: string;
|
|
546
|
+
/** A BCP-47 / ISO 639 language code (`pt`); omitted, the provider detects it. */
|
|
547
|
+
languageHint?: string;
|
|
548
|
+
/** Longest recording accepted, in seconds; a longer one is refused with `AudioTooLongError`. */
|
|
549
|
+
maxDurationSeconds?: number;
|
|
550
|
+
/** The recording's length when the host already knows it, so a too-long one is refused before any byte is sent. */
|
|
551
|
+
durationSeconds?: number;
|
|
552
|
+
/** Aborts the call. */
|
|
553
|
+
signal?: AbortSignal;
|
|
554
|
+
}
|
|
555
|
+
interface TranscriptionUsage {
|
|
556
|
+
/** The audio the provider processed, which is what it bills. */
|
|
557
|
+
audioSeconds: number;
|
|
558
|
+
}
|
|
559
|
+
interface TranscriptionResult {
|
|
560
|
+
provider: string;
|
|
561
|
+
model: string;
|
|
562
|
+
text: string;
|
|
563
|
+
/** The language the provider heard, when it says. */
|
|
564
|
+
language?: string;
|
|
565
|
+
durationSeconds: number;
|
|
566
|
+
usage: TranscriptionUsage;
|
|
567
|
+
}
|
|
568
|
+
/** What the transcriber hands an adapter for one call. */
|
|
569
|
+
interface TranscribeCall {
|
|
570
|
+
model: string;
|
|
571
|
+
request: TranscriptionRequest;
|
|
572
|
+
credential: ResolvedCredential;
|
|
573
|
+
}
|
|
574
|
+
interface TranscriptionAdapter {
|
|
575
|
+
/** The provider id a host names in `createTranscriber({ provider })`. */
|
|
576
|
+
readonly id: string;
|
|
577
|
+
/** Used when the host names no model; an adapter may ship none. */
|
|
578
|
+
readonly defaultModel?: string;
|
|
579
|
+
readonly credentialKinds: readonly CredentialKind[];
|
|
580
|
+
/** Transcribe one recording. Throws the package's typed errors. */
|
|
581
|
+
transcribe(call: TranscribeCall): Promise<TranscriptionResult>;
|
|
582
|
+
}
|
|
583
|
+
interface TranscriptionRegistry {
|
|
584
|
+
register(adapter: TranscriptionAdapter, options?: {
|
|
585
|
+
replace?: boolean;
|
|
586
|
+
}): TranscriptionRegistry;
|
|
587
|
+
get(id: string): TranscriptionAdapter;
|
|
588
|
+
has(id: string): boolean;
|
|
589
|
+
ids(): string[];
|
|
590
|
+
}
|
|
591
|
+
declare function createTranscriptionRegistry(adapters?: readonly TranscriptionAdapter[]): TranscriptionRegistry;
|
|
592
|
+
/** Fresh instances of the built-in transcription adapters, with their defaults. */
|
|
593
|
+
declare function builtInTranscriptionAdapters(): TranscriptionAdapter[];
|
|
594
|
+
interface TranscriberOptions {
|
|
595
|
+
provider: string;
|
|
596
|
+
/** Defaults to the adapter's default model. */
|
|
597
|
+
model?: string;
|
|
598
|
+
credentials: Credentials;
|
|
599
|
+
retry?: RetryPolicy;
|
|
600
|
+
/** Where to find adapters. Defaults to a registry of the built-in one (ElevenLabs). */
|
|
601
|
+
registry?: TranscriptionRegistry;
|
|
602
|
+
/** Test seams. */
|
|
603
|
+
sleep?: (ms: number) => Promise<void>;
|
|
604
|
+
now?: () => number;
|
|
605
|
+
}
|
|
606
|
+
interface Transcriber {
|
|
607
|
+
readonly provider: string;
|
|
608
|
+
readonly model: string;
|
|
609
|
+
transcribe(request: TranscriptionRequest): Promise<TranscriptionResult>;
|
|
610
|
+
}
|
|
611
|
+
declare function createTranscriber(options: TranscriberOptions): Transcriber;
|
|
612
|
+
|
|
613
|
+
declare const ELEVENLABS_PROVIDER = "elevenlabs";
|
|
614
|
+
declare const ELEVENLABS_DEFAULT_MODEL = "scribe_v2";
|
|
615
|
+
interface ElevenLabsAdapterOptions {
|
|
616
|
+
baseURL?: string;
|
|
617
|
+
/** The `fetch` the adapter sends with (a proxy, a test double). */
|
|
618
|
+
fetch?: typeof globalThis.fetch;
|
|
619
|
+
defaultModel?: string;
|
|
620
|
+
}
|
|
621
|
+
declare function elevenLabsAdapter(options?: ElevenLabsAdapterOptions): TranscriptionAdapter;
|
|
622
|
+
|
|
623
|
+
/** Fresh instances of the built-in adapters, with their defaults. */
|
|
624
|
+
declare function builtInAdapters(): ProviderAdapter[];
|
|
625
|
+
|
|
527
626
|
/**
|
|
528
627
|
* The tool-use loop: run turns until the model stops calling tools, refuses,
|
|
529
628
|
* runs out of output budget, or the turn limit is reached.
|
|
@@ -811,4 +910,4 @@ declare function toolSchemaProblems(tools: readonly ToolDefinition[]): string[];
|
|
|
811
910
|
/** Throws a TypeError naming every problem in every tool's input schema. */
|
|
812
911
|
declare function assertStrictTools(tools: readonly ToolDefinition[]): void;
|
|
813
912
|
|
|
814
|
-
export { ANTHROPIC_DEFAULT_MODEL, ANTHROPIC_PROVIDER, type AdapterRegistry, type AnthropicAdapterOptions, type AnthropicClientLike, type ApiKeyCredentials, type AssistantMessage, AuthError, type CacheTtl, type Capability, type ChatCall, type ChatRequest, type ChatResult, type CredentialKind, CredentialRevokedError, type CredentialSession, type CredentialSessionOptions, type Credentials, ESCALATE_TOOL, type Effort, type Escalation, type EscalationRequest, type EscalationTarget, GEMINI_PROVIDER, type GeminiAdapterOptions, type GeminiClientLike, InvalidRequestError, InvalidResponseError, type JsonSchema, LlmError, type LlmErrorKind, type McpClientLike, type McpListToolsResult, type McpToolInfo, type McpToolsOptions, type Message, type ModelClient, type ModelClientOptions, type OAuthCredentials, type OAuthToken, OPENAI_PROVIDER, type OpenAIAdapterOptions, type OpenAIClientLike, type ProviderAdapter, ProviderUnavailableError, RateLimitError, type RefreshReason, type Refusal, RefusalError, type RegisterOptions, ResolvedCredential, type RetryPolicy, type StopReason, type SupportAutomation, type SupportHandOff, type SupportLadder, SupportLadderError, type SupportLadderErrorCode, type SupportLadderOptions, type SupportPerson, type SupportRequest, type SupportResult, type SupportStatePort, type SupportTier, type SupportTool, type SupportToolContext, type SupportToolEntry, type SupportToolSource, type SystemBlock, type TierUsage, type ToolCall, type ToolChoice, type ToolDefinition, type ToolExecutor, type ToolLoopOptions, type ToolLoopResult, type ToolLoopStatus, type ToolOutput, type ToolResult, type ToolResultsMessage, UnsupportedError, type Usage, type UserMessage, WITH_PERSON, XAI_PROVIDER, type XaiAdapterOptions, anthropicAdapter, assertStrictTools, builtInAdapters, createAdapterRegistry, createCredentialSession, createModelClient, createSupportLadder, ensureNotRefused, geminiAdapter, mapProviderError, mcpResultToOutput, mcpTools, openaiAdapter, retryAfterFrom, retryTransient, runToolLoop, strictSchemaProblems, toolSchemaProblems, xaiAdapter };
|
|
913
|
+
export { ANTHROPIC_DEFAULT_MODEL, ANTHROPIC_PROVIDER, type AdapterRegistry, type AnthropicAdapterOptions, type AnthropicClientLike, type ApiKeyCredentials, type AssistantMessage, AudioTooLongError, AuthError, type CacheTtl, type Capability, type ChatCall, type ChatRequest, type ChatResult, type CredentialKind, CredentialRevokedError, type CredentialSession, type CredentialSessionOptions, type Credentials, ELEVENLABS_DEFAULT_MODEL, ELEVENLABS_PROVIDER, ESCALATE_TOOL, type Effort, type ElevenLabsAdapterOptions, type Escalation, type EscalationRequest, type EscalationTarget, GEMINI_PROVIDER, type GeminiAdapterOptions, type GeminiClientLike, InvalidRequestError, InvalidResponseError, type JsonSchema, LlmError, type LlmErrorKind, type McpClientLike, type McpListToolsResult, type McpToolInfo, type McpToolsOptions, type Message, type ModelClient, type ModelClientOptions, type OAuthCredentials, type OAuthToken, OPENAI_PROVIDER, type OpenAIAdapterOptions, type OpenAIClientLike, type ProviderAdapter, ProviderUnavailableError, RateLimitError, type RefreshReason, type Refusal, RefusalError, type RegisterOptions, ResolvedCredential, type RetryPolicy, type StopReason, type SupportAutomation, type SupportHandOff, type SupportLadder, SupportLadderError, type SupportLadderErrorCode, type SupportLadderOptions, type SupportPerson, type SupportRequest, type SupportResult, type SupportStatePort, type SupportTier, type SupportTool, type SupportToolContext, type SupportToolEntry, type SupportToolSource, type SystemBlock, type TierUsage, type ToolCall, type ToolChoice, type ToolDefinition, type ToolExecutor, type ToolLoopOptions, type ToolLoopResult, type ToolLoopStatus, type ToolOutput, type ToolResult, type ToolResultsMessage, type TranscribeCall, type Transcriber, type TranscriberOptions, type TranscriptionAdapter, type TranscriptionRegistry, type TranscriptionRequest, type TranscriptionResult, type TranscriptionUsage, UnsupportedError, type Usage, type UserMessage, WITH_PERSON, XAI_PROVIDER, type XaiAdapterOptions, anthropicAdapter, assertStrictTools, builtInAdapters, builtInTranscriptionAdapters, createAdapterRegistry, createCredentialSession, createModelClient, createSupportLadder, createTranscriber, createTranscriptionRegistry, elevenLabsAdapter, ensureNotRefused, geminiAdapter, mapProviderError, mcpResultToOutput, mcpTools, openaiAdapter, retryAfterFrom, retryTransient, runToolLoop, strictSchemaProblems, toolSchemaProblems, xaiAdapter };
|
package/dist/index.js
CHANGED
|
@@ -67,6 +67,22 @@ var InvalidRequestError = class extends LlmError {
|
|
|
67
67
|
this.name = "InvalidRequestError";
|
|
68
68
|
}
|
|
69
69
|
};
|
|
70
|
+
var AudioTooLongError = class extends InvalidRequestError {
|
|
71
|
+
static {
|
|
72
|
+
__name(this, "AudioTooLongError");
|
|
73
|
+
}
|
|
74
|
+
durationSeconds;
|
|
75
|
+
maxDurationSeconds;
|
|
76
|
+
/** True when the refusal came before anything was sent. */
|
|
77
|
+
beforeSending;
|
|
78
|
+
constructor(provider, durationSeconds, maxDurationSeconds, beforeSending) {
|
|
79
|
+
super(provider, `The audio lasts ${durationSeconds}s, over the ${maxDurationSeconds}s limit.`);
|
|
80
|
+
this.name = "AudioTooLongError";
|
|
81
|
+
this.durationSeconds = durationSeconds;
|
|
82
|
+
this.maxDurationSeconds = maxDurationSeconds;
|
|
83
|
+
this.beforeSending = beforeSending;
|
|
84
|
+
}
|
|
85
|
+
};
|
|
70
86
|
var ProviderUnavailableError = class extends LlmError {
|
|
71
87
|
static {
|
|
72
88
|
__name(this, "ProviderUnavailableError");
|
|
@@ -259,7 +275,7 @@ function allowed(routes, method, path) {
|
|
|
259
275
|
__name(allowed, "allowed");
|
|
260
276
|
function sealedFetch(options) {
|
|
261
277
|
const base = new URL(options.baseURL);
|
|
262
|
-
const
|
|
278
|
+
const send2 = options.fetch ?? globalThis.fetch;
|
|
263
279
|
const sealed = /* @__PURE__ */ __name(async (input, init) => {
|
|
264
280
|
const outgoing = new Request(input, init);
|
|
265
281
|
if (!within(new URL(outgoing.url), base)) {
|
|
@@ -273,7 +289,7 @@ function sealedFetch(options) {
|
|
|
273
289
|
dropPlatformHeaders(headers);
|
|
274
290
|
for (const [name, value] of Object.entries(options.auth)) headers.set(name, value);
|
|
275
291
|
const body = outgoing.method === "GET" || outgoing.method === "HEAD" ? void 0 : await outgoing.arrayBuffer();
|
|
276
|
-
const response = await
|
|
292
|
+
const response = await send2(outgoing.url, { method: outgoing.method, headers, body, signal: outgoing.signal, redirect: "manual" });
|
|
277
293
|
if (response.type === "opaqueredirect" || response.status >= 300 && response.status < 400) {
|
|
278
294
|
throw new InvalidRequestError(options.provider, "The provider answered with a redirect, which is never followed.", response.status);
|
|
279
295
|
}
|
|
@@ -779,6 +795,140 @@ function xaiAdapter(options = {}) {
|
|
|
779
795
|
}
|
|
780
796
|
__name(xaiAdapter, "xaiAdapter");
|
|
781
797
|
|
|
798
|
+
// src/adapters/elevenlabs.ts
|
|
799
|
+
var ELEVENLABS_PROVIDER = "elevenlabs";
|
|
800
|
+
var ELEVENLABS_DEFAULT_MODEL = "scribe_v2";
|
|
801
|
+
var ELEVENLABS_BASE_URL = "https://api.elevenlabs.io";
|
|
802
|
+
var ELEVENLABS_ROUTES = [{ method: "POST", path: "/v1/speech-to-text" }];
|
|
803
|
+
var EXTENSIONS = {
|
|
804
|
+
"audio/ogg": "ogg",
|
|
805
|
+
"audio/opus": "opus",
|
|
806
|
+
"audio/mpeg": "mp3",
|
|
807
|
+
"audio/mp3": "mp3",
|
|
808
|
+
"audio/mp4": "m4a",
|
|
809
|
+
"audio/x-m4a": "m4a",
|
|
810
|
+
"audio/aac": "aac",
|
|
811
|
+
"audio/wav": "wav",
|
|
812
|
+
"audio/x-wav": "wav",
|
|
813
|
+
"audio/webm": "webm",
|
|
814
|
+
"audio/flac": "flac"
|
|
815
|
+
};
|
|
816
|
+
function fileName(mimeType) {
|
|
817
|
+
const base = mimeType.split(";")[0]?.trim().toLowerCase() ?? "";
|
|
818
|
+
return `audio.${EXTENSIONS[base] ?? "bin"}`;
|
|
819
|
+
}
|
|
820
|
+
__name(fileName, "fileName");
|
|
821
|
+
function toElevenLabsForm(call) {
|
|
822
|
+
const { request, model } = call;
|
|
823
|
+
const form = new FormData();
|
|
824
|
+
form.set("model_id", model);
|
|
825
|
+
if (request.languageHint) form.set("language_code", request.languageHint);
|
|
826
|
+
const bytes = request.audio instanceof Blob ? request.audio : new Uint8Array(request.audio);
|
|
827
|
+
form.set("file", new Blob([bytes], { type: request.mimeType }), fileName(request.mimeType));
|
|
828
|
+
return form;
|
|
829
|
+
}
|
|
830
|
+
__name(toElevenLabsForm, "toElevenLabsForm");
|
|
831
|
+
function isChunk(value) {
|
|
832
|
+
return typeof value === "object" && value !== null;
|
|
833
|
+
}
|
|
834
|
+
__name(isChunk, "isChunk");
|
|
835
|
+
function numberOf(value) {
|
|
836
|
+
return typeof value === "number" && Number.isFinite(value) && value >= 0 ? value : void 0;
|
|
837
|
+
}
|
|
838
|
+
__name(numberOf, "numberOf");
|
|
839
|
+
function lastWordEnd(words) {
|
|
840
|
+
if (!Array.isArray(words)) return void 0;
|
|
841
|
+
const ends = words.map((word) => numberOf(word?.end)).filter((end) => end !== void 0);
|
|
842
|
+
return ends.length > 0 ? Math.max(...ends) : void 0;
|
|
843
|
+
}
|
|
844
|
+
__name(lastWordEnd, "lastWordEnd");
|
|
845
|
+
function textOf3(body) {
|
|
846
|
+
if (typeof body.text === "string") return body.text;
|
|
847
|
+
if (!Array.isArray(body.transcripts)) return null;
|
|
848
|
+
const parts = body.transcripts.map((channel) => isChunk(channel) && typeof channel.text === "string" ? channel.text : null);
|
|
849
|
+
return parts.every((part) => part !== null) ? parts.join("\n") : null;
|
|
850
|
+
}
|
|
851
|
+
__name(textOf3, "textOf");
|
|
852
|
+
function firstChunk(body) {
|
|
853
|
+
return Array.isArray(body.transcripts) && isChunk(body.transcripts[0]) ? body.transcripts[0] : body;
|
|
854
|
+
}
|
|
855
|
+
__name(firstChunk, "firstChunk");
|
|
856
|
+
function assertWithin(durationSeconds, max) {
|
|
857
|
+
if (max !== void 0 && durationSeconds > max) throw new AudioTooLongError(ELEVENLABS_PROVIDER, durationSeconds, max, false);
|
|
858
|
+
}
|
|
859
|
+
__name(assertWithin, "assertWithin");
|
|
860
|
+
function languageOf(chunk) {
|
|
861
|
+
return typeof chunk.language_code === "string" && chunk.language_code ? chunk.language_code : void 0;
|
|
862
|
+
}
|
|
863
|
+
__name(languageOf, "languageOf");
|
|
864
|
+
function fromElevenLabsResponse(body, model, request) {
|
|
865
|
+
if (!isChunk(body)) throw new InvalidResponseError(ELEVENLABS_PROVIDER, "The response is not an object.");
|
|
866
|
+
const text = textOf3(body);
|
|
867
|
+
if (text === null) throw new InvalidResponseError(ELEVENLABS_PROVIDER, "The response carries no transcript text.");
|
|
868
|
+
const chunk = firstChunk(body);
|
|
869
|
+
const durationSeconds = numberOf(body.audio_duration_secs) ?? numberOf(chunk.audio_duration_secs) ?? lastWordEnd(chunk.words) ?? numberOf(request.durationSeconds) ?? 0;
|
|
870
|
+
assertWithin(durationSeconds, request.maxDurationSeconds);
|
|
871
|
+
const language = languageOf(chunk);
|
|
872
|
+
return {
|
|
873
|
+
provider: ELEVENLABS_PROVIDER,
|
|
874
|
+
model,
|
|
875
|
+
text,
|
|
876
|
+
...language ? { language } : {},
|
|
877
|
+
durationSeconds,
|
|
878
|
+
usage: { audioSeconds: durationSeconds }
|
|
879
|
+
};
|
|
880
|
+
}
|
|
881
|
+
__name(fromElevenLabsResponse, "fromElevenLabsResponse");
|
|
882
|
+
function failureMessage(raw, status) {
|
|
883
|
+
try {
|
|
884
|
+
const detail = JSON.parse(raw).detail;
|
|
885
|
+
if (typeof detail === "string") return detail.slice(0, 300);
|
|
886
|
+
if (Array.isArray(detail) && typeof detail[0]?.msg === "string") return detail[0].msg.slice(0, 300);
|
|
887
|
+
const message = detail?.message;
|
|
888
|
+
if (typeof message === "string") return message.slice(0, 300);
|
|
889
|
+
} catch {
|
|
890
|
+
}
|
|
891
|
+
return `HTTP ${status}`;
|
|
892
|
+
}
|
|
893
|
+
__name(failureMessage, "failureMessage");
|
|
894
|
+
async function send(options, credential, call) {
|
|
895
|
+
const baseURL = options.baseURL ?? ELEVENLABS_BASE_URL;
|
|
896
|
+
const sealed = sealedFetch({
|
|
897
|
+
provider: ELEVENLABS_PROVIDER,
|
|
898
|
+
baseURL,
|
|
899
|
+
routes: ELEVENLABS_ROUTES,
|
|
900
|
+
auth: { "xi-api-key": credential.secret },
|
|
901
|
+
fetch: options.fetch
|
|
902
|
+
});
|
|
903
|
+
const response = await sealed(new URL("/v1/speech-to-text", baseURL), { method: "POST", body: toElevenLabsForm(call), signal: call.request.signal });
|
|
904
|
+
if (!response.ok) {
|
|
905
|
+
throw { status: response.status, headers: response.headers, message: failureMessage(await response.text(), response.status) };
|
|
906
|
+
}
|
|
907
|
+
try {
|
|
908
|
+
return await response.json();
|
|
909
|
+
} catch {
|
|
910
|
+
throw new InvalidResponseError(ELEVENLABS_PROVIDER, "The response is not JSON.");
|
|
911
|
+
}
|
|
912
|
+
}
|
|
913
|
+
__name(send, "send");
|
|
914
|
+
function elevenLabsAdapter(options = {}) {
|
|
915
|
+
return {
|
|
916
|
+
id: ELEVENLABS_PROVIDER,
|
|
917
|
+
defaultModel: options.defaultModel ?? ELEVENLABS_DEFAULT_MODEL,
|
|
918
|
+
credentialKinds: ["apiKey"],
|
|
919
|
+
async transcribe(call) {
|
|
920
|
+
let body;
|
|
921
|
+
try {
|
|
922
|
+
body = await send(options, call.credential, call);
|
|
923
|
+
} catch (error) {
|
|
924
|
+
throw mapProviderError(ELEVENLABS_PROVIDER, error, [call.credential.secret]);
|
|
925
|
+
}
|
|
926
|
+
return fromElevenLabsResponse(body, call.model, call.request);
|
|
927
|
+
}
|
|
928
|
+
};
|
|
929
|
+
}
|
|
930
|
+
__name(elevenLabsAdapter, "elevenLabsAdapter");
|
|
931
|
+
|
|
782
932
|
// src/adapters/index.ts
|
|
783
933
|
function builtInAdapters() {
|
|
784
934
|
return [anthropicAdapter(), openaiAdapter(), geminiAdapter(), xaiAdapter()];
|
|
@@ -1025,6 +1175,110 @@ function createModelClient(options) {
|
|
|
1025
1175
|
}
|
|
1026
1176
|
__name(createModelClient, "createModelClient");
|
|
1027
1177
|
|
|
1178
|
+
// src/transcription.ts
|
|
1179
|
+
function createTranscriptionRegistry(adapters = []) {
|
|
1180
|
+
const byId = /* @__PURE__ */ new Map();
|
|
1181
|
+
const registry = {
|
|
1182
|
+
register(adapter, options = {}) {
|
|
1183
|
+
if (byId.has(adapter.id) && !options.replace) {
|
|
1184
|
+
throw new TypeError(
|
|
1185
|
+
`@12-apps/llm: a transcription adapter for "${adapter.id}" is already registered; pass { replace: true } to replace it.`
|
|
1186
|
+
);
|
|
1187
|
+
}
|
|
1188
|
+
byId.set(adapter.id, adapter);
|
|
1189
|
+
return registry;
|
|
1190
|
+
},
|
|
1191
|
+
get(id) {
|
|
1192
|
+
const adapter = byId.get(id);
|
|
1193
|
+
if (!adapter) {
|
|
1194
|
+
throw new UnsupportedError(id, `No transcription adapter is registered for provider "${id}" (registered: ${[...byId.keys()].join(", ") || "none"}).`);
|
|
1195
|
+
}
|
|
1196
|
+
return adapter;
|
|
1197
|
+
},
|
|
1198
|
+
has: /* @__PURE__ */ __name((id) => byId.has(id), "has"),
|
|
1199
|
+
ids: /* @__PURE__ */ __name(() => [...byId.keys()], "ids")
|
|
1200
|
+
};
|
|
1201
|
+
for (const adapter of adapters) registry.register(adapter);
|
|
1202
|
+
return registry;
|
|
1203
|
+
}
|
|
1204
|
+
__name(createTranscriptionRegistry, "createTranscriptionRegistry");
|
|
1205
|
+
function builtInTranscriptionAdapters() {
|
|
1206
|
+
return [elevenLabsAdapter()];
|
|
1207
|
+
}
|
|
1208
|
+
__name(builtInTranscriptionAdapters, "builtInTranscriptionAdapters");
|
|
1209
|
+
var defaultSleep2 = /* @__PURE__ */ __name((ms) => new Promise((resolve) => setTimeout(resolve, ms)), "defaultSleep");
|
|
1210
|
+
function sizeOf(audio) {
|
|
1211
|
+
return audio instanceof Blob ? audio.size : audio.byteLength;
|
|
1212
|
+
}
|
|
1213
|
+
__name(sizeOf, "sizeOf");
|
|
1214
|
+
function isNonNegative(value) {
|
|
1215
|
+
return Number.isFinite(value) && value >= 0;
|
|
1216
|
+
}
|
|
1217
|
+
__name(isNonNegative, "isNonNegative");
|
|
1218
|
+
function requestProblems(request) {
|
|
1219
|
+
const { maxDurationSeconds: max, durationSeconds: known } = request;
|
|
1220
|
+
const checks = [
|
|
1221
|
+
[sizeOf(request.audio) === 0, "audio is empty"],
|
|
1222
|
+
[!request.mimeType, "mimeType is required"],
|
|
1223
|
+
[max !== void 0 && !(isNonNegative(max) && max > 0), "maxDurationSeconds must be a positive number"],
|
|
1224
|
+
[known !== void 0 && !isNonNegative(known), "durationSeconds must be a non-negative number"]
|
|
1225
|
+
];
|
|
1226
|
+
return checks.filter(([failed]) => failed).map(([, problem]) => problem);
|
|
1227
|
+
}
|
|
1228
|
+
__name(requestProblems, "requestProblems");
|
|
1229
|
+
function checkRequest2(provider, request) {
|
|
1230
|
+
const problems = requestProblems(request);
|
|
1231
|
+
if (problems.length > 0) throw new InvalidRequestError(provider, `Invalid transcription request:
|
|
1232
|
+
- ${problems.join("\n- ")}`);
|
|
1233
|
+
const { maxDurationSeconds: max, durationSeconds: known } = request;
|
|
1234
|
+
if (max !== void 0 && known !== void 0 && known > max) throw new AudioTooLongError(provider, known, max, true);
|
|
1235
|
+
}
|
|
1236
|
+
__name(checkRequest2, "checkRequest");
|
|
1237
|
+
async function callWithAuth2(adapter, call, credential, renew) {
|
|
1238
|
+
try {
|
|
1239
|
+
return await adapter.transcribe({ ...call, credential });
|
|
1240
|
+
} catch (error) {
|
|
1241
|
+
if (!(error instanceof AuthError) || error.status !== 401) throw error;
|
|
1242
|
+
const renewed = await renew(credential);
|
|
1243
|
+
if (!renewed) throw error;
|
|
1244
|
+
return adapter.transcribe({ ...call, credential: renewed });
|
|
1245
|
+
}
|
|
1246
|
+
}
|
|
1247
|
+
__name(callWithAuth2, "callWithAuth");
|
|
1248
|
+
function createTranscriber(options) {
|
|
1249
|
+
const adapter = (options.registry ?? createTranscriptionRegistry(builtInTranscriptionAdapters())).get(options.provider);
|
|
1250
|
+
if (!adapter.credentialKinds.includes(options.credentials.kind)) {
|
|
1251
|
+
throw new UnsupportedError(
|
|
1252
|
+
adapter.id,
|
|
1253
|
+
`The "${adapter.id}" adapter does not accept "${options.credentials.kind}" credentials (it accepts: ${adapter.credentialKinds.join(", ")}).`
|
|
1254
|
+
);
|
|
1255
|
+
}
|
|
1256
|
+
const session = createCredentialSession(options.credentials, { now: options.now });
|
|
1257
|
+
const model = options.model ?? adapter.defaultModel;
|
|
1258
|
+
if (!model) throw new InvalidRequestError(adapter.id, `The "${adapter.id}" adapter has no default model; pass \`model\`.`);
|
|
1259
|
+
const retry = options.retry ?? (() => null);
|
|
1260
|
+
const sleep = options.sleep ?? defaultSleep2;
|
|
1261
|
+
return {
|
|
1262
|
+
provider: adapter.id,
|
|
1263
|
+
model,
|
|
1264
|
+
async transcribe(request) {
|
|
1265
|
+
checkRequest2(adapter.id, request);
|
|
1266
|
+
for (let attempt = 1; ; attempt += 1) {
|
|
1267
|
+
try {
|
|
1268
|
+
const credential = await session.current();
|
|
1269
|
+
return await callWithAuth2(adapter, { model, request }, credential, (used) => session.afterUnauthorized(used));
|
|
1270
|
+
} catch (error) {
|
|
1271
|
+
if (!(error instanceof LlmError) || error instanceof AudioTooLongError) throw error;
|
|
1272
|
+
const delay = retry(error, attempt);
|
|
1273
|
+
if (delay === null || request.signal?.aborted) throw error;
|
|
1274
|
+
await sleep(delay);
|
|
1275
|
+
}
|
|
1276
|
+
}
|
|
1277
|
+
}
|
|
1278
|
+
};
|
|
1279
|
+
}
|
|
1280
|
+
__name(createTranscriber, "createTranscriber");
|
|
1281
|
+
|
|
1028
1282
|
// src/loop.ts
|
|
1029
1283
|
var UNKNOWN_TOOL = "This tool does not exist.";
|
|
1030
1284
|
function addUsage(total, usage) {
|
|
@@ -1418,8 +1672,11 @@ __name(mcpTools, "mcpTools");
|
|
|
1418
1672
|
export {
|
|
1419
1673
|
ANTHROPIC_DEFAULT_MODEL,
|
|
1420
1674
|
ANTHROPIC_PROVIDER,
|
|
1675
|
+
AudioTooLongError,
|
|
1421
1676
|
AuthError,
|
|
1422
1677
|
CredentialRevokedError,
|
|
1678
|
+
ELEVENLABS_DEFAULT_MODEL,
|
|
1679
|
+
ELEVENLABS_PROVIDER,
|
|
1423
1680
|
ESCALATE_TOOL,
|
|
1424
1681
|
GEMINI_PROVIDER,
|
|
1425
1682
|
InvalidRequestError,
|
|
@@ -1437,10 +1694,14 @@ export {
|
|
|
1437
1694
|
anthropicAdapter,
|
|
1438
1695
|
assertStrictTools,
|
|
1439
1696
|
builtInAdapters,
|
|
1697
|
+
builtInTranscriptionAdapters,
|
|
1440
1698
|
createAdapterRegistry,
|
|
1441
1699
|
createCredentialSession,
|
|
1442
1700
|
createModelClient,
|
|
1443
1701
|
createSupportLadder,
|
|
1702
|
+
createTranscriber,
|
|
1703
|
+
createTranscriptionRegistry,
|
|
1704
|
+
elevenLabsAdapter,
|
|
1444
1705
|
ensureNotRefused,
|
|
1445
1706
|
geminiAdapter,
|
|
1446
1707
|
mapProviderError,
|