API Endpoints and JSON Schema: 1. /api/ai-handler Description: Handles user input and decides on the appropriate action. Input: { "message": "User's message", "code": "Current code in the editor" } Output: { "response": "AI's text response to the user", "action": "actionType", "actionParams": { "prompt": "Context for the action", "changes": [ {"regex": "pattern", "replacement": "new text"}, ... ] } } 2. /api/generate-code Description: Generates new code based on a prompt. Input: { "prompt": "Description of the code to generate" } Output: { "code": "function countTo1000() {\n for (let i = 1; i <= 1000; i++) {\n console.log(i);\n }\n}" } 3. /api/complete-code Description: Completes or extends existing code. Input: { "prompt": "Description of how to complete the code", "currentCode": "Existing code in the editor" } Output: { "completedCode": "function countTo1000() {\n for (let i = 1; i <= 1000; i++) {\n console.log(i);\n }\n}\n\ncountTo1000();" } 4. /api/refactor-code Description: Refactors existing code based on a prompt. Input: { "prompt": "Description of how to refactor the code", "currentCode": "Existing code in the editor" } Output: { "refactoredCode": "const countTo1000 = () => {\n Array.from({length: 1000}, (_, i) => i + 1).forEach(console.log);\n};" } 5. /api/ai-best-judgment Description: Provides AI's best suggestion for improving the code. Input: { "prompt": "Context for the suggestion", "currentCode": "Existing code in the editor" } Output: { "suggestion": "To optimize the counting function, consider using a more efficient looping method or adding a step parameter for flexibility." } 6. /api/apply-changes Description: Applies specific changes to the existing code. Input: { "changes": [ {"regex": "for \\(let i = 1; i <= 1000; i\\+\\+\\)", "replacement": "for (let i = 1; i <= 1000; i += 2)"} ], "currentCode": "Existing code in the editor" } Output: { "updatedCode": "function countTo1000() {\n for (let i = 1; i <= 1000; i += 2) {\n console.log(i);\n }\n}" } All API responses: - Must be valid JSON - Must contain only raw code or snippet fixes in regex array replace format - No commentary, explanations, or code block formatting in JSON responses - Raw text only, no Markdown or other formatting System: - AI interprets user input, decides action - Each action corresponds to a specific API call - 'applyChanges' action for granular code updates - Debug panel shows all API activity - Error handling for each API call - Extendable: add new actions via ai-handler + new endpoint + new switch case Testing: - Verify all actions update editor content - Monitor debug panel - Test error scenarios Performance & Security: - Monitor API load - Implement caching where appropriate - Sanitize all inputs - Exercise caution when executing generated code