@morphllm/morphsdk 0.2.193 → 0.2.194

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 (210) hide show
  1. package/dist/{chunk-PI5TUSCU.js → chunk-2JKIWBME.js} +2 -2
  2. package/dist/{chunk-PPABZ2FR.js → chunk-2THF6MNE.js} +2 -2
  3. package/dist/{chunk-Z5Y5WBGI.js → chunk-33WVJLIT.js} +2 -2
  4. package/dist/{chunk-5JZHQGFA.js → chunk-74LJOCDW.js} +2 -2
  5. package/dist/{chunk-BMO22JIM.js → chunk-7KAW6EUG.js} +21 -21
  6. package/dist/{chunk-FLWWY5IP.js → chunk-A2SC5ZOG.js} +2 -2
  7. package/dist/{chunk-FLWWY5IP.js.map → chunk-A2SC5ZOG.js.map} +1 -1
  8. package/dist/{chunk-F3FIVK6A.js → chunk-C225GOV2.js} +2 -2
  9. package/dist/{chunk-OQZOP2PQ.js → chunk-CKSINYRF.js} +2 -2
  10. package/dist/{chunk-362NSH77.js → chunk-DL4EUMBV.js} +2 -2
  11. package/dist/{chunk-N4LGEYGW.js → chunk-EM7BMNSS.js} +2 -2
  12. package/dist/{chunk-HQ3N3YAO.js → chunk-FM2L35XC.js} +2 -2
  13. package/dist/{chunk-ONP3JVU3.js → chunk-GRYLQ2HM.js} +2 -2
  14. package/dist/{chunk-YTQSM7JP.js → chunk-GWXMT3S2.js} +9 -6
  15. package/dist/chunk-GWXMT3S2.js.map +1 -0
  16. package/dist/{chunk-3EVXYGJU.js → chunk-JIHDKRQW.js} +2 -2
  17. package/dist/chunk-KGHHK42W.js +399 -0
  18. package/dist/chunk-KGHHK42W.js.map +1 -0
  19. package/dist/{chunk-LATNDSMS.js → chunk-MFAIFUFK.js} +2 -2
  20. package/dist/{chunk-VLZ6PNAD.js → chunk-OPNTDMHH.js} +4 -4
  21. package/dist/{chunk-SOZM3BG3.js → chunk-PDSLRLYI.js} +3 -3
  22. package/dist/{chunk-MHWNJR5B.js → chunk-POUBLP4R.js} +4 -4
  23. package/dist/{chunk-23KRET2F.js → chunk-PPNJY7P3.js} +3 -3
  24. package/dist/{chunk-GXQXQMVG.js → chunk-PUTYJ2ET.js} +4 -4
  25. package/dist/{chunk-UXSK2O6V.js → chunk-QJSGHJ7D.js} +2 -2
  26. package/dist/{chunk-UEHQJLKC.js → chunk-S3V22W7M.js} +2 -2
  27. package/dist/{chunk-3GYCTQ3B.js → chunk-SU5FOI4R.js} +2 -2
  28. package/dist/{chunk-KGA3RGUQ.js → chunk-TAQLWNIF.js} +2 -2
  29. package/dist/{chunk-A7T7RKSW.js → chunk-TT37PYOQ.js} +2 -2
  30. package/dist/{chunk-TW3IQ357.js → chunk-U74U4ZFN.js} +2 -2
  31. package/dist/{chunk-ZWJUC7YK.js → chunk-UDZBOMRA.js} +3 -3
  32. package/dist/{chunk-WLTBLDXJ.js → chunk-VYDKGM7F.js} +9 -9
  33. package/dist/{chunk-EQI6CRLJ.js → chunk-WP44GN6A.js} +2 -2
  34. package/dist/{chunk-6HVOMKED.js → chunk-WXUMJSGG.js} +2 -2
  35. package/dist/{chunk-2BFAHP3U.js → chunk-XQR2U3LP.js} +3 -3
  36. package/dist/client.cjs +237 -26
  37. package/dist/client.cjs.map +1 -1
  38. package/dist/client.js +29 -29
  39. package/dist/core/client.cjs +1 -1
  40. package/dist/core/client.cjs.map +1 -1
  41. package/dist/core/client.js +4 -4
  42. package/dist/core/error.cjs +1 -1
  43. package/dist/core/error.cjs.map +1 -1
  44. package/dist/core/error.js +3 -3
  45. package/dist/core/index.cjs +1 -1
  46. package/dist/core/index.cjs.map +1 -1
  47. package/dist/core/index.js +4 -4
  48. package/dist/edge.cjs +225 -23
  49. package/dist/edge.cjs.map +1 -1
  50. package/dist/edge.d.ts +1 -1
  51. package/dist/edge.js +9 -7
  52. package/dist/git/client.cjs +1 -1
  53. package/dist/git/client.cjs.map +1 -1
  54. package/dist/git/client.js +5 -5
  55. package/dist/git/index.cjs +1 -1
  56. package/dist/git/index.cjs.map +1 -1
  57. package/dist/git/index.js +5 -5
  58. package/dist/index.cjs +239 -26
  59. package/dist/index.cjs.map +1 -1
  60. package/dist/index.d.ts +1 -1
  61. package/dist/index.js +33 -31
  62. package/dist/modelrouter/core.cjs +1 -1
  63. package/dist/modelrouter/core.cjs.map +1 -1
  64. package/dist/modelrouter/core.js +5 -5
  65. package/dist/modelrouter/index.cjs +1 -1
  66. package/dist/modelrouter/index.cjs.map +1 -1
  67. package/dist/modelrouter/index.js +5 -5
  68. package/dist/subagents/anthropic.cjs +1 -1
  69. package/dist/subagents/anthropic.cjs.map +1 -1
  70. package/dist/subagents/anthropic.js +10 -10
  71. package/dist/subagents/vercel.cjs +1 -1
  72. package/dist/subagents/vercel.cjs.map +1 -1
  73. package/dist/subagents/vercel.js +10 -10
  74. package/dist/tools/browser/anthropic.cjs +1 -1
  75. package/dist/tools/browser/anthropic.cjs.map +1 -1
  76. package/dist/tools/browser/anthropic.js +7 -7
  77. package/dist/tools/browser/core.cjs +1 -1
  78. package/dist/tools/browser/core.cjs.map +1 -1
  79. package/dist/tools/browser/core.js +6 -6
  80. package/dist/tools/browser/index.cjs +1 -1
  81. package/dist/tools/browser/index.cjs.map +1 -1
  82. package/dist/tools/browser/index.js +9 -9
  83. package/dist/tools/browser/openai.cjs +1 -1
  84. package/dist/tools/browser/openai.cjs.map +1 -1
  85. package/dist/tools/browser/openai.js +7 -7
  86. package/dist/tools/browser/profiles/core.cjs +1 -1
  87. package/dist/tools/browser/profiles/core.cjs.map +1 -1
  88. package/dist/tools/browser/profiles/core.js +3 -3
  89. package/dist/tools/browser/profiles/index.cjs +1 -1
  90. package/dist/tools/browser/profiles/index.cjs.map +1 -1
  91. package/dist/tools/browser/profiles/index.js +3 -3
  92. package/dist/tools/browser/vercel.cjs +1 -1
  93. package/dist/tools/browser/vercel.cjs.map +1 -1
  94. package/dist/tools/browser/vercel.js +7 -7
  95. package/dist/tools/codebase_search/anthropic.cjs +1 -1
  96. package/dist/tools/codebase_search/anthropic.cjs.map +1 -1
  97. package/dist/tools/codebase_search/anthropic.js +6 -6
  98. package/dist/tools/codebase_search/core.cjs +1 -1
  99. package/dist/tools/codebase_search/core.cjs.map +1 -1
  100. package/dist/tools/codebase_search/core.js +5 -5
  101. package/dist/tools/codebase_search/index.cjs +1 -1
  102. package/dist/tools/codebase_search/index.cjs.map +1 -1
  103. package/dist/tools/codebase_search/index.js +8 -8
  104. package/dist/tools/codebase_search/openai.cjs +1 -1
  105. package/dist/tools/codebase_search/openai.cjs.map +1 -1
  106. package/dist/tools/codebase_search/openai.js +6 -6
  107. package/dist/tools/codebase_search/vercel.cjs +1 -1
  108. package/dist/tools/codebase_search/vercel.cjs.map +1 -1
  109. package/dist/tools/codebase_search/vercel.js +6 -6
  110. package/dist/tools/compact/core.cjs +1 -1
  111. package/dist/tools/compact/core.cjs.map +1 -1
  112. package/dist/tools/compact/core.js +5 -5
  113. package/dist/tools/compact/index.cjs +1 -1
  114. package/dist/tools/compact/index.cjs.map +1 -1
  115. package/dist/tools/compact/index.js +6 -6
  116. package/dist/tools/fastapply/anthropic.cjs +237 -26
  117. package/dist/tools/fastapply/anthropic.cjs.map +1 -1
  118. package/dist/tools/fastapply/anthropic.js +7 -7
  119. package/dist/tools/fastapply/apply.cjs +225 -23
  120. package/dist/tools/fastapply/apply.cjs.map +1 -1
  121. package/dist/tools/fastapply/apply.d.ts +8 -1
  122. package/dist/tools/fastapply/apply.js +4 -2
  123. package/dist/tools/fastapply/core.cjs +239 -26
  124. package/dist/tools/fastapply/core.cjs.map +1 -1
  125. package/dist/tools/fastapply/core.d.ts +1 -1
  126. package/dist/tools/fastapply/core.js +8 -6
  127. package/dist/tools/fastapply/index.cjs +239 -26
  128. package/dist/tools/fastapply/index.cjs.map +1 -1
  129. package/dist/tools/fastapply/index.d.ts +1 -1
  130. package/dist/tools/fastapply/index.js +11 -9
  131. package/dist/tools/fastapply/openai.cjs +237 -26
  132. package/dist/tools/fastapply/openai.cjs.map +1 -1
  133. package/dist/tools/fastapply/openai.js +7 -7
  134. package/dist/tools/fastapply/types.cjs.map +1 -1
  135. package/dist/tools/fastapply/types.d.ts +10 -2
  136. package/dist/tools/fastapply/vercel.cjs +237 -26
  137. package/dist/tools/fastapply/vercel.cjs.map +1 -1
  138. package/dist/tools/fastapply/vercel.js +7 -7
  139. package/dist/tools/index.cjs +239 -26
  140. package/dist/tools/index.cjs.map +1 -1
  141. package/dist/tools/index.d.ts +1 -1
  142. package/dist/tools/index.js +11 -9
  143. package/dist/tools/reflex/core.cjs +1 -1
  144. package/dist/tools/reflex/core.cjs.map +1 -1
  145. package/dist/tools/reflex/core.js +5 -5
  146. package/dist/tools/reflex/index.cjs +1 -1
  147. package/dist/tools/reflex/index.cjs.map +1 -1
  148. package/dist/tools/reflex/index.js +5 -5
  149. package/dist/tools/utils/resilience.cjs +1 -1
  150. package/dist/tools/utils/resilience.cjs.map +1 -1
  151. package/dist/tools/utils/resilience.js +2 -2
  152. package/dist/tools/warp_grep/agent/runner.cjs +1 -1
  153. package/dist/tools/warp_grep/agent/runner.cjs.map +1 -1
  154. package/dist/tools/warp_grep/agent/runner.js +2 -2
  155. package/dist/tools/warp_grep/anthropic.cjs +1 -1
  156. package/dist/tools/warp_grep/anthropic.cjs.map +1 -1
  157. package/dist/tools/warp_grep/anthropic.js +10 -10
  158. package/dist/tools/warp_grep/client.cjs +1 -1
  159. package/dist/tools/warp_grep/client.cjs.map +1 -1
  160. package/dist/tools/warp_grep/client.js +9 -9
  161. package/dist/tools/warp_grep/gemini.cjs +1 -1
  162. package/dist/tools/warp_grep/gemini.cjs.map +1 -1
  163. package/dist/tools/warp_grep/gemini.js +9 -9
  164. package/dist/tools/warp_grep/harness.js +2 -2
  165. package/dist/tools/warp_grep/index.cjs +1 -1
  166. package/dist/tools/warp_grep/index.cjs.map +1 -1
  167. package/dist/tools/warp_grep/index.js +14 -14
  168. package/dist/tools/warp_grep/openai.cjs +1 -1
  169. package/dist/tools/warp_grep/openai.cjs.map +1 -1
  170. package/dist/tools/warp_grep/openai.js +10 -10
  171. package/dist/tools/warp_grep/providers/local.js +2 -2
  172. package/dist/tools/warp_grep/vercel.cjs +1 -1
  173. package/dist/tools/warp_grep/vercel.cjs.map +1 -1
  174. package/dist/tools/warp_grep/vercel.js +10 -10
  175. package/dist/version.cjs +1 -1
  176. package/dist/version.cjs.map +1 -1
  177. package/dist/version.js +1 -1
  178. package/package.json +1 -1
  179. package/dist/chunk-VPDOBYJE.js +0 -196
  180. package/dist/chunk-VPDOBYJE.js.map +0 -1
  181. package/dist/chunk-YTQSM7JP.js.map +0 -1
  182. /package/dist/{chunk-PI5TUSCU.js.map → chunk-2JKIWBME.js.map} +0 -0
  183. /package/dist/{chunk-PPABZ2FR.js.map → chunk-2THF6MNE.js.map} +0 -0
  184. /package/dist/{chunk-Z5Y5WBGI.js.map → chunk-33WVJLIT.js.map} +0 -0
  185. /package/dist/{chunk-5JZHQGFA.js.map → chunk-74LJOCDW.js.map} +0 -0
  186. /package/dist/{chunk-BMO22JIM.js.map → chunk-7KAW6EUG.js.map} +0 -0
  187. /package/dist/{chunk-F3FIVK6A.js.map → chunk-C225GOV2.js.map} +0 -0
  188. /package/dist/{chunk-OQZOP2PQ.js.map → chunk-CKSINYRF.js.map} +0 -0
  189. /package/dist/{chunk-362NSH77.js.map → chunk-DL4EUMBV.js.map} +0 -0
  190. /package/dist/{chunk-N4LGEYGW.js.map → chunk-EM7BMNSS.js.map} +0 -0
  191. /package/dist/{chunk-HQ3N3YAO.js.map → chunk-FM2L35XC.js.map} +0 -0
  192. /package/dist/{chunk-ONP3JVU3.js.map → chunk-GRYLQ2HM.js.map} +0 -0
  193. /package/dist/{chunk-3EVXYGJU.js.map → chunk-JIHDKRQW.js.map} +0 -0
  194. /package/dist/{chunk-LATNDSMS.js.map → chunk-MFAIFUFK.js.map} +0 -0
  195. /package/dist/{chunk-VLZ6PNAD.js.map → chunk-OPNTDMHH.js.map} +0 -0
  196. /package/dist/{chunk-SOZM3BG3.js.map → chunk-PDSLRLYI.js.map} +0 -0
  197. /package/dist/{chunk-MHWNJR5B.js.map → chunk-POUBLP4R.js.map} +0 -0
  198. /package/dist/{chunk-23KRET2F.js.map → chunk-PPNJY7P3.js.map} +0 -0
  199. /package/dist/{chunk-GXQXQMVG.js.map → chunk-PUTYJ2ET.js.map} +0 -0
  200. /package/dist/{chunk-UXSK2O6V.js.map → chunk-QJSGHJ7D.js.map} +0 -0
  201. /package/dist/{chunk-UEHQJLKC.js.map → chunk-S3V22W7M.js.map} +0 -0
  202. /package/dist/{chunk-3GYCTQ3B.js.map → chunk-SU5FOI4R.js.map} +0 -0
  203. /package/dist/{chunk-KGA3RGUQ.js.map → chunk-TAQLWNIF.js.map} +0 -0
  204. /package/dist/{chunk-A7T7RKSW.js.map → chunk-TT37PYOQ.js.map} +0 -0
  205. /package/dist/{chunk-TW3IQ357.js.map → chunk-U74U4ZFN.js.map} +0 -0
  206. /package/dist/{chunk-ZWJUC7YK.js.map → chunk-UDZBOMRA.js.map} +0 -0
  207. /package/dist/{chunk-WLTBLDXJ.js.map → chunk-VYDKGM7F.js.map} +0 -0
  208. /package/dist/{chunk-EQI6CRLJ.js.map → chunk-WP44GN6A.js.map} +0 -0
  209. /package/dist/{chunk-6HVOMKED.js.map → chunk-WXUMJSGG.js.map} +0 -0
  210. /package/dist/{chunk-2BFAHP3U.js.map → chunk-XQR2U3LP.js.map} +0 -0
@@ -89,7 +89,7 @@ var init_package = __esm({
89
89
  "package.json"() {
90
90
  package_default = {
91
91
  name: "@morphllm/morphsdk",
92
- version: "0.2.193",
92
+ version: "0.2.194",
93
93
  description: "TypeScript SDK and CLI for Morph Fast Apply integration",
94
94
  type: "module",
95
95
  main: "./dist/index.cjs",
@@ -366,9 +366,185 @@ var init_version = __esm({
366
366
  }
367
367
  });
368
368
 
369
+ // tools/fastapply/anchors.ts
370
+ function escapeRegExp(value) {
371
+ return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
372
+ }
373
+ function findNamespace(values) {
374
+ for (let namespace = 0; namespace < Number.MAX_SAFE_INTEGER; namespace++) {
375
+ const prefix = `\u27EAM${namespace}:`;
376
+ if (values.every((value) => !value.includes(prefix))) {
377
+ return namespace;
378
+ }
379
+ }
380
+ throw new Error("Unable to allocate a Fast Apply anchor namespace");
381
+ }
382
+ function htmlLabel(token) {
383
+ if (!token.startsWith("<")) return "text";
384
+ const match = token.match(/^<\s*(!doctype|\/?[a-zA-Z][\w:-]*)/);
385
+ return (match?.[1] ?? "tag").toLowerCase();
386
+ }
387
+ function htmlTokenUnits(value, visibleTags = /* @__PURE__ */ new Set()) {
388
+ return (value.match(/<[^>]*>|[^<]+/g) ?? [value]).map((token) => ({
389
+ value: token,
390
+ label: htmlLabel(token),
391
+ anchorable: token.length >= DEFAULT_MIN_ANCHOR_LINE_LENGTH && !visibleTags.has(htmlLabel(token).replace(/^\//, ""))
392
+ }));
393
+ }
394
+ function sourceUnits(originalCode, editContext) {
395
+ const trimmed = originalCode.trimStart().toLowerCase();
396
+ const looksLikeHtml = (trimmed.startsWith("<!doctype") || trimmed.startsWith("<html")) && /<\/html\s*>/i.test(originalCode);
397
+ if (looksLikeHtml) {
398
+ const contextWords = new Set(
399
+ editContext.join(" ").toLowerCase().match(/[a-z][\w:-]*/g) ?? []
400
+ );
401
+ return htmlTokenUnits(originalCode, contextWords);
402
+ }
403
+ const parts = originalCode.split(/(\r\n|\n|\r)/);
404
+ const units = [];
405
+ for (let index = 0; index < parts.length; index += 2) {
406
+ const line = parts[index] ?? "";
407
+ const lineEnding = parts[index + 1] ?? "";
408
+ units.push({
409
+ value: line,
410
+ label: "line",
411
+ anchorable: line.length >= DEFAULT_MIN_ANCHOR_LINE_LENGTH
412
+ });
413
+ if (lineEnding) {
414
+ units.push({ value: lineEnding, anchorable: false });
415
+ }
416
+ }
417
+ return units;
418
+ }
419
+ function copyCount(value) {
420
+ const words = {
421
+ one: 1,
422
+ two: 2,
423
+ three: 3,
424
+ four: 4,
425
+ five: 5,
426
+ six: 6,
427
+ seven: 7,
428
+ eight: 8,
429
+ nine: 9,
430
+ ten: 10
431
+ };
432
+ return /^\d+$/.test(value) ? Number(value) : words[value.toLowerCase()];
433
+ }
434
+ function structuralExpectation(originalCode, editContext) {
435
+ const context = editContext.join(" ");
436
+ const match = context.match(
437
+ /(?:for every|for each|each)\s+([a-z][\w:-]*)[\s\S]{0,120}?add\s+(one|two|three|four|five|six|seven|eight|nine|ten|\d+)\s+identical cop(?:y|ies)/i
438
+ );
439
+ if (!match) return void 0;
440
+ const tag = match[1].toLowerCase();
441
+ const addedCopies = copyCount(match[2]);
442
+ if (addedCopies === void 0) return void 0;
443
+ const tagPattern = new RegExp(`<${escapeRegExp(tag)}(?:\\s|>)`, "gi");
444
+ const originalCount = originalCode.match(tagPattern)?.length ?? 0;
445
+ if (originalCount === 0) return void 0;
446
+ return { tag, expectedCount: originalCount * (addedCopies + 1) };
447
+ }
448
+ function createAnchorCompactionPlan(originalCode, collisionValues = []) {
449
+ const namespace = findNamespace([originalCode, ...collisionValues]);
450
+ const expectation = structuralExpectation(originalCode, collisionValues);
451
+ const markerPrefix = `\u27EAM${namespace}:`;
452
+ const definitions = /* @__PURE__ */ new Map();
453
+ const values = [];
454
+ const markers = [];
455
+ let anchorCount = 0;
456
+ let skeleton = "";
457
+ for (const unit of sourceUnits(originalCode, collisionValues)) {
458
+ if (!unit.anchorable) {
459
+ skeleton += unit.value;
460
+ continue;
461
+ }
462
+ let id = definitions.get(unit.value);
463
+ if (id === void 0) {
464
+ id = values.length;
465
+ definitions.set(unit.value, id);
466
+ values.push(unit.value);
467
+ markers.push(`${markerPrefix}${id}:${unit.label ?? "unit"}\u27EB`);
468
+ }
469
+ skeleton += markers[id];
470
+ anchorCount++;
471
+ }
472
+ if (anchorCount === 0 || skeleton.length >= originalCode.length) {
473
+ return void 0;
474
+ }
475
+ const definitionsBegin = `\u27EAMORPH_DEFINITIONS_BEGIN_${namespace}\u27EB`;
476
+ const definitionsEnd = `\u27EAMORPH_DEFINITIONS_END_${namespace}\u27EB`;
477
+ const codeBegin = `\u27EAMORPH_CODE_BEGIN_${namespace}\u27EB`;
478
+ const serializedDefinitions = values.map((value, id) => `${markers[id]}=${JSON.stringify(value)}`).join("\n");
479
+ const code = [
480
+ definitionsBegin,
481
+ serializedDefinitions,
482
+ definitionsEnd,
483
+ codeBegin,
484
+ skeleton
485
+ ].join("\n");
486
+ const instructionSuffix = `
487
+
488
+ LARGE-FILE ANCHOR PROTOCOL:
489
+ The <code> block starts with an exact anchor definition table, then ${codeBegin}, then the source skeleton.
490
+ Each ${markerPrefix}N:kind\u27EB placeholder represents the exact source fragment in its matching JSON-string definition. The kind suffix describes its HTML tag, text, or source line.
491
+ Return only the updated source skeleton after ${codeBegin}; never return the definition table or protocol delimiters.
492
+ Copy a placeholder exactly when its source fragment is unchanged. You may repeat, reorder, or remove placeholders when the requested edit requires it.
493
+ To modify an anchored fragment, output the modified source instead of its placeholder.
494
+ Do not invent or alter placeholder IDs.`;
495
+ const markerPattern = new RegExp(`${escapeRegExp(markerPrefix)}(\\d+):[^\u27EB]+\u27EB`, "g");
496
+ return {
497
+ code,
498
+ instructionSuffix,
499
+ anchorCount,
500
+ uniqueAnchorCount: values.length,
501
+ hasStructuralValidation: expectation !== void 0,
502
+ expand(content) {
503
+ const codeBeginIndex = content.lastIndexOf(codeBegin);
504
+ const skeletonContent = codeBeginIndex >= 0 ? content.slice(codeBeginIndex + codeBegin.length).replace(/^\r?\n/, "") : content;
505
+ const expanded = skeletonContent.replace(markerPattern, (marker, idText) => {
506
+ const id = Number(idText);
507
+ const value = values[id];
508
+ return value === void 0 || markers[id] !== marker ? marker : value;
509
+ });
510
+ if (expanded.includes(markerPrefix)) {
511
+ throw new FastApplyAnchorIntegrityError();
512
+ }
513
+ if (expectation) {
514
+ const tagPattern = new RegExp(`<${escapeRegExp(expectation.tag)}(?:\\s|>)`, "gi");
515
+ const actualCount = expanded.match(tagPattern)?.length ?? 0;
516
+ if (actualCount !== expectation.expectedCount) {
517
+ throw new FastApplyAnchorIntegrityError();
518
+ }
519
+ }
520
+ return expanded;
521
+ }
522
+ };
523
+ }
524
+ var DEFAULT_MIN_ANCHOR_LINE_LENGTH, DEFAULT_ANCHOR_COMPACTION_THRESHOLD, FastApplyAnchorIntegrityError;
525
+ var init_anchors = __esm({
526
+ "tools/fastapply/anchors.ts"() {
527
+ "use strict";
528
+ DEFAULT_MIN_ANCHOR_LINE_LENGTH = 24;
529
+ DEFAULT_ANCHOR_COMPACTION_THRESHOLD = 128 * 1024;
530
+ FastApplyAnchorIntegrityError = class extends Error {
531
+ code = "anchor_integrity_failed";
532
+ completionId;
533
+ constructor(completionId) {
534
+ super(
535
+ "Fast Apply failed large-file anchor integrity checks. The partial result was discarded safely."
536
+ );
537
+ this.name = "FastApplyAnchorIntegrityError";
538
+ this.completionId = completionId;
539
+ }
540
+ };
541
+ }
542
+ });
543
+
369
544
  // tools/fastapply/apply.ts
370
545
  var apply_exports = {};
371
546
  __export(apply_exports, {
547
+ FastApplyAnchorIntegrityError: () => FastApplyAnchorIntegrityError,
372
548
  FastApplyContextLengthError: () => FastApplyContextLengthError,
373
549
  applyEdit: () => applyEdit,
374
550
  callMorphAPI: () => callMorphAPI,
@@ -444,8 +620,12 @@ async function callMorphAPI(originalCode, codeEdit, instructions, filepath, conf
444
620
  "Morph API key not found. Set MORPH_API_KEY environment variable or pass morphApiKey in config."
445
621
  );
446
622
  }
447
- const message = `<instruction>${instructions}</instruction>
448
- <code>${originalCode}</code>
623
+ const anchorThreshold = config.anchorCompactionThreshold ?? DEFAULT_ANCHOR_COMPACTION_THRESHOLD;
624
+ const anchorPlan = config.anchorCompaction !== false && originalCode.length >= anchorThreshold ? createAnchorCompactionPlan(originalCode, [codeEdit, instructions]) : void 0;
625
+ const requestCode = anchorPlan?.code ?? originalCode;
626
+ const requestInstructions = anchorPlan ? instructions + anchorPlan.instructionSuffix : instructions;
627
+ const message = `<instruction>${requestInstructions}</instruction>
628
+ <code>${requestCode}</code>
449
629
  <update>${codeEdit}</update>`;
450
630
  logger.debug("FastApply", "http_request", {
451
631
  url: `${apiUrl}/v1/chat/completions`,
@@ -453,7 +633,10 @@ async function callMorphAPI(originalCode, codeEdit, instructions, filepath, conf
453
633
  filepath,
454
634
  instruction_len: instructions.length,
455
635
  original_len: originalCode.length,
456
- code_edit_len: codeEdit.length
636
+ request_code_len: requestCode.length,
637
+ code_edit_len: codeEdit.length,
638
+ anchor_count: anchorPlan?.anchorCount ?? 0,
639
+ unique_anchor_count: anchorPlan?.uniqueAnchorCount ?? 0
457
640
  });
458
641
  const startTime = Date.now();
459
642
  const client = new import_openai.default({
@@ -461,25 +644,49 @@ async function callMorphAPI(originalCode, codeEdit, instructions, filepath, conf
461
644
  baseURL: `${apiUrl}/v1`,
462
645
  timeout,
463
646
  maxRetries: config.retryConfig?.maxRetries ?? 3,
464
- defaultHeaders: { "X-Morph-SDK-Version": SDK_VERSION }
647
+ defaultHeaders: {
648
+ "X-Morph-SDK-Version": SDK_VERSION,
649
+ ...anchorPlan ? { "X-Morph-Apply-Context": "extralong" } : {}
650
+ }
465
651
  });
466
652
  try {
467
- const response = await client.chat.completions.create({
468
- model,
469
- messages: [{ role: "user", content: message }],
470
- stream: true
471
- });
472
- const completion = await collectCompletion(response);
473
- if (completion.finishReason === "length") {
474
- throw new FastApplyContextLengthError(completion.completionId);
475
- }
476
- const content = completion.content;
477
- if (!content) {
478
- throw new Error("Morph API returned empty response");
653
+ const maxAttempts = anchorPlan?.hasStructuralValidation ? 2 : 1;
654
+ for (let attempt = 1; attempt <= maxAttempts; attempt++) {
655
+ const response = await client.chat.completions.create({
656
+ model,
657
+ messages: [{ role: "user", content: message }],
658
+ stream: true
659
+ });
660
+ const completion = await collectCompletion(response);
661
+ if (completion.finishReason === "length") {
662
+ throw new FastApplyContextLengthError(completion.completionId);
663
+ }
664
+ let content = completion.content;
665
+ if (!content) {
666
+ throw new Error("Morph API returned empty response");
667
+ }
668
+ if (anchorPlan) {
669
+ try {
670
+ content = anchorPlan.expand(content);
671
+ } catch (error) {
672
+ if (error instanceof FastApplyAnchorIntegrityError) {
673
+ if (attempt < maxAttempts) {
674
+ logger.warn("FastApply", "anchor_validation_retry", {
675
+ attempt,
676
+ completion_id: completion.completionId
677
+ });
678
+ continue;
679
+ }
680
+ throw new FastApplyAnchorIntegrityError(completion.completionId);
681
+ }
682
+ throw error;
683
+ }
684
+ }
685
+ const elapsed = Date.now() - startTime;
686
+ logger.debug("FastApply", "http_response", { status: 200, completion_id: completion.completionId, content_len: content.length, latency_ms: elapsed });
687
+ return { content, completionId: completion.completionId };
479
688
  }
480
- const elapsed = Date.now() - startTime;
481
- logger.debug("FastApply", "http_response", { status: 200, completion_id: completion.completionId, content_len: content.length, latency_ms: elapsed });
482
- return { content, completionId: completion.completionId };
689
+ throw new FastApplyAnchorIntegrityError();
483
690
  } catch (error) {
484
691
  const elapsed = Date.now() - startTime;
485
692
  const status = error?.status || error?.response?.status;
@@ -529,13 +736,13 @@ async function applyEdit(input, config = {}) {
529
736
  } catch (error) {
530
737
  const errorMessage = error instanceof Error ? error.message : "Unknown error occurred";
531
738
  logger.error("FastApply", "apply_edit_error", { error: errorMessage });
532
- const contextError = error instanceof FastApplyContextLengthError ? error : void 0;
739
+ const structuredError = error instanceof FastApplyContextLengthError || error instanceof FastApplyAnchorIntegrityError ? error : void 0;
533
740
  return {
534
741
  success: false,
535
742
  changes: { linesAdded: 0, linesRemoved: 0, linesModified: 0 },
536
743
  error: errorMessage,
537
- errorCode: contextError?.code,
538
- completionId: contextError?.completionId
744
+ errorCode: structuredError?.code,
745
+ completionId: structuredError?.completionId
539
746
  };
540
747
  }
541
748
  }
@@ -547,6 +754,8 @@ var init_apply = __esm({
547
754
  import_openai = __toESM(require("openai"), 1);
548
755
  init_version();
549
756
  init_logger();
757
+ init_anchors();
758
+ init_anchors();
550
759
  DEFAULT_API_URL = "https://api.morphllm.com";
551
760
  DEFAULT_TIMEOUT = 6e5;
552
761
  FastApplyContextLengthError = class extends Error {
@@ -568,6 +777,7 @@ var fastapply_exports = {};
568
777
  __export(fastapply_exports, {
569
778
  EDIT_FILE_SYSTEM_PROMPT: () => EDIT_FILE_SYSTEM_PROMPT,
570
779
  EDIT_FILE_TOOL_DESCRIPTION: () => EDIT_FILE_TOOL_DESCRIPTION,
780
+ FastApplyAnchorIntegrityError: () => FastApplyAnchorIntegrityError,
571
781
  FastApplyContextLengthError: () => FastApplyContextLengthError,
572
782
  anthropic: () => anthropic_exports,
573
783
  applyEdit: () => applyEdit,
@@ -599,6 +809,8 @@ var DEFAULT_CONFIG = {
599
809
  generateUdiff: true,
600
810
  autoWrite: true,
601
811
  timeout: 6e5,
812
+ anchorCompaction: true,
813
+ anchorCompactionThreshold: 128 * 1024,
602
814
  debug: false
603
815
  };
604
816
  async function executeEditFile(input, config = {}) {
@@ -651,15 +863,15 @@ async function executeEditFile(input, config = {}) {
651
863
  };
652
864
  } catch (error) {
653
865
  const errorMessage = error instanceof Error ? error.message : "Unknown error occurred";
654
- const contextError = error instanceof FastApplyContextLengthError ? error : void 0;
866
+ const structuredError = error instanceof FastApplyContextLengthError || error instanceof FastApplyAnchorIntegrityError ? error : void 0;
655
867
  logger.error("FastApply", "execute_error", { error: errorMessage, target_filepath: input.target_filepath, stack: error instanceof Error ? error.stack : void 0 });
656
868
  return {
657
869
  success: false,
658
870
  filepath: input.target_filepath,
659
871
  changes: { linesAdded: 0, linesRemoved: 0, linesModified: 0 },
660
872
  error: errorMessage,
661
- errorCode: contextError?.code,
662
- completionId: contextError?.completionId
873
+ errorCode: structuredError?.code,
874
+ completionId: structuredError?.completionId
663
875
  };
664
876
  }
665
877
  }
@@ -910,6 +1122,7 @@ var vercel_default = editFileTool3;
910
1122
  0 && (module.exports = {
911
1123
  EDIT_FILE_SYSTEM_PROMPT,
912
1124
  EDIT_FILE_TOOL_DESCRIPTION,
1125
+ FastApplyAnchorIntegrityError,
913
1126
  FastApplyContextLengthError,
914
1127
  anthropic,
915
1128
  applyEdit,