Skip to content

AI Class

The central class of @aeye/ai. Manages providers, model registry, APIs, and component factories.

Creation

typescript
const ai = AI.with<TContext, TMetadata>()
  .providers(providers)
  .create(config?);

See AI Instance for detailed configuration.

Properties

PropertyTypeDescription
configAIConfigOf<T>Full configuration
registryModelRegistryModel registry instance
chatChatAPIChat completions API
imageImageAPIImage APIs (generate, edit, analyze)
speechSpeechAPIText-to-speech API
transcribeTranscribeAPISpeech-to-text API
embedEmbedAPIEmbeddings API
modelsModelsAPIModel search and selection
providersAIProvidersProvider instances
componentsComponent[]Registered components
hooksAIHooksLifecycle hooks

Methods

withHooks(hooks)

Set lifecycle hooks. Returns this for chaining.

typescript
ai.withHooks({
  beforeModelSelection: async (ctx, request, metadata) => metadata,
  onModelSelected: async (ctx, request, selected) => {},
  beforeRequest: async (ctx, request, selected, usage, cost) => {},
  afterRequest: async (ctx, request, response, complete, selected, usage, cost) => {},
  onError: (type, message, error, ctx, request) => {},
});

prompt(input)

Create an AI-bound prompt:

typescript
const myPrompt = ai.prompt({
  name: 'myPrompt',
  description: '...',
  content: '...',
  // ... all PromptInput options
});

tool(input)

Create an AI-bound tool:

typescript
const myTool = ai.tool({
  name: 'myTool',
  description: '...',
  schema: z.object({ /* ... */ }),
  call: async (input) => { /* ... */ },
});

agent(input)

Create an AI-bound agent:

typescript
const myAgent = ai.agent({
  name: 'myAgent',
  description: '...',
  refs: [tool1, prompt1] as const,
  call: async (input, [t, p], ctx) => { /* ... */ },
});

buildContext(requiredCtx)

Build merged context: defaultContext → providedContext → requiredCtx

selectModel(metadata)

Select a model using the registry.

estimateMessageUsage(message)

Estimate token usage for a message.

estimateRequestUsage(request)

Estimate token usage for a full request.

calculateCost(model, usage)

Calculate dollar cost from model pricing and usage.

extend(config)

Create an extended AI instance with additional context/metadata types.

stats()

Get aggregate statistics:

typescript
interface LibraryStats {
  totalModels: number;
  modelsByProvider: Record<string, number>;
  totalRequests: number;
  successfulRequests: number;
  failedRequests: number;
  averageCost: number;
  averageLatency: number;
}

run(component, input, ctx?)

Run any component with AI context injection.

Released under the GPL-3.0 License.