@morphllm/morphmcp
Advanced tools
@@ -14,2 +14,4 @@ declare const DEFAULT_MODEL = "morph-v3-large"; | ||
| instruction?: string; | ||
| retries?: number; | ||
| retryDelay?: number; | ||
| } | ||
@@ -16,0 +18,0 @@ export declare class MorphClient { |
+39
-14
@@ -22,3 +22,3 @@ import axios from 'axios'; | ||
| async applyEdit(originalCode, updateSnippet, options = {}) { | ||
| const { model = DEFAULT_MODEL, timeout = 30000, instruction } = options; | ||
| const { model = DEFAULT_MODEL, timeout = 30000, instruction, retries = 2, retryDelay = 1000 } = options; | ||
| const headers = { | ||
@@ -40,18 +40,43 @@ "Authorization": `Bearer ${this.apiKey}`, | ||
| }; | ||
| try { | ||
| const response = await axios.post(`${this.baseUrl}/chat/completions`, payload, { | ||
| headers, | ||
| timeout, | ||
| }); | ||
| return response.data.choices[0].message.content.trim(); | ||
| } | ||
| catch (error) { | ||
| if (error.response) { | ||
| throw new Error(`Morph API error ${error.response.status}: ${error.response.data?.error?.message || error.response.statusText}`); | ||
| let lastError; | ||
| for (let attempt = 0; attempt <= retries; attempt++) { | ||
| try { | ||
| const response = await axios.post(`${this.baseUrl}/chat/completions`, payload, { | ||
| headers, | ||
| timeout, | ||
| }); | ||
| return response.data.choices[0].message.content.trim(); | ||
| } | ||
| else if (error.request) { | ||
| throw new Error("Failed to connect to Morph API"); | ||
| catch (error) { | ||
| lastError = error; | ||
| // Don't retry on authentication or client errors (4xx) | ||
| if (error.response && error.response.status >= 400 && error.response.status < 500) { | ||
| throw new Error(`Morph API error ${error.response.status}: ${error.response.data?.error?.message || error.response.statusText}`); | ||
| } | ||
| // If we have more retries, wait and try again | ||
| if (attempt < retries) { | ||
| const delay = retryDelay * Math.pow(2, attempt); // Exponential backoff | ||
| console.error(`Morph API request failed (attempt ${attempt + 1}/${retries + 1}), retrying in ${delay}ms...`); | ||
| await new Promise(resolve => setTimeout(resolve, delay)); | ||
| continue; | ||
| } | ||
| } | ||
| throw new Error(`Unexpected error: ${error instanceof Error ? error.message : String(error)}`); | ||
| } | ||
| // All retries exhausted | ||
| if (lastError.response) { | ||
| throw new Error(`Morph API error ${lastError.response.status}: ${lastError.response.data?.error?.message || lastError.response.statusText}`); | ||
| } | ||
| else if (lastError.request) { | ||
| // Network error - no response received | ||
| const errorDetails = []; | ||
| if (lastError.code) | ||
| errorDetails.push(`Code: ${lastError.code}`); | ||
| if (lastError.message) | ||
| errorDetails.push(`Message: ${lastError.message}`); | ||
| const detailsStr = errorDetails.length > 0 ? ` (${errorDetails.join(', ')})` : ''; | ||
| throw new Error(`Failed to connect to Morph API at ${this.baseUrl}${detailsStr}. ` + | ||
| `This may be due to network issues, firewall/proxy settings, or DNS problems. ` + | ||
| `Please check your internet connection and try again. (Failed after ${retries + 1} attempts)`); | ||
| } | ||
| throw new Error(`Unexpected error: ${lastError instanceof Error ? lastError.message : String(lastError)}`); | ||
| } | ||
@@ -58,0 +83,0 @@ /** |
+1
-1
| { | ||
| "name": "@morphllm/morphmcp", | ||
| "version": "0.8.17", | ||
| "version": "0.8.18", | ||
| "description": "Fast & accurate MCP server with AI-powered file editing and intelligent code search. Prevents context pollution and saves time for a better user experience.", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
104851
-99.78%13
-7.14%1832
-34.5%