# AI Chat Application

A full-stack AI chat application built with @worktif/runtime, featuring a React chat UI, AWS Bedrock integration, DynamoDB session memory, and streaming response support.

## Features

- 🤖 AI-powered chat with AWS Bedrock (Claude models)
- 💬 Real-time streaming responses
- 🧠 Session memory with DynamoDB persistence
- 🎨 Responsive React chat UI
- 🔄 Behavior modes (Precise/Balanced/Creative)
- 🌙 Dark mode support
- 📱 Mobile-friendly design

## Quick Start

### Prerequisites

- Node.js 18+
- AWS CLI configured with credentials
- AWS Bedrock model access enabled

### Installation

```bash
# Install dependencies
npm install

# Start development server
npm run dev
```

### Deployment

```bash
# Deploy to dev stage
npm run deploy:dev

# Deploy to production
npm run deploy:prod
```

## API Endpoints

### POST /chat

Send a chat message and receive an AI response.

**Request:**
```json
{
  "sessionId": "session-abc123",
  "message": "Hello, how are you?",
  "behavior": "balanced"
}
```

**Response:**
```json
{
  "sessionId": "session-abc123",
  "response": "Hello! I'm doing well, thank you for asking...",
  "timestamp": "2024-01-15T10:30:00.000Z"
}
```

### POST /chat/stream

Send a chat message with streaming response (Server-Sent Events).

**Request:** Same as `/chat`

**Response:** SSE stream with chunks:
```
data: {"token": "Hello"}
data: {"token": "!"}
data: {"token": " I'm"}
data: [DONE]
```

### GET /health

Health check endpoint.

**Response:**
```json
{
  "status": "healthy",
  "timestamp": "2024-01-15T10:30:00.000Z"
}
```

## Behavior Modes

| Mode | Temperature | Description |
|------|-------------|-------------|
| Precise | 0.1 | Focused, deterministic responses |
| Balanced | 0.5 | Default mode, balanced creativity |
| Creative | 0.9 | More varied, creative responses |

## Session Management

Sessions are automatically managed using localStorage in the browser:

- **Session ID**: Generated on first visit, persisted across page reloads
- **Behavior Mode**: User preference saved to localStorage
- **Conversation History**: Stored in DynamoDB, retrieved on each request

## Environment Variables

| Variable | Description | Default |
|----------|-------------|---------|
| `AWS_REGION` | AWS region for services | `us-east-1` |
| `BEDROCK_MODEL_ID` | Bedrock model to use | `anthropic.claude-3-sonnet-20240229-v1:0` |
| `MEMORY_TABLE_NAME` | DynamoDB table name | `ai-chat-sessions` |
| `ALLOWED_ORIGIN` | CORS allowed origin | `*` |
| `RUNTIME_ENV_API_URL` | API endpoint URL | (set after deployment) |

## Project Structure

```
src/
├── components/          # React UI components
│   ├── chat/           # Chat-specific components
│   └── ErrorBoundary   # Error handling
├── hooks/              # React hooks
├── services/           # Frontend services
├── backend-services/   # Lambda services
├── handlers/           # Lambda handlers
├── ties/               # DI registration
├── types/              # TypeScript types
├── styles/             # CSS styles
├── App.tsx             # Main app component
└── index.tsx           # Entry point
```

## Development

### Local Development

```bash
npm run dev
```

This starts the development server with hot module replacement.

### Testing

```bash
# Run all tests
npm test

# Run with coverage
npm test:coverage
```

### Building

```bash
npm run build
```

## Troubleshooting

### Bedrock Access Denied

Ensure your AWS account has access to the Bedrock model:
1. Go to AWS Bedrock console
2. Navigate to Model access
3. Request access to Claude models

### CORS Errors

Update `ALLOWED_ORIGIN` in your environment configuration to match your frontend domain.

### Session Not Persisting

Check that:
1. localStorage is enabled in the browser
2. DynamoDB table exists and has correct permissions
