ai-functions
Version:
Core AI primitives for building intelligent applications
111 lines • 5.12 kB
TypeScript
/**
* BatchProvider Port
*
* Defines the explicit port (interface) that every batch provider adapter must
* satisfy, plus helpers that concentrate logic genuinely shared across adapters
* (polling, concurrent processing, in-memory job tracking, JSON-Schema conversion).
*
* Each provider file (`anthropic.ts`, `openai.ts`, `google.ts`, `bedrock.ts`,
* `cloudflare.ts`, `memory.ts`) is now a small adapter that satisfies this port
* and concentrates on provider-specific HTTP/SDK calls and request/response shapes.
*
* Port + 4 adapters = real seam. The port lives here so provider files don't
* import shared logic from each other and can't accidentally diverge.
*
* @packageDocumentation
*/
import type { BatchAdapter, BatchItem, BatchJob, BatchQueueOptions, BatchResult, BatchStatus } from '../batch-queue.js';
export type { BatchAdapter, BatchItem, BatchJob, BatchQueueOptions, BatchResult, BatchSubmitResult, BatchProvider, BatchStatus, FlexAdapter, } from '../batch-queue.js';
export { registerBatchAdapter, registerFlexAdapter } from '../batch-queue.js';
/**
* Default `waitForCompletion` implementation built on top of `getStatus` +
* `getResults`. Adapters with non-standard completion semantics can still
* override `waitForCompletion`, but most don't need to.
*
* @param adapter The batch adapter to poll
* @param batchId The batch id to poll
* @param options.pollInterval Poll interval in ms (default: 5000)
* @param options.fetchResultsOn Statuses for which results should be fetched.
* Defaults to `completed`, `cancelled`, `failed`.
* @param options.throwOn Statuses that should throw rather than fetch results.
* Useful for OpenAI which throws on `cancelled`/`expired`.
*/
export declare function pollUntilComplete(adapter: Pick<BatchAdapter, 'getStatus' | 'getResults'>, batchId: string, options?: {
pollInterval?: number;
fetchResultsOn?: ReadonlySet<BatchStatus>;
throwOn?: ReadonlySet<BatchStatus>;
}): Promise<BatchResult[]>;
/**
* Run `processItem` over `items` with bounded concurrency, optionally
* sleeping between waves to respect provider rate limits. Per-item failures
* are caught and emitted as `{ status: 'failed', error }` results so a single
* bad item never poisons the whole batch.
*
* Used by flex adapters (OpenAI, Google, Bedrock) and by the "local" providers
* (Google, Bedrock, Cloudflare) that fake batch processing with concurrent
* direct API calls.
*/
export declare function processConcurrently(items: BatchItem[], processItem: (item: BatchItem) => Promise<BatchResult>, options?: {
concurrency?: number;
delayBetweenWaves?: number;
onWaveComplete?: (results: BatchResult[]) => void;
}): Promise<BatchResult[]>;
/** Build a `failed` BatchResult from an unknown thrown value. */
export declare function failedResult(item: BatchItem, error: unknown): BatchResult;
/**
* Internal job state for adapters that don't have a real provider-side batch
* API and need to track jobs locally (Google, Bedrock, Cloudflare).
*/
export interface LocalJobState {
items: BatchItem[];
options: BatchQueueOptions;
results: BatchResult[];
status: BatchStatus;
createdAt: Date;
completedAt?: Date;
/** Optional adapter-specific metadata (e.g. Bedrock's jobArn) */
meta?: Record<string, unknown>;
}
/**
* Per-provider in-memory job registry. Encapsulates the
* `Map<jobId, state>` + counter + status/result lookup pattern that
* google/bedrock/cloudflare were each duplicating.
*/
export declare class LocalJobStore {
private readonly idPrefix;
private readonly jobs;
private counter;
constructor(idPrefix: string);
create(items: BatchItem[], options: BatchQueueOptions): {
id: string;
state: LocalJobState;
};
get(id: string): LocalJobState;
has(id: string): boolean;
/** Build a `BatchJob` snapshot for a tracked job. */
snapshot(id: string, provider: BatchJob['provider']): BatchJob;
/**
* Wait for a tracked job to reach a terminal status by polling its in-memory
* state. Adapters that drive the state machine in a background promise can
* call this from `waitForCompletion`.
*/
waitForCompletion(id: string, pollInterval?: number): Promise<BatchResult[]>;
/** For tests: drop everything. */
clear(): void;
}
/**
* Minimal Zod -> JSON Schema converter.
*
* This is the same simplified converter that previously lived (duplicated)
* inside `anthropic.ts` and `openai.ts`. Extracted here so both adapters call
* the same implementation. For richer conversion use `zod-to-json-schema`.
*/
export declare function zodToJsonSchema(zodSchema: unknown): Record<string, unknown>;
/**
* Try to parse `text` as JSON when it looks like JSON or a schema is expected,
* otherwise return the text unchanged. Never throws.
*/
export declare function tryParseJson(text: string | undefined, expectJson?: boolean): unknown;
/** Promise-based setTimeout. */
export declare function sleep(ms: number): Promise<void>;
//# sourceMappingURL=provider.d.ts.map