Generate a unified AI provider abstraction layer skill using the @auto-engineer/ai-gateway npm package for multi-provider AI integration with MCP tool support
Generate a comprehensive AI provider abstraction implementation using the `@auto-engineer/ai-gateway` npm package (v1.6.0+). This skill creates a unified interface for OpenAI, Anthropic, Google, XAI, and custom OpenAI-compatible providers with support for text generation, structured data, streaming, and MCP tool integration.
Creates production-ready code that leverages `@auto-engineer/ai-gateway` to:
When a user requests AI gateway integration, follow these steps:
Ask clarifying questions if not specified:
```bash
pnpm add @auto-engineer/ai-gateway
npm install @auto-engineer/ai-gateway
```
Create or update `.env` with required API keys:
```bash
OPENAI_API_KEY=sk-...
ANTHROPIC_API_KEY=sk-ant-...
GOOGLE_API_KEY=...
XAI_API_KEY=...
CUSTOM_OPENAI_BASE_URL=https://api.custom-provider.com/v1
CUSTOM_OPENAI_API_KEY=...
```
```typescript
import { generateTextWithAI, AIProvider } from '@auto-engineer/ai-gateway';
async function generateResponse(prompt: string) {
const text = await generateTextWithAI(prompt, {
provider: AIProvider.Anthropic, // or OpenAI, Google, XAI, Custom
temperature: 0.7,
maxTokens: 1000,
});
return text;
}
```
```typescript
import { streamTextWithAI, AIProvider } from '@auto-engineer/ai-gateway';
async function streamResponse(prompt: string) {
for await (const chunk of streamTextWithAI(prompt, {
provider: AIProvider.OpenAI,
temperature: 0.7,
})) {
process.stdout.write(chunk);
}
}
// Or with callback
import { generateTextStreamingWithAI } from '@auto-engineer/ai-gateway';
async function streamWithCallback(prompt: string) {
await generateTextStreamingWithAI(prompt, {
streamCallback: (token) => {
process.stdout.write(token);
},
});
}
```
```typescript
import { generateStructuredDataWithAI, z } from '@auto-engineer/ai-gateway';
const UserSchema = z.object({
name: z.string(),
email: z.string().email(),
age: z.number().int().positive(),
role: z.enum(['admin', 'user', 'guest']),
});
async function extractUserData(text: string) {
const user = await generateStructuredDataWithAI(
`Extract user information: ${text}`,
{
schema: UserSchema,
schemaName: 'User',
provider: AIProvider.Anthropic,
}
);
return user; // TypeScript knows this matches UserSchema
}
// Stream structured data
import { streamStructuredDataWithAI } from '@auto-engineer/ai-gateway';
async function streamStructuredData(prompt: string) {
for await (const partialData of streamStructuredDataWithAI(prompt, {
schema: UserSchema,
schemaName: 'User',
})) {
console.log('Partial:', partialData);
}
}
```
```typescript
import { generateTextWithImageAI, AIProvider } from '@auto-engineer/ai-gateway';
import fs from 'fs';
async function analyzeImage(imagePath: string) {
const imageBuffer = fs.readFileSync(imagePath);
const base64Image = imageBuffer.toString('base64');
const description = await generateTextWithImageAI(
'Describe this image in detail',
base64Image,
{
provider: AIProvider.OpenAI, // or Anthropic, Google
model: 'gpt-4o', // or claude-3-5-sonnet-20241022, gemini-2.0-flash-exp
}
);
return description;
}
```
```typescript
import { registerTool, startServer, z } from '@auto-engineer/ai-gateway/node';
// Register a tool
registerTool<{ query: string }>(
'search',
{
title: 'Search Tool',
description: 'Searches a knowledge base',
inputSchema: {
query: z.string().describe('The search query'),
},
},
async ({ query }) => {
// Your search logic here
const results = await performSearch(query);
return {
content: [{ type: 'text', text: JSON.stringify(results) }],
};
}
);
// Start MCP server
await startServer({
name: 'my-mcp-server',
version: '1.0.0',
});
// Use tools with generation
import { generateTextWithToolsAI } from '@auto-engineer/ai-gateway';
const response = await generateTextWithToolsAI('Search for TypeScript tutorials', {
includeTools: true,
});
```
```typescript
// config/ai.ts
import { AIProvider, getAvailableProviders, getDefaultAIProvider } from '@auto-engineer/ai-gateway';
export const AI_CONFIG = {
defaultProvider: (process.env.AI_PROVIDER as AIProvider) || AIProvider.OpenAI,
defaultTemperature: 0.7,
defaultMaxTokens: 2000,
} as const;
export function getConfiguredProvider(): AIProvider {
const available = getAvailableProviders();
const preferred = getDefaultAIProvider();
if (available.includes(preferred)) {
return preferred;
}
return available[0] || AIProvider.OpenAI;
}
```
```typescript
// lib/ai.ts
import { generateTextWithAI, generateStructuredDataWithAI, AIProvider } from '@auto-engineer/ai-gateway';
import { z } from 'zod';
import { AI_CONFIG } from '../config/ai';
export async function generate(
prompt: string,
options?: {
provider?: AIProvider;
temperature?: number;
maxTokens?: number;
}
) {
return generateTextWithAI(prompt, {
provider: options?.provider || AI_CONFIG.defaultProvider,
temperature: options?.temperature ?? AI_CONFIG.defaultTemperature,
maxTokens: options?.maxTokens ?? AI_CONFIG.defaultMaxTokens,
});
}
export async function generateTyped<T extends z.ZodTypeAny>(
prompt: string,
schema: T,
schemaName: string,
options?: {
provider?: AIProvider;
}
): Promise<z.infer<T>> {
return generateStructuredDataWithAI(prompt, {
schema,
schemaName,
provider: options?.provider || AI_CONFIG.defaultProvider,
});
}
```
```typescript
import { generateTextWithAI, AIProvider } from '@auto-engineer/ai-gateway';
async function generateWithRetry(
prompt: string,
maxRetries = 3
): Promise<string> {
const providers = [AIProvider.OpenAI, AIProvider.Anthropic, AIProvider.Google];
let lastError: Error | undefined;
for (let attempt = 0; attempt < maxRetries; attempt++) {
const provider = providers[attempt % providers.length];
try {
return await generateTextWithAI(prompt, {
provider,
temperature: 0.7,
});
} catch (error) {
lastError = error as Error;
console.warn(`Attempt ${attempt + 1} failed with ${provider}:`, error);
if (attempt < maxRetries - 1) {
await new Promise(resolve => setTimeout(resolve, 1000 * (attempt + 1)));
}
}
}
throw lastError || new Error('All retry attempts failed');
}
```
Generate example files demonstrating common patterns:
```typescript
// examples/basic.ts - Simple text generation
// examples/streaming.ts - Streaming responses
// examples/structured.ts - Structured data extraction
// examples/images.ts - Image processing
// examples/tools.ts - MCP tool integration
```
```typescript
import { describe, it, expect, vi } from 'vitest';
import { generateTextWithAI, AIProvider } from '@auto-engineer/ai-gateway';
describe('AI Gateway', () => {
it('generates text with specified provider', async () => {
const result = await generateTextWithAI('Hello', {
provider: AIProvider.OpenAI,
});
expect(result).toBeTruthy();
expect(typeof result).toBe('string');
});
});
```
Create or update documentation:
```markdown
This project uses @auto-engineer/ai-gateway for unified AI provider access.
Set environment variables in `.env`:
See `lib/ai.ts` for type-safe wrappers.
See `examples/` for common patterns.
```
1. **Provider Selection**: Default to OpenAI but allow override. Check available providers with `getAvailableProviders()`.
2. **Entry Points**:
- `@auto-engineer/ai-gateway` - Main entry (Node.js wrappers with global context)
- `@auto-engineer/ai-gateway/core` - Pure functions requiring explicit context
- `@auto-engineer/ai-gateway/node` - Full Node.js API including MCP server
3. **Model Selection**: Use `getDefaultModel(provider)` or specify explicitly:
- OpenAI: `gpt-4o`, `gpt-4o-mini`
- Anthropic: `claude-3-5-sonnet-20241022`, `claude-3-5-haiku-20241022`
- Google: `gemini-2.0-flash-exp`, `gemini-1.5-pro`
- XAI: `grok-2-latest`
4. **Streaming**: Use async generators (`streamTextWithAI`) or callbacks (`streamCallback` option).
5. **Type Safety**: Always use Zod schemas with structured data generation for runtime validation and TypeScript inference.
6. **Error Handling**: Implement fallback providers and retry logic for production use.
7. **MCP Tools**: Only use `@auto-engineer/ai-gateway/node` entry point for MCP server functionality.
User: "Add AI text generation to my Node.js app"
→ Generate basic setup with OpenAI provider, config module, and wrapper functions.
User: "I need to extract structured data from user messages"
→ Generate structured data example with Zod schema validation and type-safe wrapper.
User: "Add streaming support for real-time responses"
→ Generate streaming implementation with async generator pattern.
User: "Support multiple AI providers with automatic fallback"
→ Generate provider rotation logic with error handling and retry mechanism.
Leave a review
No reviews yet. Be the first to review this skill!
# Download SKILL.md from killerskills.ai/api/skills/npm-package-ai-gateway-integration/raw