tenjin-cli 0.1.0-alpha.1 → 0.1.0-alpha.11

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 (193) hide show
  1. package/NOTICE.md +33 -0
  2. package/README.md +596 -79
  3. package/dist/{_esm-ZH2J4QEH.js → _esm-C5CRWCYU.js} +2 -2
  4. package/dist/_esm-N4KAVXQR.js +881 -0
  5. package/dist/buy-THMVOPUH.js +44 -0
  6. package/dist/ccip-RPPPKTJR.js +19 -0
  7. package/dist/{ccip-UCLT5VOW.js → ccip-ZGGWQN4X.js} +6 -5
  8. package/dist/chunk-2I4QTOLB.js +11 -0
  9. package/dist/chunk-2I4QTOLB.js.map +1 -0
  10. package/dist/chunk-2SDUV6MQ.js +352 -0
  11. package/dist/chunk-2SDUV6MQ.js.map +1 -0
  12. package/dist/chunk-3GSZUADW.js +199 -0
  13. package/dist/chunk-3GSZUADW.js.map +1 -0
  14. package/dist/{chunk-BRJHUQSY.js → chunk-47PP7TAG.js} +908 -812
  15. package/dist/chunk-47PP7TAG.js.map +1 -0
  16. package/dist/chunk-4NTZETIL.js +158 -0
  17. package/dist/chunk-4NTZETIL.js.map +1 -0
  18. package/dist/chunk-4TCU3YRD.js +130 -0
  19. package/dist/chunk-4TCU3YRD.js.map +1 -0
  20. package/dist/chunk-6BGYHARE.js +14620 -0
  21. package/dist/chunk-6BGYHARE.js.map +1 -0
  22. package/dist/chunk-6UY2UQMY.js +213 -0
  23. package/dist/chunk-6UY2UQMY.js.map +1 -0
  24. package/dist/{chunk-IPOWB3G3.js → chunk-7FK2UN2T.js} +3515 -507
  25. package/dist/chunk-7FK2UN2T.js.map +1 -0
  26. package/dist/chunk-ALUNME53.js +306 -0
  27. package/dist/chunk-ALUNME53.js.map +1 -0
  28. package/dist/{chunk-EXBMF5X7.js → chunk-CNUXRXI2.js} +526 -79
  29. package/dist/chunk-CNUXRXI2.js.map +1 -0
  30. package/dist/chunk-E6YOODH7.js +913 -0
  31. package/dist/chunk-E6YOODH7.js.map +1 -0
  32. package/dist/chunk-EDVI7RRD.js +317 -0
  33. package/dist/chunk-EDVI7RRD.js.map +1 -0
  34. package/dist/chunk-FKSAUOJU.js +137 -0
  35. package/dist/chunk-FKSAUOJU.js.map +1 -0
  36. package/dist/chunk-GKV6VZSZ.js +125 -0
  37. package/dist/chunk-GKV6VZSZ.js.map +1 -0
  38. package/dist/chunk-GUUAYR3K.js +175 -0
  39. package/dist/chunk-GUUAYR3K.js.map +1 -0
  40. package/dist/chunk-IAFYUFIC.js +1408 -0
  41. package/dist/chunk-IAFYUFIC.js.map +1 -0
  42. package/dist/chunk-IB63U6BM.js +185 -0
  43. package/dist/chunk-IB63U6BM.js.map +1 -0
  44. package/dist/chunk-IXTENA2H.js +235 -0
  45. package/dist/chunk-IXTENA2H.js.map +1 -0
  46. package/dist/chunk-JTYIX6OD.js +161 -0
  47. package/dist/chunk-JTYIX6OD.js.map +1 -0
  48. package/dist/chunk-KYQVEKP5.js +209 -0
  49. package/dist/chunk-KYQVEKP5.js.map +1 -0
  50. package/dist/chunk-KZAAWQNP.js +6199 -0
  51. package/dist/chunk-KZAAWQNP.js.map +1 -0
  52. package/dist/{chunk-T4LKNY7T.js → chunk-M2O56UK7.js} +225 -70
  53. package/dist/chunk-M2O56UK7.js.map +1 -0
  54. package/dist/chunk-MJZZTKCC.js +323 -0
  55. package/dist/chunk-MJZZTKCC.js.map +1 -0
  56. package/dist/chunk-NUF2PKJO.js +207 -0
  57. package/dist/chunk-NUF2PKJO.js.map +1 -0
  58. package/dist/chunk-O25VWJCG.js +1357 -0
  59. package/dist/chunk-O25VWJCG.js.map +1 -0
  60. package/dist/chunk-OCN27NJV.js +612 -0
  61. package/dist/chunk-OCN27NJV.js.map +1 -0
  62. package/dist/chunk-OZHJ3FLR.js +105 -0
  63. package/dist/chunk-OZHJ3FLR.js.map +1 -0
  64. package/dist/chunk-P74I3WH4.js +204 -0
  65. package/dist/chunk-P74I3WH4.js.map +1 -0
  66. package/dist/chunk-POJRB2AE.js +83 -0
  67. package/dist/chunk-POJRB2AE.js.map +1 -0
  68. package/dist/{chunk-YG2DO46P.js → chunk-PZRAAAY2.js} +107 -22
  69. package/dist/chunk-PZRAAAY2.js.map +1 -0
  70. package/dist/chunk-PZSUWDF2.js +35 -0
  71. package/dist/chunk-PZSUWDF2.js.map +1 -0
  72. package/dist/chunk-QW2Q2KAH.js +27 -0
  73. package/dist/chunk-QW2Q2KAH.js.map +1 -0
  74. package/dist/{chunk-CUWUHTIL.js → chunk-RBJPAZFI.js} +16 -202
  75. package/dist/chunk-RBJPAZFI.js.map +1 -0
  76. package/dist/chunk-RSBN67PA.js +152 -0
  77. package/dist/chunk-RSBN67PA.js.map +1 -0
  78. package/dist/chunk-S5RYMCV6.js +70 -0
  79. package/dist/chunk-S5RYMCV6.js.map +1 -0
  80. package/dist/chunk-SFGXAZ3M.js +316 -0
  81. package/dist/chunk-SFGXAZ3M.js.map +1 -0
  82. package/dist/chunk-SGK2B724.js +67 -0
  83. package/dist/chunk-SGK2B724.js.map +1 -0
  84. package/dist/chunk-TDDN5GXR.js +145 -0
  85. package/dist/chunk-TDDN5GXR.js.map +1 -0
  86. package/dist/{chunk-LPRVHM7P.js → chunk-U5S527PP.js} +1655 -114
  87. package/dist/chunk-U5S527PP.js.map +1 -0
  88. package/dist/chunk-UFPJBS4C.js +242 -0
  89. package/dist/chunk-UFPJBS4C.js.map +1 -0
  90. package/dist/chunk-UHTVXQ2O.js +140 -0
  91. package/dist/chunk-UHTVXQ2O.js.map +1 -0
  92. package/dist/chunk-UY6CGULW.js +83 -0
  93. package/dist/chunk-UY6CGULW.js.map +1 -0
  94. package/dist/chunk-VWY3U732.js +674 -0
  95. package/dist/chunk-VWY3U732.js.map +1 -0
  96. package/dist/{chunk-Q53IYV2G.js → chunk-W6R3HAAG.js} +20 -17
  97. package/dist/{chunk-Q53IYV2G.js.map → chunk-W6R3HAAG.js.map} +1 -1
  98. package/dist/chunk-XJN5WUKL.js +204 -0
  99. package/dist/chunk-XJN5WUKL.js.map +1 -0
  100. package/dist/chunk-XWHXX2BI.js +149 -0
  101. package/dist/chunk-XWHXX2BI.js.map +1 -0
  102. package/dist/{chunk-DXPYFVKG.js → chunk-Y7ERTA7D.js} +35 -15
  103. package/dist/chunk-Y7ERTA7D.js.map +1 -0
  104. package/dist/chunk-YLYINX2H.js +7806 -0
  105. package/dist/chunk-YLYINX2H.js.map +1 -0
  106. package/dist/chunk-ZE7KOO7A.js +233 -0
  107. package/dist/chunk-ZE7KOO7A.js.map +1 -0
  108. package/dist/chunk-ZQIQ55KK.js +51 -0
  109. package/dist/chunk-ZQIQ55KK.js.map +1 -0
  110. package/dist/{chunk-3IMLRUZE.js → chunk-ZXTAQYNA.js} +6 -2
  111. package/dist/{cli-WA65NP4Y.js → cli-Y2HBOSGK.js} +437 -175
  112. package/dist/cli-Y2HBOSGK.js.map +1 -0
  113. package/dist/config-WDGMZKHU.js +26 -0
  114. package/dist/config-WDGMZKHU.js.map +1 -0
  115. package/dist/dist-U4M6QCKE.js +2842 -0
  116. package/dist/dist-U4M6QCKE.js.map +1 -0
  117. package/dist/doctor-CSIWKWM2.js +27 -0
  118. package/dist/doctor-CSIWKWM2.js.map +1 -0
  119. package/dist/edit-E7N6F3OY.js +38 -0
  120. package/dist/edit-E7N6F3OY.js.map +1 -0
  121. package/dist/ethersCompat-Df4a6QIZ-WHYKB6HA.js +18 -0
  122. package/dist/ethersCompat-Df4a6QIZ-WHYKB6HA.js.map +1 -0
  123. package/dist/fund-OKK627GL.js +38 -0
  124. package/dist/fund-OKK627GL.js.map +1 -0
  125. package/dist/index.js +1 -1
  126. package/dist/inspect-573WHEV4.js +35 -0
  127. package/dist/inspect-573WHEV4.js.map +1 -0
  128. package/dist/install-SQAYMNQC.js +1134 -0
  129. package/dist/install-SQAYMNQC.js.map +1 -0
  130. package/dist/{mine.wasm-GQECMSGN.js → mine.wasm-6PIT3DK6.js} +2 -2
  131. package/dist/outcome-W3KYGF23.js +22 -0
  132. package/dist/outcome-W3KYGF23.js.map +1 -0
  133. package/dist/publish-N5U4XYVA.js +40 -0
  134. package/dist/publish-N5U4XYVA.js.map +1 -0
  135. package/dist/read-PJ7Z6UGF.js +158 -0
  136. package/dist/read-PJ7Z6UGF.js.map +1 -0
  137. package/dist/run-FSO3MMXN.js +16883 -0
  138. package/dist/run-FSO3MMXN.js.map +1 -0
  139. package/dist/search-ZWOP4EGK.js +23 -0
  140. package/dist/search-ZWOP4EGK.js.map +1 -0
  141. package/dist/{secp256k1-BEZS642W.js → secp256k1-XRPVBC6V.js} +5 -4
  142. package/dist/secp256k1-XRPVBC6V.js.map +1 -0
  143. package/dist/send-F34ZDXDZ.js +223 -0
  144. package/dist/send-F34ZDXDZ.js.map +1 -0
  145. package/dist/session-4N4ZT3AH.js +88 -0
  146. package/dist/session-4N4ZT3AH.js.map +1 -0
  147. package/dist/skill-heal-XBEK272G.js +85 -0
  148. package/dist/skill-heal-XBEK272G.js.map +1 -0
  149. package/dist/uninstall-BTKPHPDL.js +68 -0
  150. package/dist/uninstall-BTKPHPDL.js.map +1 -0
  151. package/dist/usdc-4VCDHFWX.js +38 -0
  152. package/dist/usdc-4VCDHFWX.js.map +1 -0
  153. package/dist/viemAdapter-By13KInL-TUQJBMHM.js +14 -0
  154. package/dist/viemAdapter-By13KInL-TUQJBMHM.js.map +1 -0
  155. package/dist/wallet-UL4P2JRO.js +42 -0
  156. package/dist/wallet-UL4P2JRO.js.map +1 -0
  157. package/dist/wallet-WJ6UP5OU.js +32 -0
  158. package/dist/wallet-WJ6UP5OU.js.map +1 -0
  159. package/docs/agent-permissions.md +294 -0
  160. package/package.json +25 -18
  161. package/skills/tenjin/SKILL.md +364 -0
  162. package/skills/tenjin-publish/SKILL.md +232 -0
  163. package/skills/tenjin-search/SKILL.md +341 -0
  164. package/dist/chunk-BRJHUQSY.js.map +0 -1
  165. package/dist/chunk-CUWUHTIL.js.map +0 -1
  166. package/dist/chunk-DXPYFVKG.js.map +0 -1
  167. package/dist/chunk-EXBMF5X7.js.map +0 -1
  168. package/dist/chunk-EZROUI5U.js +0 -135
  169. package/dist/chunk-EZROUI5U.js.map +0 -1
  170. package/dist/chunk-IPOWB3G3.js.map +0 -1
  171. package/dist/chunk-LPRVHM7P.js.map +0 -1
  172. package/dist/chunk-T4LKNY7T.js.map +0 -1
  173. package/dist/chunk-YBMMV4Y4.js +0 -51
  174. package/dist/chunk-YBMMV4Y4.js.map +0 -1
  175. package/dist/chunk-YG2DO46P.js.map +0 -1
  176. package/dist/chunk-ZPZFPVDN.js +0 -67
  177. package/dist/chunk-ZPZFPVDN.js.map +0 -1
  178. package/dist/cli-WA65NP4Y.js.map +0 -1
  179. package/dist/config-CEFTL5ZL.js +0 -164
  180. package/dist/config-CEFTL5ZL.js.map +0 -1
  181. package/dist/doctor-3QBYN46N.js +0 -338
  182. package/dist/doctor-3QBYN46N.js.map +0 -1
  183. package/dist/usdc-44R6XZF2.js +0 -18
  184. package/dist/wallet-ELBTBMXO.js +0 -20
  185. package/dist/wallet-XD576HF3.js +0 -173
  186. package/dist/wallet-XD576HF3.js.map +0 -1
  187. /package/dist/{_esm-ZH2J4QEH.js.map → _esm-C5CRWCYU.js.map} +0 -0
  188. /package/dist/{ccip-UCLT5VOW.js.map → _esm-N4KAVXQR.js.map} +0 -0
  189. /package/dist/{chunk-3IMLRUZE.js.map → buy-THMVOPUH.js.map} +0 -0
  190. /package/dist/{secp256k1-BEZS642W.js.map → ccip-RPPPKTJR.js.map} +0 -0
  191. /package/dist/{usdc-44R6XZF2.js.map → ccip-ZGGWQN4X.js.map} +0 -0
  192. /package/dist/{wallet-ELBTBMXO.js.map → chunk-ZXTAQYNA.js.map} +0 -0
  193. /package/dist/{mine.wasm-GQECMSGN.js.map → mine.wasm-6PIT3DK6.js.map} +0 -0
@@ -0,0 +1,199 @@
1
+ import { createRequire as __tenjinCreateRequire } from 'node:module'; const require = __tenjinCreateRequire(import.meta.url);
2
+ import {
3
+ package_default
4
+ } from "./chunk-UY6CGULW.js";
5
+ import {
6
+ CliError
7
+ } from "./chunk-PZRAAAY2.js";
8
+
9
+ // src/lib/client-meta.ts
10
+ var TENJIN_USER_AGENT = `tenjin-cli/${package_default.version} (+https://tenjin.blog)`;
11
+
12
+ // src/lib/http.ts
13
+ function applyUserAgent(headers) {
14
+ headers.set("user-agent", TENJIN_USER_AGENT);
15
+ return headers;
16
+ }
17
+ function withUserAgent(callerHeaders) {
18
+ return Object.fromEntries(applyUserAgent(new Headers(callerHeaders ?? {})).entries());
19
+ }
20
+ var CREDENTIAL_HEADERS = /* @__PURE__ */ new Set([
21
+ "sign-in-with-x",
22
+ "payment-signature",
23
+ "x-payment",
24
+ "tenjin-session-delegation"
25
+ ]);
26
+ function carriesSignedMaterial(headers) {
27
+ return Object.keys(headers).some((name) => CREDENTIAL_HEADERS.has(name.toLowerCase()));
28
+ }
29
+ async function fetchJson(url, opts) {
30
+ const doFetch = opts.fetchImpl ?? fetch;
31
+ const controller = new AbortController();
32
+ let timedOut = false;
33
+ const timer = setTimeout(() => {
34
+ timedOut = true;
35
+ controller.abort();
36
+ }, opts.timeoutMs);
37
+ try {
38
+ let res;
39
+ try {
40
+ res = await doFetch(url, {
41
+ signal: controller.signal,
42
+ headers: withUserAgent(opts.headers)
43
+ });
44
+ } catch (err) {
45
+ return timedOut ? timeoutFailure(url, opts.timeoutMs) : { ok: false, kind: "network", message: `Request to ${url} failed: ${errorMessage(err)}` };
46
+ }
47
+ const requestId = res.headers.get("x-request-id") ?? void 0;
48
+ if (!res.ok) {
49
+ return {
50
+ ok: false,
51
+ kind: "http",
52
+ status: res.status,
53
+ ...requestId !== void 0 ? { requestId } : {},
54
+ message: `Request to ${url} failed with status ${res.status}`
55
+ };
56
+ }
57
+ let json;
58
+ try {
59
+ json = await res.json();
60
+ } catch (err) {
61
+ if (timedOut) return timeoutFailure(url, opts.timeoutMs);
62
+ if (err instanceof SyntaxError) {
63
+ return {
64
+ ok: false,
65
+ kind: "invalid-json",
66
+ status: res.status,
67
+ ...requestId !== void 0 ? { requestId } : {},
68
+ message: `Response from ${url} was not valid JSON`
69
+ };
70
+ }
71
+ return {
72
+ ok: false,
73
+ kind: "network",
74
+ status: res.status,
75
+ ...requestId !== void 0 ? { requestId } : {},
76
+ message: `Request to ${url} failed while reading the response body: ${errorMessage(err)}`
77
+ };
78
+ }
79
+ return {
80
+ ok: true,
81
+ status: res.status,
82
+ json,
83
+ ...requestId !== void 0 ? { requestId } : {}
84
+ };
85
+ } finally {
86
+ clearTimeout(timer);
87
+ }
88
+ }
89
+ function timeoutFailure(url, timeoutMs) {
90
+ return {
91
+ ok: false,
92
+ kind: "timeout",
93
+ message: `Request to ${url} timed out after ${timeoutMs}ms`
94
+ };
95
+ }
96
+ function prepareRequest(url, opts) {
97
+ try {
98
+ let body;
99
+ const merged = new Headers(opts.headers ?? {});
100
+ if (opts.jsonBody !== void 0) {
101
+ body = JSON.stringify(opts.jsonBody);
102
+ merged.set("content-type", "application/json");
103
+ }
104
+ const wantsAccept = opts.method === "POST" || opts.method === "PUT" || body !== void 0;
105
+ if (wantsAccept && !merged.has("accept")) merged.set("accept", "application/json");
106
+ const headers = Object.fromEntries(applyUserAgent(merged).entries());
107
+ const signed = carriesSignedMaterial(headers);
108
+ return { ok: true, headers, body, signed, pinned: signed || opts.blockRedirects === true };
109
+ } catch (err) {
110
+ return {
111
+ ok: false,
112
+ kind: "network",
113
+ message: `Request to ${url} failed: ${errorMessage(err)}`
114
+ };
115
+ }
116
+ }
117
+ async function httpRequest(url, opts) {
118
+ const doFetch = opts.fetchImpl ?? fetch;
119
+ const controller = new AbortController();
120
+ let timedOut = false;
121
+ const timer = setTimeout(() => {
122
+ timedOut = true;
123
+ controller.abort();
124
+ }, opts.timeoutMs);
125
+ try {
126
+ const prepared = prepareRequest(url, opts);
127
+ if (!prepared.ok) return prepared;
128
+ const { headers, body, signed, pinned } = prepared;
129
+ let res;
130
+ try {
131
+ res = await doFetch(url, {
132
+ method: opts.method ?? "GET",
133
+ headers,
134
+ body,
135
+ signal: controller.signal,
136
+ ...pinned ? { redirect: "manual" } : {}
137
+ });
138
+ } catch (err) {
139
+ return timedOut ? timeoutFailure(url, opts.timeoutMs) : { ok: false, kind: "network", message: `Request to ${url} failed: ${errorMessage(err)}` };
140
+ }
141
+ const requestId = res.headers.get("x-request-id") ?? void 0;
142
+ if (pinned && res.status >= 300 && res.status < 400) {
143
+ return {
144
+ ok: false,
145
+ kind: "blocked-redirect",
146
+ status: res.status,
147
+ ...requestId !== void 0 ? { requestId } : {},
148
+ message: signed ? `Request to ${url} was redirected (${res.status}) while carrying a signed header; refusing to follow it, because the signature is bound to the configured origin.` : `Request to ${url} was redirected (${res.status}); refusing to follow it, because the response must come from the configured origin.`
149
+ };
150
+ }
151
+ let text;
152
+ try {
153
+ text = await res.text();
154
+ } catch (err) {
155
+ if (timedOut) return timeoutFailure(url, opts.timeoutMs);
156
+ return {
157
+ ok: false,
158
+ kind: "network",
159
+ status: res.status,
160
+ ...requestId !== void 0 ? { requestId } : {},
161
+ message: `Request to ${url} failed while reading the response body: ${errorMessage(err)}`
162
+ };
163
+ }
164
+ let json;
165
+ if (text.length > 0) {
166
+ try {
167
+ json = JSON.parse(text);
168
+ } catch {
169
+ json = void 0;
170
+ }
171
+ }
172
+ return {
173
+ ok: true,
174
+ status: res.status,
175
+ header: (name) => res.headers.get(name) ?? void 0,
176
+ json,
177
+ ...requestId !== void 0 ? { requestId } : {}
178
+ };
179
+ } finally {
180
+ clearTimeout(timer);
181
+ }
182
+ }
183
+ function fetchFailureToCliError(failure, opts = {}) {
184
+ const code = failure.kind === "network" || failure.kind === "timeout" ? "NETWORK_ERROR" : failure.kind === "invalid-json" ? "CONTRACT_MISMATCH" : "API_UNREACHABLE";
185
+ return new CliError(code, failure.message, {
186
+ ...opts.fix !== void 0 ? { fix: opts.fix } : {},
187
+ ...opts.details !== void 0 ? { details: opts.details } : {}
188
+ });
189
+ }
190
+ function errorMessage(err) {
191
+ return err instanceof Error ? err.message : String(err);
192
+ }
193
+
194
+ export {
195
+ fetchJson,
196
+ httpRequest,
197
+ fetchFailureToCliError
198
+ };
199
+ //# sourceMappingURL=chunk-3GSZUADW.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/lib/client-meta.ts","../src/lib/http.ts"],"sourcesContent":["import pkg from '../../package.json';\n\n/**\n * The CLI's identity on every HTTP request, sent as the standard `User-Agent`\n * header from the shared transport (`http.ts`). The server's\n * `parseUserAgentProduct()` (tenjin PR #544) reads the leading RFC 9110 product\n * token (`tenjin-cli/<version>`) for canonical attribution; `X-Tenjin-Client` is\n * retired and this CLI sends it nowhere.\n */\nexport const TENJIN_USER_AGENT = `tenjin-cli/${pkg.version} (+https://tenjin.blog)`;\n","import { CliError } from './errors';\nimport { TENJIN_USER_AGENT } from './client-meta';\n\n/**\n * The single JSON client the whole CLI grows on: `doctor` uses it in B1, `search`\n * reuses it in B2. It never throws on a transport/protocol failure — it returns a\n * discriminated result so callers decide how each failure class maps to a check\n * or an error. `fetchImpl` is injectable so tests never touch the network.\n */\nexport interface FetchJsonOptions {\n /** Abort the request after this many milliseconds. */\n timeoutMs: number;\n /** Override global fetch (tests inject a stub returning canned Responses). */\n fetchImpl?: typeof fetch;\n /** Optional request headers, merged onto the User-Agent this module always sends. */\n headers?: Record<string, string>;\n}\n\n/**\n * The one place the identity is written; both transports funnel their Headers\n * through it, so a third entry point cannot ship without it. `.set` on a Headers\n * object is what makes it total: a caller header spelled `User-Agent` in any\n * case lands in the same slot and is overwritten, never duplicated.\n */\nfunction applyUserAgent(headers: Headers): Headers {\n headers.set('user-agent', TENJIN_USER_AGENT);\n return headers;\n}\n\n/** The same write for `fetchJson`'s plain-object headers; Headers.entries() is lowercase-keyed. */\nfunction withUserAgent(callerHeaders?: Record<string, string>): Record<string, string> {\n return Object.fromEntries(applyUserAgent(new Headers(callerHeaders ?? {})).entries());\n}\n\n/** A successful 2xx whose body parsed as JSON. */\nexport interface FetchJsonSuccess {\n ok: true;\n status: number;\n json: unknown;\n /** The server's `x-request-id`, when it sent one — useful in error reports. */\n requestId?: string;\n}\n\n/**\n * A failed request, tagged by class so the caller maps it deliberately:\n * `network`/`timeout` (never reached the server or no answer), `http` (a non-2xx\n * status), `invalid-json` (2xx but the body was not JSON).\n */\nexport interface FetchJsonFailure {\n ok: false;\n kind: 'network' | 'timeout' | 'http' | 'invalid-json' | 'blocked-redirect';\n status?: number;\n requestId?: string;\n message: string;\n}\n\n/**\n * Header names that carry wallet-signed material.\n *\n * A request bearing any of these must NEVER follow a redirect. `resource-ref`\n * pins the invariant — \"nothing signed may leave for a host the user did not\n * configure\" — but `assertOnBaseOrigin` only checks the URL the caller asked\n * for, and `fetch`'s default `redirect: 'follow'` re-sends request headers\n * verbatim to the new host (Node strips only `Authorization`). A 3xx anywhere on\n * the configured origin would therefore hand a signature BOUND TO THE REAL\n * DOMAIN to whoever `Location` points at, replayable against it for the\n * signature's lifetime.\n *\n * Detected by header NAME rather than by a caller-supplied \"this is signed\" flag\n * so the protection cannot be forgotten at a call site: attaching the credential\n * is what arms it. Matched case-insensitively. Kept as literals so the base\n * transport keeps its single dependency; `http.test.ts` pins them against the\n * real constants in `lib/siwx` and the x402 payment headers.\n *\n * All three wallet-signed families this CLI sends are here: the SIWX auth\n * header, the x402 payment header (`payment-signature` for x402 v2, plus\n * `x-payment`, the v1 spelling `@x402/core` can emit, as drift insurance even\n * though `read-client` pins v2), and `tenjin-session-delegation` — a REUSABLE\n * session-lifetime SIWX signature `session-key` attaches to every session-signed\n * write, which makes it the most replayable credential of the set. The RFC 9421\n * `Signature`/`Signature-Input` pair riding alongside it is deliberately absent:\n * it covers method, URL, and body digest, so it is not usefully replayable at\n * another host.\n */\nconst CREDENTIAL_HEADERS = new Set([\n 'sign-in-with-x',\n 'payment-signature',\n 'x-payment',\n 'tenjin-session-delegation',\n]);\n\nfunction carriesSignedMaterial(headers: Record<string, string>): boolean {\n return Object.keys(headers).some((name) => CREDENTIAL_HEADERS.has(name.toLowerCase()));\n}\n\nexport type FetchJsonResult = FetchJsonSuccess | FetchJsonFailure;\n\nexport async function fetchJson(url: string, opts: FetchJsonOptions): Promise<FetchJsonResult> {\n const doFetch = opts.fetchImpl ?? fetch;\n const controller = new AbortController();\n let timedOut = false;\n // The timer stays armed until the body is fully consumed (cleared in the outer\n // finally), so a server that sends headers then stalls the body still trips the\n // deadline instead of hanging forever on res.json().\n const timer = setTimeout(() => {\n timedOut = true;\n controller.abort();\n }, opts.timeoutMs);\n\n try {\n let res: Response;\n try {\n res = await doFetch(url, {\n signal: controller.signal,\n headers: withUserAgent(opts.headers),\n });\n } catch (err) {\n // A timeout is a network failure the AbortController induced; distinguish it\n // from an organic one so the caller can say \"timed out\" rather than a raw\n // AbortError message.\n return timedOut\n ? timeoutFailure(url, opts.timeoutMs)\n : { ok: false, kind: 'network', message: `Request to ${url} failed: ${errorMessage(err)}` };\n }\n\n const requestId = res.headers.get('x-request-id') ?? undefined;\n\n if (!res.ok) {\n return {\n ok: false,\n kind: 'http',\n status: res.status,\n ...(requestId !== undefined ? { requestId } : {}),\n message: `Request to ${url} failed with status ${res.status}`,\n };\n }\n\n let json: unknown;\n try {\n json = await res.json();\n } catch (err) {\n // The body read can fail three ways. A timeout wins first: the abort fires\n // synchronously with `timedOut`, so any abort-induced rejection here is our\n // deadline. Otherwise a SyntaxError is a genuine parse failure (invalid\n // JSON); anything else is a transport failure mid-body (network).\n if (timedOut) return timeoutFailure(url, opts.timeoutMs);\n if (err instanceof SyntaxError) {\n return {\n ok: false,\n kind: 'invalid-json',\n status: res.status,\n ...(requestId !== undefined ? { requestId } : {}),\n message: `Response from ${url} was not valid JSON`,\n };\n }\n return {\n ok: false,\n kind: 'network',\n status: res.status,\n ...(requestId !== undefined ? { requestId } : {}),\n message: `Request to ${url} failed while reading the response body: ${errorMessage(err)}`,\n };\n }\n\n return {\n ok: true,\n status: res.status,\n json,\n ...(requestId !== undefined ? { requestId } : {}),\n };\n } finally {\n clearTimeout(timer);\n }\n}\n\nfunction timeoutFailure(url: string, timeoutMs: number): FetchJsonFailure {\n return {\n ok: false,\n kind: 'timeout',\n message: `Request to ${url} timed out after ${timeoutMs}ms`,\n };\n}\n\n/**\n * A full HTTP round trip that surfaces the STATUS, headers, and best-effort JSON\n * body for ANY status. Unlike fetchJson (2xx-only), the B2 read/pay flow needs\n * the body on a 402 (the leak-safe preview) and a 409 (the owned-re-pay gate),\n * and search/outcome need a 4xx validation envelope. A transport/timeout failure\n * still returns the discriminated FetchJsonFailure so callers map it uniformly.\n */\nexport interface HttpRequestOptions {\n method?: 'GET' | 'POST' | 'PUT';\n timeoutMs: number;\n headers?: Record<string, string>;\n /** A JSON body (POST); serialized with a content-type header set automatically. */\n jsonBody?: unknown;\n fetchImpl?: typeof fetch;\n /**\n * Refuse redirects even when the request carries no signed header. For a\n * caller whose response becomes a durable local artifact (`fetchRead`: the\n * 200 is written to the library as an entitlement record under the\n * server-chosen id/slug), following a cross-origin redirect would let\n * another host's bytes be recorded as if the configured origin served them.\n * Off by default so search/outcome/publish/doctor keep normal transport.\n */\n blockRedirects?: boolean;\n}\n\nexport interface HttpResponse {\n ok: true;\n status: number;\n /** Case-insensitive header lookup over the response. */\n header(name: string): string | undefined;\n /** Parsed JSON body, or undefined when the body was empty or not JSON. */\n json: unknown;\n requestId?: string;\n}\n\nexport type HttpResult = HttpResponse | FetchJsonFailure;\n\n/** The wire form of one request: what to send, and whether it may follow a redirect. */\ninterface PreparedRequest {\n ok: true;\n headers: Record<string, string>;\n body: string | undefined;\n signed: boolean;\n pinned: boolean;\n}\n\n/**\n * Assemble the request and DERIVE its redirect decision from the headers that\n * were actually built, in one place and one pass. The redirect flags are\n * returned rather than assigned, so they have no value until the headers do:\n * there is no state in which `pinned` is readable but not yet computed, which\n * is what would let a signed request follow a 3xx to another host. Anything\n * that refuses here (`new Headers` throws on a malformed caller header) becomes\n * the same discriminated failure a dead socket does, because this transport\n * documents a returned failure for every refusal, not an exception.\n */\nfunction prepareRequest(url: string, opts: HttpRequestOptions): PreparedRequest | FetchJsonFailure {\n try {\n let body: string | undefined;\n const merged = new Headers(opts.headers ?? {});\n if (opts.jsonBody !== undefined) {\n body = JSON.stringify(opts.jsonBody);\n merged.set('content-type', 'application/json');\n }\n const wantsAccept = opts.method === 'POST' || opts.method === 'PUT' || body !== undefined;\n if (wantsAccept && !merged.has('accept')) merged.set('accept', 'application/json');\n // An `accept`/`content-type` set here wins the slot regardless of how a\n // caller cased its own copy.\n const headers = Object.fromEntries(applyUserAgent(merged).entries());\n\n // Signed requests opt out of redirect following entirely; see CREDENTIAL_HEADERS.\n // A caller can also pin an unsigned request (blockRedirects) when the\n // response it gets back becomes a durable local record.\n const signed = carriesSignedMaterial(headers);\n return { ok: true, headers, body, signed, pinned: signed || opts.blockRedirects === true };\n } catch (err) {\n return {\n ok: false,\n kind: 'network',\n message: `Request to ${url} failed: ${errorMessage(err)}`,\n };\n }\n}\n\nexport async function httpRequest(url: string, opts: HttpRequestOptions): Promise<HttpResult> {\n const doFetch = opts.fetchImpl ?? fetch;\n const controller = new AbortController();\n let timedOut = false;\n const timer = setTimeout(() => {\n timedOut = true;\n controller.abort();\n }, opts.timeoutMs);\n\n try {\n const prepared = prepareRequest(url, opts);\n if (!prepared.ok) return prepared;\n const { headers, body, signed, pinned } = prepared;\n\n let res: Response;\n try {\n res = await doFetch(url, {\n method: opts.method ?? 'GET',\n headers,\n body,\n signal: controller.signal,\n ...(pinned ? { redirect: 'manual' as const } : {}),\n });\n } catch (err) {\n return timedOut\n ? timeoutFailure(url, opts.timeoutMs)\n : { ok: false, kind: 'network', message: `Request to ${url} failed: ${errorMessage(err)}` };\n }\n\n const requestId = res.headers.get('x-request-id') ?? undefined;\n\n // Fail CLOSED on a redirect the request was not allowed to follow. This\n // returns a failure rather than the 3xx response so no caller can mistake an\n // unfollowed redirect for a normal status and retry it by hand.\n if (pinned && res.status >= 300 && res.status < 400) {\n return {\n ok: false,\n kind: 'blocked-redirect',\n status: res.status,\n ...(requestId !== undefined ? { requestId } : {}),\n message: signed\n ? `Request to ${url} was redirected (${res.status}) while carrying a signed header; ` +\n 'refusing to follow it, because the signature is bound to the configured origin.'\n : `Request to ${url} was redirected (${res.status}); ` +\n 'refusing to follow it, because the response must come from the configured origin.',\n };\n }\n // Read the raw text once; parse best-effort. A non-JSON body (empty 202, an\n // HTML error page) yields `undefined` rather than a thrown parse, because a\n // 402/409 caller keys off the STATUS and only some statuses carry JSON.\n let text: string;\n try {\n text = await res.text();\n } catch (err) {\n if (timedOut) return timeoutFailure(url, opts.timeoutMs);\n return {\n ok: false,\n kind: 'network',\n status: res.status,\n ...(requestId !== undefined ? { requestId } : {}),\n message: `Request to ${url} failed while reading the response body: ${errorMessage(err)}`,\n };\n }\n let json: unknown;\n if (text.length > 0) {\n try {\n json = JSON.parse(text);\n } catch {\n json = undefined;\n }\n }\n return {\n ok: true,\n status: res.status,\n header: (name) => res.headers.get(name) ?? undefined,\n json,\n ...(requestId !== undefined ? { requestId } : {}),\n };\n } finally {\n clearTimeout(timer);\n }\n}\n\nexport interface FailureToCliErrorOptions {\n fix?: string;\n details?: unknown;\n}\n\n/**\n * Normalize a fetch failure into the CLI error contract: network/timeout are a\n * NETWORK_ERROR, an `http` status (server reachable but errored — commonly 5xx)\n * is API_UNREACHABLE, and a 2xx that failed to parse when JSON was required is a\n * CONTRACT_MISMATCH. Callers that need a check-specific code (doctor) build their\n * own CliError; this is the default B2's passthrough leans on.\n */\nexport function fetchFailureToCliError(\n failure: FetchJsonFailure,\n opts: FailureToCliErrorOptions = {},\n): CliError {\n const code =\n failure.kind === 'network' || failure.kind === 'timeout'\n ? 'NETWORK_ERROR'\n : failure.kind === 'invalid-json'\n ? 'CONTRACT_MISMATCH'\n : 'API_UNREACHABLE';\n return new CliError(code, failure.message, {\n ...(opts.fix !== undefined ? { fix: opts.fix } : {}),\n ...(opts.details !== undefined ? { details: opts.details } : {}),\n });\n}\n\nfunction errorMessage(err: unknown): string {\n return err instan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