@apifuse/provider-sdk 2.2.0-beta.5 → 2.2.0-beta.51

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 (368) hide show
  1. package/AUTHORING.md +537 -33
  2. package/CHANGELOG.md +205 -1
  3. package/README.md +83 -22
  4. package/SUBMISSION.md +2 -2
  5. package/bin/apifuse-check.ts +165 -11
  6. package/bin/apifuse-dev.ts +57 -13
  7. package/bin/apifuse-migrate-operation-declaration.ts +55 -0
  8. package/bin/apifuse-migrate-shape.ts +202 -0
  9. package/bin/apifuse-pack-check.ts +22 -2
  10. package/bin/apifuse-pack-smoke.ts +57 -2
  11. package/bin/apifuse-pack-types.ts +357 -38
  12. package/bin/apifuse-perf.ts +14 -13
  13. package/bin/apifuse-record.ts +709 -72
  14. package/bin/apifuse-submit-check.ts +2344 -324
  15. package/bin/apifuse-sync-assets.ts +117 -0
  16. package/bin/submit-check-delimited-text.ts +50 -0
  17. package/dist/auth-turn/index.d.ts +3 -3
  18. package/dist/auth-turn/index.js +1 -1
  19. package/dist/auth.d.ts +14 -0
  20. package/dist/auth.js +67 -0
  21. package/dist/ceremonies/index.d.ts +16 -0
  22. package/dist/ceremonies/index.js +141 -36
  23. package/dist/cli/commands.d.ts +1 -1
  24. package/dist/cli/commands.js +27 -0
  25. package/dist/cli/create.d.ts +4 -1
  26. package/dist/cli/create.js +42 -37
  27. package/dist/cli/migrate-operation-declaration.d.ts +59 -0
  28. package/dist/cli/migrate-operation-declaration.js +1249 -0
  29. package/dist/cli/migrate-operation-shape.d.ts +44 -0
  30. package/dist/cli/migrate-operation-shape.js +113 -0
  31. package/dist/cli/migrate-provider-shape.d.ts +52 -0
  32. package/dist/cli/migrate-provider-shape.js +578 -0
  33. package/dist/cli/prompt-assets.d.ts +80 -0
  34. package/dist/cli/prompt-assets.js +743 -0
  35. package/dist/cli/templates/provider/AGENTS.md.tpl +17 -8
  36. package/dist/cli/templates/provider/Dockerfile.tpl +1 -1
  37. package/dist/cli/templates/provider/README.md.tpl +5 -5
  38. package/dist/cli/templates/provider/index.ts.tpl +6 -3
  39. package/dist/cli/templates/provider/operations/ping.ts.tpl +4 -1
  40. package/dist/cli/templates/provider/provider.json.tpl +6 -0
  41. package/dist/config/loader.d.ts +179 -16
  42. package/dist/config/loader.js +435 -127
  43. package/dist/contract-serialization.js +4 -8
  44. package/dist/contract-types.d.ts +12 -5
  45. package/dist/contract.js +24 -10
  46. package/dist/declaration-validation.d.ts +33 -0
  47. package/dist/declaration-validation.js +266 -0
  48. package/dist/define.d.ts +66 -29
  49. package/dist/define.js +778 -69
  50. package/dist/dev.d.ts +3 -0
  51. package/dist/dev.js +1 -1
  52. package/dist/engine.d.ts +78 -0
  53. package/dist/engine.js +133 -0
  54. package/dist/error-observability.d.ts +7 -0
  55. package/dist/error-observability.js +61 -0
  56. package/dist/error-resolution.d.ts +4 -0
  57. package/dist/error-resolution.js +122 -0
  58. package/dist/errors.d.ts +33 -0
  59. package/dist/errors.js +40 -0
  60. package/dist/fixture-sanitization.d.ts +28 -0
  61. package/dist/fixture-sanitization.js +227 -0
  62. package/dist/health-scenario.d.ts +1842 -0
  63. package/dist/health-scenario.js +624 -0
  64. package/dist/index.d.ts +21 -10
  65. package/dist/index.js +12 -7
  66. package/dist/lint.d.ts +23 -11
  67. package/dist/lint.js +572 -69
  68. package/dist/native-address.d.ts +43 -0
  69. package/dist/native-address.js +281 -0
  70. package/dist/native-egress-policy.d.ts +31 -0
  71. package/dist/native-egress-policy.js +288 -0
  72. package/dist/observability.d.ts +5 -2
  73. package/dist/observability.js +48 -1
  74. package/dist/provider.d.ts +10 -2
  75. package/dist/provider.js +4 -1
  76. package/dist/runtime/auth-flow.d.ts +5 -1
  77. package/dist/runtime/auth-flow.js +6 -0
  78. package/dist/runtime/browser.d.ts +1 -0
  79. package/dist/runtime/browser.js +492 -49
  80. package/dist/runtime/cache.d.ts +1 -0
  81. package/dist/runtime/cache.js +169 -15
  82. package/dist/runtime/choice-wordlist.d.ts +9 -0
  83. package/dist/runtime/choice-wordlist.js +138 -0
  84. package/dist/runtime/choice.d.ts +13 -1
  85. package/dist/runtime/choice.js +490 -102
  86. package/dist/runtime/chrome149-header-order.d.ts +58 -0
  87. package/dist/runtime/chrome149-header-order.js +289 -0
  88. package/dist/runtime/env.js +12 -0
  89. package/dist/runtime/executor.d.ts +3 -2
  90. package/dist/runtime/executor.js +26 -25
  91. package/dist/runtime/http.d.ts +1 -0
  92. package/dist/runtime/http.js +515 -53
  93. package/dist/runtime/insights.js +2 -2
  94. package/dist/runtime/instrumentation.d.ts +2 -2
  95. package/dist/runtime/instrumentation.js +366 -8
  96. package/dist/runtime/native-network-errors.d.ts +33 -0
  97. package/dist/runtime/native-network-errors.js +69 -0
  98. package/dist/runtime/native-network.d.ts +96 -0
  99. package/dist/runtime/native-network.js +1232 -0
  100. package/dist/runtime/ocr.d.ts +29 -0
  101. package/dist/runtime/ocr.js +440 -0
  102. package/dist/runtime/otlp.d.ts +71 -2
  103. package/dist/runtime/otlp.js +397 -16
  104. package/dist/runtime/proxy-errors.js +6 -2
  105. package/dist/runtime/proxy-nodemaven.d.ts +56 -0
  106. package/dist/runtime/proxy-nodemaven.js +146 -0
  107. package/dist/runtime/proxy-telemetry.d.ts +80 -1
  108. package/dist/runtime/proxy-telemetry.js +154 -47
  109. package/dist/runtime/redirects.d.ts +29 -0
  110. package/dist/runtime/redirects.js +36 -0
  111. package/dist/runtime/redis.d.ts +1 -1
  112. package/dist/runtime/redis.js +4 -2
  113. package/dist/runtime/request-options.d.ts +68 -1
  114. package/dist/runtime/request-options.js +548 -0
  115. package/dist/runtime/resolver-config.d.ts +6 -0
  116. package/dist/runtime/resolver-config.js +6 -0
  117. package/dist/runtime/resolver-public.d.ts +1 -0
  118. package/dist/runtime/resolver-public.js +1 -0
  119. package/dist/runtime/resolver-shared.d.ts +14 -0
  120. package/dist/runtime/resolver-shared.js +12 -0
  121. package/dist/runtime/resolver-vendors/bindings.d.ts +48 -0
  122. package/dist/runtime/resolver-vendors/bindings.js +40 -0
  123. package/dist/runtime/resolver-vendors/browser.d.ts +22 -0
  124. package/dist/runtime/resolver-vendors/browser.js +377 -0
  125. package/dist/runtime/resolver-vendors/capsolver.d.ts +22 -0
  126. package/dist/runtime/resolver-vendors/capsolver.js +531 -0
  127. package/dist/runtime/resolver-vendors/hosts.d.ts +2 -0
  128. package/dist/runtime/resolver-vendors/hosts.js +33 -0
  129. package/dist/runtime/resolver-vendors/twocaptcha.d.ts +24 -0
  130. package/dist/runtime/resolver-vendors/twocaptcha.js +408 -0
  131. package/dist/runtime/resolver-vendors/types.d.ts +94 -0
  132. package/dist/runtime/resolver-vendors/types.js +96 -0
  133. package/dist/runtime/resolver.d.ts +72 -0
  134. package/dist/runtime/resolver.js +771 -0
  135. package/dist/runtime/secrets.d.ts +27 -0
  136. package/dist/runtime/secrets.js +51 -0
  137. package/dist/runtime/state.d.ts +3 -0
  138. package/dist/runtime/state.js +277 -71
  139. package/dist/runtime/stealth-cookies.d.ts +20 -0
  140. package/dist/runtime/stealth-cookies.js +111 -0
  141. package/dist/runtime/stealth.d.ts +41 -5
  142. package/dist/runtime/stealth.js +997 -291
  143. package/dist/runtime/stt.js +1 -12
  144. package/dist/runtime/timeout.d.ts +5 -0
  145. package/dist/runtime/timeout.js +12 -0
  146. package/dist/runtime/trace-config.d.ts +12 -0
  147. package/dist/runtime/trace-config.js +62 -0
  148. package/dist/runtime/trace.d.ts +5 -0
  149. package/dist/runtime/trace.js +43 -10
  150. package/dist/serve.d.ts +1 -1
  151. package/dist/serve.js +1 -1
  152. package/dist/server/error-observability.d.ts +1 -0
  153. package/dist/server/error-observability.js +1 -0
  154. package/dist/server/index.d.ts +5 -2
  155. package/dist/server/index.js +2 -2
  156. package/dist/server/self-test-input-tokens.d.ts +2 -1
  157. package/dist/server/self-test-input-tokens.js +18 -14
  158. package/dist/server/self-test.d.ts +14 -3
  159. package/dist/server/self-test.js +129 -58
  160. package/dist/server/serve-implementation.d.ts +230 -0
  161. package/dist/server/serve-implementation.js +2265 -0
  162. package/dist/server/serve.d.ts +1 -70
  163. package/dist/server/serve.js +1 -1131
  164. package/dist/server/trace-output.d.ts +6 -0
  165. package/dist/server/trace-output.js +79 -0
  166. package/dist/server/types.d.ts +30 -5
  167. package/dist/server/types.js +13 -1
  168. package/dist/stateful/errors.d.ts +19 -0
  169. package/dist/stateful/errors.js +24 -0
  170. package/dist/stateful/http-provider-event-emitter.d.ts +40 -0
  171. package/dist/stateful/http-provider-event-emitter.js +237 -0
  172. package/dist/stateful/http-session-owner-registry.d.ts +44 -0
  173. package/dist/stateful/http-session-owner-registry.js +210 -0
  174. package/dist/stateful/index.d.ts +18 -0
  175. package/dist/stateful/index.js +18 -0
  176. package/dist/stateful/provider-event-delivery-failures.d.ts +32 -0
  177. package/dist/stateful/provider-event-delivery-failures.js +43 -0
  178. package/dist/stateful/provider-event-pipeline-metrics.d.ts +46 -0
  179. package/dist/stateful/provider-event-pipeline-metrics.js +48 -0
  180. package/dist/stateful/provider-event-pipeline.d.ts +50 -0
  181. package/dist/stateful/provider-event-pipeline.js +1 -0
  182. package/dist/stateful/provider-events.d.ts +101 -0
  183. package/dist/stateful/provider-events.js +289 -0
  184. package/dist/stateful/session-key.d.ts +15 -0
  185. package/dist/stateful/session-key.js +86 -0
  186. package/dist/stateful/stateful-provider-adapter-context.d.ts +5 -0
  187. package/dist/stateful/stateful-provider-adapter-context.js +42 -0
  188. package/dist/stateful/stateful-provider-adapter-metrics.d.ts +15 -0
  189. package/dist/stateful/stateful-provider-adapter-metrics.js +21 -0
  190. package/dist/stateful/stateful-provider-adapter.d.ts +98 -0
  191. package/dist/stateful/stateful-provider-adapter.js +287 -0
  192. package/dist/stateful/stateful-provider-observability.d.ts +62 -0
  193. package/dist/stateful/stateful-provider-observability.js +161 -0
  194. package/dist/stateful/stateful-provider-owner-forwarder.d.ts +41 -0
  195. package/dist/stateful/stateful-provider-owner-forwarder.js +215 -0
  196. package/dist/stateful/stateful-provider-runtime-context.d.ts +32 -0
  197. package/dist/stateful/stateful-provider-runtime-context.js +60 -0
  198. package/dist/stateful/stateful-provider-runtime-executor.d.ts +34 -0
  199. package/dist/stateful/stateful-provider-runtime-executor.js +52 -0
  200. package/dist/stateful/stateful-provider-session-routing.d.ts +67 -0
  201. package/dist/stateful/stateful-provider-session-routing.js +345 -0
  202. package/dist/stateful/stateful-provider-session-runtime.d.ts +98 -0
  203. package/dist/stateful/stateful-provider-session-runtime.js +245 -0
  204. package/dist/stateful-signing.d.ts +18 -0
  205. package/dist/stateful-signing.js +27 -0
  206. package/dist/stealth/profiles.d.ts +9 -4
  207. package/dist/stealth/profiles.js +160 -211
  208. package/dist/stream-evidence.d.ts +74 -0
  209. package/dist/stream-evidence.js +785 -0
  210. package/dist/stream.js +7 -1
  211. package/dist/testing/index.d.ts +2 -1
  212. package/dist/testing/index.js +2 -1
  213. package/dist/testing/run.d.ts +32 -2
  214. package/dist/testing/run.js +489 -21
  215. package/dist/trace-sanitization.d.ts +5 -0
  216. package/dist/trace-sanitization.js +45 -0
  217. package/dist/types.d.ts +673 -131
  218. package/dist/types.js +1 -0
  219. package/package.json +45 -5
  220. package/src/auth-turn/index.ts +1 -1
  221. package/src/auth.ts +118 -0
  222. package/src/ceremonies/index.ts +189 -46
  223. package/src/cli/__tests__/fixtures/migrate-operation-declaration/approval-override.ts.txt +6 -0
  224. package/src/cli/__tests__/fixtures/migrate-operation-declaration/codemod-syntax.ts.txt +3 -0
  225. package/src/cli/__tests__/fixtures/migrate-operation-declaration/connection-precedence.ts.txt +10 -0
  226. package/src/cli/__tests__/fixtures/migrate-operation-declaration/docs-conflict.ts.txt +8 -0
  227. package/src/cli/__tests__/fixtures/migrate-operation-declaration/examples-hoisted-array-spread.ts.txt +15 -0
  228. package/src/cli/__tests__/fixtures/migrate-operation-declaration/examples-imported-array-spread.ts.txt +9 -0
  229. package/src/cli/__tests__/fixtures/migrate-operation-declaration/examples-map.ts.txt +5 -0
  230. package/src/cli/__tests__/fixtures/migrate-operation-declaration/examples-mixed-array-spread.ts.txt +24 -0
  231. package/src/cli/__tests__/fixtures/migrate-operation-declaration/examples-operation.ts.txt +16 -0
  232. package/src/cli/__tests__/fixtures/migrate-operation-declaration/factory-map.ts.txt +3 -0
  233. package/src/cli/__tests__/fixtures/migrate-operation-declaration/hoist-all.ts.txt +31 -0
  234. package/src/cli/__tests__/fixtures/migrate-operation-declaration/hoisted-const.ts.txt +11 -0
  235. package/src/cli/__tests__/fixtures/migrate-operation-declaration/imported-spread.ts.txt +11 -0
  236. package/src/cli/__tests__/fixtures/migrate-operation-declaration/inline-map.ts.txt +11 -0
  237. package/src/cli/__tests__/fixtures/migrate-operation-declaration/inline-spread-cast-tail.ts.txt +21 -0
  238. package/src/cli/__tests__/fixtures/migrate-operation-declaration/inline-spread-ekitan.ts.txt +11 -0
  239. package/src/cli/__tests__/fixtures/migrate-operation-declaration/inline-spread-override.ts.txt +14 -0
  240. package/src/cli/__tests__/fixtures/migrate-operation-declaration/missing-english-locale.ts.txt +7 -0
  241. package/src/cli/__tests__/fixtures/migrate-operation-declaration/no-safety.ts.txt +6 -0
  242. package/src/cli/__tests__/fixtures/migrate-operation-declaration/non-literal.ts.txt +7 -0
  243. package/src/cli/__tests__/fixtures/migrate-operation-declaration/redundant-approval.ts.txt +6 -0
  244. package/src/cli/__tests__/fixtures/migrate-operation-declaration/safety-conflict.ts.txt +7 -0
  245. package/src/cli/__tests__/fixtures/migrate-operation-declaration/stream.ts.txt +7 -0
  246. package/src/cli/__tests__/fixtures/migrate-operation-declaration/tool-router-spread.ts.txt +15 -0
  247. package/src/cli/__tests__/fixtures/migrate-operation-declaration/unparseable.ts.txt +4 -0
  248. package/src/cli/__tests__/fixtures/migrate-operation-declaration/verbatim-template.ts.txt +12 -0
  249. package/src/cli/commands.ts +33 -0
  250. package/src/cli/create.ts +50 -37
  251. package/src/cli/migrate-operation-declaration.ts +1751 -0
  252. package/src/cli/migrate-operation-shape.ts +184 -0
  253. package/src/cli/migrate-provider-shape.ts +772 -0
  254. package/src/cli/prompt-assets.ts +865 -0
  255. package/src/cli/templates/provider/AGENTS.md.tpl +17 -8
  256. package/src/cli/templates/provider/Dockerfile.tpl +1 -1
  257. package/src/cli/templates/provider/README.md.tpl +5 -5
  258. package/src/cli/templates/provider/index.ts.tpl +6 -3
  259. package/src/cli/templates/provider/operations/ping.ts.tpl +4 -1
  260. package/src/cli/templates/provider/provider.json.tpl +6 -0
  261. package/src/config/loader.ts +690 -163
  262. package/src/contract-serialization.ts +5 -7
  263. package/src/contract-types.ts +12 -5
  264. package/src/contract.ts +24 -10
  265. package/src/declaration-validation.ts +330 -0
  266. package/src/define.ts +1048 -131
  267. package/src/dev.ts +4 -1
  268. package/src/engine.ts +279 -0
  269. package/src/error-observability.ts +64 -0
  270. package/src/error-resolution.ts +127 -0
  271. package/src/errors.ts +68 -0
  272. package/src/fixture-sanitization.ts +264 -0
  273. package/src/health-scenario.ts +875 -0
  274. package/src/index.ts +220 -13
  275. package/src/lint.ts +698 -90
  276. package/src/native-address.ts +340 -0
  277. package/src/native-egress-policy.ts +358 -0
  278. package/src/observability.ts +51 -1
  279. package/src/provider.ts +161 -3
  280. package/src/runtime/auth-flow.ts +12 -0
  281. package/src/runtime/browser.ts +661 -63
  282. package/src/runtime/cache.ts +189 -14
  283. package/src/runtime/choice-wordlist.ts +145 -0
  284. package/src/runtime/choice.ts +631 -120
  285. package/src/runtime/chrome149-header-order.ts +330 -0
  286. package/src/runtime/env.ts +13 -0
  287. package/src/runtime/executor.ts +43 -31
  288. package/src/runtime/http.ts +641 -61
  289. package/src/runtime/insights.ts +2 -2
  290. package/src/runtime/instrumentation.ts +520 -15
  291. package/src/runtime/native-network-errors.ts +99 -0
  292. package/src/runtime/native-network.ts +1605 -0
  293. package/src/runtime/ocr.ts +523 -0
  294. package/src/runtime/otlp.ts +467 -21
  295. package/src/runtime/proxy-errors.ts +12 -4
  296. package/src/runtime/proxy-nodemaven.ts +221 -0
  297. package/src/runtime/proxy-telemetry.ts +244 -75
  298. package/src/runtime/redirects.ts +66 -0
  299. package/src/runtime/redis.ts +7 -2
  300. package/src/runtime/request-options.ts +680 -1
  301. package/src/runtime/resolver-config.ts +6 -0
  302. package/src/runtime/resolver-public.ts +23 -0
  303. package/src/runtime/resolver-shared.ts +33 -0
  304. package/src/runtime/resolver-vendors/bindings.ts +56 -0
  305. package/src/runtime/resolver-vendors/browser.ts +533 -0
  306. package/src/runtime/resolver-vendors/capsolver.ts +707 -0
  307. package/src/runtime/resolver-vendors/hosts.ts +38 -0
  308. package/src/runtime/resolver-vendors/twocaptcha.ts +540 -0
  309. package/src/runtime/resolver-vendors/types.ts +212 -0
  310. package/src/runtime/resolver.ts +1157 -0
  311. package/src/runtime/secrets.ts +64 -0
  312. package/src/runtime/state.ts +394 -77
  313. package/src/runtime/stealth-cookies.ts +132 -0
  314. package/src/runtime/stealth.ts +1280 -336
  315. package/src/runtime/stt.ts +1 -19
  316. package/src/runtime/timeout.ts +18 -0
  317. package/src/runtime/trace-config.ts +78 -0
  318. package/src/runtime/trace.ts +57 -17
  319. package/src/serve.ts +6 -1
  320. package/src/server/error-observability.ts +1 -0
  321. package/src/server/index.ts +34 -2
  322. package/src/server/self-test-input-tokens.ts +29 -14
  323. package/src/server/self-test.ts +191 -68
  324. package/src/server/serve-implementation.ts +3378 -0
  325. package/src/server/serve.ts +1 -1632
  326. package/src/server/trace-output.ts +129 -0
  327. package/src/server/types.ts +13 -1
  328. package/src/stateful/README.md +146 -0
  329. package/src/stateful/errors.ts +35 -0
  330. package/src/stateful/http-provider-event-emitter.ts +314 -0
  331. package/src/stateful/http-session-owner-registry.ts +306 -0
  332. package/src/stateful/index.ts +18 -0
  333. package/src/stateful/provider-event-delivery-failures.ts +80 -0
  334. package/src/stateful/provider-event-pipeline-metrics.ts +95 -0
  335. package/src/stateful/provider-event-pipeline.ts +61 -0
  336. package/src/stateful/provider-events.ts +462 -0
  337. package/src/stateful/session-key.ts +111 -0
  338. package/src/stateful/stateful-provider-adapter-context.ts +59 -0
  339. package/src/stateful/stateful-provider-adapter-metrics.ts +48 -0
  340. package/src/stateful/stateful-provider-adapter.ts +562 -0
  341. package/src/stateful/stateful-provider-observability.ts +261 -0
  342. package/src/stateful/stateful-provider-owner-forwarder.ts +287 -0
  343. package/src/stateful/stateful-provider-runtime-context.ts +92 -0
  344. package/src/stateful/stateful-provider-runtime-executor.ts +96 -0
  345. package/src/stateful/stateful-provider-session-routing.ts +546 -0
  346. package/src/stateful/stateful-provider-session-runtime.ts +403 -0
  347. package/src/stateful-signing.ts +46 -0
  348. package/src/stealth/profiles.ts +202 -231
  349. package/src/stream-evidence.ts +988 -0
  350. package/src/stream.ts +8 -1
  351. package/src/testing/index.ts +10 -1
  352. package/src/testing/run.ts +658 -15
  353. package/src/trace-sanitization.ts +63 -0
  354. package/src/types.ts +788 -174
  355. package/dist/cli/templates/provider/CLAUDE.md.tpl +0 -1
  356. package/src/cli/templates/provider/CLAUDE.md.tpl +0 -1
  357. /package/dist/cli/templates/provider/{skills → .agents/skills}/fixtures-and-recording/SKILL.md.tpl +0 -0
  358. /package/dist/cli/templates/provider/{skills → .agents/skills}/health-checks-and-fail-closed/SKILL.md.tpl +0 -0
  359. /package/dist/cli/templates/provider/{skills → .agents/skills}/normalization-standards/SKILL.md.tpl +0 -0
  360. /package/dist/cli/templates/provider/{skills → .agents/skills}/pagination-and-counts/SKILL.md.tpl +0 -0
  361. /package/dist/cli/templates/provider/{skills → .agents/skills}/upstream-contract-verification/SKILL.md.tpl +0 -0
  362. /package/dist/cli/templates/provider/{skills → .agents/skills}/upstream-notes/README.md.tpl +0 -0
  363. /package/src/cli/templates/provider/{skills → .agents/skills}/fixtures-and-recording/SKILL.md.tpl +0 -0
  364. /package/src/cli/templates/provider/{skills → .agents/skills}/health-checks-and-fail-closed/SKILL.md.tpl +0 -0
  365. /package/src/cli/templates/provider/{skills → .agents/skills}/normalization-standards/SKILL.md.tpl +0 -0
  366. /package/src/cli/templates/provider/{skills → .agents/skills}/pagination-and-counts/SKILL.md.tpl +0 -0
  367. /package/src/cli/templates/provider/{skills → .agents/skills}/upstream-contract-verification/SKILL.md.tpl +0 -0
  368. /package/src/cli/templates/provider/{skills → .agents/skills}/upstream-notes/README.md.tpl +0 -0
@@ -0,0 +1,306 @@
1
+ import { z } from "zod";
2
+
3
+ import { statefulSignedHeaders } from "../stateful-signing.js";
4
+ import { StatefulControlPlaneError, type StatefulControlPlaneOperation } from "./errors.js";
5
+ import { parseSessionKey, type SessionKey } from "./session-key.js";
6
+ import type {
7
+ AcquireSessionOwnerInput,
8
+ AcquireSessionOwnerResult,
9
+ ReleaseSessionOwnerInput,
10
+ RenewSessionOwnerInput,
11
+ SessionOwnerRecord,
12
+ SessionOwnerRegistry,
13
+ } from "./stateful-provider-session-runtime.js";
14
+
15
+ type FetchTransport = (url: string | URL | Request, init?: RequestInit) => Promise<Response>;
16
+
17
+ export type SessionOwnerScope = {
18
+ readonly serviceAccountId: string;
19
+ readonly providerId: string;
20
+ readonly connectionId?: string;
21
+ };
22
+
23
+ export type HttpSessionOwnerRegistryOptions = {
24
+ readonly baseUrl: string;
25
+ readonly secret: string;
26
+ readonly scope?: SessionOwnerScope;
27
+ readonly fetch?: FetchTransport;
28
+ readonly clock?: () => Date;
29
+ readonly requestTimeoutMs?: number;
30
+ };
31
+
32
+ const DEFAULT_REQUEST_TIMEOUT_MS = 10_000;
33
+
34
+ /**
35
+ * A mutating registry request failed after it may have reached the control plane. Callers must
36
+ * resolve the session owner and reconcile instead of blindly retrying the mutation.
37
+ */
38
+ export class AmbiguousRegistryOperationError extends StatefulControlPlaneError {
39
+ readonly ambiguous = true;
40
+
41
+ constructor(input: {
42
+ readonly operation: Exclude<StatefulControlPlaneOperation, "resolve">;
43
+ readonly message: string;
44
+ readonly cause: unknown;
45
+ }) {
46
+ super({
47
+ code: "STATEFUL_CONTROL_PLANE_OPERATION_AMBIGUOUS",
48
+ message: input.message,
49
+ operation: input.operation,
50
+ cause: input.cause,
51
+ });
52
+ this.name = "AmbiguousRegistryOperationError";
53
+ }
54
+ }
55
+
56
+ const SessionKeySchema = z
57
+ .string()
58
+ .min(1)
59
+ .transform((value, context): SessionKey => {
60
+ try {
61
+ parseSessionKey(value);
62
+ return value as SessionKey;
63
+ } catch (error) {
64
+ context.addIssue({
65
+ code: "custom",
66
+ message: error instanceof Error ? error.message : "Invalid canonical session key.",
67
+ });
68
+ return z.NEVER;
69
+ }
70
+ });
71
+
72
+ const OwnerRecordSchema = z.object({
73
+ sessionKey: SessionKeySchema,
74
+ ownerPodId: z.string().min(1),
75
+ ownerEndpoint: z.string().min(1),
76
+ generation: z.number().int().positive(),
77
+ leaseExpiresAt: z.string().refine(isTimestamp),
78
+ status: z.enum(["acquiring", "connected", "draining", "expired"]),
79
+ lastUsedAt: z.string().refine(isTimestamp),
80
+ });
81
+
82
+ const AcquireResultSchema = z.object({
83
+ record: OwnerRecordSchema,
84
+ acquired: z.boolean(),
85
+ });
86
+
87
+ const ResolveResponseSchema = z.union([
88
+ OwnerRecordSchema,
89
+ z.object({ record: OwnerRecordSchema }).transform((value) => value.record),
90
+ z.object({ data: OwnerRecordSchema }).transform((value) => value.data),
91
+ ]);
92
+
93
+ const AcquireResponseSchema = z.union([
94
+ AcquireResultSchema,
95
+ z.object({ data: AcquireResultSchema }).transform((value) => value.data),
96
+ ]);
97
+
98
+ const RenewResponseSchema = z.union([
99
+ OwnerRecordSchema.nullable(),
100
+ z.object({ record: OwnerRecordSchema.nullable() }).transform((value) => value.record),
101
+ z.object({ data: OwnerRecordSchema.nullable() }).transform((value) => value.data),
102
+ ]);
103
+
104
+ const ReleaseResponseSchema = z.union([
105
+ z.boolean(),
106
+ z.object({ released: z.boolean() }).transform((value) => value.released),
107
+ z.object({ data: z.boolean() }).transform((value) => value.data),
108
+ ]);
109
+
110
+ export type HttpSessionOwnerRecord = Omit<SessionOwnerRecord, "sessionKey"> & {
111
+ readonly sessionKey: SessionKey;
112
+ };
113
+
114
+ export type HttpAcquireSessionOwnerResult = Omit<AcquireSessionOwnerResult, "record"> & {
115
+ readonly record: HttpSessionOwnerRecord;
116
+ };
117
+
118
+ export class HttpSessionOwnerRegistry implements SessionOwnerRegistry {
119
+ readonly #baseUrl: string;
120
+ readonly #secret: string;
121
+ readonly #scope?: SessionOwnerScope;
122
+ readonly #fetch: FetchTransport;
123
+ readonly #clock: () => Date;
124
+ readonly #requestTimeoutMs: number;
125
+
126
+ constructor(options: HttpSessionOwnerRegistryOptions) {
127
+ if (options.baseUrl.trim().length === 0) {
128
+ throw new Error("Stateful control-plane baseUrl is required.");
129
+ }
130
+ if (options.secret.trim().length === 0) {
131
+ throw new Error("Stateful control-plane secret is required.");
132
+ }
133
+ this.#baseUrl = options.baseUrl.replace(/\/+$/, "");
134
+ this.#secret = options.secret;
135
+ this.#scope = options.scope;
136
+ this.#fetch = options.fetch ?? globalThis.fetch;
137
+ this.#clock = options.clock ?? (() => new Date());
138
+ this.#requestTimeoutMs = positiveInteger(
139
+ options.requestTimeoutMs ?? DEFAULT_REQUEST_TIMEOUT_MS,
140
+ "requestTimeoutMs",
141
+ );
142
+ }
143
+
144
+ async resolve(
145
+ sessionKey: SessionKey,
146
+ now?: Date,
147
+ signal?: AbortSignal,
148
+ ): Promise<HttpSessionOwnerRecord | null> {
149
+ const response = await this.#post(
150
+ "resolve",
151
+ {
152
+ ...this.#scope,
153
+ sessionKey,
154
+ ...(now ? { now: now.toISOString() } : {}),
155
+ },
156
+ signal,
157
+ );
158
+ if (response.status === 404) return null;
159
+ return this.#parse(response, "resolve", ResolveResponseSchema);
160
+ }
161
+
162
+ async acquire(
163
+ input: AcquireSessionOwnerInput,
164
+ signal?: AbortSignal,
165
+ ): Promise<HttpAcquireSessionOwnerResult> {
166
+ const response = await this.#post(
167
+ "acquire",
168
+ {
169
+ ...this.#scope,
170
+ ...input,
171
+ ...(input.now ? { now: input.now.toISOString() } : {}),
172
+ },
173
+ signal,
174
+ );
175
+ return this.#parse(response, "acquire", AcquireResponseSchema);
176
+ }
177
+
178
+ async renew(
179
+ input: RenewSessionOwnerInput,
180
+ signal?: AbortSignal,
181
+ ): Promise<HttpSessionOwnerRecord | null> {
182
+ const response = await this.#post(
183
+ "renew",
184
+ {
185
+ ...this.#scope,
186
+ ...input,
187
+ ...(input.now ? { now: input.now.toISOString() } : {}),
188
+ },
189
+ signal,
190
+ );
191
+ return this.#parse(response, "renew", RenewResponseSchema);
192
+ }
193
+
194
+ async release(input: ReleaseSessionOwnerInput, signal?: AbortSignal): Promise<boolean> {
195
+ const response = await this.#post(
196
+ "release",
197
+ {
198
+ ...this.#scope,
199
+ ...input,
200
+ },
201
+ signal,
202
+ );
203
+ return this.#parse(response, "release", ReleaseResponseSchema);
204
+ }
205
+
206
+ async #post(
207
+ operation: StatefulControlPlaneOperation,
208
+ body: unknown,
209
+ callerSignal?: AbortSignal,
210
+ ): Promise<Response> {
211
+ callerSignal?.throwIfAborted();
212
+ const rawBody = JSON.stringify(body);
213
+ const timestamp = this.#clock().toISOString();
214
+ const path = `/v1/stateful/sessions/owners/${operation}`;
215
+ const timeoutSignal = AbortSignal.timeout(this.#requestTimeoutMs);
216
+ const signal = AbortSignal.any(callerSignal ? [callerSignal, timeoutSignal] : [timeoutSignal]);
217
+ try {
218
+ return await this.#fetch(`${this.#baseUrl}${path}`, {
219
+ method: "POST",
220
+ headers: {
221
+ "content-type": "application/json",
222
+ ...statefulSignedHeaders({
223
+ secret: this.#secret,
224
+ timestamp,
225
+ rawBody,
226
+ method: "POST",
227
+ path,
228
+ }),
229
+ },
230
+ body: rawBody,
231
+ signal,
232
+ });
233
+ } catch (cause) {
234
+ const timedOut = timeoutSignal.aborted && !callerSignal?.aborted;
235
+ if (operation !== "resolve") {
236
+ throw new AmbiguousRegistryOperationError({
237
+ operation,
238
+ message: timedOut
239
+ ? `Stateful control-plane ${operation} timed out after ${this.#requestTimeoutMs}ms; resolve the session owner and reconcile before retrying.`
240
+ : `Stateful control-plane ${operation} request failed after it may have committed; resolve the session owner and reconcile before retrying.`,
241
+ cause,
242
+ });
243
+ }
244
+ throw new StatefulControlPlaneError({
245
+ code: "STATEFUL_CONTROL_PLANE_REQUEST_FAILED",
246
+ message: timedOut
247
+ ? `Stateful control-plane resolve timed out after ${this.#requestTimeoutMs}ms; retry resolve or check control-plane availability.`
248
+ : "Stateful control-plane resolve request failed.",
249
+ operation,
250
+ cause,
251
+ });
252
+ }
253
+ }
254
+
255
+ async #parse<T>(
256
+ response: Response,
257
+ operation: StatefulControlPlaneOperation,
258
+ schema: z.ZodType<T>,
259
+ ): Promise<T> {
260
+ if (!response.ok) {
261
+ throw new StatefulControlPlaneError({
262
+ code: "STATEFUL_CONTROL_PLANE_HTTP_ERROR",
263
+ message: `Stateful control-plane ${operation} returned status ${response.status}.`,
264
+ operation,
265
+ status: response.status,
266
+ });
267
+ }
268
+ const rawBody = await response.text();
269
+ let body: unknown;
270
+ try {
271
+ body = JSON.parse(rawBody);
272
+ } catch (cause) {
273
+ throw invalidResponse(operation, response.status, cause);
274
+ }
275
+ const parsed = schema.safeParse(body);
276
+ if (!parsed.success) {
277
+ throw invalidResponse(operation, response.status, parsed.error);
278
+ }
279
+ return parsed.data;
280
+ }
281
+ }
282
+
283
+ function invalidResponse(
284
+ operation: StatefulControlPlaneOperation,
285
+ status: number,
286
+ cause: unknown,
287
+ ): StatefulControlPlaneError {
288
+ return new StatefulControlPlaneError({
289
+ code: "STATEFUL_CONTROL_PLANE_INVALID_RESPONSE",
290
+ message: `Stateful control-plane ${operation} returned an invalid response.`,
291
+ operation,
292
+ status,
293
+ cause,
294
+ });
295
+ }
296
+
297
+ function isTimestamp(value: string): boolean {
298
+ return Number.isFinite(Date.parse(value));
299
+ }
300
+
301
+ function positiveInteger(value: number, name: string): number {
302
+ if (!Number.isInteger(value) || value <= 0) {
303
+ throw new Error(`Stateful control-plane ${name} must be a positive integer.`);
304
+ }
305
+ return value;
306
+ }
@@ -0,0 +1,18 @@
1
+ export * from "../stateful-signing.js";
2
+ export * from "./errors.js";
3
+ export * from "./http-provider-event-emitter.js";
4
+ export * from "./http-session-owner-registry.js";
5
+ export * from "./provider-event-delivery-failures.js";
6
+ export * from "./provider-event-pipeline-metrics.js";
7
+ export * from "./provider-event-pipeline.js";
8
+ export * from "./provider-events.js";
9
+ export * from "./session-key.js";
10
+ export * from "./stateful-provider-adapter-context.js";
11
+ export * from "./stateful-provider-adapter-metrics.js";
12
+ export * from "./stateful-provider-adapter.js";
13
+ export * from "./stateful-provider-observability.js";
14
+ export * from "./stateful-provider-owner-forwarder.js";
15
+ export * from "./stateful-provider-runtime-context.js";
16
+ export * from "./stateful-provider-runtime-executor.js";
17
+ export * from "./stateful-provider-session-routing.js";
18
+ export * from "./stateful-provider-session-runtime.js";
@@ -0,0 +1,80 @@
1
+ export type ProviderWebhookFailureTotals = {
2
+ retryingCount: number;
3
+ failedCount: number;
4
+ deadLetteredCount: number;
5
+ };
6
+
7
+ export function failureForStatus(statusCode: number): {
8
+ readonly statusCode: number;
9
+ readonly error?: string;
10
+ } {
11
+ if (statusCode >= 300 && statusCode < 400) {
12
+ return { statusCode, error: "Webhook redirects are not followed." };
13
+ }
14
+ return { statusCode };
15
+ }
16
+
17
+ export function failureForError(error: unknown): {
18
+ readonly statusCode?: number;
19
+ readonly error: string;
20
+ } {
21
+ if (isPublicValidationError(error)) {
22
+ return { statusCode: 400, error: error.message };
23
+ }
24
+ return { error: publicWorkerError(error) };
25
+ }
26
+
27
+ export function countProviderWebhookFailure(
28
+ totals: ProviderWebhookFailureTotals,
29
+ status: string,
30
+ ): void {
31
+ if (status === "retrying") {
32
+ totals.retryingCount += 1;
33
+ return;
34
+ }
35
+ if (status === "dead_lettered") {
36
+ totals.deadLetteredCount += 1;
37
+ return;
38
+ }
39
+ totals.failedCount += 1;
40
+ }
41
+
42
+ export type ProviderEventDeliveryFailureReason = "buffer_overflow" | "attempts_exhausted";
43
+
44
+ export type ProviderEventDeliveryFailure = {
45
+ readonly eventId: string;
46
+ readonly reason: ProviderEventDeliveryFailureReason;
47
+ readonly attempts: number;
48
+ readonly failedAt: string;
49
+ readonly error?: string;
50
+ };
51
+
52
+ export interface ProviderEventDeliveryFailureRecorder {
53
+ record(failure: ProviderEventDeliveryFailure): void | Promise<void>;
54
+ }
55
+
56
+ export const NOOP_PROVIDER_EVENT_DELIVERY_FAILURE_RECORDER = {
57
+ record() {},
58
+ } satisfies ProviderEventDeliveryFailureRecorder;
59
+
60
+ export class RecordingProviderEventDeliveryFailureRecorder
61
+ implements ProviderEventDeliveryFailureRecorder
62
+ {
63
+ readonly failures: ProviderEventDeliveryFailure[] = [];
64
+
65
+ record(failure: ProviderEventDeliveryFailure): void {
66
+ this.failures.push(failure);
67
+ }
68
+ }
69
+
70
+ function publicWorkerError(error: unknown): string {
71
+ if (isPublicValidationError(error)) return error.message;
72
+ if (error instanceof Error && error.message) {
73
+ return "Webhook delivery request failed.";
74
+ }
75
+ return "Webhook delivery failed.";
76
+ }
77
+
78
+ function isPublicValidationError(error: unknown): error is Error {
79
+ return error instanceof Error && error.name.endsWith("ValidationError");
80
+ }
@@ -0,0 +1,95 @@
1
+ import type { ProviderEvent } from "./provider-events.js";
2
+
3
+ export const PROVIDER_EVENT_METRIC_NAMES = [
4
+ "apifuse_stateful_provider_event_append_total",
5
+ "apifuse_stateful_provider_event_dedupe_total",
6
+ "apifuse_stateful_provider_event_drop_total",
7
+ "apifuse_stateful_provider_webhook_enqueue_total",
8
+ "apifuse_stateful_provider_webhook_fanout_recovered_total",
9
+ "apifuse_stateful_provider_webhook_delivered_total",
10
+ "apifuse_stateful_provider_webhook_retry_total",
11
+ "apifuse_stateful_provider_webhook_dead_letter_total",
12
+ "apifuse_stateful_provider_webhook_canceled_total",
13
+ "apifuse_stateful_provider_webhook_latency_ms",
14
+ ] as const;
15
+
16
+ export type ProviderEventMetricName = (typeof PROVIDER_EVENT_METRIC_NAMES)[number];
17
+
18
+ export type ProviderEventMetricLabelInput = {
19
+ readonly eventId?: string;
20
+ readonly deliveryId?: string;
21
+ readonly providerId?: string;
22
+ readonly connectionId?: string;
23
+ readonly serviceAccountId?: string;
24
+ readonly eventType?: string;
25
+ readonly subjectKind?: string;
26
+ readonly subjectId?: string;
27
+ readonly sessionKey?: string;
28
+ readonly generation?: number | string;
29
+ };
30
+
31
+ export interface ProviderEventMetricEmitter {
32
+ increment(
33
+ name: ProviderEventMetricName,
34
+ labels?: ProviderEventMetricLabelInput,
35
+ value?: number,
36
+ ): void;
37
+ observe(
38
+ name: ProviderEventMetricName,
39
+ labels: ProviderEventMetricLabelInput,
40
+ value: number,
41
+ ): void;
42
+ }
43
+
44
+ export const NOOP_PROVIDER_EVENT_METRIC_EMITTER = {
45
+ increment() {},
46
+ observe() {},
47
+ } satisfies ProviderEventMetricEmitter;
48
+
49
+ export function providerEventMetricLabels(event: ProviderEvent) {
50
+ return {
51
+ eventId: event.eventId,
52
+ providerId: event.providerId,
53
+ connectionId: event.connectionId,
54
+ serviceAccountId: event.serviceAccountId,
55
+ eventType: event.eventType,
56
+ subjectKind: event.subject?.kind,
57
+ subjectId: event.subject?.id,
58
+ sessionKey: event.session.sessionKey,
59
+ generation: event.session.generation,
60
+ };
61
+ }
62
+
63
+ export function providerWebhookDeliveryMetricLabels(delivery: { readonly deliveryId: string }) {
64
+ return { deliveryId: delivery.deliveryId };
65
+ }
66
+
67
+ export function emitProviderWebhookDeliveryFailureMetric(
68
+ metricEmitter: ProviderEventMetricEmitter,
69
+ delivery: { readonly deliveryId: string },
70
+ status: string,
71
+ ): void {
72
+ if (status === "retrying") {
73
+ metricEmitter.increment(
74
+ "apifuse_stateful_provider_webhook_retry_total",
75
+ providerWebhookDeliveryMetricLabels(delivery),
76
+ );
77
+ return;
78
+ }
79
+ if (status === "dead_lettered") {
80
+ metricEmitter.increment(
81
+ "apifuse_stateful_provider_webhook_dead_letter_total",
82
+ providerWebhookDeliveryMetricLabels(delivery),
83
+ );
84
+ }
85
+ }
86
+
87
+ export function providerWebhookDeliveryLatencyMs(
88
+ delivery: { readonly lastAttemptAt?: string; readonly nextAttemptAt: string },
89
+ now: string,
90
+ ): number {
91
+ const startedAt = Date.parse(delivery.lastAttemptAt ?? delivery.nextAttemptAt);
92
+ const completedAt = Date.parse(now);
93
+ if (!Number.isFinite(startedAt) || !Number.isFinite(completedAt)) return 0;
94
+ return Math.max(0, completedAt - startedAt);
95
+ }
@@ -0,0 +1,61 @@
1
+ import type { ProviderEventMetricEmitter } from "./provider-event-pipeline-metrics.js";
2
+ import type { ProviderEvent } from "./provider-events.js";
3
+ import type { SessionKey } from "./session-key.js";
4
+
5
+ export type { ProviderEventMetricEmitter } from "./provider-event-pipeline-metrics.js";
6
+
7
+ export type ProviderEventOwnerFence = {
8
+ readonly ownerPodId: string;
9
+ readonly ownerEndpoint: string;
10
+ readonly sessionKey: SessionKey;
11
+ readonly generation: number;
12
+ };
13
+
14
+ export type ProviderEventPublishOptions = {
15
+ /** Required ownership proof forwarded to the durable platform write boundary. */
16
+ readonly ownerFence: ProviderEventOwnerFence;
17
+ /** Overrides the transport idempotency key; defaults to the event id. */
18
+ readonly idempotencyKey?: string;
19
+ };
20
+
21
+ export type PublishAck = {
22
+ /** Whether this event was accepted into the pod-local retry buffer. */
23
+ readonly accepted: boolean;
24
+ /** Number of events currently held in the pod-local retry buffer. */
25
+ readonly queued: number;
26
+ };
27
+
28
+ /** Fire-and-forget publication from a provider pod into the platform ingest boundary. */
29
+ export interface ProviderEventPublisher {
30
+ publish(event: ProviderEvent, options: ProviderEventPublishOptions): PublishAck;
31
+ }
32
+
33
+ export type AppendProviderEventAndEnqueueWebhooksResult = {
34
+ readonly appended: boolean;
35
+ readonly appendedCount: number;
36
+ readonly matchedSubscriptionCount: number;
37
+ readonly enqueuedCount: number;
38
+ readonly recoveredFanoutCount?: number;
39
+ readonly fenced?: boolean;
40
+ };
41
+
42
+ export type ProviderEventPipelineAppendOptions = {
43
+ readonly now?: string;
44
+ readonly idFactory?: () => string;
45
+ readonly metricEmitter?: ProviderEventMetricEmitter;
46
+ readonly beforeAppend?: () => Promise<boolean> | boolean;
47
+ readonly ownerFence?: ProviderEventOwnerFence;
48
+ };
49
+
50
+ /**
51
+ * Durable append-and-fanout contract implemented on the platform side.
52
+ *
53
+ * Provider-pod code must depend on ProviderEventPublisher, never ProviderEventPipeline: a pod-local
54
+ * publisher cannot truthfully report a durable append or downstream webhook fanout.
55
+ */
56
+ export interface ProviderEventPipeline {
57
+ appendAndFanout(
58
+ event: ProviderEvent,
59
+ options?: ProviderEventPipelineAppendOptions,
60
+ ): Promise<AppendProviderEventAndEnqueueWebhooksResult>;
61
+ }