Skip to content

Latest commit

 

History

History
373 lines (296 loc) · 10.4 KB

File metadata and controls

373 lines (296 loc) · 10.4 KB

Building a Production-Ready GitHub Integration with Tambo AI

Introduction

In today's development landscape, integrating with GitHub's API is essential for building developer tools, project management dashboards, and automation workflows. However, creating a robust, type-safe, and user-friendly GitHub integration from scratch can be time-consuming and error-prone.

This template provides a complete, production-ready GitHub integration built with Next.js 15, TypeScript, and Tambo AI. It demonstrates how to create intelligent, conversational interfaces for GitHub data while maintaining clean architecture and solid engineering principles.

What This Template Does

Core Features

🔍 Smart GitHub Data Retrieval

  • List organization repositories with sorting and filtering
  • Fetch repository issues with state management
  • Retrieve pull requests with detailed metadata
  • Natural language intent resolution for GitHub queries

🎨 Beautiful UI Components

  • Responsive repository cards with key metrics
  • Interactive issue and PR cards with status indicators
  • Clean, modern design using Tailwind CSS
  • Accessible components following best practices

🤖 AI-Powered Interactions

  • Natural language processing for GitHub queries
  • Intelligent intent resolution ("Show me React issues" → API calls)
  • Conversational interface powered by Tambo AI
  • Context-aware responses and suggestions

⚡ Production-Ready Architecture

  • Type-safe API layer with Zod validation
  • Modular service architecture
  • Error handling and loading states
  • Optimized for performance and scalability

How It Works

Architecture Overview

The template follows a clean, layered architecture:

src/
├── app/                    # Next.js app router pages
├── components/
│   ├── ui/                # Reusable UI components
│   └── tambo/             # Tambo AI integration components
├── lib/
│   ├── tambo.ts           # Tambo configuration and tools
│   ├── types.ts           # TypeScript type definitions
│   └── utils.ts           # Utility functions
└── services/
    ├── github-api.ts      # GitHub API client
    ├── github-tools.ts    # GitHub tool implementations
    └── resolve-github-intent.ts  # Intent resolution logic

Key Components Explained

1. GitHub API Client (src/services/github-api.ts)

A robust, type-safe wrapper around GitHub's REST API:

class GitHubAPI {
  async getRepositoryIssues({
    owner,
    repo,
    state = "all",
    per_page = 30
  }: IssuesInput): Promise<GitHubIssue[]> {
    // Implementation with error handling and type validation
  }
}

Key Features:

  • Automatic error handling with custom error types
  • Request/response validation using Zod schemas
  • Rate limiting awareness
  • Configurable authentication

2. Intent Resolution (src/services/resolve-github-intent.ts)

Transforms natural language queries into structured API calls:

export async function resolveGitHubIntent(
  input: string,
  fallback_per_page: number = 4
): Promise<GitHubIntent> {
  // Uses AI to parse queries like:
  // "Show me open issues in facebook/react" 
  // → { kind: "list_issues", params: { owner: "facebook", repo: "react", state: "open" }}
}

3. UI Components (src/components/ui/)

Reusable, accessible components for displaying GitHub data:

  • RepoCard: Repository information with stars, forks, and language
  • IssueCard: Issue details with labels, assignees, and status
  • PRCard: Pull request information with merge status and reviews

4. Tambo Integration (src/lib/tambo.ts)

Configures Tambo AI tools and components for the GitHub integration:

export const tools: TamboTool[] = [
  {
    name: "resolveGitHubIntent",
    description: "Resolve GitHub-related queries into structured intents",
    toolSchema: z.object({
      input: z.string().describe("Natural language GitHub query"),
      fallback_per_page: z.number().optional()
    }),
    implementation: resolveGitHubIntent
  },
  // Additional tools...
];

Getting Started

Prerequisites

  • Node.js 18+ or Bun
  • GitHub Personal Access Token
  • Tambo AI account (for AI features)

Setup Process

  1. Clone and Install

    git clone <repository-url>
    cd tambo-github
    bun install  # or npm install
  2. Environment Configuration

    cp .env.example .env

    Add your credentials:

    GITHUB_TOKEN=your_github_token_here
    NEXT_PUBLIC_TAMBO_API_KEY=your_NEXT_PUBLIC_TAMBO_API_KEY_here
  3. Run Development Server

    bun dev  # or npm run dev
  4. Test the Integration

    • Visit http://localhost:3000/test-github
    • Try the example queries:
      • "List repositories for vercel organization"
      • "Show me issues in facebook/react"
      • "Get pull requests for microsoft/vscode"

Authentication Setup

GitHub Token

  1. Go to GitHub Settings → Developer settings → Personal access tokens
  2. Generate a new token with these scopes:
    • public_repo (for public repositories)
    • repo (for private repositories, if needed)
    • read:org (for organization data)

Tambo AI Setup

  1. Sign up at Tambo AI
  2. Create a new project
  3. Copy your API key to the .env file

Customization and Extension

Adding New GitHub Endpoints

  1. Define Types (src/lib/types.ts):

    export const githubCommitSchema = z.object({
      sha: z.string(),
      commit: z.object({
        message: z.string(),
        author: z.object({
          name: z.string(),
          date: z.string()
        })
      })
    });
  2. Add API Method (src/services/github-api.ts):

    async getRepositoryCommits({ owner, repo, per_page = 30 }) {
      const data = await this.request(`/repos/${owner}/${repo}/commits`);
      return data.map(item => githubCommitSchema.parse(item));
    }
  3. Create Tool Wrapper (src/services/github-tools.ts):

    export async function getRepositoryCommits(input: CommitsInput) {
      return githubAPI.getRepositoryCommits(input);
    }
  4. Register with Tambo (src/lib/tambo.ts):

    {
      name: "getRepositoryCommits",
      description: "Get commits for a repository",
      toolSchema: commitsInputSchema,
      implementation: getRepositoryCommits
    }

Creating Custom UI Components

  1. Component Structure:

    interface CommitCardProps {
      commit: GitHubCommit;
      onClick?: () => void;
    }
    
    export function CommitCard({ commit, onClick }: CommitCardProps) {
      return (
        <Card className="cursor-pointer hover:shadow-md transition-shadow">
          {/* Component implementation */}
        </Card>
      );
    }
  2. Register with Tambo:

    export const components: TamboComponent[] = [
      {
        name: "CommitCard",
        component: CommitCard,
        description: "Display GitHub commit information"
      }
    ];

Extending Intent Resolution

Add new intent types by modifying the intent resolution logic:

// Add new intent type
type GitHubIntent = 
  | { kind: "list_repos"; params: { org: string } }
  | { kind: "list_commits"; params: { owner: string; repo: string } }
  | // existing intents...

// Update resolution logic
if (input.includes("commits")) {
  return {
    kind: "list_commits",
    params: { owner: extractedOwner, repo: extractedRepo }
  };
}

Best Practices and Tips

Performance Optimization

  1. Implement Caching:

    const cache = new Map();
    
    async function getCachedRepositories(org: string) {
      const key = `repos:${org}`;
      if (cache.has(key)) return cache.get(key);
      
      const data = await getOrganizationRepositories(org);
      cache.set(key, data);
      return data;
    }
  2. Use React Query for data fetching:

    const { data, isLoading, error } = useQuery({
      queryKey: ['repos', org],
      queryFn: () => getOrganizationRepositories(org),
      staleTime: 5 * 60 * 1000 // 5 minutes
    });

Error Handling

  1. Graceful Degradation:

    try {
      const issues = await getRepositoryIssues(params);
      return issues;
    } catch (error) {
      if (error.status === 404) {
        return { error: "Repository not found" };
      }
      throw error;
    }
  2. User-Friendly Error Messages:

    const errorMessages = {
      401: "Please check your GitHub token",
      403: "Rate limit exceeded. Please try again later",
      404: "Repository or organization not found"
    };

Security Considerations

  1. Environment Variables: Never commit tokens to version control
  2. Rate Limiting: Implement client-side rate limiting
  3. Input Validation: Always validate user inputs
  4. CORS: Configure properly for production deployment

Deployment

Vercel (Recommended)

  1. Connect your GitHub repository to Vercel
  2. Add environment variables in Vercel dashboard
  3. Deploy automatically on push

Docker

FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
RUN npm run build
EXPOSE 3000
CMD ["npm", "start"]

Conclusion

This GitHub integration template provides a solid foundation for building sophisticated developer tools and GitHub-powered applications. The combination of type safety, clean architecture, and AI-powered interactions makes it easy to create powerful, user-friendly experiences.

Key takeaways:

  • Type safety prevents runtime errors and improves developer experience
  • Modular architecture makes the codebase maintainable and extensible
  • AI integration enables natural language interactions with GitHub data
  • Production-ready patterns ensure scalability and reliability

Whether you're building a project dashboard, developer tool, or automation platform, this template gives you everything you need to go from concept to production quickly and confidently.

Resources


Ready to build something amazing? Clone the repository and start exploring the possibilities!