@farm.js/core 0.1.15 → 0.1.17

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 (180) hide show
  1. package/dist/agent-runtime.d.mts +5 -2
  2. package/dist/agent-runtime.d.ts +5 -2
  3. package/dist/api/client.cjs +62 -28
  4. package/dist/api/client.d.mts +3 -2
  5. package/dist/api/client.d.ts +3 -2
  6. package/dist/api/client.mjs +3 -3
  7. package/dist/api/route-manager.cjs +69 -32
  8. package/dist/api/route-manager.d.mts +5 -2
  9. package/dist/api/route-manager.d.ts +5 -2
  10. package/dist/api/route-manager.mjs +5 -5
  11. package/dist/api/runtime.cjs +65 -29
  12. package/dist/api/runtime.d.mts +3 -2
  13. package/dist/api/runtime.d.ts +3 -2
  14. package/dist/api/runtime.mjs +6 -3
  15. package/dist/{api-client-C8d7BZNp.d.mts → api-client-Cx54IR5g.d.mts} +2 -1
  16. package/dist/{api-client-BWrEbxfO.d.ts → api-client-DykpaKcY.d.ts} +2 -1
  17. package/dist/api.cjs +106 -33
  18. package/dist/api.d.mts +7 -4
  19. package/dist/api.d.ts +7 -4
  20. package/dist/api.mjs +8 -8
  21. package/dist/{app-wIYse61b.d.ts → app-BHY_10Lm.d.ts} +3 -3
  22. package/dist/{app-B3nVSjen.d.mts → app-BMk0do23.d.mts} +3 -3
  23. package/dist/app-markdown.d.mts +6 -3
  24. package/dist/app-markdown.d.ts +6 -3
  25. package/dist/app-markdown.mjs +3 -3
  26. package/dist/cache.cjs +62 -28
  27. package/dist/cache.d.mts +11 -1
  28. package/dist/cache.d.ts +11 -1
  29. package/dist/cache.mjs +1 -1
  30. package/dist/{chunk-FOGLGRDS.mjs → chunk-4OOBRCJX.mjs} +122 -8
  31. package/dist/{chunk-JRF3C33L.mjs → chunk-56DAX2BE.mjs} +172 -53
  32. package/dist/{chunk-B76DT5Y6.mjs → chunk-57J3Y7Y3.mjs} +1 -1
  33. package/dist/{chunk-MOQNHLTS.mjs → chunk-5QOEPUYE.mjs} +5 -2
  34. package/dist/{chunk-HKLRP3BN.mjs → chunk-5SPPCN5J.mjs} +1 -1
  35. package/dist/{chunk-LUC2FAGW.mjs → chunk-6VQIKZZO.mjs} +1 -1
  36. package/dist/{chunk-7ZWL7XOU.mjs → chunk-76DG3LF3.mjs} +1 -1
  37. package/dist/{chunk-EWDK36MM.mjs → chunk-A72YEUZ3.mjs} +1 -1
  38. package/dist/{chunk-ZHOIQC6U.mjs → chunk-ATERBVHQ.mjs} +2 -2
  39. package/dist/{chunk-27JNCYEK.mjs → chunk-EN7WTEAY.mjs} +2 -2
  40. package/dist/{chunk-IGO2HEV7.mjs → chunk-F6SJAKFJ.mjs} +62 -28
  41. package/dist/{chunk-GSDIZACQ.mjs → chunk-FG5H2VCJ.mjs} +2 -2
  42. package/dist/{chunk-LZK5CXDW.mjs → chunk-KLSSO6YD.mjs} +2 -2
  43. package/dist/{chunk-KUO3GBPK.mjs → chunk-L6V3P73N.mjs} +1 -1
  44. package/dist/{chunk-FBHGHMOA.mjs → chunk-MVIWBEJF.mjs} +37 -27
  45. package/dist/{chunk-KTGFMHET.mjs → chunk-N6UROPT7.mjs} +1 -1
  46. package/dist/{chunk-NDD2YVGU.mjs → chunk-NRVGEI5I.mjs} +10 -3
  47. package/dist/{chunk-GNTPTTLA.mjs → chunk-QGVJVSNX.mjs} +5 -5
  48. package/dist/{chunk-6SCB2MXB.mjs → chunk-SXHV42UQ.mjs} +16 -1
  49. package/dist/{chunk-OWTIOHON.mjs → chunk-SYGGPS4A.mjs} +2 -2
  50. package/dist/{chunk-NC333MFA.mjs → chunk-TSWW5BVH.mjs} +7 -4
  51. package/dist/{chunk-LK6FGAQR.mjs → chunk-UDBJCUPA.mjs} +7 -4
  52. package/dist/{chunk-NJ47VORC.mjs → chunk-UTBVHYS3.mjs} +2 -2
  53. package/dist/{chunk-76MNRHTS.mjs → chunk-UUYF3CHN.mjs} +2 -2
  54. package/dist/{chunk-S3QW3BIS.mjs → chunk-WD4YXVLV.mjs} +99 -6
  55. package/dist/{chunk-W7IVHTOP.mjs → chunk-WD5PNSOB.mjs} +16 -3
  56. package/dist/{chunk-DPZUDPVJ.mjs → chunk-XP777LRD.mjs} +1 -1
  57. package/dist/{chunk-KATQ6I56.mjs → chunk-XWAYMDLB.mjs} +1 -1
  58. package/dist/{chunk-UE75PB52.mjs → chunk-YD4J4XCI.mjs} +1 -1
  59. package/dist/{chunk-Q3MDL7TM.mjs → chunk-ZCNJKLA2.mjs} +30 -11
  60. package/dist/{client-plugins-CslwYp3E.d.ts → client-plugins-C089bQhN.d.ts} +16 -4
  61. package/dist/{client-plugins-C1EgdoJM.d.mts → client-plugins-JXXmZliS.d.mts} +16 -4
  62. package/dist/client-plugins.d.mts +5 -2
  63. package/dist/client-plugins.d.ts +5 -2
  64. package/dist/client.cjs +62 -28
  65. package/dist/client.d.mts +8 -7
  66. package/dist/client.d.ts +8 -7
  67. package/dist/client.mjs +6 -6
  68. package/dist/config.d.mts +5 -2
  69. package/dist/config.d.ts +5 -2
  70. package/dist/cron.mjs +2 -2
  71. package/dist/{devtools-PCGEBRSC.mjs → devtools-UFVTMQX5.mjs} +7 -7
  72. package/dist/docs.cjs +83 -4
  73. package/dist/docs.mjs +9 -9
  74. package/dist/{route-CHBaW55F.d.ts → endpoint-1zA994xL.d.ts} +3 -128
  75. package/dist/{route-C6UClI1z.d.mts → endpoint-DAc96i93.d.mts} +3 -128
  76. package/dist/env.cjs +135 -8
  77. package/dist/env.d.mts +22 -2
  78. package/dist/env.d.ts +22 -2
  79. package/dist/env.mjs +4 -1
  80. package/dist/index.cjs +516 -105
  81. package/dist/index.d.mts +16 -15
  82. package/dist/index.d.ts +16 -15
  83. package/dist/index.mjs +46 -34
  84. package/dist/integrations.d.mts +5 -2
  85. package/dist/integrations.d.ts +5 -2
  86. package/dist/internal/app-markdown-runtime.d.mts +5 -2
  87. package/dist/internal/app-markdown-runtime.d.ts +5 -2
  88. package/dist/internal/app-markdown-runtime.mjs +2 -2
  89. package/dist/internal/build-runtime.cjs +600 -284
  90. package/dist/internal/build-runtime.d.mts +7 -4
  91. package/dist/internal/build-runtime.d.ts +7 -4
  92. package/dist/internal/build-runtime.mjs +23 -23
  93. package/dist/internal/cli-runtime.cjs +143 -33
  94. package/dist/internal/cli-runtime.mjs +13 -13
  95. package/dist/internal/client-error-overlay.cjs +1 -1
  96. package/dist/internal/client-error-overlay.mjs +1 -1
  97. package/dist/internal/compression-runtime.d.mts +5 -2
  98. package/dist/internal/compression-runtime.d.ts +5 -2
  99. package/dist/internal/config-runtime.cjs +80 -4
  100. package/dist/internal/config-runtime.d.mts +6 -3
  101. package/dist/internal/config-runtime.d.ts +6 -3
  102. package/dist/internal/config-runtime.mjs +7 -7
  103. package/dist/internal/product-telemetry-runtime.cjs +1 -1
  104. package/dist/internal/product-telemetry-runtime.mjs +1 -1
  105. package/dist/internal/production-runtime.cjs +101 -41
  106. package/dist/internal/production-runtime.d.mts +11 -8
  107. package/dist/internal/production-runtime.d.ts +11 -8
  108. package/dist/internal/production-runtime.mjs +6 -6
  109. package/dist/markdown.cjs +16 -1
  110. package/dist/markdown.mjs +1 -1
  111. package/dist/{metadata-BsUcOEXg.d.ts → metadata-BcNJP4fq.d.ts} +1 -1
  112. package/dist/{metadata-B1bxCPUY.d.mts → metadata-C1Z9vLbb.d.mts} +1 -1
  113. package/dist/middleware.cjs +10 -3
  114. package/dist/middleware.d.mts +7 -4
  115. package/dist/middleware.d.ts +7 -4
  116. package/dist/middleware.mjs +4 -4
  117. package/dist/{nitro-ICBP5APY.mjs → nitro-CVZ36JM5.mjs} +8 -5
  118. package/dist/plugin.cjs +358 -106
  119. package/dist/plugin.d.mts +5 -2
  120. package/dist/plugin.d.ts +5 -2
  121. package/dist/plugin.mjs +19 -19
  122. package/dist/{production-runtime-BDllolVh.d.mts → production-runtime-jcrTlRTb.d.mts} +1 -1
  123. package/dist/{production-runtime-CsNn2-zx.d.ts → production-runtime-oKmC0WU-.d.ts} +1 -1
  124. package/dist/{render-plan-CNUlzylR.d.mts → render-plan-BLIfc4jt.d.mts} +1 -1
  125. package/dist/{render-plan-EG4hNFjy.d.ts → render-plan-DOB9cV-h.d.ts} +1 -1
  126. package/dist/renderer-client.cjs +62 -28
  127. package/dist/renderer-client.d.mts +5 -4
  128. package/dist/renderer-client.d.ts +5 -4
  129. package/dist/renderer-client.mjs +3 -3
  130. package/dist/{request-Blh_f7Z3.d.mts → request-Cr53NRHr.d.mts} +1 -1
  131. package/dist/{request-DsVSQVfD.d.ts → request-Dt2gNuQD.d.ts} +1 -1
  132. package/dist/request.d.mts +6 -3
  133. package/dist/request.d.ts +6 -3
  134. package/dist/route-B0XrsrFk.d.mts +128 -0
  135. package/dist/route-DOEpbyWZ.d.ts +128 -0
  136. package/dist/{route-rendering-hUETRCbM.d.ts → route-rendering-CWYd8Mv1.d.ts} +1 -1
  137. package/dist/{route-rendering-LT4XSoQK.d.mts → route-rendering-CeFBEAoZ.d.mts} +1 -1
  138. package/dist/routes.cjs +68 -31
  139. package/dist/routes.d.mts +5 -3
  140. package/dist/routes.d.ts +5 -3
  141. package/dist/routes.mjs +2 -2
  142. package/dist/server-fn-client.d.mts +3 -2
  143. package/dist/server-fn-client.d.ts +3 -2
  144. package/dist/server-fn.cjs +212 -32
  145. package/dist/server-fn.d.mts +72 -1
  146. package/dist/server-fn.d.ts +72 -1
  147. package/dist/server-fn.mjs +13 -2
  148. package/dist/server-plugins.d.mts +6 -3
  149. package/dist/server-plugins.d.ts +6 -3
  150. package/dist/server-query-client.cjs +62 -28
  151. package/dist/server-query-client.d.mts +1 -0
  152. package/dist/server-query-client.d.ts +1 -0
  153. package/dist/server-query-client.mjs +4 -4
  154. package/dist/server-query.cjs +131 -32
  155. package/dist/server-query.d.mts +6 -1
  156. package/dist/server-query.d.ts +6 -1
  157. package/dist/server-query.mjs +4 -3
  158. package/dist/server.cjs +393 -84
  159. package/dist/server.d.mts +9 -8
  160. package/dist/server.d.ts +9 -8
  161. package/dist/server.mjs +22 -22
  162. package/dist/{spa-router-csKR9Ebj.d.mts → spa-router-CQWiYGxS.d.mts} +2 -2
  163. package/dist/{spa-router-Y_2FSJQK.d.ts → spa-router-Wjiw-8hB.d.ts} +2 -2
  164. package/dist/testing.cjs +73 -32
  165. package/dist/testing.d.mts +5 -3
  166. package/dist/testing.d.ts +5 -3
  167. package/dist/testing.mjs +11 -7
  168. package/dist/{utils-jISOE9SD.d.ts → utils-B0XJmG_b.d.ts} +1 -1
  169. package/dist/{utils-DR2wfZAn.d.mts → utils-uUxF0K0P.d.mts} +1 -1
  170. package/dist/version.cjs +1 -1
  171. package/dist/version.mjs +1 -1
  172. package/dist/vite.cjs +500 -252
  173. package/dist/vite.d.mts +5 -2
  174. package/dist/vite.d.ts +5 -2
  175. package/dist/vite.mjs +19 -19
  176. package/dist/workflows.mjs +2 -2
  177. package/package.json +2 -2
  178. package/dist/{chunk-VO5SCWF5.mjs → chunk-BNGS6KJ6.mjs} +0 -0
  179. package/dist/{chunk-MUUZJLCI.mjs → chunk-EGJDUF4Q.mjs} +3 -3
  180. package/dist/{chunk-4L7SZOHZ.mjs → chunk-YHOCRI4H.mjs} +3 -3
@@ -25,9 +25,14 @@ __export(server_fn_exports, {
25
25
  FARM_SERVER_FN_SYMBOL: () => FARM_SERVER_FN_SYMBOL,
26
26
  ServerActionError: () => ServerActionError,
27
27
  ServerFnFailure: () => ServerFnFailure,
28
+ _attachServerFunctionDescriptor: () => _attachServerFunctionDescriptor,
29
+ _describeServerFunction: () => _describeServerFunction,
30
+ _invokeServerFunction: () => _invokeServerFunction,
28
31
  _parseServerFnSchema: () => _parseServerFnSchema,
32
+ _runServerFunctionForCaller: () => _runServerFunctionForCaller,
29
33
  createServerFn: () => createServerFn,
30
34
  createServerFnTransportError: () => createServerFnTransportError,
35
+ createServerFunctionDescriptor: () => createServerFunctionDescriptor,
31
36
  createServerMiddleware: () => createServerMiddleware,
32
37
  isServerFnFailure: () => isServerFnFailure
33
38
  });
@@ -137,6 +142,26 @@ __name(isSerializedServerFnFailure, "isSerializedServerFnFailure");
137
142
  // src/server-action-security.ts
138
143
  var SERVER_ACTION_STORAGE_KEY = /* @__PURE__ */ Symbol.for("farm.serverActionStorage");
139
144
  var FALLBACK_ABORT_CONTROLLER_KEY = /* @__PURE__ */ Symbol.for("farm.serverActionFallbackAbortController");
145
+ async function runWithServerActionRequest(request, callback) {
146
+ throwIfAborted(request.signal);
147
+ const context2 = {
148
+ request,
149
+ signal: request.signal,
150
+ invalidations: /* @__PURE__ */ new Set(),
151
+ cacheTasks: /* @__PURE__ */ new Set()
152
+ };
153
+ return getServerActionStorage().run(context2, async () => {
154
+ try {
155
+ const result = await callback();
156
+ await Promise.all(context2.cacheTasks);
157
+ return result;
158
+ } catch (error) {
159
+ await Promise.allSettled(context2.cacheTasks);
160
+ throw error;
161
+ }
162
+ });
163
+ }
164
+ __name(runWithServerActionRequest, "runWithServerActionRequest");
140
165
  function getServerActionExecutionContext() {
141
166
  return getServerActionStorage().getStore();
142
167
  }
@@ -167,6 +192,12 @@ function getFallbackAbortController() {
167
192
  return globalState2[FALLBACK_ABORT_CONTROLLER_KEY];
168
193
  }
169
194
  __name(getFallbackAbortController, "getFallbackAbortController");
195
+ function throwIfAborted(signal) {
196
+ if (!signal.aborted) return;
197
+ if (signal.reason !== void 0) throw signal.reason;
198
+ throw new DOMException("The operation was aborted", "AbortError");
199
+ }
200
+ __name(throwIfAborted, "throwIfAborted");
170
201
 
171
202
  // src/server/request-bridge.ts
172
203
  var CURRENT_REQUEST_RESOLVER_KEY = /* @__PURE__ */ Symbol.for("farm.currentRequestResolver");
@@ -563,6 +594,8 @@ var _FarmDataCache = class _FarmDataCache {
563
594
  constructor(config = {}) {
564
595
  this.entries = /* @__PURE__ */ new Map();
565
596
  this.inflight = /* @__PURE__ */ new Map();
597
+ /** Local entries by tag, so tag counts and pruning never scan unrelated entries. */
598
+ this.entriesByTag = /* @__PURE__ */ new Map();
566
599
  this.invalidatedTagVersions = /* @__PURE__ */ new Map();
567
600
  this.activeGenerations = /* @__PURE__ */ new Set();
568
601
  this.version = 0;
@@ -581,6 +614,7 @@ var _FarmDataCache = class _FarmDataCache {
581
614
  configure(config = {}) {
582
615
  this.generation++;
583
616
  this.entries.clear();
617
+ this.entriesByTag.clear();
584
618
  this.inflight.clear();
585
619
  this.invalidatedTagVersions.clear();
586
620
  this.activeGenerations.clear();
@@ -747,14 +781,12 @@ var _FarmDataCache = class _FarmDataCache {
747
781
  return entry;
748
782
  }
749
783
  delete(key) {
750
- const deleted = this.entries.delete(key);
751
- if (deleted) this.pruneInvalidatedTagVersions();
784
+ const deleted = this.deleteLocalEntry(key);
752
785
  emitFarmEvent({ type: "cache.delete", key, deleted });
753
786
  return deleted;
754
787
  }
755
788
  async deleteAsync(key) {
756
- const deleted = this.entries.delete(key);
757
- if (deleted) this.pruneInvalidatedTagVersions();
789
+ const deleted = this.deleteLocalEntry(key);
758
790
  if (this.adapter) {
759
791
  await this.adapter.delete(this.createAdapterKey(key));
760
792
  }
@@ -765,6 +797,7 @@ var _FarmDataCache = class _FarmDataCache {
765
797
  const count = this.entries.size;
766
798
  this.generation++;
767
799
  this.entries.clear();
800
+ this.entriesByTag.clear();
768
801
  this.inflight.clear();
769
802
  this.invalidatedTagVersions.clear();
770
803
  this.activeGenerations.clear();
@@ -850,23 +883,32 @@ var _FarmDataCache = class _FarmDataCache {
850
883
  return cached.value;
851
884
  }
852
885
  const inflight = this.inflight.get(key);
853
- if (inflight) {
886
+ if (inflight && !this.wasInvalidatedSince(inflight.tags, inflight.startVersion)) {
854
887
  emitFarmEvent({ type: "cache.dedupe", key });
855
- return inflight;
888
+ return inflight.promise;
856
889
  }
857
890
  const tags = Array.from(normalizeCacheOptionsTags(options));
858
891
  const generation = this.generation;
892
+ const startVersion = this.version;
859
893
  const promise = this.fillCacheEntry(key, producer, options, tags, generation).catch((error) => {
860
894
  emitFarmEvent({ type: "cache.error", key, operation: "set", error });
861
895
  throw error;
862
896
  }).finally(() => {
863
- if (this.inflight.get(key) === promise) {
897
+ if (this.inflight.get(key)?.promise === promise) {
864
898
  this.inflight.delete(key);
865
899
  }
866
900
  });
867
- this.inflight.set(key, promise);
901
+ this.inflight.set(key, { promise, tags, startVersion });
868
902
  return promise;
869
903
  }
904
+ /** A fill that started before one of its tags was invalidated cannot satisfy later readers. */
905
+ wasInvalidatedSince(tags, version) {
906
+ for (const tag of tags) {
907
+ const invalidatedVersion = this.invalidatedTagVersions.get(tag);
908
+ if (invalidatedVersion !== void 0 && invalidatedVersion > version) return true;
909
+ }
910
+ return false;
911
+ }
870
912
  async fillCacheEntry(key, producer, options, tags, generation) {
871
913
  const adapter = this.adapter;
872
914
  const namespace = this.namespace;
@@ -907,7 +949,7 @@ var _FarmDataCache = class _FarmDataCache {
907
949
  return value;
908
950
  } finally {
909
951
  this.activeGenerations.delete(activeGeneration);
910
- this.pruneInvalidatedTagVersions();
952
+ this.pruneInvalidatedTagVersions(activeGeneration.tags);
911
953
  if (leaseToken && adapter?.releaseLease) {
912
954
  try {
913
955
  await adapter.releaseLease(leaseKey, leaseToken);
@@ -952,18 +994,9 @@ var _FarmDataCache = class _FarmDataCache {
952
994
  }
953
995
  return void 0;
954
996
  }
955
- countEntriesForTag(tag) {
956
- let count = 0;
957
- for (const entry of this.entries.values()) {
958
- if (entry.tags.has(tag)) {
959
- count++;
960
- }
961
- }
962
- return count;
963
- }
964
997
  countEntriesForTags(tags) {
965
998
  let count = 0;
966
- for (const entry of this.entries.values()) {
999
+ for (const entry of this.entriesByTag.get(tags[0]) ?? []) {
967
1000
  if (tags.every((tag) => entry.tags.has(tag))) {
968
1001
  count++;
969
1002
  }
@@ -971,10 +1004,10 @@ var _FarmDataCache = class _FarmDataCache {
971
1004
  return count;
972
1005
  }
973
1006
  invalidateTag(normalizedTag) {
974
- const count = this.countEntriesForTag(normalizedTag);
1007
+ const count = this.entriesByTag.get(normalizedTag)?.size ?? 0;
975
1008
  this.invalidatedTagVersions.delete(normalizedTag);
976
1009
  this.invalidatedTagVersions.set(normalizedTag, ++this.version);
977
- this.pruneInvalidatedTagVersions();
1010
+ this.pruneInvalidatedTagVersions([normalizedTag]);
978
1011
  return count;
979
1012
  }
980
1013
  toPublicEntry(entry) {
@@ -1004,19 +1037,51 @@ var _FarmDataCache = class _FarmDataCache {
1004
1037
  this.entries.set(key, entry);
1005
1038
  }
1006
1039
  storeLocalEntry(entry) {
1007
- this.entries.delete(entry.key);
1040
+ const removed = [];
1041
+ const replaced = this.detachLocalEntry(entry.key);
1042
+ if (replaced) removed.push(replaced);
1008
1043
  this.entries.set(entry.key, entry);
1044
+ for (const tag of entry.tags) {
1045
+ let tagged = this.entriesByTag.get(tag);
1046
+ if (!tagged) this.entriesByTag.set(tag, tagged = /* @__PURE__ */ new Set());
1047
+ tagged.add(entry);
1048
+ }
1009
1049
  while (this.entries.size > this.maxEntries) {
1010
1050
  const oldestKey = this.entries.keys().next().value;
1011
1051
  if (oldestKey === void 0) break;
1012
- this.entries.delete(oldestKey);
1052
+ removed.push(this.detachLocalEntry(oldestKey));
1013
1053
  }
1014
- this.pruneInvalidatedTagVersions();
1054
+ for (const removedEntry of removed) this.pruneInvalidatedTagVersions(removedEntry.tags);
1015
1055
  }
1016
- pruneInvalidatedTagVersions() {
1017
- tags: for (const [tag, invalidatedVersion] of this.invalidatedTagVersions) {
1018
- for (const entry of this.entries.values()) {
1019
- if (entry.tags.has(tag) && entry.createdVersion < invalidatedVersion) continue tags;
1056
+ deleteLocalEntry(key) {
1057
+ const entry = this.detachLocalEntry(key);
1058
+ if (!entry) return false;
1059
+ this.pruneInvalidatedTagVersions(entry.tags);
1060
+ return true;
1061
+ }
1062
+ detachLocalEntry(key) {
1063
+ const entry = this.entries.get(key);
1064
+ if (!entry) return void 0;
1065
+ this.entries.delete(key);
1066
+ for (const tag of entry.tags) {
1067
+ const tagged = this.entriesByTag.get(tag);
1068
+ if (!tagged) continue;
1069
+ tagged.delete(entry);
1070
+ if (tagged.size === 0) this.entriesByTag.delete(tag);
1071
+ }
1072
+ return entry;
1073
+ }
1074
+ /**
1075
+ * Drop invalidation versions that no local entry or active fill can be stale against.
1076
+ * Callers pass only the tags whose dependents just changed, so the cost is bounded by
1077
+ * the entries sharing those tags rather than by the whole cache.
1078
+ */
1079
+ pruneInvalidatedTagVersions(tags) {
1080
+ tags: for (const tag of tags) {
1081
+ const invalidatedVersion = this.invalidatedTagVersions.get(tag);
1082
+ if (invalidatedVersion === void 0) continue;
1083
+ for (const entry of this.entriesByTag.get(tag) ?? []) {
1084
+ if (entry.createdVersion < invalidatedVersion) continue tags;
1020
1085
  }
1021
1086
  for (const generation of this.activeGenerations) {
1022
1087
  if (generation.tags.has(tag) && generation.createdVersion < invalidatedVersion)
@@ -1379,8 +1444,100 @@ function stableSerialize(value, seen = /* @__PURE__ */ new WeakSet()) {
1379
1444
  }
1380
1445
  __name(stableSerialize, "stableSerialize");
1381
1446
 
1447
+ // src/navigation-errors.ts
1448
+ var REDIRECT_ERROR_CODE = "FARM_REDIRECT";
1449
+ var REDIRECT_ERROR_SYMBOL = /* @__PURE__ */ Symbol.for("farm.navigation.redirect");
1450
+ function getFarmRedirectError(error) {
1451
+ if (!error || typeof error !== "object") return null;
1452
+ const candidate = error;
1453
+ if (candidate[REDIRECT_ERROR_SYMBOL]) {
1454
+ return candidate[REDIRECT_ERROR_SYMBOL];
1455
+ }
1456
+ if (typeof candidate.digest === "string" && candidate.digest.startsWith(`${REDIRECT_ERROR_CODE};`)) {
1457
+ const [, status, ...urlParts] = candidate.digest.split(";");
1458
+ const parsedStatus = Number(status);
1459
+ if (isFarmRedirectStatus(parsedStatus)) {
1460
+ return {
1461
+ status: parsedStatus,
1462
+ url: urlParts.join(";")
1463
+ };
1464
+ }
1465
+ }
1466
+ return null;
1467
+ }
1468
+ __name(getFarmRedirectError, "getFarmRedirectError");
1469
+ function isFarmRedirectStatus(status) {
1470
+ return status === 301 || status === 302 || status === 303 || status === 307 || status === 308;
1471
+ }
1472
+ __name(isFarmRedirectStatus, "isFarmRedirectStatus");
1473
+
1382
1474
  // src/server-fn.ts
1383
1475
  var FARM_SERVER_FN_SYMBOL = /* @__PURE__ */ Symbol.for("farm.server-fn");
1476
+ var FARM_SERVER_FUNCTION_DESCRIPTOR = /* @__PURE__ */ Symbol.for("farm.server-function.descriptor");
1477
+ var _ServerFnInputError = class _ServerFnInputError extends Error {
1478
+ constructor(cause) {
1479
+ super("Server function input is invalid");
1480
+ this.cause = cause;
1481
+ }
1482
+ };
1483
+ __name(_ServerFnInputError, "ServerFnInputError");
1484
+ var ServerFnInputError = _ServerFnInputError;
1485
+ function createServerFunctionDescriptor(kind, options, run) {
1486
+ return Object.freeze({
1487
+ kind,
1488
+ input: options.input,
1489
+ output: options.output,
1490
+ mcp: options.mcp === void 0 ? void 0 : Object.freeze({ ...options.mcp }),
1491
+ run
1492
+ });
1493
+ }
1494
+ __name(createServerFunctionDescriptor, "createServerFunctionDescriptor");
1495
+ function _attachServerFunctionDescriptor(target, descriptor) {
1496
+ Object.defineProperty(target, FARM_SERVER_FUNCTION_DESCRIPTOR, {
1497
+ value: descriptor,
1498
+ enumerable: false
1499
+ });
1500
+ }
1501
+ __name(_attachServerFunctionDescriptor, "_attachServerFunctionDescriptor");
1502
+ function getServerFunctionRunner(value) {
1503
+ if (typeof value !== "function") return void 0;
1504
+ return value[FARM_SERVER_FUNCTION_DESCRIPTOR];
1505
+ }
1506
+ __name(getServerFunctionRunner, "getServerFunctionRunner");
1507
+ function _runServerFunctionForCaller(value, input) {
1508
+ const runner = getServerFunctionRunner(value);
1509
+ if (!runner) throw new TypeError("Expected a function created with createServerFn");
1510
+ return runner.run(input);
1511
+ }
1512
+ __name(_runServerFunctionForCaller, "_runServerFunctionForCaller");
1513
+ function _describeServerFunction(value) {
1514
+ const runner = getServerFunctionRunner(value);
1515
+ if (!runner) return void 0;
1516
+ const { kind, input, output, mcp } = runner;
1517
+ return Object.freeze({ kind, input, output, mcp });
1518
+ }
1519
+ __name(_describeServerFunction, "_describeServerFunction");
1520
+ async function _invokeServerFunction(value, input, request) {
1521
+ const runner = getServerFunctionRunner(value);
1522
+ if (!runner) {
1523
+ throw new TypeError("Expected a function created with createServerFn or createServerQuery");
1524
+ }
1525
+ try {
1526
+ const data = await runWithServerActionRequest(request, () => runner.run(input));
1527
+ return { ok: true, data };
1528
+ } catch (error) {
1529
+ if (error instanceof ServerFnInputError) {
1530
+ return { ok: false, reason: "input", error: error.cause };
1531
+ }
1532
+ if (isServerFnFailure(error)) return { ok: false, reason: "failure", failure: error };
1533
+ const redirect = getFarmRedirectError(error);
1534
+ if (redirect) {
1535
+ return { ok: false, reason: "redirect", url: redirect.url, status: redirect.status };
1536
+ }
1537
+ return { ok: false, reason: "error", error };
1538
+ }
1539
+ }
1540
+ __name(_invokeServerFunction, "_invokeServerFunction");
1384
1541
  var UNSAFE_FORM_KEYS = /* @__PURE__ */ new Set(["__proto__", "constructor", "prototype"]);
1385
1542
  function createServerMiddleware(options) {
1386
1543
  if (!options || typeof options.handler !== "function") {
@@ -1404,10 +1561,15 @@ function createServerFn(options) {
1404
1561
  const middleware = resolveServerFnMiddleware(options.middleware);
1405
1562
  const errors = normalizeServerFnErrors(options.errors);
1406
1563
  const error = createServerFnErrorHandler(errors);
1407
- const serverFn = /* @__PURE__ */ __name(async (value) => {
1564
+ const run = /* @__PURE__ */ __name(async (value, labelInputErrors) => {
1408
1565
  const formData = isFormData(value) ? value : void 0;
1409
1566
  const rawInput = formData ? formDataToObject(formData) : value;
1410
- const input = await _parseServerFnSchema(options.input, rawInput, "input");
1567
+ let input;
1568
+ try {
1569
+ input = await _parseServerFnSchema(options.input, rawInput, "input");
1570
+ } catch (cause) {
1571
+ throw labelInputErrors && isSchemaValidationFailure(cause) ? new ServerFnInputError(cause) : cause;
1572
+ }
1411
1573
  const executionContext = getServerActionExecutionContext();
1412
1574
  const request = executionContext?.request ?? _resolveCurrentRequest();
1413
1575
  const signal = executionContext?.signal ?? request?.signal ?? getServerActionSignal();
@@ -1433,7 +1595,8 @@ function createServerFn(options) {
1433
1595
  await applyFarmCacheInvalidationTargets(declaration);
1434
1596
  }
1435
1597
  return parsedResult;
1436
- }, "serverFn");
1598
+ }, "run");
1599
+ const serverFn = /* @__PURE__ */ __name((value) => run(value, false), "serverFn");
1437
1600
  Object.defineProperties(serverFn, {
1438
1601
  [FARM_SERVER_FN_SYMBOL]: {
1439
1602
  value: true,
@@ -1464,6 +1627,10 @@ function createServerFn(options) {
1464
1627
  enumerable: false
1465
1628
  }
1466
1629
  });
1630
+ _attachServerFunctionDescriptor(
1631
+ serverFn,
1632
+ createServerFunctionDescriptor("action", options, (value) => run(value, true))
1633
+ );
1467
1634
  return serverFn;
1468
1635
  }
1469
1636
  __name(createServerFn, "createServerFn");
@@ -1621,9 +1788,17 @@ async function _parseServerFnSchema(schema, value, contract, owner = "createServ
1621
1788
  throw new TypeError(`${owner} ${contract} must provide parse, parseAsync, or safeParse`);
1622
1789
  }
1623
1790
  __name(_parseServerFnSchema, "_parseServerFnSchema");
1791
+ var schemaValidationFailures = /* @__PURE__ */ new WeakSet();
1792
+ function isSchemaValidationFailure(error) {
1793
+ if (!error || typeof error !== "object") return false;
1794
+ return schemaValidationFailures.has(error) || Array.isArray(error.issues);
1795
+ }
1796
+ __name(isSchemaValidationFailure, "isSchemaValidationFailure");
1624
1797
  function unwrapSafeParseResult(result) {
1625
1798
  if (result && typeof result === "object" && "success" in result && result.success === false) {
1626
- throw result.error;
1799
+ const error = result.error;
1800
+ if (error && typeof error === "object") schemaValidationFailures.add(error);
1801
+ throw error;
1627
1802
  }
1628
1803
  if (result && typeof result === "object" && "success" in result && result.success === true) {
1629
1804
  return result.data;
@@ -1665,9 +1840,14 @@ __name(isFormData, "isFormData");
1665
1840
  FARM_SERVER_FN_SYMBOL,
1666
1841
  ServerActionError,
1667
1842
  ServerFnFailure,
1843
+ _attachServerFunctionDescriptor,
1844
+ _describeServerFunction,
1845
+ _invokeServerFunction,
1668
1846
  _parseServerFnSchema,
1847
+ _runServerFunctionForCaller,
1669
1848
  createServerFn,
1670
1849
  createServerFnTransportError,
1850
+ createServerFunctionDescriptor,
1671
1851
  createServerMiddleware,
1672
1852
  isServerFnFailure
1673
1853
  });
@@ -1,6 +1,7 @@
1
1
  import { FarmCacheInvalidationTarget } from './cache.mjs';
2
2
  import { S as ServerFnFailure, a as ServerActionError } from './server-fn-error-DDh14a0V.mjs';
3
3
  export { F as FARM_SERVER_FN_FAILURE_SYMBOL, b as SerializedServerFnFailure, c as createServerFnTransportError, i as isServerFnFailure } from './server-fn-error-DDh14a0V.mjs';
4
+ import { d as EndpointMCPMetadata } from './endpoint-DAc96i93.mjs';
4
5
 
5
6
  type MaybePromise<T> = T | Promise<T>;
6
7
  type ServerFnSchema = {
@@ -38,6 +39,10 @@ type ServerFnDeclaredError<TErrors extends ServerFnErrorDefinitions> = {
38
39
  type ServerFnError<TErrors extends ServerFnErrorDefinitions> = keyof TErrors extends never ? Error : ServerActionError | ServerFnDeclaredError<TErrors>;
39
40
  type ServerFnErrorHandler<TErrors extends ServerFnErrorDefinitions> = <TCode extends keyof TErrors & string>(code: TCode, data: InferServerFnSchemaInput<TErrors[TCode]["data"]>) => never;
40
41
  type ServerFnFormInput = FormData;
42
+ /** MCP tool metadata for a server function or query. Server functions have no path to derive a name from. */
43
+ type ServerFnMCPMetadata = EndpointMCPMetadata & {
44
+ name: string;
45
+ };
41
46
  type Simplify<T> = {
42
47
  [TKey in keyof T]: T[TKey];
43
48
  } & {};
@@ -92,6 +97,8 @@ type ServerFnOptions<TSchema extends ServerFnSchema | undefined, TResult, TMiddl
92
97
  output?: undefined;
93
98
  middleware?: TMiddlewares;
94
99
  errors?: TErrors;
100
+ /** Expose this function as an MCP tool when it is listed in `mcp.tools`. `name` is required. */
101
+ mcp?: ServerFnMCPMetadata;
95
102
  invalidates?: ServerFnInvalidations<TSchema extends ServerFnSchema ? InferServerFnSchemaOutput<TSchema> : unknown, Awaited<TResult>, ServerFnMiddlewareContext<TMiddlewares>>;
96
103
  handler: ServerFnHandler<TSchema extends ServerFnSchema ? InferServerFnSchemaOutput<TSchema> : unknown, TResult, ServerFnMiddlewareContext<TMiddlewares>, TErrors>;
97
104
  };
@@ -100,6 +107,8 @@ type ServerFnOutputOptions<TInputSchema extends ServerFnSchema | undefined, TOut
100
107
  output: TOutputSchema;
101
108
  middleware?: TMiddlewares;
102
109
  errors?: TErrors;
110
+ /** Expose this function as an MCP tool when it is listed in `mcp.tools`. `name` is required. */
111
+ mcp?: ServerFnMCPMetadata;
103
112
  invalidates?: ServerFnInvalidations<TInputSchema extends ServerFnSchema ? InferServerFnSchemaOutput<TInputSchema> : unknown, Awaited<InferServerFnSchemaOutput<TOutputSchema>>, ServerFnMiddlewareContext<TMiddlewares>>;
104
113
  handler: ServerFnHandler<TInputSchema extends ServerFnSchema ? InferServerFnSchemaOutput<TInputSchema> : unknown, unknown, ServerFnMiddlewareContext<TMiddlewares>, TErrors>;
105
114
  };
@@ -111,6 +120,68 @@ type ServerFn<TInput, TResult, TError extends Error = Error> = ([unknown] extend
111
120
  };
112
121
  type InferServerFnError<TServerFn> = TServerFn extends ServerFn<any, any, infer TError> ? TError : Error;
113
122
  declare const FARM_SERVER_FN_SYMBOL: unique symbol;
123
+ /** @internal What a non-browser caller, such as an MCP tool, needs to know about a server function. */
124
+ interface ServerFunctionDescriptor {
125
+ /** Queries only read data; actions may change it. */
126
+ readonly kind: "action" | "query";
127
+ readonly input: ServerFnSchema | undefined;
128
+ readonly output: ServerFnSchema | undefined;
129
+ readonly mcp: Readonly<ServerFnMCPMetadata> | undefined;
130
+ }
131
+ interface ServerFunctionRunner extends ServerFunctionDescriptor {
132
+ /** Runs the full pipeline and labels input validation failures with ServerFnInputError. */
133
+ readonly run: (input: unknown) => Promise<unknown>;
134
+ }
135
+ /** @internal Shared by createServerFn and createServerQuery. */
136
+ declare function createServerFunctionDescriptor(kind: ServerFunctionDescriptor["kind"], options: {
137
+ input?: ServerFnSchema;
138
+ output?: ServerFnSchema;
139
+ mcp?: ServerFnMCPMetadata;
140
+ }, run: ServerFunctionRunner["run"]): ServerFunctionRunner;
141
+ /** @internal Attaches the descriptor that `_describeServerFunction` reads. */
142
+ declare function _attachServerFunctionDescriptor(target: object, descriptor: ServerFunctionRunner): void;
143
+ /** @internal Runs a server function's pipeline with input failures labelled, for createServerQuery. */
144
+ declare function _runServerFunctionForCaller(value: unknown, input: unknown): Promise<unknown>;
145
+ /**
146
+ * @internal Describes a createServerFn or createServerQuery function, or returns undefined for
147
+ * anything else. Exposes schemas and metadata, never the handler.
148
+ */
149
+ declare function _describeServerFunction(value: unknown): ServerFunctionDescriptor | undefined;
150
+ /** @internal How a server function call ended, for callers that are not the browser action transport. */
151
+ type ServerFunctionInvocation = {
152
+ readonly ok: true;
153
+ readonly data: unknown;
154
+ }
155
+ /** The caller's input failed the input schema. The validator's error describes the caller's own data. */
156
+ | {
157
+ readonly ok: false;
158
+ readonly reason: "input";
159
+ readonly error: unknown;
160
+ }
161
+ /** A failure declared in `errors`, public by design. */
162
+ | {
163
+ readonly ok: false;
164
+ readonly reason: "failure";
165
+ readonly failure: ServerFnFailure;
166
+ } | {
167
+ readonly ok: false;
168
+ readonly reason: "redirect";
169
+ readonly url: string;
170
+ readonly status: number;
171
+ }
172
+ /** Anything else. Can contain private data, so callers must not expose it. */
173
+ | {
174
+ readonly ok: false;
175
+ readonly reason: "error";
176
+ readonly error: unknown;
177
+ };
178
+ /**
179
+ * @internal Runs a server function or query on behalf of a non-browser caller, such as an MCP
180
+ * tool call. Runs the same pipeline as a browser action (input schema, middleware, handler,
181
+ * output schema, invalidations) with `request` as the action request, so middleware reads the
182
+ * caller's headers. A query resolves to its data rather than the client cache envelope.
183
+ */
184
+ declare function _invokeServerFunction(value: unknown, input: unknown, request: Request): Promise<ServerFunctionInvocation>;
114
185
  declare function createServerMiddleware<const TMiddlewares extends readonly AnyServerFnMiddleware[] = readonly [], TProvidedContext extends object = {}>(options: ServerFnMiddlewareOptions<TMiddlewares, TProvidedContext>): ServerFnMiddleware<AssignContext<ServerFnMiddlewareContext<TMiddlewares>, TProvidedContext>>;
115
186
  declare function createServerFn<TInputSchema extends ServerFnSchema, TOutputSchema extends ServerFnSchema, const TMiddlewares extends readonly AnyServerFnMiddleware[] = readonly [], const TErrors extends ServerFnErrorDefinitions = {}>(options: ServerFnOutputOptions<TInputSchema, TOutputSchema, TMiddlewares, TErrors>): ServerFn<InferServerFnSchemaInput<TInputSchema>, Awaited<InferServerFnSchemaOutput<TOutputSchema>>, ServerFnError<TErrors>>;
116
187
  declare function createServerFn<TOutputSchema extends ServerFnSchema, const TMiddlewares extends readonly AnyServerFnMiddleware[] = readonly [], const TErrors extends ServerFnErrorDefinitions = {}>(options: ServerFnOutputOptions<undefined, TOutputSchema, TMiddlewares, TErrors>): ServerFn<unknown, Awaited<InferServerFnSchemaOutput<TOutputSchema>>, ServerFnError<TErrors>>;
@@ -118,4 +189,4 @@ declare function createServerFn<TSchema extends ServerFnSchema, TResult, const T
118
189
  declare function createServerFn<TResult, const TMiddlewares extends readonly AnyServerFnMiddleware[] = readonly [], const TErrors extends ServerFnErrorDefinitions = {}>(options: ServerFnOptions<undefined, TResult, TMiddlewares, TErrors>): ServerFn<unknown, Awaited<TResult>, ServerFnError<TErrors>>;
119
190
  declare function _parseServerFnSchema(schema: ServerFnSchema | undefined, value: unknown, contract: "input" | "output", owner?: string): Promise<any>;
120
191
 
121
- export { type AnyServerFnMiddleware, FARM_SERVER_FN_SYMBOL, type InferServerFnError, type InferServerFnSchemaInput, type InferServerFnSchemaOutput, ServerActionError, type ServerFn, type ServerFnContext, type ServerFnDeclaredError, type ServerFnError, type ServerFnErrorDefinition, type ServerFnErrorDefinitions, type ServerFnErrorHandler, type ServerFnErrorSchema, ServerFnFailure, type ServerFnFormInput, type ServerFnHandler, type ServerFnHandlerContext, type ServerFnInvalidationContext, type ServerFnInvalidations, type ServerFnMiddleware, type ServerFnMiddlewareContext, type ServerFnMiddlewareContinuation, type ServerFnMiddlewareHandler, type ServerFnMiddlewareHandlerContext, type ServerFnMiddlewareNext, type ServerFnMiddlewareOptions, type ServerFnOptions, type ServerFnOutputOptions, type ServerFnSchema, _parseServerFnSchema, createServerFn, createServerMiddleware };
192
+ export { type AnyServerFnMiddleware, FARM_SERVER_FN_SYMBOL, type InferServerFnError, type InferServerFnSchemaInput, type InferServerFnSchemaOutput, ServerActionError, type ServerFn, type ServerFnContext, type ServerFnDeclaredError, type ServerFnError, type ServerFnErrorDefinition, type ServerFnErrorDefinitions, type ServerFnErrorHandler, type ServerFnErrorSchema, ServerFnFailure, type ServerFnFormInput, type ServerFnHandler, type ServerFnHandlerContext, type ServerFnInvalidationContext, type ServerFnInvalidations, type ServerFnMCPMetadata, type ServerFnMiddleware, type ServerFnMiddlewareContext, type ServerFnMiddlewareContinuation, type ServerFnMiddlewareHandler, type ServerFnMiddlewareHandlerContext, type ServerFnMiddlewareNext, type ServerFnMiddlewareOptions, type ServerFnOptions, type ServerFnOutputOptions, type ServerFnSchema, type ServerFunctionDescriptor, type ServerFunctionInvocation, _attachServerFunctionDescriptor, _describeServerFunction, _invokeServerFunction, _parseServerFnSchema, _runServerFunctionForCaller, createServerFn, createServerFunctionDescriptor, createServerMiddleware };
@@ -1,6 +1,7 @@
1
1
  import { FarmCacheInvalidationTarget } from './cache.js';
2
2
  import { S as ServerFnFailure, a as ServerActionError } from './server-fn-error-DDh14a0V.js';
3
3
  export { F as FARM_SERVER_FN_FAILURE_SYMBOL, b as SerializedServerFnFailure, c as createServerFnTransportError, i as isServerFnFailure } from './server-fn-error-DDh14a0V.js';
4
+ import { d as EndpointMCPMetadata } from './endpoint-1zA994xL.js';
4
5
 
5
6
  type MaybePromise<T> = T | Promise<T>;
6
7
  type ServerFnSchema = {
@@ -38,6 +39,10 @@ type ServerFnDeclaredError<TErrors extends ServerFnErrorDefinitions> = {
38
39
  type ServerFnError<TErrors extends ServerFnErrorDefinitions> = keyof TErrors extends never ? Error : ServerActionError | ServerFnDeclaredError<TErrors>;
39
40
  type ServerFnErrorHandler<TErrors extends ServerFnErrorDefinitions> = <TCode extends keyof TErrors & string>(code: TCode, data: InferServerFnSchemaInput<TErrors[TCode]["data"]>) => never;
40
41
  type ServerFnFormInput = FormData;
42
+ /** MCP tool metadata for a server function or query. Server functions have no path to derive a name from. */
43
+ type ServerFnMCPMetadata = EndpointMCPMetadata & {
44
+ name: string;
45
+ };
41
46
  type Simplify<T> = {
42
47
  [TKey in keyof T]: T[TKey];
43
48
  } & {};
@@ -92,6 +97,8 @@ type ServerFnOptions<TSchema extends ServerFnSchema | undefined, TResult, TMiddl
92
97
  output?: undefined;
93
98
  middleware?: TMiddlewares;
94
99
  errors?: TErrors;
100
+ /** Expose this function as an MCP tool when it is listed in `mcp.tools`. `name` is required. */
101
+ mcp?: ServerFnMCPMetadata;
95
102
  invalidates?: ServerFnInvalidations<TSchema extends ServerFnSchema ? InferServerFnSchemaOutput<TSchema> : unknown, Awaited<TResult>, ServerFnMiddlewareContext<TMiddlewares>>;
96
103
  handler: ServerFnHandler<TSchema extends ServerFnSchema ? InferServerFnSchemaOutput<TSchema> : unknown, TResult, ServerFnMiddlewareContext<TMiddlewares>, TErrors>;
97
104
  };
@@ -100,6 +107,8 @@ type ServerFnOutputOptions<TInputSchema extends ServerFnSchema | undefined, TOut
100
107
  output: TOutputSchema;
101
108
  middleware?: TMiddlewares;
102
109
  errors?: TErrors;
110
+ /** Expose this function as an MCP tool when it is listed in `mcp.tools`. `name` is required. */
111
+ mcp?: ServerFnMCPMetadata;
103
112
  invalidates?: ServerFnInvalidations<TInputSchema extends ServerFnSchema ? InferServerFnSchemaOutput<TInputSchema> : unknown, Awaited<InferServerFnSchemaOutput<TOutputSchema>>, ServerFnMiddlewareContext<TMiddlewares>>;
104
113
  handler: ServerFnHandler<TInputSchema extends ServerFnSchema ? InferServerFnSchemaOutput<TInputSchema> : unknown, unknown, ServerFnMiddlewareContext<TMiddlewares>, TErrors>;
105
114
  };
@@ -111,6 +120,68 @@ type ServerFn<TInput, TResult, TError extends Error = Error> = ([unknown] extend
111
120
  };
112
121
  type InferServerFnError<TServerFn> = TServerFn extends ServerFn<any, any, infer TError> ? TError : Error;
113
122
  declare const FARM_SERVER_FN_SYMBOL: unique symbol;
123
+ /** @internal What a non-browser caller, such as an MCP tool, needs to know about a server function. */
124
+ interface ServerFunctionDescriptor {
125
+ /** Queries only read data; actions may change it. */
126
+ readonly kind: "action" | "query";
127
+ readonly input: ServerFnSchema | undefined;
128
+ readonly output: ServerFnSchema | undefined;
129
+ readonly mcp: Readonly<ServerFnMCPMetadata> | undefined;
130
+ }
131
+ interface ServerFunctionRunner extends ServerFunctionDescriptor {
132
+ /** Runs the full pipeline and labels input validation failures with ServerFnInputError. */
133
+ readonly run: (input: unknown) => Promise<unknown>;
134
+ }
135
+ /** @internal Shared by createServerFn and createServerQuery. */
136
+ declare function createServerFunctionDescriptor(kind: ServerFunctionDescriptor["kind"], options: {
137
+ input?: ServerFnSchema;
138
+ output?: ServerFnSchema;
139
+ mcp?: ServerFnMCPMetadata;
140
+ }, run: ServerFunctionRunner["run"]): ServerFunctionRunner;
141
+ /** @internal Attaches the descriptor that `_describeServerFunction` reads. */
142
+ declare function _attachServerFunctionDescriptor(target: object, descriptor: ServerFunctionRunner): void;
143
+ /** @internal Runs a server function's pipeline with input failures labelled, for createServerQuery. */
144
+ declare function _runServerFunctionForCaller(value: unknown, input: unknown): Promise<unknown>;
145
+ /**
146
+ * @internal Describes a createServerFn or createServerQuery function, or returns undefined for
147
+ * anything else. Exposes schemas and metadata, never the handler.
148
+ */
149
+ declare function _describeServerFunction(value: unknown): ServerFunctionDescriptor | undefined;
150
+ /** @internal How a server function call ended, for callers that are not the browser action transport. */
151
+ type ServerFunctionInvocation = {
152
+ readonly ok: true;
153
+ readonly data: unknown;
154
+ }
155
+ /** The caller's input failed the input schema. The validator's error describes the caller's own data. */
156
+ | {
157
+ readonly ok: false;
158
+ readonly reason: "input";
159
+ readonly error: unknown;
160
+ }
161
+ /** A failure declared in `errors`, public by design. */
162
+ | {
163
+ readonly ok: false;
164
+ readonly reason: "failure";
165
+ readonly failure: ServerFnFailure;
166
+ } | {
167
+ readonly ok: false;
168
+ readonly reason: "redirect";
169
+ readonly url: string;
170
+ readonly status: number;
171
+ }
172
+ /** Anything else. Can contain private data, so callers must not expose it. */
173
+ | {
174
+ readonly ok: false;
175
+ readonly reason: "error";
176
+ readonly error: unknown;
177
+ };
178
+ /**
179
+ * @internal Runs a server function or query on behalf of a non-browser caller, such as an MCP
180
+ * tool call. Runs the same pipeline as a browser action (input schema, middleware, handler,
181
+ * output schema, invalidations) with `request` as the action request, so middleware reads the
182
+ * caller's headers. A query resolves to its data rather than the client cache envelope.
183
+ */
184
+ declare function _invokeServerFunction(value: unknown, input: unknown, request: Request): Promise<ServerFunctionInvocation>;
114
185
  declare function createServerMiddleware<const TMiddlewares extends readonly AnyServerFnMiddleware[] = readonly [], TProvidedContext extends object = {}>(options: ServerFnMiddlewareOptions<TMiddlewares, TProvidedContext>): ServerFnMiddleware<AssignContext<ServerFnMiddlewareContext<TMiddlewares>, TProvidedContext>>;
115
186
  declare function createServerFn<TInputSchema extends ServerFnSchema, TOutputSchema extends ServerFnSchema, const TMiddlewares extends readonly AnyServerFnMiddleware[] = readonly [], const TErrors extends ServerFnErrorDefinitions = {}>(options: ServerFnOutputOptions<TInputSchema, TOutputSchema, TMiddlewares, TErrors>): ServerFn<InferServerFnSchemaInput<TInputSchema>, Awaited<InferServerFnSchemaOutput<TOutputSchema>>, ServerFnError<TErrors>>;
116
187
  declare function createServerFn<TOutputSchema extends ServerFnSchema, const TMiddlewares extends readonly AnyServerFnMiddleware[] = readonly [], const TErrors extends ServerFnErrorDefinitions = {}>(options: ServerFnOutputOptions<undefined, TOutputSchema, TMiddlewares, TErrors>): ServerFn<unknown, Awaited<InferServerFnSchemaOutput<TOutputSchema>>, ServerFnError<TErrors>>;
@@ -118,4 +189,4 @@ declare function createServerFn<TSchema extends ServerFnSchema, TResult, const T
118
189
  declare function createServerFn<TResult, const TMiddlewares extends readonly AnyServerFnMiddleware[] = readonly [], const TErrors extends ServerFnErrorDefinitions = {}>(options: ServerFnOptions<undefined, TResult, TMiddlewares, TErrors>): ServerFn<unknown, Awaited<TResult>, ServerFnError<TErrors>>;
119
190
  declare function _parseServerFnSchema(schema: ServerFnSchema | undefined, value: unknown, contract: "input" | "output", owner?: string): Promise<any>;
120
191
 
121
- export { type AnyServerFnMiddleware, FARM_SERVER_FN_SYMBOL, type InferServerFnError, type InferServerFnSchemaInput, type InferServerFnSchemaOutput, ServerActionError, type ServerFn, type ServerFnContext, type ServerFnDeclaredError, type ServerFnError, type ServerFnErrorDefinition, type ServerFnErrorDefinitions, type ServerFnErrorHandler, type ServerFnErrorSchema, ServerFnFailure, type ServerFnFormInput, type ServerFnHandler, type ServerFnHandlerContext, type ServerFnInvalidationContext, type ServerFnInvalidations, type ServerFnMiddleware, type ServerFnMiddlewareContext, type ServerFnMiddlewareContinuation, type ServerFnMiddlewareHandler, type ServerFnMiddlewareHandlerContext, type ServerFnMiddlewareNext, type ServerFnMiddlewareOptions, type ServerFnOptions, type ServerFnOutputOptions, type ServerFnSchema, _parseServerFnSchema, createServerFn, createServerMiddleware };
192
+ export { type AnyServerFnMiddleware, FARM_SERVER_FN_SYMBOL, type InferServerFnError, type InferServerFnSchemaInput, type InferServerFnSchemaOutput, ServerActionError, type ServerFn, type ServerFnContext, type ServerFnDeclaredError, type ServerFnError, type ServerFnErrorDefinition, type ServerFnErrorDefinitions, type ServerFnErrorHandler, type ServerFnErrorSchema, ServerFnFailure, type ServerFnFormInput, type ServerFnHandler, type ServerFnHandlerContext, type ServerFnInvalidationContext, type ServerFnInvalidations, type ServerFnMCPMetadata, type ServerFnMiddleware, type ServerFnMiddlewareContext, type ServerFnMiddlewareContinuation, type ServerFnMiddlewareHandler, type ServerFnMiddlewareHandlerContext, type ServerFnMiddlewareNext, type ServerFnMiddlewareOptions, type ServerFnOptions, type ServerFnOutputOptions, type ServerFnSchema, type ServerFunctionDescriptor, type ServerFunctionInvocation, _attachServerFunctionDescriptor, _describeServerFunction, _invokeServerFunction, _parseServerFnSchema, _runServerFunctionForCaller, createServerFn, createServerFunctionDescriptor, createServerMiddleware };