#!/usr/bin/env node /** * Sync Convex generated API to CLI package * * This script copies the generated Convex API files from the backend to the CLI package. * This allows the CLI to use the same typed API as the webapp without manual sync. * * Run this script: * - After running `npx convex dev` or `npx convex deploy` * - Before building the CLI package * - In CI as part of the build process * * Usage: node scripts/sync-cli-api.js */ import fs from 'fs'; import path from 'path'; import { fileURLToPath } from 'url'; const __dirname = path.dirname(fileURLToPath(import.meta.url)); const ROOT_DIR = path.resolve(__dirname, '..'); const SOURCE_DIR = path.join(ROOT_DIR, 'services/backend/convex/_generated'); const TARGET_DIR = path.join(ROOT_DIR, 'packages/cli/src/convex-api'); /** * Files to copy from the generated folder */ const FILES_TO_COPY = ['api.js', 'api.d.ts']; /** * Ensure target directory exists */ function ensureDir(dir) { if (!fs.existsSync(dir)) { fs.mkdirSync(dir, { recursive: true }); console.log(`📁 Created directory: ${dir}`); } } /** * Copy a file with logging */ function copyFile(src, dest) { if (!fs.existsSync(src)) { console.error(`❌ Source file not found: ${src}`); console.error(` Run 'npx convex dev' or 'npx convex deploy' first to generate API files.`); process.exit(1); } fs.copyFileSync(src, dest); console.log(`✅ Copied: ${path.basename(src)}`); } /** * Generate the index.ts that re-exports from api and adds custom types */ function generateIndex() { const indexContent = `/** * Convex API types for CLI * * This file is auto-generated by scripts/sync-cli-api.js * Do not edit directly - run 'pnpm --filter chatroom-cli sync' to regenerate. * * @module */ // Re-export the generated API export { api } from './api.js'; // Type for Convex IDs - matches the Convex GenericId type export type Id = string & { __tableName: TableName }; // Re-export common types used by the CLI export interface Chatroom { _id: Id<'chatroom_rooms'>; status: 'active' | 'interrupted' | 'completed'; ownerId: Id<'users'>; teamId?: string; teamName?: string; teamRoles?: string[]; teamEntryPoint?: string; _creationTime?: number; } export interface Message { _id: Id<'chatroom_messages'>; chatroomId: Id<'chatroom_rooms'>; senderRole: string; content: string; type: 'message' | 'handoff' | 'interrupt'; targetRole?: string; claimedByRole?: string; taskOriginMessageId?: Id<'chatroom_messages'>; taskId?: Id<'chatroom_tasks'>; _creationTime?: number; } export interface Task { _id: Id<'chatroom_tasks'>; chatroomId: Id<'chatroom_rooms'>; createdBy: string; content: string; status: 'pending' | 'in_progress' | 'queued' | 'backlog' | 'completed' | 'cancelled'; assignedTo?: string; sourceMessageId?: Id<'chatroom_messages'>; createdAt: number; updatedAt: number; startedAt?: number; completedAt?: number; queuePosition: number; } export interface TaskWithMessage { task: Task; message: Message | null; } export interface AllowedHandoffRoles { availableRoles: string[]; canHandoffToUser: boolean; restrictionReason: string | null; } export interface ContextWindow { originMessage: Message | null; contextMessages: Message[]; } export interface RolePromptResponse { prompt: string; availableHandoffRoles: string[]; canHandoffToUser: boolean; restrictionReason: string | null; } export interface Participant { _id: Id<'chatroom_participants'>; chatroomId: Id<'chatroom_rooms'>; role: string; status: 'active' | 'waiting' | 'idle'; _creationTime?: number; } export interface TeamReadinessInfo { isReady: boolean; teamName: string; expectedRoles: string[]; presentRoles: string[]; missingRoles: string[]; } // CLI Auth response types export interface AuthRequestResult { requestId: string; expiresAt: number; } export interface AuthRequestStatus { status: 'pending' | 'approved' | 'denied' | 'expired' | 'not_found'; sessionId?: string; expiresAt?: number; } export interface SessionValidation { valid: boolean; userId?: string; userName?: string; reason?: string; } `; const indexPath = path.join(TARGET_DIR, 'index.ts'); fs.writeFileSync(indexPath, indexContent); console.log(`✅ Generated: index.ts`); } /** * Main sync function */ function sync() { console.log(''); console.log('🔄 Syncing Convex API to CLI...'); console.log(` Source: ${SOURCE_DIR}`); console.log(` Target: ${TARGET_DIR}`); console.log(''); // Ensure source directory exists if (!fs.existsSync(SOURCE_DIR)) { console.error(`❌ Source directory not found: ${SOURCE_DIR}`); console.error(` Run 'npx convex dev' or 'npx convex deploy' first.`); process.exit(1); } // Ensure target directory exists ensureDir(TARGET_DIR); // Copy files for (const file of FILES_TO_COPY) { const src = path.join(SOURCE_DIR, file); const dest = path.join(TARGET_DIR, file); copyFile(src, dest); } // Generate index.ts generateIndex(); console.log(''); console.log('✨ Sync complete!'); console.log(''); } // Run sync sync();