Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 1x 1x 2x 2x 2x 3x 2x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 2x 2x 2x 2x 1x 1x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 2x 1x 1x 1x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 2x 2x 2x 2x 2x 3x 1x 1x 1x 3x | import { LogData, LogEvent } from "./types/event"; import { Result } from "./types/result"; import { HintRequest, Suggestion } from "./types/suggestion"; import { getSettings } from "../utils/index"; import { trackEvent } from "./log-api"; import { AI_SUGGESTION_ENDPOINT, ANSWER_ENDPOINT, HINT_ENDPOINT, LOG_SUGGESTION_ENDPOINT, EXPLANATION_ENDPOINT, REFINE_PROMPT_ENDPOINT, } from "./types/endpoints"; /** * Fetches AI-generated suggestions based on a given prompt. * * Logs the suggestion generation event with timing information. * * @param prompt - The user-provided prompt. * @returns A promise resolving to an array of suggestions or an error message. */ export async function fetchSuggestions( prompt: string ): Promise<{ suggestions?: string[]; error?: string }> { try { const settings = getSettings(); const startTime = Date.now(); let elapsedTime = null; const vendor = settings["vendor"] || "google"; const model = settings["model"] || "gemini-2.0-flash"; if (!vendor || !model) { console.error("Invalid vendor or model:", vendor, model); return { error: "Invalid vendor or model", }; } const response = await fetch(AI_SUGGESTION_ENDPOINT, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ prompt, vendor, model, }), }); const endTime = Date.now(); elapsedTime = endTime - startTime; if (!response.ok) { return { error: `Error: ${response.status} ${response.statusText}`, }; } const data = await response.json(); if (!data.data) { return { error: "Invalid response: Missing suggestions" }; } const suggestions = data.data?.response; if (!suggestions || suggestions.length === 0) { return { error: "No suggestions found" }; } const logData: LogData = { event: LogEvent.MODEL_GENERATE, timeLapse: elapsedTime, metadata: { suggestions: suggestions, prompt, vendor, model, }, }; trackEvent(logData); return { suggestions: suggestions }; } catch (error) { return { error: error instanceof Error ? error.message : "Unknown error occurred", }; } } /** * Refines a raw user prompt to improve suggestion quality. * * @param rawPrompt - The original raw prompt text. * @returns A promise resolving to a refined prompt or an error. */ export async function refinePrompt( rawPrompt: string ): Promise<{ refinedPrompt?: string; error?: string }> { try { const response = await fetch(REFINE_PROMPT_ENDPOINT, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ rawPrompt }), }); const data = await response.json(); const refinedPrompt = data?.data?.refinedPrompt; if (!response.ok || !refinedPrompt) { return { error: data.error || "Failed to refine prompt", }; } return { refinedPrompt }; } catch (error) { return { error: error instanceof Error ? error.message : "Prompt refinement failed", }; } } /** * Saves an AI-generated suggestion to the backend database. * * @param suggestion - The suggestion metadata to save. * @returns A promise resolving to the result of the operation. */ export async function saveSuggestionToDatabase( suggestion: Suggestion ): Promise<Result<string>> { const body = JSON.stringify(suggestion); try { const response = await fetch(LOG_SUGGESTION_ENDPOINT, { method: "POST", headers: { "Content-Type": "application/json", }, body: body, }); if (!response.ok) { throw new Error(`${response.status} ${response.statusText}`); } const result = await response.json(); return { status: response.status, success: true, data: result.data as string, }; } catch (error: any) { console.error("Error saving suggestion: ", error); return { status: 500, success: false, error: error.message, }; } } /** * Retrieves a hint to help the user correct a buggy code suggestion. * * @param request - The hint request payload (prompt, wrong code, right code). * @returns A promise resolving to the retrieved hint or an error. */ export async function getHint(request: HintRequest): Promise<Result<string>> { const body = JSON.stringify(request); try { const response = await fetch(HINT_ENDPOINT, { method: "POST", headers: { "Content-Type": "application/json", }, body: body, }); if (!response.ok) { throw new Error(`${response.status} ${response.statusText}`); } const result = await response.json(); if (!result || !result.data) { throw new Error("Invalid response from server"); } const { data } = result; return { status: response.status, success: true, data: data.hint as string, }; } catch (error: any) { console.error("Error getting hint: ", error); return { status: 500, success: false, error: error.message, }; } } /** * Retrieves a detailed explanation of why a suggestion may be incorrect. * * @param request - The explanation request payload (prompt, wrong code, right code). * @returns A promise resolving to the retrieved explanation or an error. */ export async function getExplanation( request: HintRequest ): Promise<Result<string>> { const body = JSON.stringify(request); try { const response = await fetch(EXPLANATION_ENDPOINT, { method: "POST", headers: { "Content-Type": "application/json", }, body: body, }); if (!response.ok) { throw new Error(`${response.status} ${response.statusText}`); } const result = await response.json(); if (!result || !result.data) { throw new Error("Invalid response from server"); } const { data } = result; return { status: response.status, success: true, data: data.explanation as string, }; } catch (error: any) { console.error("Error getting explanation: ", error); return { status: 500, success: false, error: error.message, }; } } /** * Submits a user's corrected code against a buggy suggestion to verify correctness. * * @param wrongCode - The original buggy code. * @param fixedCode - The user's corrected version of the code. * @param prompt - The original prompt for context. * @returns A promise resolving to `true` if the fix is correct, otherwise `false`. */ export async function submitCode( wrongCode: string, fixedCode: string, prompt: string ): Promise<boolean> { try { const response = await fetch(ANSWER_ENDPOINT, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ wrongCode, fixedCode, prompt, }), }); const data = await response.json(); console.log("Fix submitted:", data); return data.data.isCorrect; } catch (error) { console.error("Error submitting fix:", error); return false; } } |