Centralized backend for the Digital Intelligence Entity - enabling persistent memory, cross-platform sessions, and GPT-4o integration across all platforms.
- π Cross-Platform Sessions - Same AI entity across VS Code, Web, and Mobile
- π§ Persistent Memory - AI remembers conversations, preferences, and context
- β‘ Real-Time Streaming - WebSocket-based voice and text interaction
- π― Context Injection - Workspace info and memories in every response
- π₯ Multi-User Support - Individual profiles and learning patterns
- π Secure & Scalable - Production-ready with proper authentication
# Clone and install
git clone <your-repo>
cd digital-intelligence-backend
npm install
# Create environment file
cp .env.example .envEdit .env with your settings:
# Required
OPENAI_API_KEY=your_openai_api_key_here
# Database (MongoDB)
MONGODB_URI=mongodb://localhost:27017/digital-intelligence
# Server
PORT=3001
HOST=localhost
NODE_ENV=development# Start MongoDB (if local)
mongod
# Start backend in development mode
npm run dev
# Or build and start production
npm run build
npm startVisit: http://localhost:3001/health
Expected response:
{
"status": "ok",
"services": {
"mongodb": "healthy",
"openai": "healthy"
}
}| Method | Endpoint | Description |
|---|---|---|
GET |
/health |
Health check and service status |
GET |
/api/sessions/:userId/:platform |
Get active session |
POST |
/api/sessions |
Create new session |
POST |
/api/chat |
Send message (non-streaming) |
GET |
/api/stats |
Storage statistics |
| Event | Direction | Description |
|---|---|---|
join_session |
Client β Server | Join a conversation session |
chat_stream |
Client β Server | Send streaming message |
update_context |
Client β Server | Update session context |
assistant_chunk |
Server β Client | Streaming response chunk |
assistant_message |
Server β Client | Complete response |
user_message |
Server β Client | Broadcast user message |
Replace the OpenAI service in your VS Code extension:
// services/backendService.ts
import axios from 'axios';
import { io, Socket } from 'socket.io-client';
export class BackendService {
private socket: Socket;
private baseUrl = 'http://localhost:3001';
constructor() {
this.socket = io(this.baseUrl);
}
async createSession(userId: string, workspaceInfo?: any) {
const response = await axios.post(`${this.baseUrl}/api/sessions`, {
userId,
platform: 'vscode',
workspaceInfo
});
return response.data;
}
async sendStreamingMessage(
sessionId: string,
message: string,
isVoice: boolean,
onChunk: (chunk: string) => void,
onComplete: () => void
) {
this.socket.emit('join_session', { sessionId, userId: 'user_id' });
this.socket.on('assistant_chunk', (data) => {
if (!data.isComplete) {
onChunk(data.content);
} else {
onComplete();
}
});
this.socket.emit('chat_stream', {
sessionId,
message,
isVoice
});
}
}Connect your React/Next.js app:
// hooks/useDigitalIntelligence.js
import { io } from 'socket.io-client';
export function useDigitalIntelligence() {
const socket = io('http://localhost:3001');
const sendMessage = (sessionId, message, isVoice) => {
socket.emit('chat_stream', { sessionId, message, isVoice });
};
const onMessage = (callback) => {
socket.on('assistant_chunk', callback);
};
return { sendMessage, onMessage };
}{
sessionId: string;
userId: string;
platform: 'vscode' | 'web' | 'mobile';
messages: Message[];
workspaceInfo?: {
name: string;
path: string;
activeFiles: string[];
projectType: string;
};
emotionalState: 'neutral' | 'helpful' | 'excited' | 'focused';
metrics: {
totalTokensUsed: number;
averageResponseTime: number;
actionsExecuted: number;
};
}{
userId: string;
type: 'fact' | 'preference' | 'pattern' | 'context';
content: string;
importance: number; // 1-10
confidence: number; // 0-1
tags: string[];
}src/
βββ server.ts # Main Express + WebSocket server
βββ config/
β βββ environment.ts # Environment configuration
βββ models/
β βββ conversation.ts # Data models and schemas
βββ services/
βββ openaiProxy.ts # GPT-4o integration with memory
βββ memoryStore.ts # MongoDB session management
npm run dev # Development with hot reload
npm run build # Build TypeScript to JavaScript
npm start # Start production server
npm run test # Run tests (when implemented)| Variable | Required | Default | Description |
|---|---|---|---|
OPENAI_API_KEY |
β | - | OpenAI API key for GPT-4o |
MONGODB_URI |
β | mongodb://localhost:27017/digital-intelligence |
MongoDB connection string |
PORT |
β | 3001 |
Server port |
NODE_ENV |
β | development |
Environment mode |
CORS_ORIGINS |
β | http://localhost:3000,vscode:// |
Allowed CORS origins |
# Dockerfile
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY dist ./dist
EXPOSE 3001
CMD ["npm", "start"]- Set strong
JWT_SECRET - Configure production MongoDB
- Set up proper CORS origins
- Enable request rate limiting
- Configure logging and monitoring
- Set up SSL/TLS certificates
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
MIT License - Built with β€οΈ for the Digital Intelligence revolution
- Issues: GitHub Issues
- Docs: See
/docsfolder - Health Check:
GET /health
π§ Your Digital Intelligence Entity awaits...