ai-functions
Version:
Core AI primitives for building intelligent applications
174 lines • 4.72 kB
TypeScript
/**
* AI Generation functions with automatic model resolution and routing
*
* Wraps AI SDK generateObject and generateText with smart model routing:
* - Simple aliases: 'opus', 'sonnet', 'gpt-4o'
* - Full IDs: 'anthropic/claude-sonnet-4.5'
* - Auto-routes to native SDKs for openai/anthropic/google
*
* @packageDocumentation
*/
import { generateText as sdkGenerateText, streamText as sdkStreamText, type LanguageModel } from 'ai';
type ModelArg = string | LanguageModel;
interface GenerateObjectOptions<T> {
model: ModelArg;
schema: T;
prompt?: string;
messages?: Array<{
role: 'user' | 'assistant' | 'system';
content: string;
}>;
system?: string;
mode?: 'auto' | 'json' | 'tool';
maxTokens?: number;
temperature?: number;
topP?: number;
topK?: number;
presencePenalty?: number;
frequencyPenalty?: number;
seed?: number;
maxRetries?: number;
abortSignal?: AbortSignal;
headers?: Record<string, string>;
experimental_telemetry?: {
isEnabled?: boolean;
functionId?: string;
metadata?: Record<string, string>;
};
}
interface GenerateTextOptions {
model: ModelArg;
prompt?: string;
messages?: Array<{
role: 'user' | 'assistant' | 'system';
content: string;
}>;
system?: string;
maxTokens?: number;
temperature?: number;
topP?: number;
topK?: number;
presencePenalty?: number;
frequencyPenalty?: number;
seed?: number;
maxRetries?: number;
abortSignal?: AbortSignal;
headers?: Record<string, string>;
tools?: Record<string, unknown>;
toolChoice?: 'auto' | 'none' | 'required' | {
type: 'tool';
toolName: string;
};
maxSteps?: number;
experimental_telemetry?: {
isEnabled?: boolean;
functionId?: string;
metadata?: Record<string, string>;
};
}
/**
* Generate a typed object from a prompt using AI
*
* Automatically resolves model aliases and routes to the best provider.
* Supports both Zod schemas and simplified schema syntax.
*
* @example
* ```ts
* import { generateObject } from 'ai-functions'
*
* // Simplified schema syntax
* const { object } = await generateObject({
* model: 'sonnet',
* schema: {
* recipe: {
* name: 'What is the recipe name?',
* type: 'food | drink | dessert',
* ingredients: ['List all ingredients'],
* steps: ['List all cooking steps'],
* },
* },
* prompt: 'Generate a lasagna recipe.',
* })
*
* // Zod schema also works
* import { z } from 'zod'
* const { object } = await generateObject({
* model: 'sonnet',
* schema: z.object({
* name: z.string(),
* ingredients: z.array(z.string()),
* }),
* prompt: 'Generate a lasagna recipe.',
* })
* ```
*/
export declare function generateObject<T>(options: GenerateObjectOptions<T>): Promise<{
object: T;
usage?: unknown;
warnings?: unknown[] | undefined;
}>;
/**
* Generate text from a prompt using AI
*
* Automatically resolves model aliases and routes to the best provider.
*
* @example
* ```ts
* import { generateText } from 'ai-functions'
*
* const { text } = await generateText({
* model: 'opus', // → anthropic/claude-opus-4.5
* prompt: 'Write a haiku about programming.',
* })
*
* // With tools
* const { text, toolResults } = await generateText({
* model: 'gpt-4o', // → openai/gpt-4o
* prompt: 'What is the weather in San Francisco?',
* tools: { ... },
* maxSteps: 5,
* })
* ```
*/
export declare function generateText(options: GenerateTextOptions): Promise<Awaited<ReturnType<typeof sdkGenerateText>>>;
/**
* Stream a typed object from a prompt using AI
*
* @example
* ```ts
* import { streamObject } from 'ai-functions'
*
* const { partialObjectStream } = streamObject({
* model: 'sonnet',
* schema: { story: 'Write a creative story' },
* prompt: 'Write a short story.',
* })
*
* for await (const partial of partialObjectStream) {
* console.log(partial.story)
* }
* ```
*/
export declare function streamObject<T>(options: GenerateObjectOptions<T>): Promise<{
partialObjectStream: AsyncIterable<T>;
}>;
/**
* Stream text from a prompt using AI
*
* @example
* ```ts
* import { streamText } from 'ai-functions'
*
* const { textStream } = streamText({
* model: 'gemini', // → google/gemini-2.5-flash
* prompt: 'Explain quantum computing.',
* })
*
* for await (const chunk of textStream) {
* process.stdout.write(chunk)
* }
* ```
*/
export declare function streamText(options: GenerateTextOptions): Promise<ReturnType<typeof sdkStreamText>>;
export {};
//# sourceMappingURL=generate.d.ts.map