UNPKG

@codai/cbd

Version:

Codai Better Database - High-Performance Vector Memory System with HPKV-inspired architecture and MCP server

295 lines 8.14 kB
/** * CBD Client - CND Compatibility Layer with Full METU Support * * Provides CND-compatible interface using CBD Universal Service backend * Maintains backward compatibility while leveraging CBD's multi-paradigm capabilities * Includes full METU device management, conversation tracking, and message handling */ import { EventEmitter } from 'events'; export interface CBDClientConfig { name?: string; cbd?: { host?: string; port?: number; database?: string; memory?: { maxMemory?: number; persistenceInterval?: number; }; }; enterprise?: { authentication?: { enabled?: boolean; jwtSecret?: string; tokenExpiry?: string; }; serviceDiscovery?: { enabled?: boolean; port?: number; healthCheckInterval?: number; }; auditLog?: { enabled?: boolean; logLevel?: string; includeRequestData?: boolean; }; metrics?: { enabled?: boolean; prometheusPort?: number; customMetrics?: string[]; }; security?: { encryption?: { enabled?: boolean; algorithm?: string; }; rateLimit?: { enabled?: boolean; windowMs?: number; maxRequests?: number; }; }; }; auth?: { enabled?: boolean; provider?: string; config?: { secret?: string; }; }; serviceDiscovery?: { enabled?: boolean; serviceName?: string; tags?: string[]; healthCheckInterval?: number; }; security?: { audit?: { enabled?: boolean; logLevel?: string; storage?: string; retentionDays?: number; }; }; } export interface CBDQueryResult { data?: any[]; rows?: any[]; insertId?: number; affectedRows?: number; error?: string; } export interface MetuDevice { id?: string; name: string; type: string; status: 'active' | 'inactive' | 'maintenance' | 'error'; lastSeen: Date; metadata?: Record<string, any>; version?: string; location?: string; capabilities?: string[]; configuration?: Record<string, any>; healthScore?: number; createdAt?: Date; updatedAt?: Date; } export interface MetuConversation { id?: string; deviceId: string; title?: string; status: 'active' | 'completed' | 'archived'; participants?: string[]; metadata?: Record<string, any>; createdAt?: Date; updatedAt?: Date; lastActivity?: Date; messageCount?: number; } export interface MetuMessage { id?: string; conversationId: string; deviceId: string; content: string; type: 'text' | 'image' | 'file' | 'system' | 'error' | 'command'; sender?: string; metadata?: Record<string, any>; createdAt?: Date; processed?: boolean; responseTime?: number; } export declare class CBDClient extends EventEmitter { private baseUrl; private connected; private healthCheckInterval?; private config; private currentDevices; private currentConversations; private isCleaningUp; constructor(config?: CBDClientConfig); /** * Connect to CBD Universal Service * Replaces CND connect() method */ connect(): Promise<void>; /** * Initialize database schema for METU compatibility */ private initializeSchema; /** * Start health monitoring for the CBD connection */ private startHealthMonitoring; /** * Create a new METU device */ createDevice(device: Omit<MetuDevice, 'id' | 'createdAt' | 'updatedAt'>): Promise<string>; /** * Get METU device by ID */ getDevice(deviceId: string): Promise<MetuDevice | null>; /** * Get all METU devices */ getAllDevices(): Promise<MetuDevice[]>; /** * Update METU device */ updateDevice(deviceId: string, updates: Partial<MetuDevice>): Promise<boolean>; /** * Delete METU device */ deleteDevice(deviceId: string): Promise<boolean>; /** * Create a new conversation for a device */ createConversation(deviceId: string, title?: string, metadata?: Record<string, any>): Promise<string>; /** * Get conversation by ID */ getConversation(conversationId: string): Promise<MetuConversation | null>; /** * Get all conversations for a device */ getDeviceConversations(deviceId: string): Promise<MetuConversation[]>; /** * Update conversation */ updateConversation(conversationId: string, updates: Partial<MetuConversation>): Promise<boolean>; /** * Create a new message in a conversation */ createMessage(message: Omit<MetuMessage, 'id' | 'createdAt'>): Promise<string>; /** * Get message by ID */ getMessage(messageId: string): Promise<MetuMessage | null>; /** * Get all messages in a conversation */ getConversationMessages(conversationId: string, limit?: number, offset?: number): Promise<MetuMessage[]>; /** * Get recent messages for a device */ getDeviceMessages(deviceId: string, limit?: number): Promise<MetuMessage[]>; /** * Update message (mark as processed, add response time, etc.) */ updateMessage(messageId: string, updates: Partial<MetuMessage>): Promise<boolean>; /** * Parse device data from database row */ private parseDeviceFromDB; /** * Parse conversation data from database row */ private parseConversationFromDB; /** * Parse message data from database row */ private parseMessageFromDB; /** * Get device statistics */ getDeviceStats(deviceId: string): Promise<any>; /** * Cleanup old data (messages older than specified days) */ cleanup(olderThanDays?: number): Promise<{ deletedMessages: number; deletedConversations: number; }>; /** * Get comprehensive analytics for METU system */ getAnalytics(): Promise<any>; /** * SQL interface that maps to CBD document operations * Maintains CND compatibility while using CBD backend */ sql(): { query: (sql: string, params?: any[]) => Promise<CBDQueryResult>; }; /** * Parse SQL to determine operation and extract table/collection info */ private parseSQLOperation; /** * Handle CREATE TABLE by creating a CBD document collection */ private handleCreateTable; /** * Handle INSERT by adding document to CBD collection */ private handleInsert; /** * Handle SELECT by querying CBD document collection */ private handleSelect; /** * Handle UPDATE by updating CBD document */ private handleUpdate; /** * Handle DELETE by removing CBD document */ private handleDelete; /** * Update device last seen timestamp */ updateDeviceLastSeen(deviceId: string): Promise<void>; /** * Extract insert data from SQL and parameters */ private extractInsertData; /** * Extract WHERE clause from SQL */ private extractWhereClause; /** * Extract LIMIT and OFFSET from SQL */ private extractLimitOffset; /** * Extract UPDATE data from SQL */ private extractUpdateData; /** * Filter documents based on WHERE clause operators */ private filterDocuments; /** * Get health status (replaces CND health methods) */ getHealthStatus(): Promise<any>; } export declare const CND: typeof CBDClient; /** * Factory function for easy CBD client creation */ export declare function createCBDClient(config?: CBDClientConfig): CBDClient; /** * Create METU-compatible CBD client (replaces MetuCNDClient) */ export declare function createMetuCBDClient(config?: CBDClientConfig): CBDClient; //# sourceMappingURL=CBDClient.d.ts.map