AI SDK Tools Documentation

repository·main·Indexed 24 days ago

https://github.com/midday-ai/ai-sdk-tools

A collection of utilities for building production-ready AI applications using the Vercel AI SDK. It provides tools for state management via @ai-sdk-tools/store, multi-agent orchestration with handoffs, structured streaming with @ai-sdk-tools/artifacts, caching for tool executions, persistent memory (including Upstash Redis support), and real-time debugging with AIDevtools.

Tokens
37.2K
Snippets
107
Records
185
Agent score
84%

What's inside AI SDK Tools

  1. Universal Redis Support Requirements

    main

    The caching layer works with any Redis client that implements the following interface:

    • get(key: string): Promise<string | null>
    • set(key: string, value: string): Promise<void>
    • del(key: string): Promise<void>
    • setex?(key: string, seconds: number, value: string): Promise<void> (optional)

    Supported clients include @upstash/redis, redis, ioredis, and any Redis Cluster client.

  2. Compare Drizzle vs Upstash Memory Providers

    main

    Decide which provider to use based on your infrastructure and requirements:

    FeatureDrizzleUpstash
    Type Safety
    PostgreSQL
    MySQL
    SQLite/Turso
    Redis
    Edge Compatible
    ORM Integration
    Existing Schema
    Multi-Database

    Choose Drizzle if:

    • You already use Drizzle in your project.
    • You need PostgreSQL, MySQL, or SQLite support.
    • You want to reuse existing database tables.
    • You prefer type-safe ORM queries.
    • You use Turso for edge SQLite.

    Choose Upstash if:

    • You want Redis for caching/sessions.
    • You need ultra-low latency at the edge.
    • You don't need relational queries.
    • You prefer key-value storage.
  3. Choosing between useArtifact and useArtifacts

    main

    Use useArtifact when:

    • You need to display/work with a specific artifact type.
    • You want detailed status, progress, and error handling for one item.
    • You need type-safe access to a specific artifact's payload.

    Use useArtifacts when:

    • You want to render different artifact types using switch cases (e.g., a Canvas-style UI).
    • You need to listen to all artifacts for logging, analytics, or notifications.
    • You want to show an overview of all available artifacts (e.g., a sidebar list).
  4. How @ai-sdk-tools/ocr works

    main

    The extraction process follows a multi-stage fallback strategy to ensure high accuracy:

    1. Primary Attempt: Uses Mistral OCR with direct PDF/image processing.
    2. Quality Check: Validates if the extracted data meets minimum standards.
    3. Fallback: If the primary fails or quality is low, it attempts Gemini OCR.
    4. OCR Fallback: If vision models fail, it extracts raw text and uses an LLM to structure it.
    5. Result Merging: Combines results from multiple attempts for the best possible accuracy.
  5. How multi-agent orchestration works with Agent handoffs

    main

    You can build intelligent workflows by creating specialized Agent instances. Agents can be configured with a handoff array, allowing one agent to route a conversation to another. This enables complex task delegation where a general agent (like a SupportAgent) can hand off a specific issue to a specialized agent (like a BillingAgent).

    import { Agent } from 'ai-sdk-tools';
    import { openai } from '@ai-sdk/openai';
    
    const supportAgent = new Agent({
      model: openai('gpt-4'),
      name: 'SupportAgent',
      instructions: 'You handle customer support queries.',
    });
    
    const billingAgent = new Agent({
      model: openai('gpt-4'),
      name: 'BillingAgent',
      instructions: 'You handle billing and payment issues.',
      handoff: [supportAgent], // Can hand off back to support
    });
    
    // Support agent can route to billing
    supportAgent.handoff = [billingAgent];
    
    const result = await supportAgent.generateText({
      prompt: 'I need help with my invoice',
    });
  6. Install @ai-sdk-tools/devtools

    main

    Install the @ai-sdk-tools/devtools package to access development tools for debugging AI applications. These tools allow you to inspect tool calls, messages, and execution flow directly within your application.

    npm i @ai-sdk-tools/devtools
  7. Install @ai-sdk-tools/artifacts

    main

    To use the artifacts package, you must install both @ai-sdk-tools/artifacts and @ai-sdk-tools/store.

    • @ai-sdk-tools/artifacts provides the artifact streaming and management APIs.
    • @ai-sdk-tools/store is required for message state management and React hooks. The artifacts package uses the store package's useChatMessages hook to efficiently track artifact data from AI SDK message streams.
    npm install @ai-sdk-tools/artifacts @ai-sdk-tools/store
  8. Install optional memory providers

    main

    Depending on your production environment, you may need to install additional dependencies:

    • Drizzle ORM (for PostgreSQL, MySQL, or SQLite): npm install drizzle-orm
    • Upstash Redis (for serverless/edge environments): npm install @upstash/redis
    • Standard Redis (for self-hosted/traditional environments): npm install redis or npm install ioredis
    npm install drizzle-orm
    # or
    npm install @upstash/redis
    # or
    npm install redis
  9. Install individual AI SDK tools

    main

    If you prefer to minimize your bundle size and only need specific features, you can install the individual packages separately.

    npm install @ai-sdk-tools/agents
    npm install @ai-sdk-tools/artifacts
    npm install @ai-sdk-tools/cache
    npm install @ai-sdk-tools/devtools
    npm install @ai-sdk-tools/memory
    npm install @ai-sdk-tools/store
  10. Configure memory with agents

    main

    When using memory with agents via buildAppContext, the system automatically:

    1. Loads working memory into the system prompt.
    2. Injects the updateWorkingMemory tool.
    3. Captures conversation messages.

    You can configure workingMemory (with a scope of 'chat' or 'user') and history (with a limit).

    import { InMemoryProvider } from "@ai-sdk-tools/memory";
    
    const appContext = buildAppContext({
      userId: "user-123",
      // ... other context
      metadata: {
        chatId: "chat_abc123",
        userId: "user-123",
      },
      memory: {
        provider: new InMemoryProvider(),
        workingMemory: {
          enabled: true,
          scope: "chat", // or 'user'
          template: `# Working Memory\n\n## Key Facts\n- [Important information]\n\n## Preferences\n- [User preferences]\n`,
        },
        history: {
          enabled: true,
          limit: 10,
        },
      },
    });
  11. Install AI SDK Devtools

    main

    Install the core devtools package using npm:

    npm install @ai-sdk-tools/devtools

    Optional Store Integration

    For enhanced state debugging (specifically for debugging Zustand stores via a dedicated State tab), you can optionally install the @ai-sdk-tools/store package. The devtools will automatically detect and integrate with it if available.