@providerkit/core 0.29.0 → 0.30.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 +25 -4
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +29 -2
- package/dist/errors.js.map +1 -1
- package/dist/jev.d.ts +123 -0
- package/dist/jev.d.ts.map +1 -0
- package/dist/jev.js +278 -0
- package/dist/jev.js.map +1 -0
- package/dist/presets.d.ts +35 -4
- package/dist/presets.d.ts.map +1 -1
- package/dist/presets.js +32 -8
- package/dist/presets.js.map +1 -1
- package/dist/providers/deepgram-transcription.d.ts +24 -0
- package/dist/providers/deepgram-transcription.d.ts.map +1 -0
- package/dist/providers/deepgram-transcription.js +84 -0
- package/dist/providers/deepgram-transcription.js.map +1 -0
- package/dist/providers/elevenlabs-speech.d.ts +1 -1
- package/dist/providers/elevenlabs-speech.d.ts.map +1 -1
- package/dist/providers/elevenlabs-speech.js +1 -1
- package/dist/providers/elevenlabs-speech.js.map +1 -1
- package/dist/providers/openai-transcription.d.ts +21 -0
- package/dist/providers/openai-transcription.d.ts.map +1 -0
- package/dist/providers/openai-transcription.js +144 -0
- package/dist/providers/openai-transcription.js.map +1 -0
- package/dist/providers/transcription-factory.d.ts +30 -0
- package/dist/providers/transcription-factory.d.ts.map +1 -0
- package/dist/providers/transcription-factory.js +38 -0
- package/dist/providers/transcription-factory.js.map +1 -0
- package/dist/speech-core.d.ts +2 -17
- package/dist/speech-core.d.ts.map +1 -1
- package/dist/speech-core.js +10 -50
- package/dist/speech-core.js.map +1 -1
- package/dist/transcription-core.d.ts +146 -0
- package/dist/transcription-core.d.ts.map +1 -0
- package/dist/transcription-core.js +193 -0
- package/dist/transcription-core.js.map +1 -0
- package/dist/transcription.d.ts +7 -0
- package/dist/transcription.d.ts.map +1 -0
- package/dist/transcription.js +12 -0
- package/dist/transcription.js.map +1 -0
- package/dist/transport.d.ts +26 -0
- package/dist/transport.d.ts.map +1 -1
- package/dist/transport.js +57 -2
- package/dist/transport.js.map +1 -1
- package/package.json +5 -1
- package/src/errors.ts +35 -2
- package/src/presets.ts +54 -8
- package/src/providers/deepgram-transcription.ts +109 -0
- package/src/providers/elevenlabs-speech.ts +2 -2
- package/src/providers/openai-transcription.ts +177 -0
- package/src/providers/transcription-factory.ts +68 -0
- package/src/speech-core.ts +9 -47
- package/src/transcription-core.ts +345 -0
- package/src/transcription.ts +24 -0
- package/src/transport.ts +77 -3
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// `@providerkit/core/transcription`: one recording to its text, the inverse of
|
|
2
|
+
// `@providerkit/core/speech`. It is its own entry point, and `index.ts` does not export it, so
|
|
3
|
+
// an app that only chats loads none of the transcription code.
|
|
4
|
+
//
|
|
5
|
+
// Named, not `export *`: transcription-core.ts also holds helpers the two wires share, and a
|
|
6
|
+
// helper exported here is one this package can never change without a major version.
|
|
7
|
+
export {
|
|
8
|
+
DEFAULT_TRANSCRIPTION_LIMITS,
|
|
9
|
+
TranscriptionError,
|
|
10
|
+
type TranscriptionLimits,
|
|
11
|
+
type TranscriptionProvider,
|
|
12
|
+
type TranscriptionRequest,
|
|
13
|
+
type TranscriptionResult,
|
|
14
|
+
type TranscriptionSegment,
|
|
15
|
+
type TranscriptionShape,
|
|
16
|
+
type TranscriptionUsage,
|
|
17
|
+
} from "./transcription-core.ts";
|
|
18
|
+
// The `fetchImpl` option's type, so a transcription-only app can type its wrapper without the
|
|
19
|
+
// main entry.
|
|
20
|
+
export type { FetchLike } from "./transport.ts";
|
|
21
|
+
export * from "./providers/openai-transcription.ts";
|
|
22
|
+
export * from "./providers/deepgram-transcription.ts";
|
|
23
|
+
export * from "./providers/transcription-factory.ts";
|
|
24
|
+
export { TRANSCRIPTION_PRESET_IDS, type PresetTranscription } from "./presets.ts";
|
package/src/transport.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
//
|
|
5
5
|
// Adapters keep only their per-event mapping; everything about being an HTTP
|
|
6
6
|
// client lives here once.
|
|
7
|
-
import { classifyHttp, parseRetryAfterMs, ProviderError } from "./errors.ts";
|
|
7
|
+
import { classifyHttp, parseRetryAfterMs, ProviderError, type ErrorKind } from "./errors.ts";
|
|
8
8
|
import {
|
|
9
9
|
parseRateLimitReset,
|
|
10
10
|
parseRateLimitResponse,
|
|
@@ -198,13 +198,17 @@ function originOf(url: string): string {
|
|
|
198
198
|
*/
|
|
199
199
|
async function send(opts: RequestInit_, method = "POST", redact = false): Promise<Response> {
|
|
200
200
|
const doFetch = opts.fetchImpl ?? globalThis.fetch;
|
|
201
|
+
// A Blob (a recording) or a FormData (a multipart upload) goes as itself, and fetch writes its
|
|
202
|
+
// Content-Type: a multipart boundary is one only fetch knows, so a JSON type here would break it.
|
|
203
|
+
const payload = opts.body;
|
|
204
|
+
const raw = payload instanceof Blob || payload instanceof FormData;
|
|
201
205
|
// Serialized outside the catch below: a body that cannot become JSON is a
|
|
202
206
|
// bug of ours, and must not be dressed as a network blip and retried.
|
|
203
|
-
const body = typeof
|
|
207
|
+
const body = raw || typeof payload === "string" ? payload : JSON.stringify(payload);
|
|
204
208
|
try {
|
|
205
209
|
return await doFetch(opts.url, {
|
|
206
210
|
method,
|
|
207
|
-
headers: { "content-type": "application/json", ...opts.headers },
|
|
211
|
+
headers: raw ? { ...opts.headers } : { "content-type": "application/json", ...opts.headers },
|
|
208
212
|
body,
|
|
209
213
|
signal: opts.signal,
|
|
210
214
|
...(opts.redirect ? { redirect: opts.redirect } : {}),
|
|
@@ -361,6 +365,76 @@ export async function withDeadline<T>(
|
|
|
361
365
|
}
|
|
362
366
|
}
|
|
363
367
|
|
|
368
|
+
type ProviderErrorOptions = NonNullable<ConstructorParameters<typeof ProviderError>[3]>;
|
|
369
|
+
|
|
370
|
+
/** How a capability copies one of its own errors, so a subclass comes back as itself
|
|
371
|
+
* with what it carries (the usage a speech or transcription error keeps). */
|
|
372
|
+
export type ErrorRebuild = (err: ProviderError, opts: ProviderErrorOptions) => ProviderError;
|
|
373
|
+
|
|
374
|
+
const plainCopy: ErrorRebuild = (err, opts) =>
|
|
375
|
+
new ProviderError(err.provider, err.kind, err.message, opts);
|
|
376
|
+
|
|
377
|
+
/** Kinds that can land after the vendor started the work it bills for. */
|
|
378
|
+
const REPLAY_UNSAFE: ReadonlySet<ErrorKind> = new Set<ErrorKind>([
|
|
379
|
+
"network",
|
|
380
|
+
"timeout",
|
|
381
|
+
"overload",
|
|
382
|
+
"unknown",
|
|
383
|
+
]);
|
|
384
|
+
|
|
385
|
+
/**
|
|
386
|
+
* @internal The "could the vendor already have billed this?" rule (invariant 16), for every
|
|
387
|
+
* call that is paid for whole and answered whole: speech and transcription. Kept here, once,
|
|
388
|
+
* so a new capability or a new failure path cannot forget it.
|
|
389
|
+
*
|
|
390
|
+
* - The caller's Stop comes back untouched.
|
|
391
|
+
* - A timeout, a dead socket, a 5xx or anything unclassified may have landed after
|
|
392
|
+
* the vendor started work, so it is marked `shouldRetry: false`, and a vendor's
|
|
393
|
+
* `x-should-retry: true` does not undo that: the header says the vendor thinks a
|
|
394
|
+
* resend is fine, not that the first call was not billed.
|
|
395
|
+
* - Every other kind keeps what it came with. A 429 means the vendor refused before
|
|
396
|
+
* doing any work, so sending it again is safe, and `Retry-After` is honoured by
|
|
397
|
+
* the caller's own retry. `x-should-retry` is believed there, in either direction.
|
|
398
|
+
*/
|
|
399
|
+
export function noReplay(
|
|
400
|
+
provider: string,
|
|
401
|
+
error: unknown,
|
|
402
|
+
signal?: AbortSignal,
|
|
403
|
+
rebuild: ErrorRebuild = plainCopy,
|
|
404
|
+
): unknown {
|
|
405
|
+
if (signal?.aborted) return error;
|
|
406
|
+
const err = ProviderError.from(provider, error);
|
|
407
|
+
if (!REPLAY_UNSAFE.has(err.kind) || err.shouldRetry === false) return err;
|
|
408
|
+
return rebuild(err, {
|
|
409
|
+
status: err.status,
|
|
410
|
+
code: err.code,
|
|
411
|
+
retryAfterMs: err.retryAfterMs,
|
|
412
|
+
resetAtMs: err.resetAtMs,
|
|
413
|
+
window: err.window,
|
|
414
|
+
shouldRetry: false,
|
|
415
|
+
body: err.body,
|
|
416
|
+
cause: err.cause,
|
|
417
|
+
});
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
/**
|
|
421
|
+
* @internal The one place a paid call runs: under one deadline, once, with `noReplay`
|
|
422
|
+
* applied to whatever it throws.
|
|
423
|
+
*/
|
|
424
|
+
export async function callOnce<T>(
|
|
425
|
+
provider: string,
|
|
426
|
+
timeoutMs: number,
|
|
427
|
+
signal: AbortSignal | undefined,
|
|
428
|
+
call: (signal: AbortSignal) => Promise<T>,
|
|
429
|
+
rebuild?: ErrorRebuild,
|
|
430
|
+
): Promise<T> {
|
|
431
|
+
try {
|
|
432
|
+
return await withDeadline(provider, timeoutMs, signal, call);
|
|
433
|
+
} catch (error) {
|
|
434
|
+
throw noReplay(provider, error, signal, rebuild);
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
|
|
364
438
|
/** Pull the `data:` payload out of one SSE frame, joining continuation lines
|
|
365
439
|
* the way the spec says to. Returns null for a comment or a frame carrying
|
|
366
440
|
* only an `event:` name. */
|