@farm.js/core 0.1.25 → 0.1.27

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 (181) hide show
  1. package/dist/agent-runtime.d.mts +8 -10
  2. package/dist/agent-runtime.d.ts +8 -10
  3. package/dist/api/client.cjs +3 -3
  4. package/dist/api/client.d.mts +4 -5
  5. package/dist/api/client.d.ts +4 -5
  6. package/dist/api/client.mjs +2 -2
  7. package/dist/api/route-manager.cjs +6 -5
  8. package/dist/api/route-manager.d.mts +7 -9
  9. package/dist/api/route-manager.d.ts +7 -9
  10. package/dist/api/route-manager.mjs +5 -4
  11. package/dist/api/runtime.cjs +10 -4
  12. package/dist/api/runtime.d.mts +35 -3
  13. package/dist/api/runtime.d.ts +35 -3
  14. package/dist/api/runtime.mjs +9 -3
  15. package/dist/{api-client-Cx54IR5g.d.mts → api-client-BjMmY8OD.d.mts} +2 -3
  16. package/dist/{api-client-DykpaKcY.d.ts → api-client-CyFtZFJ4.d.ts} +2 -3
  17. package/dist/api.cjs +9 -8
  18. package/dist/api.d.mts +9 -11
  19. package/dist/api.d.ts +9 -11
  20. package/dist/api.mjs +9 -8
  21. package/dist/{app-ZuWrAMc_.d.mts → app-BfzZdjgX.d.mts} +3 -4
  22. package/dist/{app-BF-ctoxh.d.ts → app-CXK7Prrt.d.ts} +3 -4
  23. package/dist/app-markdown.cjs +12 -4
  24. package/dist/app-markdown.d.mts +9 -11
  25. package/dist/app-markdown.d.ts +9 -11
  26. package/dist/app-markdown.mjs +11 -3
  27. package/dist/{chunk-2BIJGZR3.mjs → chunk-3PTVHMLD.mjs} +1 -1
  28. package/dist/{chunk-XME4TCLB.mjs → chunk-4FHDV7VM.mjs} +2 -2
  29. package/dist/{chunk-KLSSO6YD.mjs → chunk-4UNKCNSL.mjs} +1 -1
  30. package/dist/chunk-5MKEDGLD.cjs +53 -0
  31. package/dist/{chunk-E3RDL2DA.cjs → chunk-DQMTHWPI.cjs} +61 -31
  32. package/dist/{chunk-TTVHACQV.cjs → chunk-FOT7LZEO.cjs} +1 -1
  33. package/dist/{chunk-2KI3CL3N.mjs → chunk-FPW4W3VW.mjs} +1 -1
  34. package/dist/{chunk-6K4SPQWL.cjs → chunk-HF5SIACK.cjs} +3 -3
  35. package/dist/{chunk-T2OPSH6S.mjs → chunk-HG237IEY.mjs} +12 -5
  36. package/dist/chunk-J63ZH5OQ.cjs +62 -0
  37. package/dist/{chunk-BD2U4ZL6.cjs → chunk-JQGDQO2I.cjs} +8 -8
  38. package/dist/{chunk-RR6UGYUT.mjs → chunk-JT25KHDI.mjs} +39 -9
  39. package/dist/{chunk-O5DVPF3R.cjs → chunk-KAERLFD3.cjs} +16 -11
  40. package/dist/{chunk-H7CJRJ4L.cjs → chunk-LAHZ7LZA.cjs} +16 -16
  41. package/dist/{chunk-XM4ZDVD7.mjs → chunk-MBFURXAC.mjs} +1 -1
  42. package/dist/{chunk-6VEHUKZ2.mjs → chunk-MM5D6VLP.mjs} +4 -20
  43. package/dist/chunk-MVL2RAJW.mjs +62 -0
  44. package/dist/{chunk-YA6LLTIJ.cjs → chunk-ND224OE2.cjs} +4 -20
  45. package/dist/{chunk-RYDW7GPR.cjs → chunk-NPFGGBM5.cjs} +13 -13
  46. package/dist/{chunk-VZWO3XVL.mjs → chunk-O2LV5XDG.mjs} +2 -2
  47. package/dist/chunk-O6XNAMBV.mjs +53 -0
  48. package/dist/{chunk-N2Z4ZFDE.mjs → chunk-OGHTEYXS.mjs} +22 -14
  49. package/dist/{chunk-U4MP7XTD.mjs → chunk-POXI72VV.mjs} +180 -16
  50. package/dist/{chunk-QGG336NI.mjs → chunk-Q4BHBO6V.mjs} +1 -1
  51. package/dist/{chunk-AXYTGLF3.cjs → chunk-QI7WQSMI.cjs} +4 -4
  52. package/dist/{chunk-WOI2HOGA.mjs → chunk-QQQNJS2E.mjs} +1 -1
  53. package/dist/{chunk-CFNOGDUS.cjs → chunk-TDWFCC2T.cjs} +2 -2
  54. package/dist/{chunk-R7LCG3BT.cjs → chunk-UNSKL6BI.cjs} +89 -81
  55. package/dist/{chunk-WJYCI56T.mjs → chunk-UWTQTZFN.mjs} +52 -33
  56. package/dist/{chunk-5YYJ6Q2L.mjs → chunk-WIVTGKLY.mjs} +7 -2
  57. package/dist/{chunk-65P6OGYN.cjs → chunk-WJKVTJJR.cjs} +12 -5
  58. package/dist/{chunk-OAVTUMJP.cjs → chunk-YXM74XOW.cjs} +9 -9
  59. package/dist/{chunk-QYMRIVG6.cjs → chunk-YZBV7733.cjs} +120 -101
  60. package/dist/{chunk-2ITKVAIF.cjs → chunk-ZSO5GWGD.cjs} +203 -39
  61. package/dist/{client-plugins-DPG0NeZN.d.mts → client-plugins-1GS4zubE.d.mts} +1940 -1677
  62. package/dist/{client-plugins-Fgni-_25.d.ts → client-plugins-B1Za6F5x.d.ts} +1940 -1677
  63. package/dist/client-plugins.d.mts +8 -10
  64. package/dist/client-plugins.d.ts +8 -10
  65. package/dist/client.cjs +4 -4
  66. package/dist/client.d.mts +10 -12
  67. package/dist/client.d.ts +10 -12
  68. package/dist/client.mjs +3 -3
  69. package/dist/config.d.mts +8 -10
  70. package/dist/config.d.ts +8 -10
  71. package/dist/{devtools-I7TBFOKD.mjs → devtools-6RN6KHTW.mjs} +8 -3
  72. package/dist/{devtools-6OA7FEF6.cjs → devtools-CQJ3H6MX.cjs} +9 -4
  73. package/dist/docs.cjs +11 -6
  74. package/dist/docs.mjs +10 -5
  75. package/dist/{endpoint-1zA994xL.d.ts → endpoint-CA-TNX60.d.ts} +1 -1
  76. package/dist/{endpoint-DAc96i93.d.mts → endpoint-CCHFqyTE.d.mts} +1 -1
  77. package/dist/index.cjs +26 -21
  78. package/dist/index.d.mts +16 -18
  79. package/dist/index.d.ts +16 -18
  80. package/dist/index.mjs +26 -21
  81. package/dist/{integration-api-BbaiokCC.d.ts → integration-api-0y4FXJpC.d.mts} +128 -1
  82. package/dist/{integration-api-BbaiokCC.d.mts → integration-api-BPJo5oXb.d.ts} +128 -1
  83. package/dist/integrations.d.mts +8 -10
  84. package/dist/integrations.d.ts +8 -10
  85. package/dist/internal/app-markdown-runtime.cjs +11 -3
  86. package/dist/internal/app-markdown-runtime.d.mts +9 -10
  87. package/dist/internal/app-markdown-runtime.d.ts +9 -10
  88. package/dist/internal/app-markdown-runtime.mjs +10 -2
  89. package/dist/internal/build-runtime.cjs +19 -17
  90. package/dist/internal/build-runtime.d.mts +10 -12
  91. package/dist/internal/build-runtime.d.ts +10 -12
  92. package/dist/internal/build-runtime.mjs +18 -16
  93. package/dist/internal/cli-runtime.cjs +9 -5
  94. package/dist/internal/cli-runtime.mjs +8 -4
  95. package/dist/internal/client-error-overlay.cjs +4 -4
  96. package/dist/internal/client-error-overlay.mjs +1 -1
  97. package/dist/internal/compression-runtime.d.mts +8 -10
  98. package/dist/internal/compression-runtime.d.ts +8 -10
  99. package/dist/internal/config-runtime.cjs +9 -4
  100. package/dist/internal/config-runtime.d.mts +9 -11
  101. package/dist/internal/config-runtime.d.ts +9 -11
  102. package/dist/internal/config-runtime.mjs +8 -3
  103. package/dist/internal/infra-analytics-runtime.cjs +4 -3
  104. package/dist/internal/infra-analytics-runtime.mjs +2 -1
  105. package/dist/internal/product-telemetry-runtime.cjs +2 -2
  106. package/dist/internal/product-telemetry-runtime.mjs +1 -1
  107. package/dist/internal/production-runtime.cjs +8 -5
  108. package/dist/internal/production-runtime.d.mts +16 -16
  109. package/dist/internal/production-runtime.d.ts +16 -16
  110. package/dist/internal/production-runtime.mjs +7 -4
  111. package/dist/markdown.cjs +10 -2
  112. package/dist/markdown.d.mts +35 -153
  113. package/dist/markdown.d.ts +35 -153
  114. package/dist/markdown.mjs +9 -1
  115. package/dist/{metadata-CDnRHKVq.d.ts → metadata-7LKgQEWF.d.ts} +1 -1
  116. package/dist/{metadata-D0EF7esB.d.mts → metadata-B9mCxKgp.d.mts} +1 -1
  117. package/dist/middleware.d.mts +9 -11
  118. package/dist/middleware.d.ts +9 -11
  119. package/dist/navigation.d.mts +10 -12
  120. package/dist/navigation.d.ts +10 -12
  121. package/dist/{nitro-ZKLGOTVY.mjs → nitro-NAKCRVHX.mjs} +4 -3
  122. package/dist/{nitro-KQDU7CFE.cjs → nitro-VYDZGYFY.cjs} +4 -3
  123. package/dist/observability-BupkiN0m.d.mts +447 -0
  124. package/dist/observability-BupkiN0m.d.ts +447 -0
  125. package/dist/observability.d.mts +2 -417
  126. package/dist/observability.d.ts +2 -417
  127. package/dist/plugin.cjs +14 -12
  128. package/dist/plugin.d.mts +9 -11
  129. package/dist/plugin.d.ts +9 -11
  130. package/dist/plugin.mjs +13 -11
  131. package/dist/{production-runtime-DpCt0opU.d.ts → production-runtime-B3xnY6fI.d.ts} +1 -1
  132. package/dist/{production-runtime-x3W4wVqx.d.mts → production-runtime-DramhI0c.d.mts} +1 -1
  133. package/dist/{render-plan-D5oFRVBV.d.ts → render-plan-CpW32tOL.d.ts} +1 -1
  134. package/dist/{render-plan-tazUJalK.d.mts → render-plan-CtQB-zHS.d.mts} +1 -1
  135. package/dist/renderer-client.d.mts +6 -8
  136. package/dist/renderer-client.d.ts +6 -8
  137. package/dist/{request-CzZX0Aew.d.ts → request-BVGGeQv4.d.ts} +1 -1
  138. package/dist/{request-CXmQy5ks.d.mts → request-DlLVBKe3.d.mts} +1 -1
  139. package/dist/request.d.mts +9 -11
  140. package/dist/request.d.ts +9 -11
  141. package/dist/{route-rendering-Dw3C362T.d.ts → route-rendering-BdEldajn.d.ts} +1 -1
  142. package/dist/{route-rendering-B-qpe32B.d.mts → route-rendering-DChYLNdh.d.mts} +1 -1
  143. package/dist/{router-CLl3xHwl.d.ts → router-D0Az-1Nu.d.ts} +1 -1
  144. package/dist/{router-CafM2jlw.d.mts → router-D5htHn_M.d.mts} +1 -1
  145. package/dist/routes.d.mts +6 -8
  146. package/dist/routes.d.ts +6 -8
  147. package/dist/server-fn-client.d.mts +3 -4
  148. package/dist/server-fn-client.d.ts +3 -4
  149. package/dist/server-fn.d.mts +1 -1
  150. package/dist/server-fn.d.ts +1 -1
  151. package/dist/server-plugins.d.mts +8 -10
  152. package/dist/server-plugins.d.ts +8 -10
  153. package/dist/server-query-client.d.mts +1 -1
  154. package/dist/server-query-client.d.ts +1 -1
  155. package/dist/server-query.d.mts +1 -1
  156. package/dist/server-query.d.ts +1 -1
  157. package/dist/server.cjs +292 -141
  158. package/dist/server.d.mts +13 -15
  159. package/dist/server.d.ts +13 -15
  160. package/dist/server.mjs +278 -127
  161. package/dist/{spa-router-6sJfsMlQ.d.mts → spa-router-DVfSJZ5o.d.mts} +2 -2
  162. package/dist/{spa-router-DePr2aZ6.d.ts → spa-router-DmwP_7QL.d.ts} +2 -2
  163. package/dist/testing.cjs +6 -5
  164. package/dist/testing.d.mts +7 -9
  165. package/dist/testing.d.ts +7 -9
  166. package/dist/testing.mjs +5 -4
  167. package/dist/{utils-BPHr-1mP.d.mts → utils-B_Rmr0-n.d.mts} +1 -1
  168. package/dist/{utils-BBKC3cf3.d.ts → utils-Dv0OuLtF.d.ts} +1 -1
  169. package/dist/version.cjs +2 -2
  170. package/dist/version.mjs +1 -1
  171. package/dist/vite.cjs +14 -12
  172. package/dist/vite.d.mts +8 -10
  173. package/dist/vite.d.ts +8 -10
  174. package/dist/vite.mjs +13 -11
  175. package/package.json +1 -1
  176. package/dist/route-B0XrsrFk.d.mts +0 -128
  177. package/dist/route-DOEpbyWZ.d.ts +0 -128
  178. package/dist/{chunk-GDUYXFIS.mjs → chunk-6EG7U4NF.mjs} +3 -3
  179. package/dist/{chunk-YAOX43YI.cjs → chunk-C7OUJ6JI.cjs} +2 -2
  180. package/dist/{chunk-IXTFU4PL.cjs → chunk-NL3ZSMZB.cjs} +2 -2
  181. package/dist/{chunk-L6V3P73N.mjs → chunk-UOXGA6TO.mjs} +4 -4
@@ -27,7 +27,7 @@ import {
27
27
  renderFarmLocaleAlternateLinks,
28
28
  resolveDefaultErrorStatus,
29
29
  resolveFarmApiCatalogInput
30
- } from "./chunk-T2OPSH6S.mjs";
30
+ } from "./chunk-HG237IEY.mjs";
31
31
  import {
32
32
  isFarmPreviewDeploymentEnvironment
33
33
  } from "./chunk-BOYNDCZL.mjs";
@@ -107,7 +107,7 @@ import {
107
107
  resolveFarmLlmsTxtConfig,
108
108
  resolveFarmPerformanceConfig,
109
109
  resolveFarmSecurityConfig
110
- } from "./chunk-RR6UGYUT.mjs";
110
+ } from "./chunk-JT25KHDI.mjs";
111
111
  import {
112
112
  getPublicFarmImageConfig,
113
113
  resolveFarmImageConfig
@@ -125,12 +125,12 @@ import {
125
125
  import {
126
126
  createFarmMarkdownRouteModuleFromFile,
127
127
  loadFarmMdxComponents
128
- } from "./chunk-2BIJGZR3.mjs";
128
+ } from "./chunk-3PTVHMLD.mjs";
129
129
  import {
130
130
  createFarmMarkdownSourceResponse,
131
131
  isFarmMarkdownPageFile,
132
132
  normalizeFarmMarkdownRoutePath
133
- } from "./chunk-XM4ZDVD7.mjs";
133
+ } from "./chunk-MBFURXAC.mjs";
134
134
  import {
135
135
  resolveFarmMdxComponentsModulePath,
136
136
  resolveMdxConfig
@@ -144,7 +144,7 @@ import {
144
144
  getFarmMarkdownMirrorPath,
145
145
  resolveMarkdownConfig,
146
146
  resolveMarkdownMirrorTarget
147
- } from "./chunk-U4MP7XTD.mjs";
147
+ } from "./chunk-POXI72VV.mjs";
148
148
  import {
149
149
  resolveWorkflowsConfig
150
150
  } from "./chunk-3ATMYLLJ.mjs";
@@ -185,7 +185,7 @@ import {
185
185
  } from "./chunk-IOEH4VWN.mjs";
186
186
  import {
187
187
  APIRouteManager
188
- } from "./chunk-QGG336NI.mjs";
188
+ } from "./chunk-Q4BHBO6V.mjs";
189
189
  import {
190
190
  findProgrammaticRouteFiles,
191
191
  loadProgrammaticRouteManifests
@@ -204,7 +204,7 @@ import {
204
204
  } from "./chunk-YOZYCI6Z.mjs";
205
205
  import {
206
206
  isFarmAPIPathname
207
- } from "./chunk-GDUYXFIS.mjs";
207
+ } from "./chunk-6EG7U4NF.mjs";
208
208
  import {
209
209
  resolveFarmAPIServerBasePath
210
210
  } from "./chunk-I6XDAKWQ.mjs";
@@ -274,7 +274,7 @@ import {
274
274
  } from "./chunk-Z2RZJZIZ.mjs";
275
275
  import {
276
276
  FARM_VERSION
277
- } from "./chunk-WOI2HOGA.mjs";
277
+ } from "./chunk-QQQNJS2E.mjs";
278
278
  import {
279
279
  resolveFarmTrailingSlashRedirect,
280
280
  setFarmTrailingSlashPreference
@@ -3042,7 +3042,7 @@ __name(collectDevStylesheetUrls, "collectDevStylesheetUrls");
3042
3042
 
3043
3043
  // src/server/full-document.ts
3044
3044
  var CONTENTS_WRAPPER = /^<div\b[^>]*style="display\s*:\s*contents"[^>]*>/i;
3045
- var HOISTED_HEAD_ELEMENT = /^(?:<(?:meta|link|base)\b[^>]*>|<title\b[^>]*>[\s\S]*?<\/title>)/i;
3045
+ var HOISTED_HEAD_ELEMENT = /^(?:<(?:meta|link|base)\b[^>]*>|<title\b[^>]*>[\s\S]*?<\/title>|<script\b[^>]*\basync\b[^>]*><\/script>|<style\b[^>]*\bdata-precedence\b[^>]*>[\s\S]*?<\/style>)/i;
3046
3046
  function peelDocumentPrefix(markup) {
3047
3047
  let inner = markup.replace(/^\s+/, "");
3048
3048
  let hoisted = "";
@@ -6131,7 +6131,8 @@ var _ServerRenderer = class _ServerRenderer {
6131
6131
  readMarkdown: llmsContext.markdown
6132
6132
  }) : value;
6133
6133
  const response = createFarmMetadataRouteResponse(match.metadata.kind, body, routeModule, {
6134
- method
6134
+ method,
6135
+ request: request2
6135
6136
  });
6136
6137
  await sendWebResponse(res, response);
6137
6138
  } catch (error) {
@@ -6169,7 +6170,7 @@ var _ServerRenderer = class _ServerRenderer {
6169
6170
  kind,
6170
6171
  body,
6171
6172
  { revalidate: config.revalidate },
6172
- { method: req.method }
6173
+ { method: req.method, request: request2 }
6173
6174
  )
6174
6175
  );
6175
6176
  } catch (error) {
@@ -9517,7 +9518,7 @@ function loadFarmWorkflowsDevRuntime() {
9517
9518
  }
9518
9519
  __name(loadFarmWorkflowsDevRuntime, "loadFarmWorkflowsDevRuntime");
9519
9520
  function loadFarmDevtoolsSnapshotRuntime() {
9520
- return import("./devtools-I7TBFOKD.mjs");
9521
+ return import("./devtools-6RN6KHTW.mjs");
9521
9522
  }
9522
9523
  __name(loadFarmDevtoolsSnapshotRuntime, "loadFarmDevtoolsSnapshotRuntime");
9523
9524
  function loadFarmDevtoolsUIRuntime() {
@@ -10818,7 +10819,10 @@ window.__FARM_MANIFEST__ = ${inlineValue({
10818
10819
  res,
10819
10820
  createFarmApiCatalogResponse(
10820
10821
  requestMethod,
10821
- resolveFarmApiCatalogInput({ ...options, api: farmConfig.api })
10822
+ resolveFarmApiCatalogInput({ ...options, api: farmConfig.api }),
10823
+ new Request(parsedRequestUrl, {
10824
+ headers: { "user-agent": String(req.headers["user-agent"] ?? "") }
10825
+ })
10822
10826
  )
10823
10827
  );
10824
10828
  return;
@@ -10880,7 +10884,10 @@ window.__FARM_MANIFEST__ = ${inlineValue({
10880
10884
  }),
10881
10885
  config: farmApp.getConfig().md,
10882
10886
  routeExists: /* @__PURE__ */ __name((pathname2) => Boolean(farmApp.getRouteManager().matchRoute(pathname2).route), "routeExists"),
10883
- renderPage: /* @__PURE__ */ __name(async (request2) => fetch(request2), "renderPage")
10887
+ // Keep the page's own redirect (a sign-in gate, say) so the mirror
10888
+ // answers with it, as production does, instead of the target page.
10889
+ renderPage: /* @__PURE__ */ __name(async (request2) => fetch(request2, { redirect: "manual" }), "renderPage"),
10890
+ apiServerBasePath
10884
10891
  });
10885
10892
  if (markdownResponse) {
10886
10893
  if (await runAppMiddlewareForContentRoute()) return;
@@ -14424,6 +14431,7 @@ export {
14424
14431
  createMarkdownPageHydrationPlanner,
14425
14432
  enforceFarmIsolatedHydrationRouteBudget,
14426
14433
  RouteManager,
14434
+ HOISTED_HEAD_ELEMENT,
14427
14435
  resolveFarmNotFoundComponentPath,
14428
14436
  farmFontImportsPlugin,
14429
14437
  mergeFarmFontCss,
@@ -1,3 +1,12 @@
1
+ import {
2
+ emitFarmAgentRead
3
+ } from "./chunk-MVL2RAJW.mjs";
4
+ import {
5
+ _findFarmMCPEndpointTool
6
+ } from "./chunk-O6XNAMBV.mjs";
7
+ import {
8
+ stripFarmBasePath
9
+ } from "./chunk-4RB44K6Z.mjs";
1
10
  import {
2
11
  __name
3
12
  } from "./chunk-LPEX4YW6.mjs";
@@ -39,10 +48,12 @@ function parseFarmMarkdownHeadings(markdown) {
39
48
  end: lines.length
40
49
  });
41
50
  }
42
- for (let headingIndex = 0; headingIndex < headings.length; headingIndex++) {
43
- const heading = headings[headingIndex];
44
- const next = headings.slice(headingIndex + 1).find((candidate) => candidate.level <= heading.level);
45
- if (next) heading.end = next.start;
51
+ const open = [];
52
+ for (const heading of headings) {
53
+ while (open.length > 0 && open[open.length - 1].level >= heading.level) {
54
+ open.pop().end = heading.start;
55
+ }
56
+ open.push(heading);
46
57
  }
47
58
  return headings;
48
59
  }
@@ -55,23 +66,49 @@ function estimateFarmMarkdownTokens(text) {
55
66
  return Math.ceil(text.length / 4);
56
67
  }
57
68
  __name(estimateFarmMarkdownTokens, "estimateFarmMarkdownTokens");
69
+ var FARM_MARKDOWN_MIN_TOKEN_BUDGET = 100;
70
+ var FARM_MARKDOWN_VIEW_PARAMS = [
71
+ "outline",
72
+ "section",
73
+ "actions",
74
+ "max_tokens",
75
+ "cursor"
76
+ ];
58
77
  function selectFarmMarkdownView(markdown, searchParams, markdownPath) {
59
78
  const wantsOutline = searchParams.has("outline");
60
79
  const sectionId = searchParams.get("section");
61
- if (!wantsOutline && sectionId === null) return { status: 200, body: markdown };
80
+ const budget = searchParams.get("max_tokens");
81
+ if (!wantsOutline && sectionId === null && budget === null)
82
+ return { status: 200, body: markdown };
83
+ const viewUrl = /* @__PURE__ */ __name((view) => {
84
+ const query = new URLSearchParams(searchParams);
85
+ for (const name of FARM_MARKDOWN_VIEW_PARAMS) query.delete(name);
86
+ for (const [name, value] of Object.entries(view)) {
87
+ if (value !== null && value !== void 0) query.set(name, value);
88
+ }
89
+ const search = query.toString();
90
+ return search ? `${markdownPath}?${search}` : markdownPath;
91
+ }, "viewUrl");
92
+ const withinBudget = /* @__PURE__ */ __name((document) => budget === null ? { status: 200, body: document } : selectFarmMarkdownPart(
93
+ document,
94
+ budget,
95
+ searchParams.get("cursor"),
96
+ (cursor) => viewUrl({ section: sectionId, max_tokens: budget, cursor })
97
+ ), "withinBudget");
98
+ if (!wantsOutline && sectionId === null) return withinBudget(markdown);
62
99
  const lines = markdown.split("\n");
63
100
  const headings = parseFarmMarkdownHeadings(markdown);
64
101
  const sectionText = /* @__PURE__ */ __name((heading) => lines.slice(heading.start, heading.end).join("\n").trim(), "sectionText");
65
102
  if (sectionId !== null) {
66
103
  const heading = headings.find((candidate) => candidate.id === sectionId);
67
- if (heading) return { status: 200, body: `${sectionText(heading)}
68
- ` };
104
+ if (heading) return withinBudget(`${sectionText(heading)}
105
+ `);
69
106
  const available = headings.map((candidate) => `- \`${candidate.id}\``).join("\n");
70
107
  return {
71
108
  status: 404,
72
109
  body: `# Section not found
73
110
 
74
- \`${sectionId}\` is not a section of ${markdownPath}.
111
+ ${markdownCodeSpan(sectionId)} is not a section of ${markdownPath}.
75
112
 
76
113
  ` + (available ? `Available sections:
77
114
 
@@ -79,23 +116,31 @@ ${available}
79
116
  ` : "This page has no sections.\n")
80
117
  };
81
118
  }
82
- const minLevel = Math.min(...headings.map((heading) => heading.level));
119
+ const minLevel = headings.reduce((min, heading) => Math.min(min, heading.level), 6);
83
120
  const entries = headings.map((heading) => {
84
121
  const indent = " ".repeat(heading.level - minLevel);
85
122
  const tokens = estimateFarmMarkdownTokens(sectionText(heading));
86
- return `${indent}- [${heading.text}](${markdownPath}?section=${encodeURIComponent(heading.id)}) ~${tokens} tokens`;
123
+ return `${indent}- [${heading.text}](${viewUrl({ section: heading.id })}) ~${tokens} tokens`;
87
124
  });
88
125
  return {
89
126
  status: 200,
90
127
  body: `# Outline of ${markdownPath}
91
128
 
92
- About ${estimateFarmMarkdownTokens(markdown)} tokens in total. Request one section with \`?section=<id>\`.
129
+ About ${estimateFarmMarkdownTokens(markdown)} tokens in total. Request one section with \`?section=<id>\`, or read in parts with \`?max_tokens=<n>\`.
93
130
 
94
131
  ` + (entries.length > 0 ? `${entries.join("\n")}
95
132
  ` : "This page has no headings.\n")
96
133
  };
97
134
  }
98
135
  __name(selectFarmMarkdownView, "selectFarmMarkdownView");
136
+ function markdownCodeSpan(value) {
137
+ const text = value.replace(/[\p{Cc}\u2028\u2029]+/gu, " ");
138
+ const longestRun = (text.match(/`+/g) ?? []).reduce((max, run) => Math.max(max, run.length), 0);
139
+ const fence = "`".repeat(longestRun + 1);
140
+ const pad = text.startsWith("`") || text.endsWith("`") ? " " : "";
141
+ return `${fence}${pad}${text}${pad}${fence}`;
142
+ }
143
+ __name(markdownCodeSpan, "markdownCodeSpan");
99
144
  function selectFarmPageActionsView(markdownPath, actions, headers) {
100
145
  headers.set("Content-Type", "application/json; charset=utf-8");
101
146
  const page = markdownPath === "/index.md" ? "/" : markdownPath.replace(/\.md$/, "");
@@ -106,7 +151,117 @@ function selectFarmPageActionsView(markdownPath, actions, headers) {
106
151
  };
107
152
  }
108
153
  __name(selectFarmPageActionsView, "selectFarmPageActionsView");
154
+ function selectFarmMarkdownPart(document, rawBudget, rawCursor, partUrl) {
155
+ const budget = /^\d{1,9}$/.test(rawBudget) ? Number(rawBudget) : Number.NaN;
156
+ if (!(budget >= FARM_MARKDOWN_MIN_TOKEN_BUDGET)) {
157
+ return {
158
+ status: 400,
159
+ body: `# Invalid max_tokens
160
+
161
+ \`max_tokens\` must be a whole number of at least ${FARM_MARKDOWN_MIN_TOKEN_BUDGET}.
162
+ `
163
+ };
164
+ }
165
+ const lines = document.replace(/\n$/, "").split("\n");
166
+ const version = hashFarmMarkdown(document).slice(0, 8);
167
+ let start = 0;
168
+ if (rawCursor !== null) {
169
+ const match = /^(\d{1,9})\.([0-9a-z]+)$/.exec(rawCursor);
170
+ if (!match || Number(match[1]) <= 0 || Number(match[1]) >= lines.length) {
171
+ return {
172
+ status: 400,
173
+ body: `# Invalid cursor
174
+
175
+ Start again at ${partUrl()}.
176
+ `
177
+ };
178
+ }
179
+ if (match[2] !== version) {
180
+ return {
181
+ status: 409,
182
+ body: `# Page changed
183
+
184
+ The page changed after this cursor was issued. Start again at ${partUrl()}.
185
+ `
186
+ };
187
+ }
188
+ start = Number(match[1]);
189
+ }
190
+ const fences = readOpenFences(lines);
191
+ const reopen = fences[start];
192
+ const room = budget * 4 - 200;
193
+ let size = reopen ? reopen.opener.length + 1 : 0;
194
+ let end = start;
195
+ let lastBreak = -1;
196
+ while (end < lines.length) {
197
+ const length = lines[end].length + 1;
198
+ if (end > start && size + length > room) break;
199
+ size += length;
200
+ end++;
201
+ if (end < lines.length && isPartBreak(lines, fences, end)) lastBreak = end;
202
+ }
203
+ const head = reopen ? `${reopen.opener}
204
+ ` : "";
205
+ if (end >= lines.length) {
206
+ return { status: 200, body: `${head}${lines.slice(start).join("\n")}
207
+ ` };
208
+ }
209
+ if (lastBreak > start) end = lastBreak;
210
+ const open = fences[end];
211
+ const part = `${head}${lines.slice(start, end).join("\n").replace(/\n+$/, "")}${open ? `
212
+ ${open.closer}` : ""}`;
213
+ const next = partUrl(`${end}.${version}`);
214
+ const remaining = estimateFarmMarkdownTokens(lines.slice(end).join("\n"));
215
+ return {
216
+ status: 200,
217
+ body: `${part}
218
+
219
+ ---
220
+
221
+ Continue: ${next} (about ${remaining} more tokens)
222
+ `,
223
+ headers: { Link: `<${next}>; rel="next"` }
224
+ };
225
+ }
226
+ __name(selectFarmMarkdownPart, "selectFarmMarkdownPart");
227
+ function readOpenFences(lines) {
228
+ const open = [];
229
+ let fence;
230
+ let frontMatter = lines[0]?.trim() === "---";
231
+ for (let index = 0; index < lines.length; index++) {
232
+ open.push(frontMatter && index > 0 ? { opener: "", closer: "" } : fence);
233
+ const line = lines[index];
234
+ if (frontMatter) {
235
+ if (index > 0 && line.trim() === "---") frontMatter = false;
236
+ continue;
237
+ }
238
+ const match = FENCE.exec(line);
239
+ if (!match) continue;
240
+ const marker = match[1];
241
+ if (!fence) fence = { opener: line.trim(), closer: marker, marker };
242
+ else if (marker[0] === fence.marker[0] && marker.length >= fence.marker.length)
243
+ fence = void 0;
244
+ }
245
+ return open;
246
+ }
247
+ __name(readOpenFences, "readOpenFences");
248
+ function isPartBreak(lines, fences, index) {
249
+ if (fences[index]) return false;
250
+ return lines[index - 1].trim() === "" || ATX_HEADING.test(lines[index]);
251
+ }
252
+ __name(isPartBreak, "isPartBreak");
253
+ function readMarkdownViewName(searchParams) {
254
+ if (searchParams.has("actions")) return "actions";
255
+ if (searchParams.has("outline")) return "outline";
256
+ if (searchParams.has("max_tokens")) return "parts";
257
+ return searchParams.has("section") ? "section" : void 0;
258
+ }
259
+ __name(readMarkdownViewName, "readMarkdownViewName");
109
260
  function createFarmMarkdownETag(body) {
261
+ return `W/"${hashFarmMarkdown(body)}-${body.length.toString(36)}"`;
262
+ }
263
+ __name(createFarmMarkdownETag, "createFarmMarkdownETag");
264
+ function hashFarmMarkdown(body) {
110
265
  let h1 = 3735928559;
111
266
  let h2 = 1103547991;
112
267
  for (let index = 0; index < body.length; index++) {
@@ -116,10 +271,9 @@ function createFarmMarkdownETag(body) {
116
271
  }
117
272
  h1 = Math.imul(h1 ^ h1 >>> 16, 2246822507) ^ Math.imul(h2 ^ h2 >>> 13, 3266489909);
118
273
  h2 = Math.imul(h2 ^ h2 >>> 16, 2246822507) ^ Math.imul(h1 ^ h1 >>> 13, 3266489909);
119
- const hash = (4294967296 * (2097151 & h2) + (h1 >>> 0)).toString(36);
120
- return `W/"${hash}-${body.length.toString(36)}"`;
274
+ return (4294967296 * (2097151 & h2) + (h1 >>> 0)).toString(36);
121
275
  }
122
- __name(createFarmMarkdownETag, "createFarmMarkdownETag");
276
+ __name(hashFarmMarkdown, "hashFarmMarkdown");
123
277
  function ifNoneMatchIncludes(header, etag) {
124
278
  if (!header) return false;
125
279
  const opaque = etag.replace(/^W\//, "");
@@ -130,6 +284,9 @@ function createFarmMarkdownViewResponse(options) {
130
284
  const url = options.requestUrl ?? new URL(options.request.url);
131
285
  const view = url.searchParams.has("actions") ? selectFarmPageActionsView(options.markdownPath, options.actions ?? [], options.headers) : selectFarmMarkdownView(options.markdown, url.searchParams, options.markdownPath);
132
286
  const status = view.status === 200 ? options.status : view.status;
287
+ emitFarmAgentRead(options.request, "markdown", status, readMarkdownViewName(url.searchParams));
288
+ for (const [name, value] of Object.entries(view.headers ?? {}))
289
+ options.headers.append(name, value);
133
290
  const etag = createFarmMarkdownETag(view.body);
134
291
  options.headers.set("ETag", etag);
135
292
  if (status === 200 && ifNoneMatchIncludes(options.request.headers.get("if-none-match"), etag)) {
@@ -220,7 +377,7 @@ async function createMarkdownMirrorResponse(options) {
220
377
  }
221
378
  const pageUrl = new URL(options.request.url);
222
379
  pageUrl.pathname = target.pathname;
223
- for (const name of ["outline", "section", "actions"]) pageUrl.searchParams.delete(name);
380
+ for (const name of FARM_MARKDOWN_VIEW_PARAMS) pageUrl.searchParams.delete(name);
224
381
  const headers = new Headers(options.request.headers);
225
382
  headers.set("accept", "text/html");
226
383
  const pageResponse = await options.renderPage(
@@ -229,7 +386,8 @@ async function createMarkdownMirrorResponse(options) {
229
386
  headers
230
387
  })
231
388
  );
232
- if (!isHtmlResponse(pageResponse)) {
389
+ const redirects = pageResponse.status >= 300 && pageResponse.status < 400 && pageResponse.headers.has("location");
390
+ if (redirects || !isHtmlResponse(pageResponse)) {
233
391
  return createMarkdownAccessResponse(
234
392
  pageResponse,
235
393
  target.pathname,
@@ -240,6 +398,12 @@ async function createMarkdownMirrorResponse(options) {
240
398
  const html = await pageResponse.text();
241
399
  const markdownPath = target.pathname === "/" ? "/index.md" : `${target.pathname}.md`;
242
400
  const actions = extractFarmPageActions(html, pageUrl);
401
+ for (const form of actions) {
402
+ if (!form.action?.startsWith("/")) continue;
403
+ const pathname = new URL(form.action, pageUrl.origin).pathname;
404
+ const tool = _findFarmMCPEndpointTool(form.method, pathname, options.apiServerBasePath) ?? _findFarmMCPEndpointTool(form.method, stripFarmBasePath(pathname), options.apiServerBasePath);
405
+ if (tool) form.tool = tool;
406
+ }
243
407
  const markdown = htmlToMarkdown(html, {
244
408
  title: target.route?.title,
245
409
  includeMetadata: options.config?.includeMetadata ?? true,
@@ -17,7 +17,7 @@ import {
17
17
  matchAPIRouteAtBasePath,
18
18
  mergePluginAPIRoutes,
19
19
  resolveAPIRouteEndpoint
20
- } from "./chunk-GDUYXFIS.mjs";
20
+ } from "./chunk-6EG7U4NF.mjs";
21
21
  import {
22
22
  registerAPIRouteShape
23
23
  } from "./chunk-W3B4PKAW.mjs";
@@ -15,7 +15,7 @@ var _chunkIK6LVLKCcjs = require('./chunk-IK6LVLKC.cjs');
15
15
  var _chunkHREOKQWTcjs = require('./chunk-HREOKQWT.cjs');
16
16
 
17
17
 
18
- var _chunk2ITKVAIFcjs = require('./chunk-2ITKVAIF.cjs');
18
+ var _chunkZSO5GWGDcjs = require('./chunk-ZSO5GWGD.cjs');
19
19
 
20
20
 
21
21
  var _chunkEM6SBQKMcjs = require('./chunk-EM6SBQKM.cjs');
@@ -1181,11 +1181,11 @@ function shouldReturnMarkdown(request) {
1181
1181
  if (url.pathname.endsWith(".md")) return true;
1182
1182
  const accept = request.headers.get("accept");
1183
1183
  const markdown = Math.max(
1184
- _chunk2ITKVAIFcjs.farmAcceptQuality.call(void 0, accept, "text/markdown"),
1185
- _chunk2ITKVAIFcjs.farmAcceptQuality.call(void 0, accept, "text/plain")
1184
+ _chunkZSO5GWGDcjs.farmAcceptQuality.call(void 0, accept, "text/markdown"),
1185
+ _chunkZSO5GWGDcjs.farmAcceptQuality.call(void 0, accept, "text/plain")
1186
1186
  );
1187
1187
  if (markdown <= 0) return false;
1188
- return markdown >= _chunk2ITKVAIFcjs.farmAcceptQuality.call(void 0, accept, "text/html", { wildcards: true });
1188
+ return markdown >= _chunkZSO5GWGDcjs.farmAcceptQuality.call(void 0, accept, "text/html", { wildcards: true });
1189
1189
  }
1190
1190
  _chunkS3JO3N4Hcjs.__name.call(void 0, shouldReturnMarkdown, "shouldReturnMarkdown");
1191
1191
  function escapeAttribute(value) {
@@ -1,5 +1,5 @@
1
1
  // package.json
2
- var version = "0.1.25";
2
+ var version = "0.1.27";
3
3
 
4
4
  // src/version.ts
5
5
  var FARM_VERSION = version;
@@ -1,6 +1,6 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
2
2
 
3
- var _chunk6K4SPQWLcjs = require('./chunk-6K4SPQWL.cjs');
3
+ var _chunkHF5SIACKcjs = require('./chunk-HF5SIACK.cjs');
4
4
 
5
5
 
6
6
  var _chunkS3JO3N4Hcjs = require('./chunk-S3JO3N4H.cjs');
@@ -11,7 +11,7 @@ var _path = require('path'); var _path2 = _interopRequireDefault(_path);
11
11
  var _url = require('url');
12
12
  async function createFarmMarkdownRouteModuleFromFile(filePath, options = {}) {
13
13
  const source = await _promises.readFile.call(void 0, filePath, "utf8");
14
- return _chunk6K4SPQWLcjs.createFarmMarkdownRouteModule.call(void 0, {
14
+ return _chunkHF5SIACKcjs.createFarmMarkdownRouteModule.call(void 0, {
15
15
  source,
16
16
  filePath,
17
17
  components: options.components,