@morphllm/morphsdk 0.2.113 → 0.2.114

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/dist/{chunk-2SOSDQFG.js → chunk-3U7AWFBN.js} +5 -5
  2. package/dist/{chunk-Z6TF5G4R.js → chunk-FXBT4UDY.js} +16 -32
  3. package/dist/chunk-FXBT4UDY.js.map +1 -0
  4. package/dist/{chunk-TGUQHW3I.js → chunk-HMAX2FEE.js} +2 -2
  5. package/dist/{chunk-R3BSIJCL.js → chunk-M5DR2WOZ.js} +1 -1
  6. package/dist/{chunk-A3PK4H7A.js → chunk-UNHHRMU7.js} +2 -2
  7. package/dist/{chunk-JD4H6U3Z.js → chunk-Y5BB7JFH.js} +2 -2
  8. package/dist/client.cjs +93 -119
  9. package/dist/client.cjs.map +1 -1
  10. package/dist/client.js +6 -6
  11. package/dist/edge.cjs +99 -102
  12. package/dist/edge.cjs.map +1 -1
  13. package/dist/edge.js +1 -1
  14. package/dist/index.cjs +93 -119
  15. package/dist/index.cjs.map +1 -1
  16. package/dist/index.js +6 -6
  17. package/dist/tools/fastapply/anthropic.cjs +16 -108
  18. package/dist/tools/fastapply/anthropic.cjs.map +1 -1
  19. package/dist/tools/fastapply/anthropic.js +3 -4
  20. package/dist/tools/fastapply/apply.cjs +25 -102
  21. package/dist/tools/fastapply/apply.cjs.map +1 -1
  22. package/dist/tools/fastapply/apply.d.ts +3 -2
  23. package/dist/tools/fastapply/apply.js +1 -2
  24. package/dist/tools/fastapply/core.cjs +16 -108
  25. package/dist/tools/fastapply/core.cjs.map +1 -1
  26. package/dist/tools/fastapply/core.js +2 -3
  27. package/dist/tools/fastapply/index.cjs +16 -108
  28. package/dist/tools/fastapply/index.cjs.map +1 -1
  29. package/dist/tools/fastapply/index.js +5 -6
  30. package/dist/tools/fastapply/openai.cjs +16 -108
  31. package/dist/tools/fastapply/openai.cjs.map +1 -1
  32. package/dist/tools/fastapply/openai.js +3 -4
  33. package/dist/tools/fastapply/vercel.cjs +16 -108
  34. package/dist/tools/fastapply/vercel.cjs.map +1 -1
  35. package/dist/tools/fastapply/vercel.js +3 -4
  36. package/dist/tools/index.cjs +16 -108
  37. package/dist/tools/index.cjs.map +1 -1
  38. package/dist/tools/index.js +5 -6
  39. package/package.json +1 -1
  40. package/dist/chunk-Z6TF5G4R.js.map +0 -1
  41. /package/dist/{chunk-2SOSDQFG.js.map → chunk-3U7AWFBN.js.map} +0 -0
  42. /package/dist/{chunk-TGUQHW3I.js.map → chunk-HMAX2FEE.js.map} +0 -0
  43. /package/dist/{chunk-R3BSIJCL.js.map → chunk-M5DR2WOZ.js.map} +0 -0
  44. /package/dist/{chunk-A3PK4H7A.js.map → chunk-UNHHRMU7.js.map} +0 -0
  45. /package/dist/{chunk-JD4H6U3Z.js.map → chunk-Y5BB7JFH.js.map} +0 -0
@@ -24,16 +24,16 @@ import {
24
24
  } from "./chunk-WM77HRKO.js";
25
25
  import {
26
26
  createEditFileTool as createEditFileTool3
27
- } from "./chunk-TGUQHW3I.js";
27
+ } from "./chunk-HMAX2FEE.js";
28
28
  import {
29
29
  createEditFileTool as createEditFileTool2
30
- } from "./chunk-JD4H6U3Z.js";
30
+ } from "./chunk-Y5BB7JFH.js";
31
31
  import {
32
32
  createEditFileTool
33
- } from "./chunk-A3PK4H7A.js";
33
+ } from "./chunk-UNHHRMU7.js";
34
34
  import {
35
35
  FastApplyClient
36
- } from "./chunk-R3BSIJCL.js";
36
+ } from "./chunk-M5DR2WOZ.js";
37
37
  import {
38
38
  BrowserClient
39
39
  } from "./chunk-4IOC2D5Y.js";
@@ -643,4 +643,4 @@ export {
643
643
  VercelToolFactory,
644
644
  MorphClient
645
645
  };
646
- //# sourceMappingURL=chunk-2SOSDQFG.js.map
646
+ //# sourceMappingURL=chunk-3U7AWFBN.js.map
@@ -1,10 +1,6 @@
1
- import {
2
- fetchWithRetry,
3
- withTimeout
4
- } from "./chunk-4VWJFZVS.js";
5
-
6
1
  // tools/fastapply/apply.ts
7
2
  import { createTwoFilesPatch } from "diff";
3
+ import OpenAI from "openai";
8
4
  var DEFAULT_API_URL = "https://api.morphllm.com";
9
5
  var DEFAULT_TIMEOUT = 3e4;
10
6
  function generateUdiff(original, modified, filepath) {
@@ -55,37 +51,25 @@ async function callMorphAPI(originalCode, codeEdit, instructions, filepath, conf
55
51
  console.log(`[FastApply] Original: ${originalCode.length} chars, Edit: ${codeEdit.length} chars`);
56
52
  }
57
53
  const startTime = Date.now();
58
- const fetchPromise = fetchWithRetry(
59
- `${apiUrl}/v1/chat/completions`,
60
- {
61
- method: "POST",
62
- headers: {
63
- "Content-Type": "application/json",
64
- "Authorization": `Bearer ${apiKey}`
65
- },
66
- body: JSON.stringify({
67
- model: "morph-v3-fast",
68
- messages: [{ role: "user", content: message }]
69
- })
70
- },
71
- config.retryConfig
72
- );
73
- const response = await withTimeout(
74
- fetchPromise,
54
+ const client = new OpenAI({
55
+ apiKey,
56
+ baseURL: `${apiUrl}/v1`,
75
57
  timeout,
76
- `Morph API request timed out after ${timeout}ms`
77
- );
78
- if (!response.ok) {
79
- const error = await response.text();
80
- if (debug) console.error(`[FastApply] API error: ${response.status} - ${error}`);
81
- throw new Error(`Morph API error (${response.status}): ${error}`);
58
+ maxRetries: config.retryConfig?.maxRetries ?? 3
59
+ });
60
+ const completion = await client.chat.completions.create({
61
+ model: "morph-v3-fast",
62
+ messages: [{ role: "user", content: message }]
63
+ });
64
+ const content = completion.choices[0]?.message?.content;
65
+ if (!content) {
66
+ throw new Error("Morph API returned empty response");
82
67
  }
83
- const data = await response.json();
84
68
  const elapsed = Date.now() - startTime;
85
69
  if (debug) {
86
- console.log(`[FastApply] \u2705 Success in ${elapsed}ms, merged: ${data.choices[0].message.content.length} chars`);
70
+ console.log(`[FastApply] Success in ${elapsed}ms, merged: ${content.length} chars`);
87
71
  }
88
- return { content: data.choices[0].message.content, completionId: data.id };
72
+ return { content, completionId: completion.id };
89
73
  }
90
74
  async function applyEdit(input, config = {}) {
91
75
  const debug = config.debug || false;
@@ -127,4 +111,4 @@ export {
127
111
  callMorphAPI,
128
112
  applyEdit
129
113
  };
130
- //# sourceMappingURL=chunk-Z6TF5G4R.js.map
114
+ //# sourceMappingURL=chunk-FXBT4UDY.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../tools/fastapply/apply.ts"],"sourcesContent":["/**\n * Edge-compatible code application API\n *\n * This module works on:\n * - Node.js\n * - Cloudflare Workers\n * - Vercel Edge Functions\n * - Deno Deploy\n * - Browser environments\n *\n * For file-based operations, use executeEditFile from core.ts\n */\n\nimport { createTwoFilesPatch } from 'diff';\nimport OpenAI from 'openai';\nimport type {\n EditChanges,\n ApplyEditInput,\n ApplyEditResult,\n ApplyEditConfig,\n} from './types.js';\n\nconst DEFAULT_API_URL = 'https://api.morphllm.com';\nconst DEFAULT_TIMEOUT = 30000;\n\n/**\n * Generate a unified diff between two strings\n */\nexport function generateUdiff(\n original: string,\n modified: string,\n filepath: string\n): string {\n return createTwoFilesPatch(\n filepath,\n filepath,\n original,\n modified,\n 'Original',\n 'Modified'\n );\n}\n\n/**\n * Count changes from a unified diff\n */\nexport function countChanges(original: string, modified: string): EditChanges {\n const diff = generateUdiff(original, modified, 'file');\n const lines = diff.split('\\n');\n\n let linesAdded = 0;\n let linesRemoved = 0;\n\n for (const line of lines) {\n if (line.startsWith('+') && !line.startsWith('+++')) {\n linesAdded++;\n } else if (line.startsWith('-') && !line.startsWith('---')) {\n linesRemoved++;\n }\n }\n\n const linesModified = Math.min(linesAdded, linesRemoved);\n\n return {\n linesAdded: linesAdded - linesModified,\n linesRemoved: linesRemoved - linesModified,\n linesModified,\n };\n}\n\n/**\n * Call Morph Apply API to merge code edits\n * Uses OpenAI SDK for reliable connection handling, retries, and timeouts\n */\nexport async function callMorphAPI(\n originalCode: string,\n codeEdit: string,\n instructions: string,\n filepath: string,\n config: ApplyEditConfig\n): Promise<{ content: string; completionId?: string }> {\n const apiKey = config.morphApiKey || (typeof process !== 'undefined' ? process.env?.MORPH_API_KEY : undefined);\n const apiUrl = config.morphApiUrl || DEFAULT_API_URL;\n const timeout = config.timeout || DEFAULT_TIMEOUT;\n const debug = config.debug || false;\n\n if (!apiKey) {\n throw new Error(\n 'Morph API key not found. Set MORPH_API_KEY environment variable or pass morphApiKey in config.'\n );\n }\n\n // Format message with XML tags as per Morph Fast Apply spec\n const message = `<instruction>${instructions}</instruction>\\n<code>${originalCode}</code>\\n<update>${codeEdit}</update>`;\n\n if (debug) {\n console.log(`[FastApply] Calling ${apiUrl}/v1/chat/completions`);\n console.log(`[FastApply] File: ${filepath}, Instructions: ${instructions.slice(0, 60)}...`);\n console.log(`[FastApply] Original: ${originalCode.length} chars, Edit: ${codeEdit.length} chars`);\n }\n\n const startTime = Date.now();\n\n const client = new OpenAI({\n apiKey,\n baseURL: `${apiUrl}/v1`,\n timeout,\n maxRetries: config.retryConfig?.maxRetries ?? 3,\n });\n\n const completion = await client.chat.completions.create({\n model: 'morph-v3-fast',\n messages: [{ role: 'user', content: message }],\n });\n\n const content = completion.choices[0]?.message?.content;\n if (!content) {\n throw new Error('Morph API returned empty response');\n }\n\n const elapsed = Date.now() - startTime;\n\n if (debug) {\n console.log(`[FastApply] Success in ${elapsed}ms, merged: ${content.length} chars`);\n }\n\n return { content, completionId: completion.id };\n}\n\n/**\n * Apply an edit to code directly without file I/O\n *\n * This is the edge-compatible code-in/code-out API that accepts code content directly\n * and returns the merged result without reading or writing any files.\n *\n * Works on Cloudflare Workers, Vercel Edge Functions, Deno, and browsers.\n *\n * @param input - Code and edit parameters\n * @param config - Optional configuration\n * @returns Result with merged code\n *\n * @example\n * ```typescript\n * import { applyEdit } from '@morphllm/morphsdk';\n *\n * const result = await applyEdit({\n * originalCode: fs.readFileSync('file.ts', 'utf-8'),\n * codeEdit: '// ... existing code ...\\nconst newVar = 42;\\n// ... existing code ...',\n * instructions: 'Add a new variable',\n * filepath: 'file.ts' // optional, for udiff context\n * });\n *\n * if (result.success) {\n * fs.writeFileSync('file.ts', result.mergedCode);\n * }\n * ```\n */\nexport async function applyEdit(\n input: ApplyEditInput,\n config: ApplyEditConfig = {}\n): Promise<ApplyEditResult> {\n const debug = config.debug || false;\n const filepath = input.filepath || 'file';\n\n try {\n if (debug) {\n console.log(`[FastApply] Applying edit to code (${input.originalCode.length} chars)`);\n }\n\n const { content: mergedCode, completionId } = await callMorphAPI(\n input.originalCode,\n input.codeEdit,\n input.instructions,\n filepath,\n config\n );\n\n const udiff = config.generateUdiff !== false\n ? generateUdiff(input.originalCode, mergedCode, filepath)\n : undefined;\n\n const changes = countChanges(input.originalCode, mergedCode);\n\n return {\n success: true,\n mergedCode,\n udiff,\n changes,\n completionId,\n };\n } catch (error) {\n const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred';\n if (debug) console.error(`[FastApply] Error: ${errorMessage}`);\n\n return {\n success: false,\n changes: { linesAdded: 0, linesRemoved: 0, linesModified: 0 },\n error: errorMessage,\n };\n }\n}\n"],"mappings":"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","names":[]}
@@ -4,7 +4,7 @@ import {
4
4
  } from "./chunk-63WE2C5R.js";
5
5
  import {
6
6
  executeEditFile
7
- } from "./chunk-R3BSIJCL.js";
7
+ } from "./chunk-M5DR2WOZ.js";
8
8
  import {
9
9
  __export
10
10
  } from "./chunk-PZ5AY32C.js";
@@ -86,4 +86,4 @@ export {
86
86
  vercel_default,
87
87
  vercel_exports
88
88
  };
89
- //# sourceMappingURL=chunk-TGUQHW3I.js.map
89
+ //# sourceMappingURL=chunk-HMAX2FEE.js.map
@@ -112,4 +112,4 @@ export {
112
112
  FastApplyClient,
113
113
  executeEditFile
114
114
  };
115
- //# sourceMappingURL=chunk-R3BSIJCL.js.map
115
+ //# sourceMappingURL=chunk-M5DR2WOZ.js.map
@@ -4,7 +4,7 @@ import {
4
4
  } from "./chunk-63WE2C5R.js";
5
5
  import {
6
6
  executeEditFile
7
- } from "./chunk-R3BSIJCL.js";
7
+ } from "./chunk-M5DR2WOZ.js";
8
8
  import {
9
9
  __export
10
10
  } from "./chunk-PZ5AY32C.js";
@@ -91,4 +91,4 @@ export {
91
91
  openai_default,
92
92
  openai_exports
93
93
  };
94
- //# sourceMappingURL=chunk-A3PK4H7A.js.map
94
+ //# sourceMappingURL=chunk-UNHHRMU7.js.map
@@ -4,7 +4,7 @@ import {
4
4
  } from "./chunk-63WE2C5R.js";
5
5
  import {
6
6
  executeEditFile
7
- } from "./chunk-R3BSIJCL.js";
7
+ } from "./chunk-M5DR2WOZ.js";
8
8
  import {
9
9
  __export
10
10
  } from "./chunk-PZ5AY32C.js";
@@ -79,4 +79,4 @@ export {
79
79
  getSystemPrompt,
80
80
  anthropic_exports
81
81
  };
82
- //# sourceMappingURL=chunk-JD4H6U3Z.js.map
82
+ //# sourceMappingURL=chunk-Y5BB7JFH.js.map
package/dist/client.cjs CHANGED
@@ -30,86 +30,6 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
30
30
  ));
31
31
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
32
32
 
33
- // tools/utils/resilience.ts
34
- async function fetchWithRetry(url, options, retryConfig = {}) {
35
- const {
36
- maxRetries = DEFAULT_RETRY_CONFIG.maxRetries,
37
- initialDelay = DEFAULT_RETRY_CONFIG.initialDelay,
38
- maxDelay = DEFAULT_RETRY_CONFIG.maxDelay,
39
- backoffMultiplier = DEFAULT_RETRY_CONFIG.backoffMultiplier,
40
- retryableErrors = DEFAULT_RETRY_CONFIG.retryableErrors,
41
- onRetry
42
- } = retryConfig;
43
- let lastError = null;
44
- let delay = initialDelay;
45
- for (let attempt = 0; attempt <= maxRetries; attempt++) {
46
- try {
47
- const response = await fetch(url, options);
48
- if (response.status === 429 || response.status === 503) {
49
- if (attempt < maxRetries) {
50
- const retryAfter = response.headers.get("Retry-After");
51
- const waitTime = retryAfter ? parseInt(retryAfter) * 1e3 : Math.min(delay, maxDelay);
52
- const error = new Error(`HTTP ${response.status}: Retrying after ${waitTime}ms`);
53
- if (onRetry) {
54
- onRetry(attempt + 1, error);
55
- }
56
- await sleep(waitTime);
57
- delay *= backoffMultiplier;
58
- continue;
59
- }
60
- }
61
- return response;
62
- } catch (error) {
63
- lastError = error;
64
- const isRetryable = retryableErrors.some(
65
- (errType) => lastError?.message?.includes(errType)
66
- );
67
- if (!isRetryable || attempt === maxRetries) {
68
- throw lastError;
69
- }
70
- const waitTime = Math.min(delay, maxDelay);
71
- if (onRetry) {
72
- onRetry(attempt + 1, lastError);
73
- }
74
- await sleep(waitTime);
75
- delay *= backoffMultiplier;
76
- }
77
- }
78
- throw lastError || new Error("Max retries exceeded");
79
- }
80
- async function withTimeout(promise, timeoutMs, errorMessage) {
81
- let timeoutId;
82
- const timeoutPromise = new Promise((_, reject) => {
83
- timeoutId = setTimeout(() => {
84
- reject(new Error(errorMessage || `Operation timed out after ${timeoutMs}ms`));
85
- }, timeoutMs);
86
- });
87
- try {
88
- const result = await Promise.race([promise, timeoutPromise]);
89
- clearTimeout(timeoutId);
90
- return result;
91
- } catch (error) {
92
- clearTimeout(timeoutId);
93
- throw error;
94
- }
95
- }
96
- function sleep(ms) {
97
- return new Promise((resolve2) => setTimeout(resolve2, ms));
98
- }
99
- var DEFAULT_RETRY_CONFIG;
100
- var init_resilience = __esm({
101
- "tools/utils/resilience.ts"() {
102
- "use strict";
103
- DEFAULT_RETRY_CONFIG = {
104
- maxRetries: 3,
105
- initialDelay: 1e3,
106
- maxDelay: 3e4,
107
- backoffMultiplier: 2,
108
- retryableErrors: ["ECONNREFUSED", "ETIMEDOUT", "ENOTFOUND"]
109
- };
110
- }
111
- });
112
-
113
33
  // tools/fastapply/apply.ts
114
34
  var apply_exports = {};
115
35
  __export(apply_exports, {
@@ -166,37 +86,25 @@ async function callMorphAPI(originalCode, codeEdit, instructions, filepath, conf
166
86
  console.log(`[FastApply] Original: ${originalCode.length} chars, Edit: ${codeEdit.length} chars`);
167
87
  }
168
88
  const startTime = Date.now();
169
- const fetchPromise = fetchWithRetry(
170
- `${apiUrl}/v1/chat/completions`,
171
- {
172
- method: "POST",
173
- headers: {
174
- "Content-Type": "application/json",
175
- "Authorization": `Bearer ${apiKey}`
176
- },
177
- body: JSON.stringify({
178
- model: "morph-v3-fast",
179
- messages: [{ role: "user", content: message }]
180
- })
181
- },
182
- config.retryConfig
183
- );
184
- const response = await withTimeout(
185
- fetchPromise,
89
+ const client = new import_openai.default({
90
+ apiKey,
91
+ baseURL: `${apiUrl}/v1`,
186
92
  timeout,
187
- `Morph API request timed out after ${timeout}ms`
188
- );
189
- if (!response.ok) {
190
- const error = await response.text();
191
- if (debug) console.error(`[FastApply] API error: ${response.status} - ${error}`);
192
- throw new Error(`Morph API error (${response.status}): ${error}`);
93
+ maxRetries: config.retryConfig?.maxRetries ?? 3
94
+ });
95
+ const completion = await client.chat.completions.create({
96
+ model: "morph-v3-fast",
97
+ messages: [{ role: "user", content: message }]
98
+ });
99
+ const content = completion.choices[0]?.message?.content;
100
+ if (!content) {
101
+ throw new Error("Morph API returned empty response");
193
102
  }
194
- const data = await response.json();
195
103
  const elapsed = Date.now() - startTime;
196
104
  if (debug) {
197
- console.log(`[FastApply] \u2705 Success in ${elapsed}ms, merged: ${data.choices[0].message.content.length} chars`);
105
+ console.log(`[FastApply] Success in ${elapsed}ms, merged: ${content.length} chars`);
198
106
  }
199
- return { content: data.choices[0].message.content, completionId: data.id };
107
+ return { content, completionId: completion.id };
200
108
  }
201
109
  async function applyEdit(input, config = {}) {
202
110
  const debug = config.debug || false;
@@ -231,12 +139,12 @@ async function applyEdit(input, config = {}) {
231
139
  };
232
140
  }
233
141
  }
234
- var import_diff, DEFAULT_API_URL, DEFAULT_TIMEOUT;
142
+ var import_diff, import_openai, DEFAULT_API_URL, DEFAULT_TIMEOUT;
235
143
  var init_apply = __esm({
236
144
  "tools/fastapply/apply.ts"() {
237
145
  "use strict";
238
146
  import_diff = require("diff");
239
- init_resilience();
147
+ import_openai = __toESM(require("openai"), 1);
240
148
  DEFAULT_API_URL = "https://api.morphllm.com";
241
149
  DEFAULT_TIMEOUT = 3e4;
242
150
  }
@@ -894,8 +802,81 @@ async function executeEditFile(input, config = {}) {
894
802
  }
895
803
  }
896
804
 
805
+ // tools/utils/resilience.ts
806
+ var DEFAULT_RETRY_CONFIG = {
807
+ maxRetries: 3,
808
+ initialDelay: 1e3,
809
+ maxDelay: 3e4,
810
+ backoffMultiplier: 2,
811
+ retryableErrors: ["ECONNREFUSED", "ETIMEDOUT", "ENOTFOUND"]
812
+ };
813
+ async function fetchWithRetry(url, options, retryConfig = {}) {
814
+ const {
815
+ maxRetries = DEFAULT_RETRY_CONFIG.maxRetries,
816
+ initialDelay = DEFAULT_RETRY_CONFIG.initialDelay,
817
+ maxDelay = DEFAULT_RETRY_CONFIG.maxDelay,
818
+ backoffMultiplier = DEFAULT_RETRY_CONFIG.backoffMultiplier,
819
+ retryableErrors = DEFAULT_RETRY_CONFIG.retryableErrors,
820
+ onRetry
821
+ } = retryConfig;
822
+ let lastError = null;
823
+ let delay = initialDelay;
824
+ for (let attempt = 0; attempt <= maxRetries; attempt++) {
825
+ try {
826
+ const response = await fetch(url, options);
827
+ if (response.status === 429 || response.status === 503) {
828
+ if (attempt < maxRetries) {
829
+ const retryAfter = response.headers.get("Retry-After");
830
+ const waitTime = retryAfter ? parseInt(retryAfter) * 1e3 : Math.min(delay, maxDelay);
831
+ const error = new Error(`HTTP ${response.status}: Retrying after ${waitTime}ms`);
832
+ if (onRetry) {
833
+ onRetry(attempt + 1, error);
834
+ }
835
+ await sleep(waitTime);
836
+ delay *= backoffMultiplier;
837
+ continue;
838
+ }
839
+ }
840
+ return response;
841
+ } catch (error) {
842
+ lastError = error;
843
+ const isRetryable = retryableErrors.some(
844
+ (errType) => lastError?.message?.includes(errType)
845
+ );
846
+ if (!isRetryable || attempt === maxRetries) {
847
+ throw lastError;
848
+ }
849
+ const waitTime = Math.min(delay, maxDelay);
850
+ if (onRetry) {
851
+ onRetry(attempt + 1, lastError);
852
+ }
853
+ await sleep(waitTime);
854
+ delay *= backoffMultiplier;
855
+ }
856
+ }
857
+ throw lastError || new Error("Max retries exceeded");
858
+ }
859
+ async function withTimeout(promise, timeoutMs, errorMessage) {
860
+ let timeoutId;
861
+ const timeoutPromise = new Promise((_, reject) => {
862
+ timeoutId = setTimeout(() => {
863
+ reject(new Error(errorMessage || `Operation timed out after ${timeoutMs}ms`));
864
+ }, timeoutMs);
865
+ });
866
+ try {
867
+ const result = await Promise.race([promise, timeoutPromise]);
868
+ clearTimeout(timeoutId);
869
+ return result;
870
+ } catch (error) {
871
+ clearTimeout(timeoutId);
872
+ throw error;
873
+ }
874
+ }
875
+ function sleep(ms) {
876
+ return new Promise((resolve2) => setTimeout(resolve2, ms));
877
+ }
878
+
897
879
  // tools/codebase_search/core.ts
898
- init_resilience();
899
880
  var CodebaseSearchClient = class {
900
881
  config;
901
882
  constructor(config = {}) {
@@ -990,9 +971,6 @@ async function executeCodebaseSearch(input, config) {
990
971
  }
991
972
  }
992
973
 
993
- // tools/browser/core.ts
994
- init_resilience();
995
-
996
974
  // tools/browser/live.ts
997
975
  var LIVE_PRESETS = {
998
976
  /** Read-only monitoring (no interaction) */
@@ -1093,9 +1071,6 @@ function resolvePreset(optionsOrPreset) {
1093
1071
  return optionsOrPreset;
1094
1072
  }
1095
1073
 
1096
- // tools/browser/profiles/core.ts
1097
- init_resilience();
1098
-
1099
1074
  // tools/browser/errors.ts
1100
1075
  var MorphError = class extends Error {
1101
1076
  /** Error code for programmatic handling */
@@ -3025,7 +3000,7 @@ function enforceContextLimit(messages, maxChars = AGENT_CONFIG.MAX_CONTEXT_CHARS
3025
3000
  }
3026
3001
 
3027
3002
  // tools/warp_grep/agent/runner.ts
3028
- var import_openai = __toESM(require("openai"), 1);
3003
+ var import_openai2 = __toESM(require("openai"), 1);
3029
3004
  var import_path3 = __toESM(require("path"), 1);
3030
3005
  var parser = new LLMResponseParser();
3031
3006
  var DEFAULT_API_URL3 = "https://api.morphllm.com";
@@ -3033,7 +3008,7 @@ async function callModel(messages, model, options = {}) {
3033
3008
  const baseUrl = options.morphApiUrl || DEFAULT_API_URL3;
3034
3009
  const apiKey = options.morphApiKey || process.env.MORPH_API_KEY || "";
3035
3010
  const timeoutMs = options.timeout ?? AGENT_CONFIG.TIMEOUT_MS;
3036
- const client = new import_openai.default({
3011
+ const client = new import_openai2.default({
3037
3012
  apiKey,
3038
3013
  baseURL: `${baseUrl}/v1`,
3039
3014
  maxRetries: options.retryConfig?.maxRetries,
@@ -3048,7 +3023,7 @@ async function callModel(messages, model, options = {}) {
3048
3023
  messages
3049
3024
  });
3050
3025
  } catch (error) {
3051
- if (error instanceof import_openai.default.APIError && error.status === 404) {
3026
+ if (error instanceof import_openai2.default.APIError && error.status === 404) {
3052
3027
  throw new Error(
3053
3028
  "The endpoint you are trying to call is likely deprecated. Please update with: npm cache clean --force && npx -y @morphllm/morphmcp@latest or visit: https://morphllm.com/mcp"
3054
3029
  );
@@ -4555,7 +4530,6 @@ var import_isomorphic_git2 = __toESM(require("isomorphic-git"), 1);
4555
4530
  var import_node2 = __toESM(require("isomorphic-git/http/node"), 1);
4556
4531
 
4557
4532
  // modelrouter/core.ts
4558
- init_resilience();
4559
4533
  var DEFAULT_CONFIG3 = {
4560
4534
  apiUrl: "https://api.morphllm.com",
4561
4535
  timeout: 5e3,