nuxt-ai-ready 3.6.0 → 3.6.2

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 (161) hide show
  1. package/dist/agent-skills-config-CmZq9uWW.mjs +18 -0
  2. package/dist/agent-skills-zGLgFB6H.mjs +468 -0
  3. package/dist/cli.d.mts +1 -0
  4. package/dist/cli.mjs +656 -570
  5. package/dist/devtools/pages/ai-ready/debug.vue +9 -3
  6. package/dist/devtools/pages/ai-ready/index.vue +3 -1
  7. package/dist/devtools/pages/ai-ready/llms-txt.vue +15 -4
  8. package/dist/devtools/pages/ai-ready.vue +28 -4
  9. package/dist/module.d.mts +106 -112
  10. package/dist/module.json +3 -3
  11. package/dist/module.mjs +2732 -26
  12. package/dist/package-metadata-B1darl8W.mjs +28 -0
  13. package/dist/prerender-QMv07W4h.mjs +650 -0
  14. package/dist/runtime/app/composables/webmcp.js +90 -97
  15. package/dist/runtime/app/plugins/md-alternate.server.js +29 -26
  16. package/dist/runtime/app/plugins/md-hints.prerender.js +13 -15
  17. package/dist/runtime/app/plugins/webmcp.client.js +49 -53
  18. package/dist/runtime/app/webmcp-config.js +2 -2
  19. package/dist/runtime/cache-control.js +16 -10
  20. package/dist/runtime/i18n-url.js +6 -6
  21. package/dist/runtime/index.js +4 -12
  22. package/dist/runtime/llms-txt-directive.js +20 -13
  23. package/dist/runtime/llms-txt-files.js +188 -187
  24. package/dist/runtime/llms-txt-format.js +54 -55
  25. package/dist/runtime/llms-txt-i18n.js +22 -19
  26. package/dist/runtime/llms-txt-utils.js +140 -95
  27. package/dist/runtime/markdown-links.js +45 -39
  28. package/dist/runtime/markdown-path.js +29 -9
  29. package/dist/runtime/markdown-text.js +2 -1
  30. package/dist/runtime/mcp.js +5 -1
  31. package/dist/runtime/route-path.js +14 -8
  32. package/dist/runtime/server/db/drizzle/client.js +71 -75
  33. package/dist/runtime/server/db/drizzle/index.js +16 -28
  34. package/dist/runtime/server/db/drizzle/providers/bun.d.ts +2 -1
  35. package/dist/runtime/server/db/drizzle/providers/bun.js +10 -7
  36. package/dist/runtime/server/db/drizzle/providers/d1.js +16 -13
  37. package/dist/runtime/server/db/drizzle/providers/dbPath.js +60 -37
  38. package/dist/runtime/server/db/drizzle/providers/libsql.js +23 -11
  39. package/dist/runtime/server/db/drizzle/providers/neon.js +13 -10
  40. package/dist/runtime/server/db/drizzle/providers/node-sqlite.js +10 -7
  41. package/dist/runtime/server/db/drizzle/providers/postgres.js +13 -10
  42. package/dist/runtime/server/db/drizzle/queries.js +633 -532
  43. package/dist/runtime/server/db/drizzle/raw.js +200 -176
  44. package/dist/runtime/server/db/index.js +3 -0
  45. package/dist/runtime/server/db/queries.js +816 -607
  46. package/dist/runtime/server/db/schema/postgres.js +56 -50
  47. package/dist/runtime/server/db/schema/sqlite.js +56 -50
  48. package/dist/runtime/server/db/schema-sql.js +35 -38
  49. package/dist/runtime/server/db/seed.js +60 -45
  50. package/dist/runtime/server/db/shared.js +300 -213
  51. package/dist/runtime/server/index.js +1 -17
  52. package/dist/runtime/server/mcp/cache-key.js +5 -1
  53. package/dist/runtime/server/mcp/resources/pages.js +32 -33
  54. package/dist/runtime/server/mcp/tools/get-page-markdown.js +41 -27
  55. package/dist/runtime/server/mcp/tools/list-pages.js +40 -34
  56. package/dist/runtime/server/mcp/tools/search-pages.js +24 -16
  57. package/dist/runtime/server/middleware/markdown.d.ts +1 -1
  58. package/dist/runtime/server/middleware/markdown.js +15 -13
  59. package/dist/runtime/server/middleware/markdown.prerender.d.ts +1 -1
  60. package/dist/runtime/server/middleware/markdown.prerender.js +135 -119
  61. package/dist/runtime/server/middleware/render-markdown.d.ts +1 -1
  62. package/dist/runtime/server/middleware/render-markdown.js +164 -144
  63. package/dist/runtime/server/plugins/ai-terms.d.ts +1 -1
  64. package/dist/runtime/server/plugins/ai-terms.js +4 -8
  65. package/dist/runtime/server/plugins/content-signal-capture-nitro3.prerender.d.ts +1 -1
  66. package/dist/runtime/server/plugins/content-signal-capture-nitro3.prerender.js +7 -8
  67. package/dist/runtime/server/plugins/content-signal-capture.prerender.d.ts +1 -1
  68. package/dist/runtime/server/plugins/content-signal-capture.prerender.js +7 -7
  69. package/dist/runtime/server/plugins/db-lifecycle-nitro3.d.ts +1 -1
  70. package/dist/runtime/server/plugins/db-lifecycle-nitro3.js +13 -14
  71. package/dist/runtime/server/plugins/db-lifecycle.js +13 -11
  72. package/dist/runtime/server/plugins/html-capture.prerender.js +6 -6
  73. package/dist/runtime/server/plugins/link-header-nitro3.d.ts +1 -1
  74. package/dist/runtime/server/plugins/link-header-nitro3.js +23 -13
  75. package/dist/runtime/server/plugins/link-header.d.ts +1 -1
  76. package/dist/runtime/server/plugins/link-header.js +30 -30
  77. package/dist/runtime/server/plugins/llms-txt-directive-html.d.ts +1 -1
  78. package/dist/runtime/server/plugins/llms-txt-directive-html.js +4 -3
  79. package/dist/runtime/server/plugins/markdown-negotiation.d.ts +1 -1
  80. package/dist/runtime/server/plugins/markdown-negotiation.js +64 -45
  81. package/dist/runtime/server/plugins/sitemap-seeder.js +144 -112
  82. package/dist/runtime/server/routes/__ai-ready/cron.get.d.ts +1 -1
  83. package/dist/runtime/server/routes/__ai-ready/cron.get.js +17 -11
  84. package/dist/runtime/server/routes/__ai-ready/devtools.get.d.ts +1 -1
  85. package/dist/runtime/server/routes/__ai-ready/devtools.get.js +60 -45
  86. package/dist/runtime/server/routes/__ai-ready/pages.get.d.ts +36 -1
  87. package/dist/runtime/server/routes/__ai-ready/pages.get.js +44 -40
  88. package/dist/runtime/server/routes/__ai-ready/poll.post.d.ts +15 -1
  89. package/dist/runtime/server/routes/__ai-ready/poll.post.js +30 -30
  90. package/dist/runtime/server/routes/__ai-ready/prune.post.d.ts +13 -1
  91. package/dist/runtime/server/routes/__ai-ready/prune.post.js +29 -15
  92. package/dist/runtime/server/routes/__ai-ready/reindex.post.d.ts +22 -1
  93. package/dist/runtime/server/routes/__ai-ready/reindex.post.js +31 -21
  94. package/dist/runtime/server/routes/__ai-ready/restore.post.d.ts +4 -1
  95. package/dist/runtime/server/routes/__ai-ready/restore.post.js +28 -22
  96. package/dist/runtime/server/routes/__ai-ready/status.get.d.ts +1 -1
  97. package/dist/runtime/server/routes/__ai-ready/status.get.js +52 -53
  98. package/dist/runtime/server/routes/__ai-ready-debug.get.d.ts +83 -1
  99. package/dist/runtime/server/routes/__ai-ready-debug.get.js +209 -189
  100. package/dist/runtime/server/routes/agent-skills-artifact.d.ts +1 -1
  101. package/dist/runtime/server/routes/agent-skills-artifact.js +16 -14
  102. package/dist/runtime/server/routes/agent-skills-index.d.ts +1 -1
  103. package/dist/runtime/server/routes/agent-skills-index.js +8 -6
  104. package/dist/runtime/server/routes/ai-catalog.js +19 -19
  105. package/dist/runtime/server/routes/ai-terms.js +23 -19
  106. package/dist/runtime/server/routes/api-catalog.js +23 -23
  107. package/dist/runtime/server/routes/llms-full.txt.get.d.ts +1 -1
  108. package/dist/runtime/server/routes/llms-full.txt.get.js +42 -41
  109. package/dist/runtime/server/routes/llms.txt.get.d.ts +1 -1
  110. package/dist/runtime/server/routes/llms.txt.get.js +38 -33
  111. package/dist/runtime/server/routes/mcp-server-card.js +19 -19
  112. package/dist/runtime/server/routes/sitemap.md.get.d.ts +1 -1
  113. package/dist/runtime/server/routes/sitemap.md.get.js +38 -39
  114. package/dist/runtime/server/tasks/ai-ready-cron.d.ts +1 -1
  115. package/dist/runtime/server/tasks/ai-ready-cron.js +17 -14
  116. package/dist/runtime/server/utils/auth.js +76 -53
  117. package/dist/runtime/server/utils/batchIndex.js +72 -60
  118. package/dist/runtime/server/utils/checkStale.js +181 -134
  119. package/dist/runtime/server/utils/cloudflare-prerendered.js +23 -20
  120. package/dist/runtime/server/utils/cloudflare.js +69 -51
  121. package/dist/runtime/server/utils/content-negotiation.js +30 -18
  122. package/dist/runtime/server/utils/content.js +4 -1
  123. package/dist/runtime/server/utils/context.js +30 -19
  124. package/dist/runtime/server/utils/cron-plan.js +6 -6
  125. package/dist/runtime/server/utils/discovery-response.js +5 -6
  126. package/dist/runtime/server/utils/fetch.js +2 -1
  127. package/dist/runtime/server/utils/frontmatter.js +44 -57
  128. package/dist/runtime/server/utils/i18n.js +3 -8
  129. package/dist/runtime/server/utils/indexPage.js +152 -113
  130. package/dist/runtime/server/utils/keywords.js +278 -277
  131. package/dist/runtime/server/utils/link-header.js +67 -56
  132. package/dist/runtime/server/utils/llms-full.js +39 -49
  133. package/dist/runtime/server/utils/markdown-redirect.js +26 -14
  134. package/dist/runtime/server/utils/markdown-request.js +62 -47
  135. package/dist/runtime/server/utils/negotiation-decision.d.ts +8 -0
  136. package/dist/runtime/server/utils/negotiation-decision.js +106 -34
  137. package/dist/runtime/server/utils/negotiation-response.d.ts +1 -1
  138. package/dist/runtime/server/utils/negotiation-response.js +195 -144
  139. package/dist/runtime/server/utils/page-updated-at.js +23 -15
  140. package/dist/runtime/server/utils/prerender-html.js +31 -17
  141. package/dist/runtime/server/utils/runCron.js +263 -192
  142. package/dist/runtime/server/utils/sitemap-crawl-state.js +57 -39
  143. package/dist/runtime/server/utils/sitemap-lastmod.js +16 -14
  144. package/dist/runtime/server/utils/sitemap-md.js +39 -50
  145. package/dist/runtime/server/utils/sitemap-routes.js +7 -7
  146. package/dist/runtime/server/utils/sitemap.js +459 -414
  147. package/dist/runtime/server/utils.js +141 -122
  148. package/dist/runtime/site-tool-catalog.js +39 -43
  149. package/dist/runtime/site-tool-config.js +1 -0
  150. package/dist/runtime/sync-secret.js +11 -11
  151. package/dist/runtime/types.js +1 -0
  152. package/dist/runtime/webmcp-site-tools.js +278 -250
  153. package/dist/runtime/webmcp.js +53 -42
  154. package/dist/static-headers-G7hFL3wE.mjs +328 -0
  155. package/dist/types.d.mts +1 -9
  156. package/package.json +20 -24
  157. package/skills/nuxt-ai-ready/SKILL.md +8 -2
  158. package/dist/chunks/agent-skills.mjs +0 -383
  159. package/dist/chunks/prerender.mjs +0 -683
  160. package/dist/shared/nuxt-ai-ready.Bzx0dNWj.mjs +0 -3034
  161. package/dist/shared/nuxt-ai-ready.DCd-1s1G.mjs +0 -29
@@ -2,281 +2,309 @@ import { toMarkdownPath } from "./markdown-path.js";
2
2
  import { toDeployedRoute } from "./route-path.js";
3
3
  import { normalizeSiteRoute, SITE_TOOL_CATALOG } from "./site-tool-catalog.js";
4
4
  import { toolError, toolText, truncateToolOutput, WEB_MCP_BUDGET } from "./webmcp.js";
5
- const READ_ONLY = { readOnlyHint: true, untrustedContentHint: true };
5
+ /** Page content is whatever the site indexed, so treat it as untrusted input. */
6
+ const READ_ONLY = {
7
+ readOnlyHint: true,
8
+ untrustedContentHint: true
9
+ };
6
10
  const RE_FTS_CHARS = /[*:^"()]/g;
7
11
  const RE_WHITESPACE = /\s+/;
12
+ /** Field weights matching the server-side FTS query, excluding unavailable markdown. */
8
13
  const SEARCH_WEIGHTS = [
9
- ["route", 5],
10
- ["title", 3],
11
- ["headings", 2],
12
- ["keywords", 2],
13
- ["description", 1]
14
+ ["route", 5],
15
+ ["title", 3],
16
+ ["headings", 2],
17
+ ["keywords", 2],
18
+ ["description", 1]
14
19
  ];
15
20
  function isRecord(input) {
16
- return typeof input === "object" && input !== null;
21
+ return typeof input === "object" && input !== null;
17
22
  }
18
23
  function parsePageSummary(input) {
19
- if (!isRecord(input) || typeof input.route !== "string" || !input.route)
20
- return void 0;
21
- return {
22
- route: input.route,
23
- ...typeof input.title === "string" ? { title: input.title } : {},
24
- ...typeof input.description === "string" ? { description: input.description } : {},
25
- ...typeof input.headings === "string" ? { headings: input.headings } : {},
26
- ...Array.isArray(input.keywords) ? { keywords: input.keywords.filter((keyword) => typeof keyword === "string") } : {}
27
- };
24
+ if (!isRecord(input) || typeof input.route !== "string" || !input.route) return undefined;
25
+ return {
26
+ route: input.route,
27
+ ...typeof input.title === "string" ? { title: input.title } : {},
28
+ ...typeof input.description === "string" ? { description: input.description } : {},
29
+ ...typeof input.headings === "string" ? { headings: input.headings } : {},
30
+ ...Array.isArray(input.keywords) ? { keywords: input.keywords.filter((keyword) => typeof keyword === "string") } : {}
31
+ };
28
32
  }
29
33
  function parsePageList(input) {
30
- if (!Array.isArray(input))
31
- return [];
32
- return input.map(parsePageSummary).filter((page) => !!page);
34
+ if (!Array.isArray(input)) return [];
35
+ return input.map(parsePageSummary).filter((page) => !!page);
33
36
  }
34
37
  function parsePagesResponse(input) {
35
- if (!isRecord(input))
36
- return {};
37
- const page = input.page === null ? null : parsePageSummary(input.page);
38
- return {
39
- ...page !== void 0 ? { page } : {},
40
- pages: parsePageList(input.pages),
41
- results: parsePageList(input.results),
42
- ...typeof input.total === "number" && Number.isFinite(input.total) ? { total: input.total } : {}
43
- };
38
+ if (!isRecord(input)) return {};
39
+ const page = input.page === null ? null : parsePageSummary(input.page);
40
+ return {
41
+ ...page !== undefined ? { page } : {},
42
+ pages: parsePageList(input.pages),
43
+ results: parsePageList(input.results),
44
+ ...typeof input.total === "number" && Number.isFinite(input.total) ? { total: input.total } : {}
45
+ };
44
46
  }
45
47
  function getErrorStatus(error) {
46
- if (!isRecord(error))
47
- return void 0;
48
- const response = error.response;
49
- if (isRecord(response) && typeof response.status === "number")
50
- return response.status;
51
- return typeof error.statusCode === "number" ? error.statusCode : void 0;
48
+ if (!isRecord(error)) return undefined;
49
+ const response = error.response;
50
+ if (isRecord(response) && typeof response.status === "number") return response.status;
51
+ return typeof error.statusCode === "number" ? error.statusCode : undefined;
52
52
  }
53
53
  function fetchResource(path, options) {
54
- return Promise.resolve().then(() => options ? globalThis.$fetch(path, options) : globalThis.$fetch(path)).then(
55
- (value) => ({ _tag: "Ok", value }),
56
- (error) => getErrorStatus(error) === 404 ? { _tag: "NotFound" } : { _tag: "Error", error }
57
- );
54
+ return Promise.resolve().then(() => options ? globalThis.$fetch(path, options) : globalThis.$fetch(path)).then((value) => ({
55
+ _tag: "Ok",
56
+ value
57
+ }), (error) => getErrorStatus(error) === 404 ? { _tag: "NotFound" } : {
58
+ _tag: "Error",
59
+ error
60
+ });
58
61
  }
59
62
  function reportFetchError(source, result) {
60
- if (result._tag === "Error")
61
- console.error(`[nuxt-ai-ready] Failed to read WebMCP ${source}.`, result.error);
63
+ if (result._tag === "Error") console.error(`[nuxt-ai-ready] Failed to read WebMCP ${source}.`, result.error);
62
64
  }
65
+ /**
66
+ * Rank the prerendered index in the browser. Only reached on fully static
67
+ * deployments, where there is no server left to run the FTS query.
68
+ */
63
69
  function searchIndex(pages, query, limit) {
64
- const terms = query.replace(RE_FTS_CHARS, " ").toLowerCase().split(RE_WHITESPACE).filter(Boolean);
65
- if (!terms.length)
66
- return [];
67
- return pages.map((page) => {
68
- let score = 0;
69
- for (const [field, weight] of SEARCH_WEIGHTS) {
70
- const value = page[field];
71
- const haystack = (Array.isArray(value) ? value.join(" ") : value || "").toLowerCase();
72
- for (const term of terms) {
73
- if (haystack.includes(term))
74
- score += weight;
75
- }
76
- }
77
- return { page, score };
78
- }).filter((entry) => entry.score > 0).sort((a, b) => b.score - a.score).slice(0, limit).map((entry) => entry.page);
70
+ const terms = query.replace(RE_FTS_CHARS, " ").toLowerCase().split(RE_WHITESPACE).filter(Boolean);
71
+ if (!terms.length) return [];
72
+ return pages.map((page) => {
73
+ let score = 0;
74
+ for (const [field, weight] of SEARCH_WEIGHTS) {
75
+ const value = page[field];
76
+ const haystack = (Array.isArray(value) ? value.join(" ") : value || "").toLowerCase();
77
+ for (const term of terms) {
78
+ if (haystack.includes(term)) score += weight;
79
+ }
80
+ }
81
+ return {
82
+ page,
83
+ score
84
+ };
85
+ }).filter((entry) => entry.score > 0).sort((a, b) => b.score - a.score).slice(0, limit).map((entry) => entry.page);
79
86
  }
87
+ /**
88
+ * Coerce whatever the agent passed into a site route. Schemas stay loose so the
89
+ * model is not penalised for sending `about` or `/about.md` instead of `/about`.
90
+ */
80
91
  function clamp(input, fallback, max) {
81
- const value = Math.trunc(Number(input));
82
- if (!Number.isFinite(value) || value < 1)
83
- return fallback;
84
- return Math.min(value, max);
92
+ const value = Math.trunc(Number(input));
93
+ if (!Number.isFinite(value) || value < 1) return fallback;
94
+ return Math.min(value, max);
85
95
  }
86
96
  function positiveInteger(input, fallback) {
87
- const value = Math.trunc(Number(input));
88
- return Number.isFinite(value) && value > 0 ? value : fallback;
97
+ const value = Math.trunc(Number(input));
98
+ return Number.isFinite(value) && value > 0 ? value : fallback;
89
99
  }
90
100
  function cleanSummaryPart(input) {
91
- return input.replace(RE_WHITESPACE, " ").trim();
101
+ return input.replace(RE_WHITESPACE, " ").trim();
92
102
  }
103
+ /**
104
+ * Read-only tools mirroring the MCP server, so an agent running inside the
105
+ * browser can discover and read site content without leaving the page.
106
+ */
93
107
  export function createSiteTools(options = {}) {
94
- const baseURL = options.baseURL || "/";
95
- const listPagesOptions = options.tools?.listPages;
96
- const searchPagesOptions = options.tools?.searchPages;
97
- const getPageMarkdownOptions = options.tools?.getPageMarkdown;
98
- const listPagesLimit = clamp(listPagesOptions?.defaultLimit, 20, 50);
99
- const searchPagesLimit = clamp(searchPagesOptions?.defaultLimit, 10, 50);
100
- const listPagesMaxOutputChars = positiveInteger(listPagesOptions?.maxOutputChars, WEB_MCP_BUDGET.output);
101
- const searchPagesMaxOutputChars = positiveInteger(searchPagesOptions?.maxOutputChars, WEB_MCP_BUDGET.output);
102
- const getPageMarkdownMaxOutputChars = positiveInteger(getPageMarkdownOptions?.maxOutputChars, WEB_MCP_BUDGET.output);
103
- const pagesPath = toDeployedRoute("/__ai-ready/pages", baseURL);
104
- const indexPath = toDeployedRoute("/__ai-ready/pages.json", baseURL);
105
- function formatPages(pages, summaryForCount, recovery, maxOutputChars) {
106
- const pageLines = pages.map((page) => `
107
- ${[page.route, page.title, page.description].filter((part) => !!part).map(cleanSummaryPart).join(" | ")}`);
108
- let lines = [];
109
- const render = (visibleLines) => {
110
- const omitted = pageLines.length - visibleLines.length;
111
- const note = omitted ? ` ${omitted} left out to fit; ${recovery}.` : "";
112
- return `${summaryForCount(visibleLines.length)}${note}${visibleLines.join("")}`;
113
- };
114
- for (const line of pageLines) {
115
- const candidate = [...lines, line];
116
- if (maxOutputChars > 0 && render(candidate).length > maxOutputChars)
117
- break;
118
- lines = candidate;
119
- }
120
- return toolText(truncateToolOutput(render(lines), maxOutputChars));
121
- }
122
- function fetchPages(query) {
123
- return fetchResource(pagesPath, { query }).then(
124
- (result) => result._tag === "Ok" ? { _tag: "Ok", value: parsePagesResponse(result.value) } : result
125
- );
126
- }
127
- let index;
128
- function fetchIndex() {
129
- index ??= fetchResource(indexPath).then((result) => {
130
- if (result._tag === "Error")
131
- index = void 0;
132
- if (result._tag !== "Ok")
133
- return result;
134
- const data = isRecord(result.value) ? result.value.pages : void 0;
135
- return { _tag: "Ok", value: parsePageList(data) };
136
- });
137
- return index;
138
- }
139
- async function indexedRouteExists(route) {
140
- const runtime = await fetchPages({ route });
141
- reportFetchError("page index", runtime);
142
- if (runtime._tag === "Ok" && runtime.value.page)
143
- return { _tag: "Ok", value: true };
144
- const prerendered = await fetchIndex();
145
- reportFetchError("prerendered page index", prerendered);
146
- if (prerendered._tag === "Ok")
147
- return { _tag: "Ok", value: prerendered.value.some((page) => page.route === route) };
148
- if (runtime._tag === "Ok")
149
- return { _tag: "Ok", value: false };
150
- if (runtime._tag === "Error")
151
- return runtime;
152
- if (prerendered._tag === "Error")
153
- return prerendered;
154
- return { _tag: "Ok", value: false };
155
- }
156
- const tools = [
157
- {
158
- name: SITE_TOOL_CATALOG.list_pages.name,
159
- title: SITE_TOOL_CATALOG.list_pages.title,
160
- description: SITE_TOOL_CATALOG.list_pages.description,
161
- inputSchema: {
162
- type: "object",
163
- properties: {
164
- limit: {
165
- type: "number",
166
- description: `${SITE_TOOL_CATALOG.list_pages.parameters.limit} Defaults to ${listPagesLimit}.`
167
- },
168
- offset: { type: "number", description: SITE_TOOL_CATALOG.list_pages.parameters.offset }
169
- }
170
- },
171
- annotations: READ_ONLY,
172
- async execute({ limit, offset }) {
173
- const size = clamp(limit, listPagesLimit, 50);
174
- const start = Math.max(0, Math.trunc(Number(offset)) || 0);
175
- const runtime = await fetchPages({ limit: size, offset: start });
176
- reportFetchError("page index", runtime);
177
- let pages = runtime._tag === "Ok" ? runtime.value.pages || [] : [];
178
- let total = runtime._tag === "Ok" ? runtime.value.total ?? 0 : 0;
179
- if (!pages.length) {
180
- const prerendered = await fetchIndex();
181
- reportFetchError("prerendered page index", prerendered);
182
- if (prerendered._tag === "Ok") {
183
- pages = prerendered.value.slice(start, start + size);
184
- total = prerendered.value.length;
185
- } else if (runtime._tag === "Error" || prerendered._tag === "Error") {
186
- return toolError("The page index is temporarily unavailable. Retry list_pages shortly.");
187
- }
188
- }
189
- if (!pages.length)
190
- return toolText(start ? `No pages past offset ${start}.` : "This site has no indexed pages yet.");
191
- return formatPages(
192
- pages,
193
- (count) => count ? `Pages ${start + 1} to ${start + count} of ${total || pages.length}.` : `Pages from offset ${start} of ${total || pages.length}.`,
194
- "use a higher offset",
195
- listPagesMaxOutputChars
196
- );
197
- }
198
- },
199
- {
200
- name: SITE_TOOL_CATALOG.search_pages.name,
201
- title: SITE_TOOL_CATALOG.search_pages.title,
202
- description: SITE_TOOL_CATALOG.search_pages.description,
203
- inputSchema: {
204
- type: "object",
205
- properties: {
206
- query: { type: "string", description: SITE_TOOL_CATALOG.search_pages.parameters.query },
207
- limit: { type: "number", description: `How many results to return. Defaults to ${searchPagesLimit}.` }
208
- },
209
- required: ["query"]
210
- },
211
- annotations: READ_ONLY,
212
- async execute({ query, limit }) {
213
- const q = cleanSummaryPart(String(query ?? ""));
214
- if (!q)
215
- return toolError("A search query is required. Pass the words to search for as `query`.");
216
- const size = clamp(limit, searchPagesLimit, 50);
217
- const runtime = await fetchPages({ q, limit: size });
218
- reportFetchError("page search", runtime);
219
- let results = runtime._tag === "Ok" ? runtime.value.results || [] : [];
220
- if (!results.length) {
221
- const prerendered = await fetchIndex();
222
- reportFetchError("prerendered page index", prerendered);
223
- if (prerendered._tag === "Ok")
224
- results = searchIndex(prerendered.value, q, size);
225
- else if (runtime._tag === "Error" || prerendered._tag === "Error")
226
- return toolError("Page search is temporarily unavailable. Retry search_pages shortly.");
227
- }
228
- if (!results.length)
229
- return toolText(`Nothing matched "${q}". Try fewer or broader words, or call list_pages to see what the site covers.`);
230
- return formatPages(
231
- results,
232
- (count) => count ? `${count} result${count === 1 ? "" : "s"} for "${q}".` : `Results for "${q}".`,
233
- "narrow the query",
234
- searchPagesMaxOutputChars
235
- );
236
- }
237
- },
238
- {
239
- name: SITE_TOOL_CATALOG.get_page_markdown.name,
240
- title: SITE_TOOL_CATALOG.get_page_markdown.title,
241
- description: SITE_TOOL_CATALOG.get_page_markdown.description,
242
- inputSchema: {
243
- type: "object",
244
- properties: {
245
- route: { type: "string", description: SITE_TOOL_CATALOG.get_page_markdown.parameters.route }
246
- },
247
- required: ["route"]
248
- },
249
- annotations: READ_ONLY,
250
- async execute({ route }) {
251
- const path = normalizeSiteRoute(route);
252
- if (!path)
253
- return toolError("A site route is required, such as /about. Call list_pages to see the available routes.");
254
- const indexed = await indexedRouteExists(path);
255
- if (indexed._tag === "Error")
256
- return toolError("The page index is temporarily unavailable. Retry get_page_markdown shortly.");
257
- if (!indexed.value)
258
- return toolError(`No indexed page found at ${path}. Call search_pages or list_pages to find the correct route.`);
259
- const markdownPath = toDeployedRoute(toMarkdownPath(path), baseURL);
260
- const markdown = await fetchResource(markdownPath, { responseType: "text" });
261
- reportFetchError("page markdown", markdown);
262
- if (markdown._tag === "Error")
263
- return toolError(`Markdown for ${path} is temporarily unavailable. Retry get_page_markdown shortly.`);
264
- if (markdown._tag === "NotFound" || typeof markdown.value !== "string")
265
- return toolError(`No page found at ${path}. Call search_pages or list_pages to find the correct route.`);
266
- if (!markdown.value)
267
- return toolText(`The indexed page at ${path} has no markdown content.`);
268
- return toolText(truncateToolOutput(
269
- markdown.value,
270
- getPageMarkdownMaxOutputChars,
271
- `Read ${markdownPath} directly for the full page.`
272
- ));
273
- }
274
- }
275
- ];
276
- const enabled = /* @__PURE__ */ new Set([
277
- ...listPagesOptions?.enabled === false ? [] : [SITE_TOOL_CATALOG.list_pages.name],
278
- ...searchPagesOptions?.enabled === false ? [] : [SITE_TOOL_CATALOG.search_pages.name],
279
- ...getPageMarkdownOptions?.enabled === false ? [] : [SITE_TOOL_CATALOG.get_page_markdown.name]
280
- ]);
281
- return tools.filter((tool) => enabled.has(tool.name));
108
+ const baseURL = options.baseURL || "/";
109
+ const listPagesOptions = options.tools?.listPages;
110
+ const searchPagesOptions = options.tools?.searchPages;
111
+ const getPageMarkdownOptions = options.tools?.getPageMarkdown;
112
+ const listPagesLimit = clamp(listPagesOptions?.defaultLimit, 20, 50);
113
+ const searchPagesLimit = clamp(searchPagesOptions?.defaultLimit, 10, 50);
114
+ const listPagesMaxOutputChars = positiveInteger(listPagesOptions?.maxOutputChars, WEB_MCP_BUDGET.output);
115
+ const searchPagesMaxOutputChars = positiveInteger(searchPagesOptions?.maxOutputChars, WEB_MCP_BUDGET.output);
116
+ const getPageMarkdownMaxOutputChars = positiveInteger(getPageMarkdownOptions?.maxOutputChars, WEB_MCP_BUDGET.output);
117
+ const pagesPath = toDeployedRoute("/__ai-ready/pages", baseURL);
118
+ const indexPath = toDeployedRoute("/__ai-ready/pages.json", baseURL);
119
+ /**
120
+ * One page per line rather than JSON, so trimming to the output budget drops
121
+ * whole entries instead of leaving the agent with a half-parsed blob.
122
+ */
123
+ function formatPages(pages, summaryForCount, recovery, maxOutputChars) {
124
+ const pageLines = pages.map((page) => `\n${[
125
+ page.route,
126
+ page.title,
127
+ page.description
128
+ ].filter((part) => !!part).map(cleanSummaryPart).join(" | ")}`);
129
+ let lines = [];
130
+ const render = (visibleLines) => {
131
+ const omitted = pageLines.length - visibleLines.length;
132
+ const note = omitted ? ` ${omitted} left out to fit; ${recovery}.` : "";
133
+ return `${summaryForCount(visibleLines.length)}${note}${visibleLines.join("")}`;
134
+ };
135
+ for (const line of pageLines) {
136
+ const candidate = [...lines, line];
137
+ if (maxOutputChars > 0 && render(candidate).length > maxOutputChars) break;
138
+ lines = candidate;
139
+ }
140
+ return toolText(truncateToolOutput(render(lines), maxOutputChars));
141
+ }
142
+ function fetchPages(query) {
143
+ return fetchResource(pagesPath, { query }).then((result) => result._tag === "Ok" ? {
144
+ _tag: "Ok",
145
+ value: parsePagesResponse(result.value)
146
+ } : result);
147
+ }
148
+ /**
149
+ * The index written during prerendering. Used when there is no server route,
150
+ * and when the runtime database has not been populated yet.
151
+ */
152
+ let index;
153
+ function fetchIndex() {
154
+ index ??= fetchResource(indexPath).then((result) => {
155
+ if (result._tag === "Error") index = undefined;
156
+ if (result._tag !== "Ok") return result;
157
+ const data = isRecord(result.value) ? result.value.pages : undefined;
158
+ return {
159
+ _tag: "Ok",
160
+ value: parsePageList(data)
161
+ };
162
+ });
163
+ return index;
164
+ }
165
+ async function indexedRouteExists(route) {
166
+ const runtime = await fetchPages({ route });
167
+ reportFetchError("page index", runtime);
168
+ if (runtime._tag === "Ok" && runtime.value.page) return {
169
+ _tag: "Ok",
170
+ value: true
171
+ };
172
+ const prerendered = await fetchIndex();
173
+ reportFetchError("prerendered page index", prerendered);
174
+ if (prerendered._tag === "Ok") {
175
+ return {
176
+ _tag: "Ok",
177
+ value: prerendered.value.some((page) => page.route === route)
178
+ };
179
+ }
180
+ if (runtime._tag === "Ok") return {
181
+ _tag: "Ok",
182
+ value: false
183
+ };
184
+ if (runtime._tag === "Error") return runtime;
185
+ if (prerendered._tag === "Error") return prerendered;
186
+ return {
187
+ _tag: "Ok",
188
+ value: false
189
+ };
190
+ }
191
+ const tools = [
192
+ {
193
+ name: SITE_TOOL_CATALOG.list_pages.name,
194
+ title: SITE_TOOL_CATALOG.list_pages.title,
195
+ description: SITE_TOOL_CATALOG.list_pages.description,
196
+ inputSchema: {
197
+ type: "object",
198
+ properties: {
199
+ limit: {
200
+ type: "number",
201
+ description: `${SITE_TOOL_CATALOG.list_pages.parameters.limit} Defaults to ${listPagesLimit}.`
202
+ },
203
+ offset: {
204
+ type: "number",
205
+ description: SITE_TOOL_CATALOG.list_pages.parameters.offset
206
+ }
207
+ }
208
+ },
209
+ annotations: READ_ONLY,
210
+ async execute({ limit, offset }) {
211
+ const size = clamp(limit, listPagesLimit, 50);
212
+ const start = Math.max(0, Math.trunc(Number(offset)) || 0);
213
+ const runtime = await fetchPages({
214
+ limit: size,
215
+ offset: start
216
+ });
217
+ reportFetchError("page index", runtime);
218
+ let pages = runtime._tag === "Ok" ? runtime.value.pages || [] : [];
219
+ let total = runtime._tag === "Ok" ? runtime.value.total ?? 0 : 0;
220
+ if (!pages.length) {
221
+ const prerendered = await fetchIndex();
222
+ reportFetchError("prerendered page index", prerendered);
223
+ if (prerendered._tag === "Ok") {
224
+ pages = prerendered.value.slice(start, start + size);
225
+ total = prerendered.value.length;
226
+ } else if (runtime._tag === "Error" || prerendered._tag === "Error") {
227
+ return toolError("The page index is temporarily unavailable. Retry list_pages shortly.");
228
+ }
229
+ }
230
+ if (!pages.length) {
231
+ return toolText(start ? `No pages past offset ${start}.` : "This site has no indexed pages yet.");
232
+ }
233
+ return formatPages(pages, (count) => count ? `Pages ${start + 1} to ${start + count} of ${total || pages.length}.` : `Pages from offset ${start} of ${total || pages.length}.`, "use a higher offset", listPagesMaxOutputChars);
234
+ }
235
+ },
236
+ {
237
+ name: SITE_TOOL_CATALOG.search_pages.name,
238
+ title: SITE_TOOL_CATALOG.search_pages.title,
239
+ description: SITE_TOOL_CATALOG.search_pages.description,
240
+ inputSchema: {
241
+ type: "object",
242
+ properties: {
243
+ query: {
244
+ type: "string",
245
+ description: SITE_TOOL_CATALOG.search_pages.parameters.query
246
+ },
247
+ limit: {
248
+ type: "number",
249
+ description: `How many results to return. Defaults to ${searchPagesLimit}.`
250
+ }
251
+ },
252
+ required: ["query"]
253
+ },
254
+ annotations: READ_ONLY,
255
+ async execute({ query, limit }) {
256
+ const q = cleanSummaryPart(String(query ?? ""));
257
+ if (!q) return toolError("A search query is required. Pass the words to search for as `query`.");
258
+ const size = clamp(limit, searchPagesLimit, 50);
259
+ const runtime = await fetchPages({
260
+ q,
261
+ limit: size
262
+ });
263
+ reportFetchError("page search", runtime);
264
+ let results = runtime._tag === "Ok" ? runtime.value.results || [] : [];
265
+ if (!results.length) {
266
+ const prerendered = await fetchIndex();
267
+ reportFetchError("prerendered page index", prerendered);
268
+ if (prerendered._tag === "Ok") results = searchIndex(prerendered.value, q, size);
269
+ else if (runtime._tag === "Error" || prerendered._tag === "Error") return toolError("Page search is temporarily unavailable. Retry search_pages shortly.");
270
+ }
271
+ if (!results.length) return toolText(`Nothing matched "${q}". Try fewer or broader words, or call list_pages to see what the site covers.`);
272
+ return formatPages(results, (count) => count ? `${count} result${count === 1 ? "" : "s"} for "${q}".` : `Results for "${q}".`, "narrow the query", searchPagesMaxOutputChars);
273
+ }
274
+ },
275
+ {
276
+ name: SITE_TOOL_CATALOG.get_page_markdown.name,
277
+ title: SITE_TOOL_CATALOG.get_page_markdown.title,
278
+ description: SITE_TOOL_CATALOG.get_page_markdown.description,
279
+ inputSchema: {
280
+ type: "object",
281
+ properties: { route: {
282
+ type: "string",
283
+ description: SITE_TOOL_CATALOG.get_page_markdown.parameters.route
284
+ } },
285
+ required: ["route"]
286
+ },
287
+ annotations: READ_ONLY,
288
+ async execute({ route }) {
289
+ const path = normalizeSiteRoute(route);
290
+ if (!path) return toolError("A site route is required, such as /about. Call list_pages to see the available routes.");
291
+ const indexed = await indexedRouteExists(path);
292
+ if (indexed._tag === "Error") return toolError("The page index is temporarily unavailable. Retry get_page_markdown shortly.");
293
+ if (!indexed.value) return toolError(`No indexed page found at ${path}. Call search_pages or list_pages to find the correct route.`);
294
+ const markdownPath = toDeployedRoute(toMarkdownPath(path), baseURL);
295
+ const markdown = await fetchResource(markdownPath, { responseType: "text" });
296
+ reportFetchError("page markdown", markdown);
297
+ if (markdown._tag === "Error") return toolError(`Markdown for ${path} is temporarily unavailable. Retry get_page_markdown shortly.`);
298
+ if (markdown._tag === "NotFound" || typeof markdown.value !== "string") return toolError(`No page found at ${path}. Call search_pages or list_pages to find the correct route.`);
299
+ if (!markdown.value) return toolText(`The indexed page at ${path} has no markdown content.`);
300
+ return toolText(truncateToolOutput(markdown.value, getPageMarkdownMaxOutputChars, `Read ${markdownPath} directly for the full page.`));
301
+ }
302
+ }
303
+ ];
304
+ const enabled = new Set([
305
+ ...listPagesOptions?.enabled === false ? [] : [SITE_TOOL_CATALOG.list_pages.name],
306
+ ...searchPagesOptions?.enabled === false ? [] : [SITE_TOOL_CATALOG.search_pages.name],
307
+ ...getPageMarkdownOptions?.enabled === false ? [] : [SITE_TOOL_CATALOG.get_page_markdown.name]
308
+ ]);
309
+ return tools.filter((tool) => enabled.has(tool.name));
282
310
  }