milieu-cli 0.3.2 → 0.4.0

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 (330) hide show
  1. package/README.md +177 -76
  2. package/dist/bridges/context/agents-json.d.ts +0 -1
  3. package/dist/bridges/context/agents-json.js +21 -12
  4. package/dist/bridges/context/auth-clarity.d.ts +0 -1
  5. package/dist/bridges/context/auth-clarity.js +11 -9
  6. package/dist/bridges/context/auth-legibility.d.ts +2 -2
  7. package/dist/bridges/context/auth-legibility.js +67 -60
  8. package/dist/bridges/context/index.d.ts +1 -2
  9. package/dist/bridges/context/index.js +19 -40
  10. package/dist/bridges/context/oauth-metadata.d.ts +14 -0
  11. package/dist/bridges/context/oauth-metadata.js +127 -0
  12. package/dist/bridges/context/probe.d.ts +33 -0
  13. package/dist/bridges/context/probe.js +157 -0
  14. package/dist/bridges/context/rate-limits.d.ts +5 -2
  15. package/dist/bridges/context/rate-limits.js +27 -51
  16. package/dist/bridges/context/tos-contact.d.ts +13 -4
  17. package/dist/bridges/context/tos-contact.js +108 -46
  18. package/dist/bridges/context/versioning.d.ts +1 -2
  19. package/dist/bridges/context/versioning.js +42 -7
  20. package/dist/bridges/index.d.ts +0 -1
  21. package/dist/bridges/index.js +0 -1
  22. package/dist/bridges/reachability/crawler-policy.d.ts +30 -10
  23. package/dist/bridges/reachability/crawler-policy.js +41 -56
  24. package/dist/bridges/reachability/http-status.d.ts +3 -2
  25. package/dist/bridges/reachability/http-status.js +18 -3
  26. package/dist/bridges/reachability/https-check.d.ts +2 -2
  27. package/dist/bridges/reachability/https-check.js +6 -3
  28. package/dist/bridges/reachability/index.d.ts +1 -2
  29. package/dist/bridges/reachability/index.js +34 -43
  30. package/dist/bridges/reachability/meta-robots.d.ts +0 -13
  31. package/dist/bridges/reachability/meta-robots.js +89 -101
  32. package/dist/bridges/reachability/robots-parser.d.ts +7 -7
  33. package/dist/bridges/reachability/robots-parser.js +65 -15
  34. package/dist/bridges/reachability/robots-txt.d.ts +4 -2
  35. package/dist/bridges/reachability/robots-txt.js +33 -60
  36. package/dist/bridges/schema/descriptions.d.ts +2 -2
  37. package/dist/bridges/schema/descriptions.js +79 -44
  38. package/dist/bridges/schema/error-schemas.d.ts +2 -2
  39. package/dist/bridges/schema/error-schemas.js +31 -18
  40. package/dist/bridges/schema/index.d.ts +2 -2
  41. package/dist/bridges/schema/index.js +48 -30
  42. package/dist/bridges/schema/oas-types.d.ts +65 -26
  43. package/dist/bridges/schema/oas-types.js +37 -7
  44. package/dist/bridges/schema/operation-ids.d.ts +2 -2
  45. package/dist/bridges/schema/operation-ids.js +2 -3
  46. package/dist/bridges/schema/required-fields.d.ts +2 -2
  47. package/dist/bridges/schema/required-fields.js +12 -10
  48. package/dist/bridges/schema/schema-sources.d.ts +19 -0
  49. package/dist/bridges/schema/schema-sources.js +182 -0
  50. package/dist/bridges/schema/schema-types.d.ts +2 -2
  51. package/dist/bridges/schema/schema-types.js +20 -21
  52. package/dist/bridges/separation/api-presence.d.ts +3 -15
  53. package/dist/bridges/separation/api-presence.js +34 -85
  54. package/dist/bridges/separation/developer-docs.d.ts +6 -18
  55. package/dist/bridges/separation/developer-docs.js +100 -99
  56. package/dist/bridges/separation/docs-discovery.d.ts +28 -0
  57. package/dist/bridges/separation/docs-discovery.js +95 -0
  58. package/dist/bridges/separation/evidence-summary.d.ts +2 -0
  59. package/dist/bridges/separation/evidence-summary.js +67 -0
  60. package/dist/bridges/separation/index.d.ts +2 -19
  61. package/dist/bridges/separation/index.js +35 -39
  62. package/dist/bridges/separation/published-routes.d.ts +69 -0
  63. package/dist/bridges/separation/published-routes.js +271 -0
  64. package/dist/bridges/separation/sdk-references.d.ts +8 -3
  65. package/dist/bridges/separation/sdk-references.js +86 -61
  66. package/dist/bridges/separation/webhook-support.d.ts +3 -23
  67. package/dist/bridges/separation/webhook-support.js +61 -162
  68. package/dist/bridges/standards/a2a-agent-card.d.ts +0 -1
  69. package/dist/bridges/standards/a2a-agent-card.js +24 -9
  70. package/dist/bridges/standards/graphql.d.ts +3 -4
  71. package/dist/bridges/standards/graphql.js +70 -66
  72. package/dist/bridges/standards/index.d.ts +1 -2
  73. package/dist/bridges/standards/index.js +57 -47
  74. package/dist/bridges/standards/json-ld.d.ts +25 -11
  75. package/dist/bridges/standards/json-ld.js +114 -56
  76. package/dist/bridges/standards/llms-txt.d.ts +4 -2
  77. package/dist/bridges/standards/llms-txt.js +46 -32
  78. package/dist/bridges/standards/markdown-negotiation.d.ts +4 -1
  79. package/dist/bridges/standards/markdown-negotiation.js +16 -11
  80. package/dist/bridges/standards/mcp.d.ts +3 -12
  81. package/dist/bridges/standards/mcp.js +89 -132
  82. package/dist/bridges/standards/openapi.d.ts +7 -15
  83. package/dist/bridges/standards/openapi.js +657 -207
  84. package/dist/bridges/standards/probe-evidence.d.ts +30 -0
  85. package/dist/bridges/standards/probe-evidence.js +90 -0
  86. package/dist/bridges/standards/schema-org.d.ts +15 -9
  87. package/dist/bridges/standards/schema-org.js +38 -64
  88. package/dist/bridges/standards/sitemap.d.ts +2 -3
  89. package/dist/bridges/standards/sitemap.js +161 -169
  90. package/dist/bridges/standards/well-known.d.ts +3 -7
  91. package/dist/bridges/standards/well-known.js +28 -12
  92. package/dist/cli/index.d.ts +1 -1
  93. package/dist/cli/index.js +107 -22
  94. package/dist/cli/journey-commands.d.ts +3 -0
  95. package/dist/cli/journey-commands.js +35 -0
  96. package/dist/cli/read-report.d.ts +2 -0
  97. package/dist/cli/read-report.js +11 -0
  98. package/dist/core/bridge-output.d.ts +11 -0
  99. package/dist/core/bridge-output.js +86 -0
  100. package/dist/core/bridge-session.d.ts +28 -0
  101. package/dist/core/bridge-session.js +58 -0
  102. package/dist/core/bridge-worker.d.ts +1 -0
  103. package/dist/core/bridge-worker.js +66 -0
  104. package/dist/core/diff.d.ts +79 -0
  105. package/dist/core/diff.js +499 -0
  106. package/dist/core/explanations.d.ts +1 -3
  107. package/dist/core/explanations.js +36 -59
  108. package/dist/core/index.d.ts +4 -2
  109. package/dist/core/index.js +2 -3
  110. package/dist/core/scan-options.d.ts +0 -1
  111. package/dist/core/scan-options.js +14 -6
  112. package/dist/core/scan.d.ts +0 -1
  113. package/dist/core/scan.js +91 -41
  114. package/dist/core/scoring.d.ts +6 -0
  115. package/dist/core/scoring.js +27 -0
  116. package/dist/core/types.d.ts +97 -61
  117. package/dist/core/types.js +9 -3
  118. package/dist/core/validate-report.d.ts +5 -0
  119. package/dist/core/validate-report.js +95 -0
  120. package/dist/core/version.d.ts +0 -1
  121. package/dist/core/version.js +0 -1
  122. package/dist/index.d.ts +0 -1
  123. package/dist/index.js +0 -1
  124. package/dist/journey/body-worker.d.ts +27 -0
  125. package/dist/journey/body-worker.js +140 -0
  126. package/dist/journey/case.d.ts +74 -0
  127. package/dist/journey/case.js +187 -0
  128. package/dist/journey/cli.d.ts +14 -0
  129. package/dist/journey/cli.js +226 -0
  130. package/dist/journey/compare.d.ts +43 -0
  131. package/dist/journey/compare.js +83 -0
  132. package/dist/journey/contracts.d.ts +143 -0
  133. package/dist/journey/contracts.js +37 -0
  134. package/dist/journey/decision.d.ts +72 -0
  135. package/dist/journey/decision.js +121 -0
  136. package/dist/journey/demo.d.ts +36 -0
  137. package/dist/journey/demo.js +137 -0
  138. package/dist/journey/document.d.ts +8 -0
  139. package/dist/journey/document.js +113 -0
  140. package/dist/journey/excerpt.d.ts +6 -0
  141. package/dist/journey/excerpt.js +54 -0
  142. package/dist/journey/name-guidance.d.ts +2 -0
  143. package/dist/journey/name-guidance.js +41 -0
  144. package/dist/journey/providers.d.ts +34 -0
  145. package/dist/journey/providers.js +197 -0
  146. package/dist/journey/report-validation.d.ts +3 -0
  147. package/dist/journey/report-validation.js +457 -0
  148. package/dist/journey/runner.d.ts +12 -0
  149. package/dist/journey/runner.js +321 -0
  150. package/dist/journey/scope.d.ts +8 -0
  151. package/dist/journey/scope.js +50 -0
  152. package/dist/journey/transport.d.ts +3 -0
  153. package/dist/journey/transport.js +45 -0
  154. package/dist/journey/verify.d.ts +21 -0
  155. package/dist/journey/verify.js +213 -0
  156. package/dist/render/colors.d.ts +6 -7
  157. package/dist/render/colors.js +16 -25
  158. package/dist/render/discovery-guidance.d.ts +3 -0
  159. package/dist/render/discovery-guidance.js +66 -0
  160. package/dist/render/format-bridge.d.ts +1 -2
  161. package/dist/render/format-bridge.js +22 -17
  162. package/dist/render/format-diff.d.ts +2 -0
  163. package/dist/render/format-diff.js +169 -0
  164. package/dist/render/format-scan.d.ts +0 -1
  165. package/dist/render/format-scan.js +6 -4
  166. package/dist/render/format-verbose.d.ts +0 -1
  167. package/dist/render/format-verbose.js +7 -4
  168. package/dist/render/index.d.ts +0 -1
  169. package/dist/render/index.js +1 -2
  170. package/dist/render/oauth-observations.d.ts +4 -0
  171. package/dist/render/oauth-observations.js +27 -0
  172. package/dist/render/progress-bar.d.ts +1 -2
  173. package/dist/render/progress-bar.js +8 -6
  174. package/dist/render/robots-inventory.d.ts +3 -0
  175. package/dist/render/robots-inventory.js +38 -0
  176. package/dist/render/symbols.d.ts +4 -10
  177. package/dist/render/symbols.js +15 -17
  178. package/dist/render/terminal-text.d.ts +1 -3
  179. package/dist/render/terminal-text.js +1 -5
  180. package/dist/utils/analysis-worker-entry.d.ts +3 -0
  181. package/dist/utils/analysis-worker-entry.js +12 -0
  182. package/dist/utils/analysis-worker.d.ts +12 -0
  183. package/dist/utils/analysis-worker.js +54 -0
  184. package/dist/utils/html.d.ts +39 -0
  185. package/dist/utils/html.js +261 -0
  186. package/dist/utils/http-client.d.ts +24 -17
  187. package/dist/utils/http-client.js +166 -200
  188. package/dist/utils/http-types.d.ts +43 -0
  189. package/dist/utils/http-types.js +11 -0
  190. package/dist/utils/index.d.ts +0 -1
  191. package/dist/utils/index.js +0 -1
  192. package/dist/utils/package-registry.d.ts +11 -0
  193. package/dist/utils/package-registry.js +69 -0
  194. package/dist/utils/read-json-file.d.ts +3 -0
  195. package/dist/utils/read-json-file.js +41 -0
  196. package/dist/utils/record.d.ts +1 -0
  197. package/dist/utils/record.js +3 -0
  198. package/dist/utils/response-body.d.ts +7 -0
  199. package/dist/utils/response-body.js +71 -0
  200. package/dist/utils/ssrf.d.ts +11 -15
  201. package/dist/utils/ssrf.js +80 -44
  202. package/dist/utils/terminal-text.d.ts +2 -0
  203. package/dist/utils/terminal-text.js +4 -0
  204. package/dist/utils/url.d.ts +29 -1
  205. package/dist/utils/url.js +147 -17
  206. package/docs/examples/onboarding/account-claim.json +25 -0
  207. package/docs/examples/onboarding/pages/account-claim.html +3 -0
  208. package/docs/examples/onboarding/pages/production-activation.html +3 -0
  209. package/docs/examples/onboarding/pages/test-credential.html +3 -0
  210. package/docs/examples/onboarding/production-activation.json +25 -0
  211. package/docs/examples/onboarding/test-credential.json +25 -0
  212. package/docs/journey-qualification.md +46 -0
  213. package/docs/journeys.md +83 -0
  214. package/package.json +28 -8
  215. package/dist/bridges/context/agents-json.d.ts.map +0 -1
  216. package/dist/bridges/context/agents-json.js.map +0 -1
  217. package/dist/bridges/context/auth-clarity.d.ts.map +0 -1
  218. package/dist/bridges/context/auth-clarity.js.map +0 -1
  219. package/dist/bridges/context/auth-legibility.d.ts.map +0 -1
  220. package/dist/bridges/context/auth-legibility.js.map +0 -1
  221. package/dist/bridges/context/index.d.ts.map +0 -1
  222. package/dist/bridges/context/index.js.map +0 -1
  223. package/dist/bridges/context/rate-limits.d.ts.map +0 -1
  224. package/dist/bridges/context/rate-limits.js.map +0 -1
  225. package/dist/bridges/context/tos-contact.d.ts.map +0 -1
  226. package/dist/bridges/context/tos-contact.js.map +0 -1
  227. package/dist/bridges/context/versioning.d.ts.map +0 -1
  228. package/dist/bridges/context/versioning.js.map +0 -1
  229. package/dist/bridges/index.d.ts.map +0 -1
  230. package/dist/bridges/index.js.map +0 -1
  231. package/dist/bridges/reachability/crawler-policy.d.ts.map +0 -1
  232. package/dist/bridges/reachability/crawler-policy.js.map +0 -1
  233. package/dist/bridges/reachability/http-status.d.ts.map +0 -1
  234. package/dist/bridges/reachability/http-status.js.map +0 -1
  235. package/dist/bridges/reachability/https-check.d.ts.map +0 -1
  236. package/dist/bridges/reachability/https-check.js.map +0 -1
  237. package/dist/bridges/reachability/index.d.ts.map +0 -1
  238. package/dist/bridges/reachability/index.js.map +0 -1
  239. package/dist/bridges/reachability/meta-robots.d.ts.map +0 -1
  240. package/dist/bridges/reachability/meta-robots.js.map +0 -1
  241. package/dist/bridges/reachability/robots-parser.d.ts.map +0 -1
  242. package/dist/bridges/reachability/robots-parser.js.map +0 -1
  243. package/dist/bridges/reachability/robots-txt.d.ts.map +0 -1
  244. package/dist/bridges/reachability/robots-txt.js.map +0 -1
  245. package/dist/bridges/schema/descriptions.d.ts.map +0 -1
  246. package/dist/bridges/schema/descriptions.js.map +0 -1
  247. package/dist/bridges/schema/error-schemas.d.ts.map +0 -1
  248. package/dist/bridges/schema/error-schemas.js.map +0 -1
  249. package/dist/bridges/schema/index.d.ts.map +0 -1
  250. package/dist/bridges/schema/index.js.map +0 -1
  251. package/dist/bridges/schema/oas-types.d.ts.map +0 -1
  252. package/dist/bridges/schema/oas-types.js.map +0 -1
  253. package/dist/bridges/schema/operation-ids.d.ts.map +0 -1
  254. package/dist/bridges/schema/operation-ids.js.map +0 -1
  255. package/dist/bridges/schema/required-fields.d.ts.map +0 -1
  256. package/dist/bridges/schema/required-fields.js.map +0 -1
  257. package/dist/bridges/schema/schema-types.d.ts.map +0 -1
  258. package/dist/bridges/schema/schema-types.js.map +0 -1
  259. package/dist/bridges/separation/api-presence.d.ts.map +0 -1
  260. package/dist/bridges/separation/api-presence.js.map +0 -1
  261. package/dist/bridges/separation/developer-docs.d.ts.map +0 -1
  262. package/dist/bridges/separation/developer-docs.js.map +0 -1
  263. package/dist/bridges/separation/index.d.ts.map +0 -1
  264. package/dist/bridges/separation/index.js.map +0 -1
  265. package/dist/bridges/separation/sdk-references.d.ts.map +0 -1
  266. package/dist/bridges/separation/sdk-references.js.map +0 -1
  267. package/dist/bridges/separation/webhook-support.d.ts.map +0 -1
  268. package/dist/bridges/separation/webhook-support.js.map +0 -1
  269. package/dist/bridges/standards/a2a-agent-card.d.ts.map +0 -1
  270. package/dist/bridges/standards/a2a-agent-card.js.map +0 -1
  271. package/dist/bridges/standards/graphql.d.ts.map +0 -1
  272. package/dist/bridges/standards/graphql.js.map +0 -1
  273. package/dist/bridges/standards/index.d.ts.map +0 -1
  274. package/dist/bridges/standards/index.js.map +0 -1
  275. package/dist/bridges/standards/json-ld.d.ts.map +0 -1
  276. package/dist/bridges/standards/json-ld.js.map +0 -1
  277. package/dist/bridges/standards/llms-txt.d.ts.map +0 -1
  278. package/dist/bridges/standards/llms-txt.js.map +0 -1
  279. package/dist/bridges/standards/markdown-negotiation.d.ts.map +0 -1
  280. package/dist/bridges/standards/markdown-negotiation.js.map +0 -1
  281. package/dist/bridges/standards/mcp.d.ts.map +0 -1
  282. package/dist/bridges/standards/mcp.js.map +0 -1
  283. package/dist/bridges/standards/openapi.d.ts.map +0 -1
  284. package/dist/bridges/standards/openapi.js.map +0 -1
  285. package/dist/bridges/standards/schema-org.d.ts.map +0 -1
  286. package/dist/bridges/standards/schema-org.js.map +0 -1
  287. package/dist/bridges/standards/sitemap.d.ts.map +0 -1
  288. package/dist/bridges/standards/sitemap.js.map +0 -1
  289. package/dist/bridges/standards/well-known.d.ts.map +0 -1
  290. package/dist/bridges/standards/well-known.js.map +0 -1
  291. package/dist/cli/index.d.ts.map +0 -1
  292. package/dist/cli/index.js.map +0 -1
  293. package/dist/core/explanations.d.ts.map +0 -1
  294. package/dist/core/explanations.js.map +0 -1
  295. package/dist/core/index.d.ts.map +0 -1
  296. package/dist/core/index.js.map +0 -1
  297. package/dist/core/scan-options.d.ts.map +0 -1
  298. package/dist/core/scan-options.js.map +0 -1
  299. package/dist/core/scan.d.ts.map +0 -1
  300. package/dist/core/scan.js.map +0 -1
  301. package/dist/core/types.d.ts.map +0 -1
  302. package/dist/core/types.js.map +0 -1
  303. package/dist/core/version.d.ts.map +0 -1
  304. package/dist/core/version.js.map +0 -1
  305. package/dist/index.d.ts.map +0 -1
  306. package/dist/index.js.map +0 -1
  307. package/dist/render/colors.d.ts.map +0 -1
  308. package/dist/render/colors.js.map +0 -1
  309. package/dist/render/format-bridge.d.ts.map +0 -1
  310. package/dist/render/format-bridge.js.map +0 -1
  311. package/dist/render/format-scan.d.ts.map +0 -1
  312. package/dist/render/format-scan.js.map +0 -1
  313. package/dist/render/format-verbose.d.ts.map +0 -1
  314. package/dist/render/format-verbose.js.map +0 -1
  315. package/dist/render/index.d.ts.map +0 -1
  316. package/dist/render/index.js.map +0 -1
  317. package/dist/render/progress-bar.d.ts.map +0 -1
  318. package/dist/render/progress-bar.js.map +0 -1
  319. package/dist/render/symbols.d.ts.map +0 -1
  320. package/dist/render/symbols.js.map +0 -1
  321. package/dist/render/terminal-text.d.ts.map +0 -1
  322. package/dist/render/terminal-text.js.map +0 -1
  323. package/dist/utils/http-client.d.ts.map +0 -1
  324. package/dist/utils/http-client.js.map +0 -1
  325. package/dist/utils/index.d.ts.map +0 -1
  326. package/dist/utils/index.js.map +0 -1
  327. package/dist/utils/ssrf.d.ts.map +0 -1
  328. package/dist/utils/ssrf.js.map +0 -1
  329. package/dist/utils/url.d.ts.map +0 -1
  330. package/dist/utils/url.js.map +0 -1
@@ -0,0 +1,6 @@
1
+ export declare function containsRoute(text: string, path: string, begin?: number, end?: number): boolean;
2
+ export interface ExcerptRead {
3
+ coverage: "complete" | "clipped" | "redacted";
4
+ matched?: string;
5
+ }
6
+ export declare function examineExcerpt(original: string, safe: string, visible: string, bytes: number, expected: string, path: string): ExcerptRead;
@@ -0,0 +1,54 @@
1
+ import { clip, normalize } from "./document.js";
2
+ /** A whole path starts/ends at a text edge, whitespace, quote, inline-code
3
+ * delimiter or matching side of a bracket. URI continuations and ambiguous
4
+ * sentence punctuation (e.g. dots, matrix parameters and commas) stay attached. */
5
+ function startsPath(char) {
6
+ return !char || /[\s"'(\[{<`]/u.test(char);
7
+ }
8
+ function endsPath(char) {
9
+ return !char || /[\s"')\]}>`]/u.test(char);
10
+ }
11
+ export function containsRoute(text, path, begin = 0, end = text.length) {
12
+ if (!path)
13
+ return false;
14
+ for (let at = text.indexOf(path, begin); at !== -1 && at + path.length <= end; at = text.indexOf(path, at + 1)) {
15
+ if (startsPath(text.charAt(at - 1)) && endsPath(text.charAt(at + path.length)))
16
+ return true;
17
+ }
18
+ return false;
19
+ }
20
+ // Reject fragments inside alphabetic words. Han and kana text can put the next
21
+ // word directly against the excerpt, without a space or punctuation.
22
+ function joinsWord(left, right) {
23
+ const word = /[\p{L}\p{N}_]/u;
24
+ const unspaced = /[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}]/u;
25
+ return word.test(left) && word.test(right) && !unspaced.test(left) && !unspaced.test(right);
26
+ }
27
+ function matchedSpan(text, excerpt, path, visibleEnd) {
28
+ if (!excerpt)
29
+ return undefined;
30
+ for (let at = text.indexOf(excerpt); at !== -1 && at + excerpt.length <= visibleEnd; at = text.indexOf(excerpt, at + 1)) {
31
+ // Check the delimiter against the full normalized text, including a lookahead
32
+ // after a clipped observation. Only span characters actually shown can match.
33
+ // Do not find a verb inside a different word (e.g. FORGET /catalog).
34
+ if (at > 0 && joinsWord(text[at - 1], excerpt[0]))
35
+ continue;
36
+ const end = at + excerpt.length;
37
+ if (end < text.length && joinsWord(excerpt.at(-1), text[end]))
38
+ continue;
39
+ if (containsRoute(text, path, at, end))
40
+ return text.slice(at, at + excerpt.length);
41
+ }
42
+ return undefined;
43
+ }
44
+ export function examineExcerpt(original, safe, visible, bytes, expected, path) {
45
+ const excerpt = normalize(expected);
46
+ const matched = matchedSpan(safe, excerpt, path, visible.length);
47
+ if (original !== safe) {
48
+ const originalMatch = matchedSpan(original, excerpt, path, clip(original, bytes).length);
49
+ if ((matched !== undefined) !== (originalMatch !== undefined))
50
+ return { coverage: "redacted" };
51
+ }
52
+ const coverage = visible.length === safe.length ? "complete" : "clipped";
53
+ return matched === undefined ? { coverage } : { coverage, matched };
54
+ }
@@ -0,0 +1,2 @@
1
+ export declare function requestedNameType(task: string): string | undefined;
2
+ export declare function nameFormatHint(type: string, visibleText: string): string;
@@ -0,0 +1,41 @@
1
+ const nameRequest = /\b(?:answer\s+with|respond\s+with|return|give|provide|report|state|enter|what\s+is)\s+(?:(?:only|just|the|its|their|exact|official|full|formal)\s+)*(?:([a-z][a-z-]{0,30})\s+)?name\b/i;
2
+ const nameDeclaration = /\b(?:official|formal|registered|full)\s+(?:[\w-]+\s+){0,2}name\b|\bname\s*(?::|=|\bis\b)|\b(?:named|called|known as)\b/i;
3
+ const quotedLabels = /"([^"\n]{1,160})"|“([^“”\n]{1,160})”|‘([^‘’\n]{1,160})’|«([^«»\n]{1,160})»|(?:^|[\s(:=])'([^'\n]{1,160})'(?=$|[\s.,;:!?)])/gu;
4
+ const genericNameWords = new Set(["your", "my", "first", "last", "full", "given", "family", "their", "user"]);
5
+ // Ordinary name text cannot consume sentence punctuation. A dotted initial is
6
+ // a separate token in a proper name, as in “A. B. Frost” or “J.P.”.
7
+ const properWord = String.raw `[\p{Lu}\p{Lt}\d][\p{L}\p{M}\d’'&-]*`;
8
+ const initials = String.raw `(?:[\p{Lu}\p{Lt}]\.)+`;
9
+ const nameToken = `(?:${initials}|${properWord})`;
10
+ const initialledName = new RegExp(`^${nameToken}(?: +${nameToken})*$`, "u");
11
+ // Only the public question and actor-visible page text select the fixed formatting cue.
12
+ export function requestedNameType(task) {
13
+ const request = nameRequest.exec(task);
14
+ if (!request || /\b(?:do not|don't|never)\s*$/i.test(task.slice(0, request.index)))
15
+ return;
16
+ const type = request[1]
17
+ ?? /^\s+of (?:the|this|that) ([a-z][a-z-]{0,30})\b/i.exec(task.slice(request.index + request[0].length))?.[1]
18
+ ?? /\bwhich ([a-z][a-z-]{0,30})\b/i.exec(task)?.[1];
19
+ const lower = type?.toLowerCase();
20
+ return lower && !genericNameWords.has(lower) ? lower : undefined;
21
+ }
22
+ export function nameFormatHint(type, visibleText) {
23
+ if (!/^[a-z][a-z-]{0,30}$/.test(type))
24
+ return "";
25
+ // An explicitly named or quoted label may contain a type word, even in lowercase.
26
+ // Leave those pages to the actor's ordinary exact-value instruction.
27
+ const typeWord = new RegExp(String.raw `\b${type}\b`, "iu");
28
+ if (nameDeclaration.test(visibleText))
29
+ return "";
30
+ for (const [quoted] of visibleText.matchAll(quotedLabels))
31
+ if (typeWord.test(quoted))
32
+ return "";
33
+ // Consume only the article so a rejected frame cannot hide a later valid one.
34
+ const frame = new RegExp(String.raw `\b[Tt]he +(?=([\p{Lu}\p{Lt}\d][\p{L}\p{M}\d’'&. -]{0,70}?) +${type}\b)`, "gu");
35
+ for (const [, label] of visibleText.matchAll(frame)) {
36
+ if (!label.includes(".") || initialledName.test(label)) {
37
+ return `For the requested ${type} name, fill the NAME slot in “the [NAME] ${type}”. The surrounding article and descriptive word “${type}” belong to the sentence. Put the distinctive name alone in answer and the page citation in evidenceIds. Follow the task's instructions if the page does not establish the answer.`;
38
+ }
39
+ }
40
+ return "";
41
+ }
@@ -0,0 +1,34 @@
1
+ import { type ModelBackend, type ModelIdentity } from "./contracts.js";
2
+ type ProviderName = "ollama" | "openai";
3
+ type Fetch = typeof globalThis.fetch;
4
+ /** Redact at the CLI output boundary too, including pre-adapter validation errors. */
5
+ export declare function redactCredential(text: string): string;
6
+ export declare function createProvider(provider: string, requested: string, signal?: AbortSignal, fetcher?: Fetch): Promise<ModelBackend>;
7
+ export declare function doctor(options: {
8
+ provider?: string;
9
+ model?: string;
10
+ probe?: boolean;
11
+ }, signal?: AbortSignal, fetcher?: Fetch): Promise<{
12
+ setup: {
13
+ ollama: string;
14
+ openai: string;
15
+ };
16
+ probed: boolean;
17
+ schemaVersion: 1;
18
+ kind: "journey_doctor";
19
+ } | {
20
+ provider: ProviderName;
21
+ installedModels: number | undefined;
22
+ setup: string;
23
+ probed: boolean;
24
+ schemaVersion: 1;
25
+ kind: "journey_doctor";
26
+ } | {
27
+ provider: ProviderName;
28
+ model: ModelIdentity;
29
+ setup: string;
30
+ probed: boolean;
31
+ schemaVersion: 1;
32
+ kind: "journey_doctor";
33
+ }>;
34
+ export {};
@@ -0,0 +1,197 @@
1
+ import { redactLiteral, budgets, JourneyFault } from "./contracts.js";
2
+ import { decisionSchema, parseDecision, readDecisionContent } from "./decision.js";
3
+ import { isRecord } from "../utils/record.js";
4
+ const ollama = "http://127.0.0.1:11434/api";
5
+ const openai = "https://api.openai.com/v1/chat/completions";
6
+ const record = (value) => isRecord(value) ? value : {};
7
+ /** Redact at the CLI output boundary too, including pre-adapter validation errors. */
8
+ export function redactCredential(text) {
9
+ return redactLiteral(text, process.env.OPENAI_API_KEY?.trim());
10
+ }
11
+ function providerName(name) {
12
+ if (name !== "ollama" && name !== "openai")
13
+ throw new JourneyFault("INVALID_PROVIDER");
14
+ return name;
15
+ }
16
+ function validModel(value) {
17
+ return typeof value === "string" && /^[A-Za-z0-9][A-Za-z0-9./:_-]{0,255}$/u.test(value);
18
+ }
19
+ function requireKey() {
20
+ const key = process.env.OPENAI_API_KEY?.trim();
21
+ if (!key)
22
+ throw new JourneyFault("MODEL_AUTH");
23
+ return key;
24
+ }
25
+ /** Bound every provider response, including the local model inventory. Never emit its body. */
26
+ async function requestJson(fetcher, url, signal, payload, key) {
27
+ const timeout = AbortSignal.timeout(budgets.modelMs);
28
+ const deadline = signal ? AbortSignal.any([signal, timeout]) : timeout;
29
+ try {
30
+ deadline.throwIfAborted();
31
+ const response = await fetcher(url, {
32
+ method: payload ? "POST" : "GET", credentials: "omit", redirect: "manual", signal: deadline,
33
+ headers: { ...(payload && { "Content-Type": "application/json" }), ...(key && { Authorization: `Bearer ${key}` }) },
34
+ ...(payload && { body: JSON.stringify(payload) }),
35
+ });
36
+ if (!response.ok) {
37
+ void response.body?.cancel().catch(() => { });
38
+ const status = response.status;
39
+ if (status === 401 || status === 403)
40
+ throw new JourneyFault("MODEL_AUTH");
41
+ if (status === 429)
42
+ throw new JourneyFault("MODEL_RATE_LIMIT");
43
+ if (status >= 300 && status < 400)
44
+ throw new JourneyFault("MODEL_REDIRECT");
45
+ throw new JourneyFault("MODEL_UNAVAILABLE");
46
+ }
47
+ if (!response.body)
48
+ throw new JourneyFault("MODEL_PROTOCOL");
49
+ const reader = response.body.getReader();
50
+ const chunks = [];
51
+ let size = 0;
52
+ const cancel = () => { void reader.cancel().catch(() => { }); };
53
+ deadline.addEventListener("abort", cancel, { once: true });
54
+ try {
55
+ while (true) {
56
+ deadline.throwIfAborted();
57
+ const { done, value } = await reader.read();
58
+ deadline.throwIfAborted();
59
+ if (done)
60
+ break;
61
+ size += value.length;
62
+ if (size > budgets.responseBytes)
63
+ throw new JourneyFault("MODEL_RESPONSE_LIMIT");
64
+ chunks.push(value);
65
+ }
66
+ }
67
+ finally {
68
+ deadline.removeEventListener("abort", cancel);
69
+ cancel();
70
+ }
71
+ try {
72
+ return record(JSON.parse(Buffer.concat(chunks, size).toString("utf8")));
73
+ }
74
+ catch {
75
+ throw new JourneyFault("MODEL_PROTOCOL");
76
+ }
77
+ }
78
+ catch (error) {
79
+ if (deadline.aborted)
80
+ throw new JourneyFault(deadline.reason?.name === "TimeoutError" ? "MODEL_TIMEOUT" : "CANCELLED");
81
+ if (error instanceof JourneyFault)
82
+ throw error;
83
+ throw new JourneyFault("MODEL_UNAVAILABLE");
84
+ }
85
+ }
86
+ async function inventory(fetcher, signal) {
87
+ const { models } = await requestJson(fetcher, ollama + "/tags", signal);
88
+ if (!Array.isArray(models))
89
+ throw new JourneyFault("MODEL_PROTOCOL");
90
+ return models.map(record);
91
+ }
92
+ function installed(models, requested) {
93
+ const match = models.find(entry => entry.name === requested || entry.name === requested + ":latest");
94
+ if (!match || !validModel(match.name) || typeof match.digest !== "string" ||
95
+ !/^(?:sha256:)?[a-f\d]{64}$/iu.test(match.digest))
96
+ throw new JourneyFault("MODEL_NOT_INSTALLED");
97
+ if (match.remote_model || match.remote_host || /[:-]cloud$/iu.test(match.name)) {
98
+ throw new JourneyFault("MODEL_NOT_LOCAL");
99
+ }
100
+ return { name: match.name, digest: match.digest };
101
+ }
102
+ export async function createProvider(provider, requested, signal, fetcher = globalThis.fetch) {
103
+ const name = providerName(provider);
104
+ if (!validModel(requested) || redactCredential(requested) !== requested)
105
+ throw new JourneyFault("INVALID_MODEL");
106
+ const identity = { provider: name, requested, resolved: [], digest: null };
107
+ if (name === "ollama") {
108
+ const selected = installed(await inventory(fetcher, signal), requested);
109
+ identity.resolved.push(selected.name);
110
+ identity.digest = selected.digest;
111
+ // Allow roughly two UTF-8 bytes per token, plus space for the decision.
112
+ const contextTokens = Math.ceil(budgets.historyBytes / 2) + budgets.outputTokens;
113
+ const settings = { contextTokens, temperature: 0, seed: 0 };
114
+ identity.settings = settings;
115
+ return {
116
+ identity, credentialForRedaction: () => process.env.OPENAI_API_KEY?.trim(),
117
+ async decide(messages, turnSignal) {
118
+ const response = await requestJson(fetcher, ollama + "/chat", turnSignal, {
119
+ model: selected.name, messages, stream: false, format: decisionSchema,
120
+ options: { num_predict: budgets.outputTokens, num_ctx: contextTokens, temperature: settings.temperature, seed: settings.seed },
121
+ });
122
+ if (response.done_reason === "length")
123
+ throw new JourneyFault("MODEL_TRUNCATED");
124
+ if (response.done !== true)
125
+ throw new JourneyFault("MODEL_PROTOCOL");
126
+ return readDecisionContent(record(response.message).content);
127
+ },
128
+ async finalize(endSignal) {
129
+ const current = installed(await inventory(fetcher, endSignal), requested);
130
+ if (current.digest !== selected.digest || current.name !== selected.name)
131
+ throw new JourneyFault("MODEL_CHANGED");
132
+ },
133
+ };
134
+ }
135
+ const key = requireKey();
136
+ // These non-reasoning snapshot families support temperature sampling.
137
+ if (/^gpt-4(?:o|\.1)(?:-|$)/u.test(requested))
138
+ identity.settings = { temperature: 0 };
139
+ let missingIdentity = false;
140
+ return {
141
+ identity, credentialForRedaction: () => key,
142
+ async decide(messages, turnSignal) {
143
+ const response = await requestJson(fetcher, openai, turnSignal, {
144
+ model: requested, messages, store: false, max_completion_tokens: budgets.outputTokens,
145
+ ...identity.settings,
146
+ response_format: { type: "json_schema", json_schema: { name: "journey_decision", strict: true, schema: decisionSchema } },
147
+ }, key);
148
+ // Missing identity on any completed call makes the run incomparable.
149
+ if (!validModel(response.model) || redactLiteral(response.model, key) !== response.model) {
150
+ missingIdentity = true;
151
+ identity.resolved.length = 0;
152
+ }
153
+ else if (!missingIdentity && !identity.resolved.includes(response.model)) {
154
+ identity.resolved.push(response.model);
155
+ }
156
+ const choice = record(Array.isArray(response.choices) ? response.choices[0] : null);
157
+ const message = record(choice.message);
158
+ if (message.refusal || choice.finish_reason === "content_filter")
159
+ throw new JourneyFault("MODEL_REFUSAL");
160
+ if (choice.finish_reason === "length")
161
+ throw new JourneyFault("MODEL_TRUNCATED");
162
+ if (choice.finish_reason !== "stop")
163
+ throw new JourneyFault("MODEL_PROTOCOL");
164
+ return readDecisionContent(message.content);
165
+ },
166
+ };
167
+ }
168
+ const setup = {
169
+ ollama: "Run Ollama locally with OLLAMA_NO_CLOUD=1, install a model and pass its inventory name to --model.",
170
+ openai: "Set OPENAI_API_KEY and select a snapshot model with structured outputs using --model.",
171
+ };
172
+ const probeMessages = [{ role: "user", content: 'Return a finish decision. Set disposition to "answer", answer to "ready", linkId and path to null, and evidenceIds to an empty array.' }];
173
+ export async function doctor(options, signal, fetcher = globalThis.fetch) {
174
+ const envelope = { schemaVersion: 1, kind: "journey_doctor" };
175
+ if (!options.provider) {
176
+ if (options.model !== undefined || options.probe)
177
+ throw new JourneyFault("PROVIDER_REQUIRED");
178
+ return { ...envelope, setup, probed: false };
179
+ }
180
+ const provider = providerName(options.provider);
181
+ if (options.model === undefined) {
182
+ if (options.probe)
183
+ throw new JourneyFault("MODEL_REQUIRED");
184
+ if (provider === "openai")
185
+ requireKey();
186
+ const installedModels = provider === "ollama" ? (await inventory(fetcher, signal)).length : undefined;
187
+ return { ...envelope, provider, installedModels, setup: setup[provider], probed: false };
188
+ }
189
+ const backend = await createProvider(provider, options.model, signal, fetcher);
190
+ if (options.probe) {
191
+ const decision = parseDecision(await backend.decide(probeMessages, signal ?? AbortSignal.timeout(budgets.modelMs)));
192
+ if (typeof decision === "string" || decision.action !== "finish" || decision.disposition !== "answer" ||
193
+ decision.answer !== "ready" || decision.evidenceIds.length)
194
+ throw new JourneyFault("PROBE_FAILED");
195
+ }
196
+ return { ...envelope, provider, model: backend.identity, setup: setup[provider], probed: !!options.probe };
197
+ }
@@ -0,0 +1,3 @@
1
+ import { type RunReport } from './contracts.js';
2
+ export declare function validateRunReport(value: unknown): RunReport;
3
+ export declare function readRunReport(path: string): Promise<RunReport>;