@hop-top/kit 0.5.0-alpha.0 → 0.5.0-alpha.10

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 (284) hide show
  1. package/CHANGELOG.md +262 -0
  2. package/LICENSE +1 -1
  3. package/LICENSE-cobra.txt +174 -0
  4. package/NOTICE +15 -0
  5. package/README.md +55 -259
  6. package/dist/aim-DZHwwiIc.d.ts +139 -0
  7. package/dist/aim.d.ts +1 -116
  8. package/dist/aim.js +13 -414
  9. package/dist/alias-D9_qFlyP.d.ts +174 -0
  10. package/dist/alias.d.ts +2 -73
  11. package/dist/alias.js +27 -207
  12. package/dist/api-DAexsu2H.d.ts +77 -0
  13. package/dist/api.d.ts +1 -62
  14. package/dist/api.js +10 -164
  15. package/dist/auth-2-jfgiHn.d.ts +34 -0
  16. package/dist/auth.d.ts +1 -26
  17. package/dist/auth.js +6 -40
  18. package/dist/chunk-2JV3A7OP.js +1894 -0
  19. package/dist/chunk-3FIKGFKI.js +54 -0
  20. package/dist/chunk-4UE5TCMQ.js +104 -0
  21. package/dist/chunk-6HFAEZ52.js +29 -0
  22. package/dist/chunk-6SVOHNIC.js +17 -0
  23. package/dist/chunk-72XZWQX7.js +504 -0
  24. package/dist/chunk-ABQSE7AI.js +513 -0
  25. package/dist/chunk-BDNNPRX6.js +41 -0
  26. package/dist/chunk-BLS2OBIQ.js +263 -0
  27. package/dist/chunk-BRNEKPWX.js +37 -0
  28. package/dist/chunk-CSJWB6D3.js +151 -0
  29. package/dist/chunk-GGWJRWWA.js +309 -0
  30. package/dist/chunk-GHPJJV5L.js +613 -0
  31. package/dist/chunk-IXVQ62NZ.js +334 -0
  32. package/dist/chunk-JKMQSAJP.js +174 -0
  33. package/dist/chunk-JZ22ZUFA.js +1131 -0
  34. package/dist/chunk-K7VCKJ27.js +814 -0
  35. package/dist/chunk-KVG7YCTS.js +115 -0
  36. package/dist/chunk-L23O2FFE.js +51 -0
  37. package/dist/chunk-MQXYDYRD.js +968 -0
  38. package/dist/chunk-MUIQVU6E.js +251 -0
  39. package/dist/chunk-NLMS7HGP.js +178 -0
  40. package/dist/chunk-POR5L2DG.js +247 -0
  41. package/dist/chunk-PSFYJGDG.js +1445 -0
  42. package/dist/chunk-QJVSKKRQ.js +369 -0
  43. package/dist/chunk-QPUCXQXT.js +67 -0
  44. package/dist/chunk-QQQYCWSC.js +240 -0
  45. package/dist/chunk-R5U7XKVJ.js +16 -0
  46. package/dist/chunk-SNVGDYX7.js +187 -0
  47. package/dist/chunk-T46YPHKN.js +669 -0
  48. package/dist/chunk-UKZIAQA7.js +26 -0
  49. package/dist/chunk-WBVO43HU.js +355 -0
  50. package/dist/chunk-WUOAQVOA.js +86 -0
  51. package/dist/chunk-WWUKGJB5.js +90 -0
  52. package/dist/chunk-XJTRY453.js +868 -0
  53. package/dist/chunk-YGYQUFGY.js +219 -0
  54. package/dist/chunk-YOBAUCVN.js +583 -0
  55. package/dist/cli-C5B5tnpA.d.ts +400 -0
  56. package/dist/cli.d.ts +7 -309
  57. package/dist/cli.js +70 -885
  58. package/dist/completion-csVun6SP.d.ts +112 -0
  59. package/dist/completion-values-By-mnjNt.d.ts +142 -0
  60. package/dist/completion-values.d.ts +1 -0
  61. package/dist/completion-values.js +29 -0
  62. package/dist/completion.d.ts +3 -0
  63. package/dist/completion.js +34 -0
  64. package/dist/compliance-AxFf-uMy.d.ts +60 -0
  65. package/dist/compliance.d.ts +1 -0
  66. package/dist/compliance.js +17 -0
  67. package/dist/config-CaK60ByQ.d.ts +241 -0
  68. package/dist/config.d.ts +1 -97
  69. package/dist/config.js +8 -78
  70. package/dist/errcorrect-BcX1Ft0K.d.ts +44 -0
  71. package/dist/errcorrect.d.ts +1 -37
  72. package/dist/errcorrect.js +8 -68
  73. package/dist/error-Do8Qji43.d.ts +231 -0
  74. package/dist/id/index.d.ts +2 -83
  75. package/dist/id/index.js +14 -65
  76. package/dist/index-BKJZClJx.d.ts +1486 -0
  77. package/dist/index-C3Zw4wDJ.d.ts +359 -0
  78. package/dist/index-DCe1boBz.d.ts +328 -0
  79. package/dist/index-VLDcI5NF.d.ts +95 -0
  80. package/dist/index.d.ts +37 -0
  81. package/dist/index.js +132 -0
  82. package/dist/llm-DYuR7_uX.d.ts +1069 -0
  83. package/dist/llm-anthropic-Cd_I2xXB.d.ts +67 -0
  84. package/dist/llm-anthropic.d.ts +2 -0
  85. package/dist/llm-anthropic.js +13 -0
  86. package/dist/llm-openai-DyFrucmV.d.ts +87 -0
  87. package/dist/llm-openai.d.ts +2 -0
  88. package/dist/llm-openai.js +19 -0
  89. package/dist/llm.d.ts +1 -188
  90. package/dist/llm.js +101 -356
  91. package/dist/log-CkaxMX3U.d.ts +115 -0
  92. package/dist/log.d.ts +2 -0
  93. package/dist/log.js +14 -0
  94. package/dist/mcp/index.d.ts +4 -0
  95. package/dist/mcp/index.js +184 -0
  96. package/dist/netpolicy-Brx5XZUB.d.ts +151 -0
  97. package/dist/netpolicy.d.ts +1 -0
  98. package/dist/netpolicy.js +25 -0
  99. package/dist/output-CjZWmL8s.d.ts +420 -0
  100. package/dist/output.d.ts +3 -213
  101. package/dist/output.js +137 -447
  102. package/dist/parity-CvMuYcVI.d.ts +61 -0
  103. package/dist/progress-CAzyxeWl.d.ts +38 -0
  104. package/dist/progress.d.ts +1 -30
  105. package/dist/progress.js +6 -55
  106. package/dist/provenance-SMFHm5gx.d.ts +30 -0
  107. package/dist/provenance.d.ts +1 -24
  108. package/dist/provenance.js +6 -31
  109. package/dist/routellm-B0nv59mH.d.ts +111 -0
  110. package/dist/routellm.d.ts +2 -64
  111. package/dist/routellm.js +13 -255
  112. package/dist/rpc-D31Lg0Kc.d.ts +32 -0
  113. package/dist/rpc.d.ts +2 -25
  114. package/dist/rpc.js +4 -98
  115. package/dist/safety-CUaYEjZc.d.ts +30 -0
  116. package/dist/safety.d.ts +1 -24
  117. package/dist/safety.js +6 -43
  118. package/dist/scope-BrhYB4ij.d.ts +141 -0
  119. package/dist/scope.d.ts +1 -121
  120. package/dist/scope.js +20 -377
  121. package/dist/serve-D8yWMrTc.d.ts +646 -0
  122. package/dist/serve.d.ts +3 -0
  123. package/dist/serve.js +75 -0
  124. package/dist/sqlstore.js +6 -39
  125. package/dist/stream-CrjeCp0M.d.ts +44 -0
  126. package/dist/stream.d.ts +1 -35
  127. package/dist/stream.js +8 -51
  128. package/dist/telemetry/index.d.ts +1 -315
  129. package/dist/telemetry/index.js +26 -599
  130. package/dist/toolspec-DcPwwoAZ.d.ts +368 -0
  131. package/dist/toolspec.d.ts +2 -0
  132. package/dist/toolspec.js +37 -0
  133. package/dist/tui/index.d.ts +6 -298
  134. package/dist/tui/index.js +18 -322
  135. package/dist/upgrade-p_zh3pvV.d.ts +102 -0
  136. package/dist/upgrade.d.ts +1 -95
  137. package/dist/upgrade.js +6 -114
  138. package/dist/xdg-DB7Shoib.d.ts +179 -0
  139. package/dist/xdg.d.ts +1 -169
  140. package/dist/xdg.js +14 -128
  141. package/package.json +236 -46
  142. package/src/README.md +15 -0
  143. package/src/alias.conformance.test.ts +250 -0
  144. package/src/alias.test.ts +206 -2
  145. package/src/alias.ts +196 -21
  146. package/src/cli.help-command.test.ts +105 -0
  147. package/src/cli.offline.test.ts +93 -0
  148. package/src/cli.parity.test.ts +192 -0
  149. package/src/cli.test.ts +95 -1
  150. package/src/cli.ts +195 -25
  151. package/src/completion-scripts.ts +16 -0
  152. package/src/completion-values.test.ts +76 -16
  153. package/src/completion-values.ts +170 -66
  154. package/src/completion.conformance.test.ts +297 -0
  155. package/src/completion.test.ts +256 -145
  156. package/src/completion.ts +767 -121
  157. package/src/compliance.test.ts +196 -2
  158. package/src/compliance.ts +440 -20
  159. package/src/config-merge.test.ts +425 -0
  160. package/src/config.test.ts +228 -1
  161. package/src/config.ts +429 -16
  162. package/src/gen/crud_pb.ts +7 -7
  163. package/src/id/README.md +38 -0
  164. package/src/id/contract.test.ts +1 -1
  165. package/src/id/index.test.ts +1 -1
  166. package/src/id/index.ts +3 -2
  167. package/src/index.test.ts +71 -0
  168. package/src/index.ts +46 -0
  169. package/src/llm-anthropic.conformance.test.ts +1135 -0
  170. package/src/llm-anthropic.test.ts +152 -0
  171. package/src/llm-anthropic.ts +940 -0
  172. package/src/llm-choice-hermetic.test.ts +335 -0
  173. package/src/llm-config.test.ts +232 -0
  174. package/src/llm-config.ts +106 -0
  175. package/src/llm-endpoint.ts +254 -0
  176. package/src/llm-error.ts +17 -0
  177. package/src/llm-errors.ts +152 -0
  178. package/src/llm-file.ts +126 -0
  179. package/src/llm-http.ts +317 -0
  180. package/src/llm-keys.ts +167 -0
  181. package/src/llm-media.conformance.test.ts +87 -0
  182. package/src/llm-media.test.ts +279 -0
  183. package/src/llm-media.ts +322 -0
  184. package/src/llm-openai.conformance.test.ts +407 -0
  185. package/src/llm-openai.test.ts +510 -0
  186. package/src/llm-openai.ts +708 -0
  187. package/src/llm-payload.ts +155 -0
  188. package/src/llm-quote.ts +46 -0
  189. package/src/llm-routellm.conformance.test.ts +44 -0
  190. package/src/llm-toolnames.conformance.test.ts +112 -0
  191. package/src/llm-toolnames.test.ts +132 -0
  192. package/src/llm-toolnames.ts +298 -0
  193. package/src/llm-uri.ts +99 -0
  194. package/src/llm.test.ts +43 -15
  195. package/src/llm.toolcall.test.ts +175 -0
  196. package/src/llm.ts +602 -190
  197. package/src/log.parity.test.ts +105 -0
  198. package/src/log.test.ts +270 -1
  199. package/src/log.ts +228 -49
  200. package/src/mcp/README.md +63 -0
  201. package/src/mcp/args-separator.test.ts +164 -0
  202. package/src/mcp/args.test.ts +223 -0
  203. package/src/mcp/args.ts +160 -0
  204. package/src/mcp/audit.test.ts +520 -0
  205. package/src/mcp/audit.ts +70 -0
  206. package/src/mcp/bridge.test.ts +222 -0
  207. package/src/mcp/bridge.ts +291 -0
  208. package/src/mcp/conformance.test.ts +357 -0
  209. package/src/mcp/dispatch.ts +359 -0
  210. package/src/mcp/identity.test.ts +166 -0
  211. package/src/mcp/identity.ts +72 -0
  212. package/src/mcp/index.ts +176 -0
  213. package/src/mcp/legacy-notify.test.ts +59 -0
  214. package/src/mcp/legacy.ts +367 -0
  215. package/src/mcp/mcp.test.ts +707 -0
  216. package/src/mcp/modern-confirm.test.ts +520 -0
  217. package/src/mcp/modern-confirm.ts +484 -0
  218. package/src/mcp/modern.ts +788 -0
  219. package/src/mcp/stdio.test.ts +313 -0
  220. package/src/mcp/stdio.ts +254 -0
  221. package/src/mcp/tasks.ts +82 -0
  222. package/src/mcp/testtree.ts +250 -0
  223. package/src/mcp/toolname.test.ts +147 -0
  224. package/src/mcp/types.ts +689 -0
  225. package/src/netpolicy.test.ts +280 -0
  226. package/src/netpolicy.ts +256 -0
  227. package/src/output/README.md +46 -0
  228. package/src/output/builtins.ts +5 -5
  229. package/src/output/dispatch.ts +10 -2
  230. package/src/output/error.ts +459 -0
  231. package/src/output/formatter.ts +29 -5
  232. package/src/output/formatters/README.md +50 -0
  233. package/src/output/formatters/csv.ts +48 -12
  234. package/src/output/formatters/json.ts +4 -25
  235. package/src/output/formatters/project.ts +34 -0
  236. package/src/output/formatters/table.ts +8 -6
  237. package/src/output/formatters/text.ts +7 -3
  238. package/src/output/formatters/yaml.ts +5 -22
  239. package/src/output/projection.test.ts +298 -0
  240. package/src/output/projection.ts +33 -59
  241. package/src/output/template.ts +11 -21
  242. package/src/output.surface.test.ts +41 -0
  243. package/src/output.ts +64 -3
  244. package/src/published-types.test.ts +133 -0
  245. package/src/routellm.test.ts +209 -28
  246. package/src/routellm.toolcall.test.ts +521 -0
  247. package/src/routellm.ts +213 -177
  248. package/src/router/README.md +44 -0
  249. package/src/router/server.test.ts +41 -0
  250. package/src/router/server.ts +11 -0
  251. package/src/safety.test.ts +19 -9
  252. package/src/scope-defaults.json +29 -29
  253. package/src/serve.test.ts +1257 -0
  254. package/src/serve.ts +1665 -0
  255. package/src/telemetry/README.md +48 -0
  256. package/src/telemetry/client.test.ts +31 -0
  257. package/src/telemetry/client.ts +12 -6
  258. package/src/telemetry/offline.test.ts +64 -0
  259. package/src/toolspec-internal.ts +279 -0
  260. package/src/toolspec-manifest.test.ts +167 -0
  261. package/src/toolspec-manifest.ts +410 -0
  262. package/src/toolspec-spec-schemas.ts +12 -0
  263. package/src/toolspec-spec.test.ts +273 -0
  264. package/src/toolspec-spec.ts +458 -0
  265. package/src/toolspec.from-command.test.ts +350 -0
  266. package/src/toolspec.test.ts +2 -2
  267. package/src/toolspec.ts +222 -0
  268. package/src/triton/README.md +34 -0
  269. package/src/tui/README.md +42 -0
  270. package/src/tui/parity.test.ts +142 -0
  271. package/src/tui/parity.ts +59 -13
  272. package/src/wizard.test.ts +7 -7
  273. package/test/llm/conformance-helpers.ts +100 -0
  274. package/test/output/csv.test.ts +164 -0
  275. package/test/output/dispatch.test.ts +1 -2
  276. package/test/output/error.test.ts +277 -0
  277. package/test/output/smoke-capture.ts +2 -2
  278. package/test/output/smoke.test.ts +2 -2
  279. package/test/output/taxonomy-contract.test.ts +148 -0
  280. package/test/setup-env.ts +16 -0
  281. package/test/toolspec/fixture.ts +79 -0
  282. package/tsconfig.json +3 -1
  283. package/tsconfig.test.json +10 -0
  284. package/vitest.config.ts +9 -0
@@ -0,0 +1,583 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }
2
+
3
+ var _chunkKVG7YCTSjs = require('./chunk-KVG7YCTS.js');
4
+
5
+
6
+ var _chunkR5U7XKVJjs = require('./chunk-R5U7XKVJ.js');
7
+
8
+ // src/telemetry/index.ts
9
+ var telemetry_exports = {};
10
+ _chunkR5U7XKVJjs.__export.call(void 0, telemetry_exports, {
11
+ Client: () => Client,
12
+ Mode: () => Mode,
13
+ consentPath: () => consentPath,
14
+ deniedConsent: () => deniedConsent,
15
+ getInstallId: () => getInstallId,
16
+ installIdPath: () => installIdPath,
17
+ loadConsent: () => loadConsent,
18
+ parseMode: () => parseMode,
19
+ redact: () => redact,
20
+ redactString: () => redactString,
21
+ resetForTest: () => resetForTest,
22
+ resolveMode: () => resolveMode,
23
+ rotate: () => rotate
24
+ });
25
+
26
+ // src/telemetry/mode.ts
27
+ var Mode = {
28
+ Off: "off",
29
+ Anon: "anon",
30
+ Full: "full"
31
+ };
32
+ function parseMode(s) {
33
+ if (s === void 0 || s === null || s === "") {
34
+ return [Mode.Off, true];
35
+ }
36
+ const low = s.trim().toLowerCase();
37
+ if (low === "") {
38
+ return [Mode.Off, true];
39
+ }
40
+ for (const m of Object.values(Mode)) {
41
+ if (m === low) {
42
+ return [m, true];
43
+ }
44
+ }
45
+ return [Mode.Off, false];
46
+ }
47
+ function resolveAppPrefix(env) {
48
+ return (_nullishCoalesce(env.KIT_APP_PREFIX, () => ( ""))).trim().toUpperCase();
49
+ }
50
+ function resolveMode(env = process.env) {
51
+ const app = resolveAppPrefix(env);
52
+ if (app) {
53
+ const v2 = (_nullishCoalesce(env[`${app}_TELEMETRY_MODE`], () => ( ""))).trim();
54
+ if (v2) {
55
+ const [m, ok] = parseMode(v2);
56
+ if (ok) {
57
+ return m;
58
+ }
59
+ }
60
+ }
61
+ const v = (_nullishCoalesce(env.KIT_TELEMETRY_MODE, () => ( ""))).trim();
62
+ if (v) {
63
+ const [m, ok] = parseMode(v);
64
+ if (ok) {
65
+ return m;
66
+ }
67
+ }
68
+ return Mode.Off;
69
+ }
70
+
71
+ // src/telemetry/installId.ts
72
+ var _fs = require('fs');
73
+
74
+ var _crypto = require('crypto');
75
+ var _os = require('os'); var os = _interopRequireWildcard(_os);
76
+ var _path = require('path'); var path = _interopRequireWildcard(_path); var path2 = _interopRequireWildcard(_path); var path3 = _interopRequireWildcard(_path);
77
+ var installIDSize = 32;
78
+ var installIDFilePerm = 384;
79
+ var installIDDirPerm = 448;
80
+ function xdgStateHome() {
81
+ return _nullishCoalesce(process.env.XDG_STATE_HOME, () => ( path.join(_os.homedir.call(void 0, ), ".local", "state")));
82
+ }
83
+ function installIdPath() {
84
+ return path.join(xdgStateHome(), "kit", "telemetry", "installation_id");
85
+ }
86
+ function sha256Hex(buf) {
87
+ return _crypto.createHash.call(void 0, "sha256").update(buf).digest("hex");
88
+ }
89
+ async function readAndValidate(p) {
90
+ const data = await _fs.promises.readFile(p);
91
+ if (data.length !== installIDSize) {
92
+ throw new Error(
93
+ `install_id: file ${p} has wrong size ${data.length} bytes, expected ${installIDSize}`
94
+ );
95
+ }
96
+ return data;
97
+ }
98
+ async function getInstallId() {
99
+ const p = installIdPath();
100
+ if (_fs.existsSync.call(void 0, p)) {
101
+ const data = await readAndValidate(p);
102
+ return sha256Hex(data);
103
+ }
104
+ const parent = path.dirname(p);
105
+ await _fs.promises.mkdir(parent, { recursive: true, mode: installIDDirPerm });
106
+ try {
107
+ await _fs.promises.chmod(parent, installIDDirPerm);
108
+ } catch (e2) {
109
+ }
110
+ const fresh = _crypto.randomBytes.call(void 0, installIDSize);
111
+ const tmp = p + ".new";
112
+ await _fs.promises.writeFile(tmp, fresh, { mode: installIDFilePerm });
113
+ if (_fs.existsSync.call(void 0, p)) {
114
+ try {
115
+ await _fs.promises.unlink(tmp);
116
+ } catch (e3) {
117
+ }
118
+ const data = await readAndValidate(p);
119
+ return sha256Hex(data);
120
+ }
121
+ await _fs.promises.rename(tmp, p);
122
+ return sha256Hex(fresh);
123
+ }
124
+ async function rotate() {
125
+ const p = installIdPath();
126
+ const parent = path.dirname(p);
127
+ await _fs.promises.mkdir(parent, { recursive: true, mode: installIDDirPerm });
128
+ try {
129
+ await _fs.promises.chmod(parent, installIDDirPerm);
130
+ } catch (e4) {
131
+ }
132
+ const fresh = _crypto.randomBytes.call(void 0, installIDSize);
133
+ const tmp = p + ".new";
134
+ await _fs.promises.writeFile(tmp, fresh, { mode: installIDFilePerm });
135
+ await _fs.promises.rename(tmp, p);
136
+ return sha256Hex(fresh);
137
+ }
138
+ async function resetForTest() {
139
+ const p = installIdPath();
140
+ try {
141
+ await _fs.promises.unlink(p);
142
+ } catch (e5) {
143
+ }
144
+ try {
145
+ await _fs.promises.unlink(p + ".new");
146
+ } catch (e6) {
147
+ }
148
+ }
149
+
150
+ // src/telemetry/consent.ts
151
+
152
+
153
+
154
+
155
+ var _jsyaml = require('js-yaml'); var yaml = _interopRequireWildcard(_jsyaml); var yaml2 = _interopRequireWildcard(_jsyaml);
156
+ var deniedConsent = {
157
+ allowed: false,
158
+ promptVersion: 0,
159
+ decisionSource: "config"
160
+ };
161
+ function xdgConfigHome() {
162
+ return _nullishCoalesce(process.env.XDG_CONFIG_HOME, () => ( path2.join(_os.homedir.call(void 0, ), ".config")));
163
+ }
164
+ function consentPath() {
165
+ return path2.join(xdgConfigHome(), "kit", "config.yaml");
166
+ }
167
+ function legacyConsentPath() {
168
+ return path2.join(xdgConfigHome(), "kit", "telemetry.yaml");
169
+ }
170
+ async function loadConsent() {
171
+ const canonical = await readConsentBlock(consentPath(), "canonical");
172
+ if (canonical !== null) {
173
+ return canonical;
174
+ }
175
+ const legacy = await readConsentBlock(legacyConsentPath(), "legacy");
176
+ if (legacy !== null) {
177
+ return legacy;
178
+ }
179
+ return deniedConsent;
180
+ }
181
+ async function readConsentBlock(p, variant) {
182
+ if (!_fs.existsSync.call(void 0, p)) {
183
+ return null;
184
+ }
185
+ let data;
186
+ try {
187
+ const raw = await _fs.promises.readFile(p, "utf8");
188
+ data = yaml.load(raw);
189
+ } catch (e7) {
190
+ return null;
191
+ }
192
+ if (typeof data !== "object" || data === null) {
193
+ return null;
194
+ }
195
+ const root = data;
196
+ let telemetry;
197
+ if (variant === "canonical") {
198
+ const kit = root.kit;
199
+ if (typeof kit !== "object" || kit === null) {
200
+ return null;
201
+ }
202
+ telemetry = kit.telemetry;
203
+ } else {
204
+ telemetry = root.telemetry;
205
+ }
206
+ if (typeof telemetry !== "object" || telemetry === null) {
207
+ return null;
208
+ }
209
+ const block = telemetry.consent;
210
+ if (typeof block !== "object" || block === null) {
211
+ return null;
212
+ }
213
+ const c = block;
214
+ const state = c.state;
215
+ if (state !== "granted" && state !== "denied") {
216
+ return null;
217
+ }
218
+ return {
219
+ allowed: state === "granted",
220
+ promptVersion: typeof c.prompt_version === "number" ? c.prompt_version : 0,
221
+ decisionSource: typeof c.decision_source === "string" ? c.decision_source : "config",
222
+ decidedAt: parseDecidedAt(c.decided_at)
223
+ };
224
+ }
225
+ function parseDecidedAt(v) {
226
+ if (typeof v === "string") {
227
+ return v;
228
+ }
229
+ if (v instanceof Date && !Number.isNaN(v.getTime())) {
230
+ return v.toISOString().replace(/\.\d{3}Z$/, "Z");
231
+ }
232
+ return void 0;
233
+ }
234
+
235
+ // src/telemetry/redact.ts
236
+
237
+ var TOKEN_SK = /\bsk-[A-Za-z0-9_-]{8,}\b/g;
238
+ var TOKEN_GH = /\bgh[pousr]_[A-Za-z0-9_-]{16,}\b/g;
239
+ var TOKEN_XOXB = /\bxoxb-[0-9]+-[0-9]+-[A-Za-z0-9]{24,}\b/g;
240
+ var EMAIL = /\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b/g;
241
+ var IPV6 = /(?<![0-9a-fA-F:])(?:(?:[0-9a-fA-F]{1,4}:){1,7}(?::[0-9a-fA-F]{1,4})+|(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4})(?![0-9a-fA-F:])/g;
242
+ var IPV4 = /\b(?:\d{1,3}\.){3}\d{1,3}\b/g;
243
+ function escapeRegex(s) {
244
+ return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
245
+ }
246
+ var HOME = os.homedir();
247
+ var HOME_PATTERN = HOME && HOME !== "~" ? new RegExp(escapeRegex(HOME) + "([/\\w.\\-]*)", "g") : /(?!x)x/g;
248
+ var REPLACEMENTS = [
249
+ [TOKEN_SK, "<redacted:token>"],
250
+ [TOKEN_GH, "<redacted:token>"],
251
+ [TOKEN_XOXB, "<redacted:token>"],
252
+ [EMAIL, "<redacted:email>"],
253
+ [IPV6, "<redacted:ipv6>"],
254
+ [IPV4, "<redacted:ipv4>"],
255
+ [HOME_PATTERN, "$HOME$1"]
256
+ ];
257
+ function redactString(s) {
258
+ let out = s;
259
+ for (const [pat, repl] of REPLACEMENTS) {
260
+ out = out.replace(pat, repl);
261
+ }
262
+ return out;
263
+ }
264
+ function redact(value) {
265
+ if (typeof value === "string") return redactString(value);
266
+ if (Array.isArray(value)) return value.map(redact);
267
+ if (value !== null && typeof value === "object") {
268
+ const out = {};
269
+ for (const [k, v] of Object.entries(value)) {
270
+ out[k] = redact(v);
271
+ }
272
+ return out;
273
+ }
274
+ return value;
275
+ }
276
+
277
+ // src/telemetry/client.ts
278
+
279
+
280
+
281
+
282
+ var DEFAULT_QUEUE_SIZE = 1024;
283
+ var FLUSH_INTERVAL_MS = 5e3;
284
+ var FLUSH_BATCH_TRIGGER = 64;
285
+ var HTTPS_CONNECT_TIMEOUT_MS = 5e3;
286
+ var HTTPS_TOTAL_TIMEOUT_MS = 1e4;
287
+ var JSONL_ROTATE_BYTES = 10 * 1024 * 1024;
288
+ var FALLBACK_SDK_VERSION = "0.0.0-dev";
289
+ var SCHEMA_VERSION = "1";
290
+ var SDK_LANG = "ts";
291
+ function defaultJsonlSinkFile() {
292
+ const state = _nullishCoalesce(process.env.XDG_STATE_HOME, () => ( path3.join(_os.homedir.call(void 0, ), ".local", "state")));
293
+ return path3.join(state, "kit", "telemetry", "events.jsonl");
294
+ }
295
+ function consentAllowedSync() {
296
+ if (readGrantedFrom(consentPath(), "canonical")) return true;
297
+ if (readGrantedFrom(legacyConsentPath(), "legacy")) return true;
298
+ return false;
299
+ }
300
+ function readGrantedFrom(p, variant) {
301
+ if (!_fs.existsSync.call(void 0, p)) return false;
302
+ let parsed;
303
+ try {
304
+ const raw = _fs.readFileSync.call(void 0, p, "utf8");
305
+ parsed = yaml2.load(raw);
306
+ } catch (e8) {
307
+ return false;
308
+ }
309
+ if (typeof parsed !== "object" || parsed === null) return false;
310
+ const root = parsed;
311
+ let tel;
312
+ if (variant === "canonical") {
313
+ const kit = root.kit;
314
+ if (typeof kit !== "object" || kit === null) return false;
315
+ tel = kit.telemetry;
316
+ } else {
317
+ tel = root.telemetry;
318
+ }
319
+ if (typeof tel !== "object" || tel === null) return false;
320
+ const block = tel.consent;
321
+ if (typeof block !== "object" || block === null) return false;
322
+ return block.state === "granted";
323
+ }
324
+ function resolveSdkVersion(explicit) {
325
+ if (explicit && explicit.length > 0) return explicit;
326
+ try {
327
+ const here = __dirname;
328
+ const candidates = [
329
+ path3.join(here, "..", "..", "package.json"),
330
+ path3.join(here, "..", "..", "..", "package.json")
331
+ ];
332
+ for (const candidate of candidates) {
333
+ try {
334
+ const raw = _fs.readFileSync.call(void 0, candidate, "utf8");
335
+ const pkg = JSON.parse(raw);
336
+ if (typeof pkg.version === "string" && pkg.version.length > 0) {
337
+ return pkg.version;
338
+ }
339
+ } catch (e9) {
340
+ }
341
+ }
342
+ } catch (e10) {
343
+ }
344
+ return FALLBACK_SDK_VERSION;
345
+ }
346
+ var Client = class {
347
+ constructor(opts = {}) {
348
+ this.queue = [];
349
+ this.dropped = 0;
350
+ this.flushing = false;
351
+ this.closed = false;
352
+ const env = process.env;
353
+ this.endpoint = _nullishCoalesce(opts.endpoint, () => ( env.KIT_TELEMETRY_ENDPOINT));
354
+ const rawSink = _nullishCoalesce(opts.sink, () => ( env.KIT_TELEMETRY_SINK));
355
+ this.sinkKind = rawSink === "https" || rawSink === "jsonl" ? rawSink : "jsonl";
356
+ this.sinkFile = _nullishCoalesce(_nullishCoalesce(opts.sinkFile, () => ( env.KIT_TELEMETRY_SINK_FILE)), () => ( defaultJsonlSinkFile()));
357
+ const rawQueue = _nullishCoalesce(opts.queueSize, () => ( parseQueueSize(env.KIT_TELEMETRY_QUEUE_SIZE)));
358
+ this.queueCap = Number.isFinite(rawQueue) && rawQueue > 0 ? Math.floor(rawQueue) : DEFAULT_QUEUE_SIZE;
359
+ this.userRedactor = opts.redactor;
360
+ this.sdkVersion = resolveSdkVersion(opts.sdkVersion);
361
+ this.flushTimer = setInterval(() => {
362
+ void this.drainOnce();
363
+ }, FLUSH_INTERVAL_MS);
364
+ if (typeof this.flushTimer.unref === "function") {
365
+ this.flushTimer.unref();
366
+ }
367
+ }
368
+ /**
369
+ * record enqueues an event for background delivery. Returns
370
+ * synchronously; never throws. Gating order: closed → consent → mode.
371
+ * Queue-full bumps `droppedCount` and returns silently.
372
+ */
373
+ record(event, attrs = {}) {
374
+ if (this.closed) return;
375
+ if (!consentAllowedSync()) return;
376
+ const mode = resolveMode();
377
+ if (mode === Mode.Off) return;
378
+ if (this.queue.length >= this.queueCap) {
379
+ this.dropped += 1;
380
+ return;
381
+ }
382
+ this.queue.push({
383
+ event,
384
+ attrs,
385
+ mode,
386
+ occurred_at: (/* @__PURE__ */ new Date()).toISOString()
387
+ });
388
+ if (this.queue.length >= FLUSH_BATCH_TRIGGER) {
389
+ setImmediate(() => void this.drainOnce());
390
+ }
391
+ }
392
+ /** Saturation counter — reads, not writes. */
393
+ get droppedCount() {
394
+ return this.dropped;
395
+ }
396
+ /**
397
+ * shutdown stops the flush timer, drains pending events to the sink,
398
+ * and resolves once drained OR the timeout elapses (default 5000ms).
399
+ * Idempotent.
400
+ */
401
+ async shutdown(timeoutMs = 5e3) {
402
+ if (this.closed) return;
403
+ this.closed = true;
404
+ if (this.flushTimer) {
405
+ clearInterval(this.flushTimer);
406
+ this.flushTimer = void 0;
407
+ }
408
+ const deadline = Date.now() + timeoutMs;
409
+ while (this.queue.length > 0 && Date.now() < deadline) {
410
+ await this.drainOnce();
411
+ if (this.queue.length > 0) {
412
+ await new Promise((r) => setTimeout(r, 25));
413
+ }
414
+ }
415
+ }
416
+ /**
417
+ * drainOnce ships every event currently in the queue (snapshot at
418
+ * call time) to the configured sink. Errors bump `dropped` but never
419
+ * propagate.
420
+ */
421
+ async drainOnce() {
422
+ if (this.flushing) return;
423
+ if (this.queue.length === 0) return;
424
+ this.flushing = true;
425
+ try {
426
+ const batch = this.queue.splice(0, this.queue.length);
427
+ const envelopes = await this.envelopeBatch(batch);
428
+ if (envelopes.length === 0) return;
429
+ if (this.sinkKind === "https") {
430
+ await this.shipHttps(envelopes);
431
+ } else {
432
+ await this.shipJsonl(envelopes);
433
+ }
434
+ } catch (e11) {
435
+ } finally {
436
+ this.flushing = false;
437
+ }
438
+ }
439
+ /**
440
+ * envelopeBatch materialises pending records into on-wire envelopes,
441
+ * applying the user redactor first and then the default `redact()`
442
+ * pass. install_id is resolved lazily + cached.
443
+ */
444
+ async envelopeBatch(batch) {
445
+ let installationId = "";
446
+ try {
447
+ if (!this.installIdPromise) {
448
+ this.installIdPromise = getInstallId();
449
+ }
450
+ installationId = await this.installIdPromise;
451
+ } catch (e12) {
452
+ this.dropped += batch.length;
453
+ return [];
454
+ }
455
+ return batch.map((p) => {
456
+ let cleaned;
457
+ if (p.mode === Mode.Anon) {
458
+ cleaned = null;
459
+ } else {
460
+ const customised = this.userRedactor ? safeUserRedact(this.userRedactor, p.attrs, () => {
461
+ this.dropped += 1;
462
+ }) : p.attrs;
463
+ cleaned = redact(customised);
464
+ }
465
+ return {
466
+ schema_version: SCHEMA_VERSION,
467
+ sdk_lang: SDK_LANG,
468
+ sdk_version: this.sdkVersion,
469
+ installation_id: installationId,
470
+ mode: p.mode,
471
+ occurred_at: p.occurred_at,
472
+ event: p.event,
473
+ attrs: cleaned
474
+ };
475
+ });
476
+ }
477
+ /**
478
+ * shipHttps POSTs the batch as NDJSON. One retry on 5xx OR network
479
+ * error, then drops the batch. `AbortController` enforces the
480
+ * connect + total timeouts.
481
+ */
482
+ async shipHttps(envelopes) {
483
+ if (!this.endpoint) {
484
+ this.dropped += envelopes.length;
485
+ return;
486
+ }
487
+ const body = envelopes.map((e) => JSON.stringify(e)).join("\n") + "\n";
488
+ const headers = {
489
+ "content-type": "application/x-ndjson",
490
+ "user-agent": `kit-sdk-ts/${this.sdkVersion}`
491
+ };
492
+ for (let attempt = 0; attempt < 2; attempt += 1) {
493
+ const total = new AbortController();
494
+ const totalTimer = setTimeout(
495
+ () => total.abort(),
496
+ HTTPS_TOTAL_TIMEOUT_MS
497
+ );
498
+ const connect = setTimeout(
499
+ () => total.abort(),
500
+ HTTPS_CONNECT_TIMEOUT_MS
501
+ );
502
+ try {
503
+ const res = await _chunkKVG7YCTSjs.observabilityFetch.call(void 0, )(this.endpoint, {
504
+ method: "POST",
505
+ headers,
506
+ body,
507
+ signal: total.signal
508
+ });
509
+ clearTimeout(connect);
510
+ clearTimeout(totalTimer);
511
+ if (res.status < 500) {
512
+ if (res.status >= 400) {
513
+ this.dropped += envelopes.length;
514
+ }
515
+ return;
516
+ }
517
+ } catch (e13) {
518
+ clearTimeout(connect);
519
+ clearTimeout(totalTimer);
520
+ }
521
+ }
522
+ this.dropped += envelopes.length;
523
+ }
524
+ /**
525
+ * shipJsonl appends envelopes as NDJSON to `sinkFile`. Rotates to
526
+ * `<sinkFile>.1` once the file crosses `JSONL_ROTATE_BYTES`. Single
527
+ * append per batch keeps the I/O cost predictable.
528
+ */
529
+ async shipJsonl(envelopes) {
530
+ await _fs.promises.mkdir(path3.dirname(this.sinkFile), { recursive: true });
531
+ await this.maybeRotateJsonl();
532
+ const body = envelopes.map((e) => JSON.stringify(e)).join("\n") + "\n";
533
+ try {
534
+ await _fs.promises.appendFile(this.sinkFile, body, { mode: 384 });
535
+ } catch (e14) {
536
+ this.dropped += envelopes.length;
537
+ }
538
+ }
539
+ /** Rotate the JSONL sink when it crosses the size threshold. */
540
+ async maybeRotateJsonl() {
541
+ try {
542
+ const stat = await _fs.promises.stat(this.sinkFile);
543
+ if (stat.size < JSONL_ROTATE_BYTES) return;
544
+ } catch (e15) {
545
+ return;
546
+ }
547
+ const rotated = this.sinkFile + ".1";
548
+ try {
549
+ await _fs.promises.rename(this.sinkFile, rotated);
550
+ } catch (e16) {
551
+ }
552
+ }
553
+ };
554
+ function parseQueueSize(raw) {
555
+ if (!raw) return DEFAULT_QUEUE_SIZE;
556
+ const n = Number.parseInt(raw, 10);
557
+ return Number.isFinite(n) && n > 0 ? n : DEFAULT_QUEUE_SIZE;
558
+ }
559
+ function safeUserRedact(fn, attrs, onError) {
560
+ try {
561
+ return fn(attrs);
562
+ } catch (e17) {
563
+ onError();
564
+ return attrs;
565
+ }
566
+ }
567
+
568
+
569
+
570
+
571
+
572
+
573
+
574
+
575
+
576
+
577
+
578
+
579
+
580
+
581
+
582
+
583
+ exports.Mode = Mode; exports.parseMode = parseMode; exports.resolveMode = resolveMode; exports.installIdPath = installIdPath; exports.getInstallId = getInstallId; exports.rotate = rotate; exports.resetForTest = resetForTest; exports.deniedConsent = deniedConsent; exports.consentPath = consentPath; exports.loadConsent = loadConsent; exports.redactString = redactString; exports.redact = redact; exports.Client = Client; exports.telemetry_exports = telemetry_exports;