Skip to content

VishalPatil18/cf_ai_codistant

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Codistant Logo

Codistant

AI-powered code assistant built on Cloudflare
Chat with Llama 3.3 70B, get debugging help, and run multi-step code reviews — all with persistent memory.

Cloudflare Workers AI Llama 3.3 Durable Objects Workflows


Features

  • Streaming AI Chat — Real-time streaming responses from Llama 3.3 70B via Workers AI
  • Conversation Memory — Messages persist across page reloads using Durable Objects with SQLite
  • Project Context — Set your project details (e.g., "React + TypeScript app") so the AI gives tailored responses
  • Multi-Step Code Review — Paste code and get a 3-step review via Cloudflare Workflows: structure analysis, issue detection, improvement suggestions
  • Dark/Light Theme — Toggle between themes with Cloudflare-inspired color palette
  • Clear History — Reset your conversation at any time

Architecture

┌──────────────────────┐
│   Browser (Chat UI)  │  Static HTML/CSS/JS
└──────────┬───────────┘
           │
    ┌──────▼──────┐
    │   Worker     │  API Router (src/index.ts)
    └──┬───────┬──┘
       │       │
┌──────▼───┐ ┌─▼─────────────────┐
│  Durable │ │    Workflow        │
│  Object  │ │ (CodeReview)       │
│(Session) │ │                    │
│          │ │ Step 1: Analyze    │
│ SQLite:  │ │ Step 2: Find Issues│
│ messages │ │ Step 3: Suggest    │
│ context  │ │   Improvements     │
└────┬─────┘ └────────┬──────────┘
     │                │
     └───────┬────────┘
             │
      ┌──────▼──────┐
      │  Workers AI  │
      │ Llama 3.3   │
      │    70B       │
      └─────────────┘

Tech Stack

Component Technology
LLM Llama 3.3 70B (@cf/meta/llama-3.3-70b-instruct-fp8-fast) via Workers AI
State/Memory Durable Objects with embedded SQLite
Workflow Cloudflare Workflows (durable execution engine)
Frontend Static HTML/CSS/JS served via Workers Static Assets
Backend Cloudflare Workers (TypeScript)

Prerequisites

Setup & Running

1. Clone the repository

git clone https://github.com/<your-username>/cf_ai_codistant.git
cd cf_ai_codistant

2. Install dependencies

npm install

3. Authenticate with Cloudflare

npx wrangler login

This opens a browser window for OAuth authentication.

4. Run locally

npm run dev

Open http://localhost:8787 in your browser. The local dev server simulates Durable Objects and proxies Workers AI calls to Cloudflare's network.

5. Deploy to production

npm run deploy

Your app will be live at https://cf-ai-codistant.<your-subdomain>.workers.dev.

API Endpoints

Method Path Description
POST /api/chat Send a chat message. Body: { message, sessionId }. Returns SSE stream.
GET /api/history?sessionId=xxx Get conversation history for a session.
DELETE /api/history Clear history. Body: { sessionId }.
POST /api/context Set user context. Body: { sessionId, key, value }.
GET /api/context?sessionId=xxx Get all saved context for a session.
POST /api/review Start a code review workflow. Body: { sessionId, code, language }. Returns { instanceId }.
GET /api/review?instanceId=xxx Poll code review workflow status.

Project Structure

cf_ai_codistant/
├── src/
│   ├── index.ts                  # Worker entry point & API router
│   ├── chat-session.ts           # Durable Object: conversation memory & AI chat
│   ├── code-review-workflow.ts   # Workflow: 3-step code review
│   └── types.ts                  # Shared TypeScript types
├── frontend/
│   ├── index.html                # Chat UI
│   ├── app.js                    # Client-side logic (SSE, sessions, markdown)
│   ├── styles.css                # Cloudflare-themed styles with dark/light mode
│   └── logo.svg                  # Codistant logo
├── wrangler.toml                 # Cloudflare Workers configuration
├── tsconfig.json                 # TypeScript configuration
├── package.json                  # Dependencies and scripts
├── README.md                     # This file
├── PROMPTS.md                    # AI prompts documentation
└── .gitignore

How It Works

Chat Flow

  1. User sends a message from the browser
  2. The Worker routes it to the user's Durable Object (identified by sessionId)
  3. The Durable Object stores the message in SQLite, builds a prompt with system message + project context + conversation history, and calls Workers AI
  4. The AI response streams back via Server-Sent Events (SSE) to the browser
  5. The full response is accumulated in the background and saved to SQLite for future context

Code Review Flow

  1. User pastes code in the sidebar and clicks "Run Code Review"
  2. The Worker creates a Cloudflare Workflow instance
  3. The Workflow runs 3 sequential AI steps (each with automatic retries):
    • Analyze: Examines code structure and architecture
    • Find Issues: Identifies bugs, security issues, and code smells
    • Suggest Improvements: Provides actionable refactoring suggestions
  4. The browser polls for completion and displays the combined report

Memory & State

  • Conversation history: Stored in SQLite within a Durable Object, one instance per sessionId
  • Project context: Key-value pairs stored in SQLite, injected into the system prompt for every AI call
  • Session ID: Generated client-side as a UUID and persisted in localStorage

Local Development Notes

  • wrangler dev runs a local development server that simulates Durable Objects and Workflows
  • Workers AI calls are proxied to Cloudflare's network (requires authentication via wrangler login)
  • SQLite data is stored locally in the .wrangler/ directory during development
  • Changes to source files trigger automatic rebuilds

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages