@ponythewhite/base-context 1.0.0 → 1.0.1

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 (285) hide show
  1. package/CHANGELOG.md +33 -0
  2. package/NOTICE +0 -5
  3. package/README.md +44 -27
  4. package/dist/NOTICE +0 -5
  5. package/dist/base-context-runtime/pyproject.toml +1 -1
  6. package/dist/base-context-runtime/src/rlm/__init__.py +7 -5
  7. package/dist/base-context-runtime/test/test_subagent_registry.py +28 -1
  8. package/dist/base-context-runtime/uv.lock +1 -1
  9. package/dist/build-info.json +1 -1
  10. package/dist/bundle/amazon-bedrock.js +21 -0
  11. package/dist/bundle/{anthropic-YY4S534V.js → anthropic-UV5GZDGU.js} +5 -5
  12. package/dist/bundle/{azure-openai-responses-JRP6MIM3.js → azure-openai-responses-WSNPXLYC.js} +6 -6
  13. package/dist/bundle/{bundled-modules-P5HARVZA.js → bundled-modules-TXUMJ2XQ.js} +71 -16
  14. package/dist/bundle/chunk-27QCJ2AZ.js +2578 -0
  15. package/dist/bundle/{chunk-MZHTNNVA.js → chunk-4GXPNXOJ.js} +8 -0
  16. package/dist/bundle/{chunk-SC5KG5R4.js → chunk-DGNX7HQ3.js} +1 -7
  17. package/dist/bundle/{chunk-A53O363M.js → chunk-GRJFXLBM.js} +0 -1
  18. package/dist/bundle/chunk-HKINIBP4.js +29 -0
  19. package/dist/bundle/{chunk-UDSW6FNG.js → chunk-KTNETTHI.js} +1 -1
  20. package/dist/bundle/{chunk-BYVP7LWV.js → chunk-S3INFPZZ.js} +1861 -4662
  21. package/dist/bundle/{chunk-73DCPU3D.js → chunk-V3L54UD6.js} +1019 -3427
  22. package/dist/bundle/{chunk-U7AUVG3B.js → chunk-VSIYFWYI.js} +24 -6
  23. package/dist/bundle/{chunk-IW77OXFS.js → chunk-W46VZFI6.js} +0 -2099
  24. package/dist/bundle/{cli-main-O4X7GFGY.js → cli-main-BYMSRPH6.js} +8 -6
  25. package/dist/bundle/cli.js +2 -2
  26. package/dist/bundle/{google-AUTVGDGZ.js → google-T32FMD4G.js} +6 -6
  27. package/dist/bundle/{google-vertex-Q75D7RKS.js → google-vertex-56KP6I3B.js} +3 -3
  28. package/dist/bundle/{main-XKOW2BV6.js → main-ODOYCEQC.js} +7 -5
  29. package/dist/bundle/{mistral-7ESB7QUD.js → mistral-WGBMYIVU.js} +5 -5
  30. package/dist/bundle/{openai-codex-responses-LV3QRPN4.js → openai-codex-responses-PIV2RQ3G.js} +6 -6
  31. package/dist/bundle/{openai-completions-OJECLPXJ.js → openai-completions-64C3JPR4.js} +11 -22
  32. package/dist/bundle/{openai-responses-FN6HZMBC.js → openai-responses-UI7RSTDV.js} +6 -6
  33. package/dist/cli/args.d.ts.map +1 -1
  34. package/dist/cli/args.js +108 -59
  35. package/dist/cli/args.js.map +1 -1
  36. package/dist/cli/daemon-ps.d.ts +4 -3
  37. package/dist/cli/daemon-ps.d.ts.map +1 -1
  38. package/dist/cli/daemon-ps.js +6 -1
  39. package/dist/cli/daemon-ps.js.map +1 -1
  40. package/dist/cli/product-doctor.d.ts +0 -1
  41. package/dist/cli/product-doctor.d.ts.map +1 -1
  42. package/dist/cli/product-doctor.js +6 -3
  43. package/dist/cli/product-doctor.js.map +1 -1
  44. package/dist/config.d.ts +0 -3
  45. package/dist/config.d.ts.map +1 -1
  46. package/dist/config.js +0 -8
  47. package/dist/config.js.map +1 -1
  48. package/dist/core/agent-session-config.d.ts +1 -2
  49. package/dist/core/agent-session-config.d.ts.map +1 -1
  50. package/dist/core/agent-session-config.js +0 -3
  51. package/dist/core/agent-session-config.js.map +1 -1
  52. package/dist/core/agent-session-runtime.d.ts.map +1 -1
  53. package/dist/core/agent-session-runtime.js +5 -4
  54. package/dist/core/agent-session-runtime.js.map +1 -1
  55. package/dist/core/agent-session-services.d.ts +5 -3
  56. package/dist/core/agent-session-services.d.ts.map +1 -1
  57. package/dist/core/agent-session-services.js +2 -27
  58. package/dist/core/agent-session-services.js.map +1 -1
  59. package/dist/core/agent-session.d.ts +10 -6
  60. package/dist/core/agent-session.d.ts.map +1 -1
  61. package/dist/core/agent-session.js +107 -107
  62. package/dist/core/agent-session.js.map +1 -1
  63. package/dist/core/auth-storage.d.ts +8 -35
  64. package/dist/core/auth-storage.d.ts.map +1 -1
  65. package/dist/core/auth-storage.js +33 -243
  66. package/dist/core/auth-storage.js.map +1 -1
  67. package/dist/core/cron-jobs.d.ts.map +1 -1
  68. package/dist/core/cron-jobs.js +7 -1
  69. package/dist/core/cron-jobs.js.map +1 -1
  70. package/dist/core/inference-coordinator.d.ts +6 -1
  71. package/dist/core/inference-coordinator.d.ts.map +1 -1
  72. package/dist/core/inference-coordinator.js +18 -2
  73. package/dist/core/inference-coordinator.js.map +1 -1
  74. package/dist/core/kernel/bootstrap.d.ts.map +1 -1
  75. package/dist/core/kernel/bootstrap.js +2 -2
  76. package/dist/core/kernel/bootstrap.js.map +1 -1
  77. package/dist/core/kernel/repl-manager.d.ts +2 -0
  78. package/dist/core/kernel/repl-manager.d.ts.map +1 -1
  79. package/dist/core/kernel/repl-manager.js +44 -1
  80. package/dist/core/kernel/repl-manager.js.map +1 -1
  81. package/dist/core/logging.d.ts +4 -3
  82. package/dist/core/logging.d.ts.map +1 -1
  83. package/dist/core/logging.js +6 -6
  84. package/dist/core/logging.js.map +1 -1
  85. package/dist/core/mcp/mcp-manager.d.ts.map +1 -1
  86. package/dist/core/mcp/mcp-manager.js +3 -3
  87. package/dist/core/mcp/mcp-manager.js.map +1 -1
  88. package/dist/core/model-registry.d.ts +1 -16
  89. package/dist/core/model-registry.d.ts.map +1 -1
  90. package/dist/core/model-registry.js +88 -196
  91. package/dist/core/model-registry.js.map +1 -1
  92. package/dist/core/model-resolver.d.ts +2 -19
  93. package/dist/core/model-resolver.d.ts.map +1 -1
  94. package/dist/core/model-resolver.js +36 -266
  95. package/dist/core/model-resolver.js.map +1 -1
  96. package/dist/core/provider-contracts.d.ts +5 -6
  97. package/dist/core/provider-contracts.d.ts.map +1 -1
  98. package/dist/core/provider-contracts.js +25 -36
  99. package/dist/core/provider-contracts.js.map +1 -1
  100. package/dist/core/provider-display-names.d.ts.map +1 -1
  101. package/dist/core/provider-display-names.js +0 -2
  102. package/dist/core/provider-display-names.js.map +1 -1
  103. package/dist/core/rlm-max-subagents.d.ts +32 -0
  104. package/dist/core/rlm-max-subagents.d.ts.map +1 -0
  105. package/dist/core/rlm-max-subagents.js +50 -0
  106. package/dist/core/rlm-max-subagents.js.map +1 -0
  107. package/dist/core/rlm-runtime.d.ts +3 -3
  108. package/dist/core/rlm-runtime.d.ts.map +1 -1
  109. package/dist/core/rlm-runtime.js.map +1 -1
  110. package/dist/core/sdk.d.ts +4 -3
  111. package/dist/core/sdk.d.ts.map +1 -1
  112. package/dist/core/sdk.js +27 -14
  113. package/dist/core/sdk.js.map +1 -1
  114. package/dist/core/settings-manager.d.ts +3 -16
  115. package/dist/core/settings-manager.d.ts.map +1 -1
  116. package/dist/core/settings-manager.js +12 -44
  117. package/dist/core/settings-manager.js.map +1 -1
  118. package/dist/core/slash-commands.d.ts.map +1 -1
  119. package/dist/core/slash-commands.js +6 -6
  120. package/dist/core/slash-commands.js.map +1 -1
  121. package/dist/index.d.ts +1 -1
  122. package/dist/index.d.ts.map +1 -1
  123. package/dist/index.js.map +1 -1
  124. package/dist/installer.mjs +2 -3
  125. package/dist/main.d.ts.map +1 -1
  126. package/dist/main.js +39 -34
  127. package/dist/main.js.map +1 -1
  128. package/dist/modes/acp/acp-mode.d.ts.map +1 -1
  129. package/dist/modes/acp/acp-mode.js +1 -1
  130. package/dist/modes/acp/acp-mode.js.map +1 -1
  131. package/dist/modes/agent-connection/daemon-agent-connection.d.ts +6 -2
  132. package/dist/modes/agent-connection/daemon-agent-connection.d.ts.map +1 -1
  133. package/dist/modes/agent-connection/daemon-agent-connection.js +13 -2
  134. package/dist/modes/agent-connection/daemon-agent-connection.js.map +1 -1
  135. package/dist/modes/agent-connection/in-process-agent-connection.d.ts +2 -0
  136. package/dist/modes/agent-connection/in-process-agent-connection.d.ts.map +1 -1
  137. package/dist/modes/agent-connection/in-process-agent-connection.js +6 -0
  138. package/dist/modes/agent-connection/in-process-agent-connection.js.map +1 -1
  139. package/dist/modes/agent-connection/types.d.ts +3 -0
  140. package/dist/modes/agent-connection/types.d.ts.map +1 -1
  141. package/dist/modes/agent-connection/types.js.map +1 -1
  142. package/dist/modes/agents-view/agents-view-mode.d.ts.map +1 -1
  143. package/dist/modes/agents-view/agents-view-mode.js +2 -19
  144. package/dist/modes/agents-view/agents-view-mode.js.map +1 -1
  145. package/dist/modes/daemon/daemon-mode.d.ts +5 -0
  146. package/dist/modes/daemon/daemon-mode.d.ts.map +1 -1
  147. package/dist/modes/daemon/daemon-mode.js +107 -12
  148. package/dist/modes/daemon/daemon-mode.js.map +1 -1
  149. package/dist/modes/daemon/daemon-protocol.d.ts +194 -165
  150. package/dist/modes/daemon/daemon-protocol.d.ts.map +1 -1
  151. package/dist/modes/daemon/daemon-protocol.js +16 -11
  152. package/dist/modes/daemon/daemon-protocol.js.map +1 -1
  153. package/dist/modes/daemon/daemon-rlm-capacity-coordinator.d.ts +19 -0
  154. package/dist/modes/daemon/daemon-rlm-capacity-coordinator.d.ts.map +1 -0
  155. package/dist/modes/daemon/daemon-rlm-capacity-coordinator.js +74 -0
  156. package/dist/modes/daemon/daemon-rlm-capacity-coordinator.js.map +1 -0
  157. package/dist/modes/daemon/daemon-rlm-capacity.d.ts +19 -0
  158. package/dist/modes/daemon/daemon-rlm-capacity.d.ts.map +1 -0
  159. package/dist/modes/daemon/daemon-rlm-capacity.js +85 -0
  160. package/dist/modes/daemon/daemon-rlm-capacity.js.map +1 -0
  161. package/dist/modes/daemon/daemon-session-summarizer.d.ts +2 -3
  162. package/dist/modes/daemon/daemon-session-summarizer.d.ts.map +1 -1
  163. package/dist/modes/daemon/daemon-session-summarizer.js +32 -23
  164. package/dist/modes/daemon/daemon-session-summarizer.js.map +1 -1
  165. package/dist/modes/daemon/daemon-state-root.d.ts +15 -0
  166. package/dist/modes/daemon/daemon-state-root.d.ts.map +1 -0
  167. package/dist/modes/daemon/daemon-state-root.js +40 -0
  168. package/dist/modes/daemon/daemon-state-root.js.map +1 -0
  169. package/dist/modes/daemon/daemon-supervisor-ownership.d.ts +2 -0
  170. package/dist/modes/daemon/daemon-supervisor-ownership.d.ts.map +1 -1
  171. package/dist/modes/daemon/daemon-supervisor-ownership.js +27 -0
  172. package/dist/modes/daemon/daemon-supervisor-ownership.js.map +1 -1
  173. package/dist/modes/daemon/daemon-supervisor.d.ts +10 -1
  174. package/dist/modes/daemon/daemon-supervisor.d.ts.map +1 -1
  175. package/dist/modes/daemon/daemon-supervisor.js +213 -20
  176. package/dist/modes/daemon/daemon-supervisor.js.map +1 -1
  177. package/dist/modes/daemon/daemon-worker-protocol.d.ts +28 -1
  178. package/dist/modes/daemon/daemon-worker-protocol.d.ts.map +1 -1
  179. package/dist/modes/daemon/daemon-worker-protocol.js +16 -2
  180. package/dist/modes/daemon/daemon-worker-protocol.js.map +1 -1
  181. package/dist/modes/daemon/saved-session-catalog.d.ts.map +1 -1
  182. package/dist/modes/daemon/saved-session-catalog.js +2 -0
  183. package/dist/modes/daemon/saved-session-catalog.js.map +1 -1
  184. package/dist/modes/interactive/auth-flows.d.ts +0 -6
  185. package/dist/modes/interactive/auth-flows.d.ts.map +1 -1
  186. package/dist/modes/interactive/auth-flows.js +6 -126
  187. package/dist/modes/interactive/auth-flows.js.map +1 -1
  188. package/dist/modes/interactive/components/configuration-menu.d.ts +3 -0
  189. package/dist/modes/interactive/components/configuration-menu.d.ts.map +1 -1
  190. package/dist/modes/interactive/components/configuration-menu.js +13 -2
  191. package/dist/modes/interactive/components/configuration-menu.js.map +1 -1
  192. package/dist/modes/interactive/components/footer.d.ts +1 -1
  193. package/dist/modes/interactive/components/footer.d.ts.map +1 -1
  194. package/dist/modes/interactive/components/footer.js +2 -2
  195. package/dist/modes/interactive/components/footer.js.map +1 -1
  196. package/dist/modes/interactive/components/index.d.ts +0 -1
  197. package/dist/modes/interactive/components/index.d.ts.map +1 -1
  198. package/dist/modes/interactive/components/index.js +0 -1
  199. package/dist/modes/interactive/components/index.js.map +1 -1
  200. package/dist/modes/interactive/components/login-dialog.d.ts +0 -1
  201. package/dist/modes/interactive/components/login-dialog.d.ts.map +1 -1
  202. package/dist/modes/interactive/components/login-dialog.js +3 -40
  203. package/dist/modes/interactive/components/login-dialog.js.map +1 -1
  204. package/dist/modes/interactive/components/model-selector.d.ts +2 -0
  205. package/dist/modes/interactive/components/model-selector.d.ts.map +1 -1
  206. package/dist/modes/interactive/components/model-selector.js +13 -2
  207. package/dist/modes/interactive/components/model-selector.js.map +1 -1
  208. package/dist/modes/interactive/components/oauth-selector.d.ts.map +1 -1
  209. package/dist/modes/interactive/components/oauth-selector.js +2 -11
  210. package/dist/modes/interactive/components/oauth-selector.js.map +1 -1
  211. package/dist/modes/interactive/feature-hints.d.ts.map +1 -1
  212. package/dist/modes/interactive/feature-hints.js +0 -4
  213. package/dist/modes/interactive/feature-hints.js.map +1 -1
  214. package/dist/modes/interactive/interactive-mode.d.ts +3 -12
  215. package/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
  216. package/dist/modes/interactive/interactive-mode.js +81 -507
  217. package/dist/modes/interactive/interactive-mode.js.map +1 -1
  218. package/dist/modes/interactive/onboarding.d.ts +0 -3
  219. package/dist/modes/interactive/onboarding.d.ts.map +1 -1
  220. package/dist/modes/interactive/onboarding.js +0 -17
  221. package/dist/modes/interactive/onboarding.js.map +1 -1
  222. package/dist/modes/rpc/rpc-client.d.ts +2 -1
  223. package/dist/modes/rpc/rpc-client.d.ts.map +1 -1
  224. package/dist/modes/rpc/rpc-client.js +24 -11
  225. package/dist/modes/rpc/rpc-client.js.map +1 -1
  226. package/dist/node/amazon-bedrock.d.ts +2 -0
  227. package/dist/node/amazon-bedrock.d.ts.map +1 -0
  228. package/dist/node/amazon-bedrock.js +6 -0
  229. package/dist/node/amazon-bedrock.js.map +1 -0
  230. package/dist/product-identity.d.ts +0 -1
  231. package/dist/product-identity.d.ts.map +1 -1
  232. package/dist/product-identity.js +0 -1
  233. package/dist/product-identity.js.map +1 -1
  234. package/docs/development.md +1 -1
  235. package/docs/fork-philosophy.md +4 -8
  236. package/docs/installation.md +83 -62
  237. package/docs/providers.md +23 -25
  238. package/docs/quickstart.md +19 -10
  239. package/docs/rlm-runtime.md +13 -5
  240. package/docs/rpc.md +4 -4
  241. package/docs/sdk.md +20 -11
  242. package/docs/sessions.md +0 -1
  243. package/docs/settings.md +9 -42
  244. package/docs/skills.md +0 -1
  245. package/docs/upstream-0.9.5.md +171 -0
  246. package/docs/usage.md +14 -10
  247. package/package.json +4 -4
  248. package/dist/core/agent-traces.d.ts +0 -149
  249. package/dist/core/agent-traces.d.ts.map +0 -1
  250. package/dist/core/agent-traces.js +0 -904
  251. package/dist/core/agent-traces.js.map +0 -1
  252. package/dist/core/prime-inference-auth.d.ts +0 -70
  253. package/dist/core/prime-inference-auth.d.ts.map +0 -1
  254. package/dist/core/prime-inference-auth.js +0 -352
  255. package/dist/core/prime-inference-auth.js.map +0 -1
  256. package/dist/core/prime-inference-model-selection.d.ts +0 -16
  257. package/dist/core/prime-inference-model-selection.d.ts.map +0 -1
  258. package/dist/core/prime-inference-model-selection.js +0 -16
  259. package/dist/core/prime-inference-model-selection.js.map +0 -1
  260. package/dist/core/prime-inference-models.d.ts +0 -6
  261. package/dist/core/prime-inference-models.d.ts.map +0 -1
  262. package/dist/core/prime-inference-models.js +0 -62
  263. package/dist/core/prime-inference-models.js.map +0 -1
  264. package/dist/core/telemetry.d.ts +0 -95
  265. package/dist/core/telemetry.d.ts.map +0 -1
  266. package/dist/core/telemetry.js +0 -637
  267. package/dist/core/telemetry.js.map +0 -1
  268. package/dist/modes/interactive/components/prime-onboarding-splash.d.ts +0 -39
  269. package/dist/modes/interactive/components/prime-onboarding-splash.d.ts.map +0 -1
  270. package/dist/modes/interactive/components/prime-onboarding-splash.js +0 -286
  271. package/dist/modes/interactive/components/prime-onboarding-splash.js.map +0 -1
  272. package/dist/modes/interactive/components/prime-team-selector.d.ts +0 -30
  273. package/dist/modes/interactive/components/prime-team-selector.d.ts.map +0 -1
  274. package/dist/modes/interactive/components/prime-team-selector.js +0 -166
  275. package/dist/modes/interactive/components/prime-team-selector.js.map +0 -1
  276. package/dist/skills/prime-intellect/SKILL.md +0 -87
  277. package/dist/skills/prime-intellect/references/compute.md +0 -45
  278. package/dist/skills/prime-intellect/references/environments.md +0 -73
  279. package/dist/skills/prime-intellect/references/inference.md +0 -47
  280. package/dist/skills/prime-intellect/references/sandboxes.md +0 -75
  281. package/skills/prime-intellect/SKILL.md +0 -87
  282. package/skills/prime-intellect/references/compute.md +0 -45
  283. package/skills/prime-intellect/references/environments.md +0 -73
  284. package/skills/prime-intellect/references/inference.md +0 -47
  285. package/skills/prime-intellect/references/sandboxes.md +0 -75
@@ -1,637 +0,0 @@
1
- import { randomUUID } from "node:crypto";
2
- import { lstatSync, mkdirSync, readFileSync, renameSync, unlinkSync, writeFileSync } from "node:fs";
3
- import { arch, platform } from "node:os";
4
- import { join } from "node:path";
5
- import { detectInstallMethod, VERSION } from "../config.js";
6
- import { assertProductStatePath } from "../runtime-paths.js";
7
- import { isBuiltinSlashCommandName, resolveBuiltinSlashCommandName } from "./slash-commands.js";
8
- const TELEMETRY_STATE_FILE = "telemetry.json";
9
- const TELEMETRY_STATE_VERSION = 1;
10
- const DEFAULT_BATCH_SIZE = 10;
11
- const DEFAULT_FLUSH_INTERVAL_MS = 10_000;
12
- const DEFAULT_REQUEST_TIMEOUT_MS = 1_500;
13
- const EMPTY_USAGE_TOTALS = {
14
- input: 0,
15
- output: 0,
16
- cacheRead: 0,
17
- cacheWrite: 0,
18
- totalTokens: 0,
19
- cost: {
20
- input: 0,
21
- output: 0,
22
- cacheRead: 0,
23
- cacheWrite: 0,
24
- total: 0,
25
- },
26
- modelCallCount: 0,
27
- };
28
- function newUsageTotals() {
29
- return structuredClone(EMPTY_USAGE_TOTALS);
30
- }
31
- function addUsage(target, usage) {
32
- target.input += usage.input;
33
- target.output += usage.output;
34
- target.cacheRead += usage.cacheRead;
35
- target.cacheWrite += usage.cacheWrite;
36
- target.totalTokens += usage.totalTokens;
37
- target.cost.input += usage.cost.input;
38
- target.cost.output += usage.cost.output;
39
- target.cost.cacheRead += usage.cost.cacheRead;
40
- target.cost.cacheWrite += usage.cost.cacheWrite;
41
- target.cost.total += usage.cost.total;
42
- target.modelCallCount++;
43
- }
44
- function mergeUsage(target, usage) {
45
- target.input += usage.input;
46
- target.output += usage.output;
47
- target.cacheRead += usage.cacheRead;
48
- target.cacheWrite += usage.cacheWrite;
49
- target.totalTokens += usage.totalTokens;
50
- target.cost.input += usage.cost.input;
51
- target.cost.output += usage.cost.output;
52
- target.cost.cacheRead += usage.cost.cacheRead;
53
- target.cost.cacheWrite += usage.cost.cacheWrite;
54
- target.cost.total += usage.cost.total;
55
- target.modelCallCount += usage.modelCallCount;
56
- }
57
- function parseBooleanOverride(value) {
58
- if (value === undefined) {
59
- return undefined;
60
- }
61
- const normalized = value.trim().toLowerCase();
62
- if (["1", "true", "yes", "on"].includes(normalized)) {
63
- return true;
64
- }
65
- if (["0", "false", "no", "off"].includes(normalized)) {
66
- return false;
67
- }
68
- return undefined;
69
- }
70
- export function isTelemetryEnabled(settingsManager) {
71
- if (parseBooleanOverride(process.env.BASE_CONTEXT_OFFLINE) === true) {
72
- return false;
73
- }
74
- if (parseBooleanOverride(process.env.DO_NOT_TRACK) === true) {
75
- return false;
76
- }
77
- const override = parseBooleanOverride(process.env.BASE_CONTEXT_TELEMETRY);
78
- if (override !== undefined) {
79
- return override;
80
- }
81
- return settingsManager.getTelemetryEnabled();
82
- }
83
- function isInstallationId(value) {
84
- return (typeof value === "string" &&
85
- /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(value));
86
- }
87
- function readInstallationId(path) {
88
- try {
89
- const parsed = JSON.parse(readFileSync(path, "utf8"));
90
- return parsed.version === TELEMETRY_STATE_VERSION && isInstallationId(parsed.installationId)
91
- ? parsed.installationId
92
- : undefined;
93
- }
94
- catch {
95
- return undefined;
96
- }
97
- }
98
- function writeTelemetryStateAtomically(path, state) {
99
- const temporaryPath = `${path}.${process.pid}.${randomUUID()}.tmp`;
100
- try {
101
- writeFileSync(temporaryPath, JSON.stringify(state, null, 2), {
102
- encoding: "utf8",
103
- flag: "wx",
104
- mode: 0o600,
105
- });
106
- renameSync(temporaryPath, path);
107
- }
108
- finally {
109
- try {
110
- unlinkSync(temporaryPath);
111
- }
112
- catch {
113
- // The rename succeeded or the temporary file was never created.
114
- }
115
- }
116
- }
117
- export function getOrCreateTelemetryInstallationId(agentDir, randomId = randomUUID) {
118
- const path = assertProductStatePath(join(agentDir, TELEMETRY_STATE_FILE));
119
- let replaceInvalidState = false;
120
- try {
121
- const stats = lstatSync(path);
122
- if (!stats.isFile()) {
123
- throw new Error("Telemetry state path must be a regular file");
124
- }
125
- const existing = readInstallationId(path);
126
- if (existing) {
127
- return existing;
128
- }
129
- replaceInvalidState = true;
130
- }
131
- catch (error) {
132
- const code = typeof error === "object" && error !== null && "code" in error
133
- ? String(error.code)
134
- : undefined;
135
- if (code !== "ENOENT") {
136
- throw error;
137
- }
138
- }
139
- const installationId = randomId();
140
- if (!isInstallationId(installationId)) {
141
- throw new Error("Telemetry installation ID generator returned an invalid UUID");
142
- }
143
- mkdirSync(agentDir, { recursive: true });
144
- const state = {
145
- version: TELEMETRY_STATE_VERSION,
146
- installationId,
147
- };
148
- try {
149
- if (replaceInvalidState) {
150
- writeTelemetryStateAtomically(path, state);
151
- }
152
- else {
153
- writeFileSync(path, JSON.stringify(state, null, 2), {
154
- encoding: "utf8",
155
- flag: "wx",
156
- mode: 0o600,
157
- });
158
- }
159
- return installationId;
160
- }
161
- catch (error) {
162
- const code = typeof error === "object" && error !== null && "code" in error
163
- ? String(error.code)
164
- : undefined;
165
- if (code !== "EEXIST") {
166
- throw error;
167
- }
168
- return readInstallationId(path) ?? installationId;
169
- }
170
- }
171
- export class TelemetryClient {
172
- options;
173
- endpoint;
174
- apiKey;
175
- fetchImpl;
176
- now;
177
- randomId;
178
- batchSize;
179
- flushIntervalMs;
180
- requestTimeoutMs;
181
- installationId;
182
- queue = [];
183
- flushTimer;
184
- flushInFlight;
185
- disabled = false;
186
- constructor(options) {
187
- this.options = options;
188
- this.endpoint = (options.endpoint ?? process.env.BASE_CONTEXT_TELEMETRY_ENDPOINT)?.trim() || undefined;
189
- this.apiKey = (options.apiKey ?? process.env.BASE_CONTEXT_TELEMETRY_API_KEY)?.trim() || undefined;
190
- this.disabled = !this.endpoint || !this.apiKey;
191
- this.fetchImpl = options.fetch ?? fetch;
192
- this.now = options.now ?? Date.now;
193
- this.randomId = options.randomId ?? randomUUID;
194
- this.batchSize = options.batchSize ?? DEFAULT_BATCH_SIZE;
195
- this.flushIntervalMs = options.flushIntervalMs ?? DEFAULT_FLUSH_INTERVAL_MS;
196
- this.requestTimeoutMs = options.requestTimeoutMs ?? DEFAULT_REQUEST_TIMEOUT_MS;
197
- }
198
- capture(name, properties) {
199
- if (this.disabled) {
200
- return;
201
- }
202
- try {
203
- this.installationId ??= getOrCreateTelemetryInstallationId(this.options.agentDir, this.randomId);
204
- this.queue.push({
205
- id: this.randomId(),
206
- name,
207
- timestamp: new Date(this.now()).toISOString(),
208
- properties,
209
- });
210
- }
211
- catch {
212
- this.disabled = true;
213
- this.queue = [];
214
- return;
215
- }
216
- if (this.queue.length >= this.batchSize) {
217
- void this.flush();
218
- return;
219
- }
220
- this.scheduleFlush();
221
- }
222
- scheduleFlush() {
223
- if (this.flushTimer) {
224
- return;
225
- }
226
- this.flushTimer = setTimeout(() => {
227
- this.flushTimer = undefined;
228
- void this.flush();
229
- }, this.flushIntervalMs);
230
- this.flushTimer.unref?.();
231
- }
232
- async flush() {
233
- if (this.flushTimer) {
234
- clearTimeout(this.flushTimer);
235
- this.flushTimer = undefined;
236
- }
237
- if (this.flushInFlight) {
238
- await this.flushInFlight;
239
- if (this.queue.length > 0) {
240
- await this.flush();
241
- }
242
- return;
243
- }
244
- if (this.queue.length === 0 || !this.installationId) {
245
- return;
246
- }
247
- const drain = this.drainQueue();
248
- this.flushInFlight = drain;
249
- try {
250
- await drain;
251
- }
252
- finally {
253
- if (this.flushInFlight === drain) {
254
- this.flushInFlight = undefined;
255
- }
256
- }
257
- if (this.queue.length > 0) {
258
- await this.flush();
259
- }
260
- }
261
- async drainQueue() {
262
- while (this.queue.length > 0 && this.installationId) {
263
- const events = this.queue.splice(0, this.batchSize);
264
- const batch = {
265
- installation_id: this.installationId,
266
- events,
267
- };
268
- await this.send(batch);
269
- }
270
- }
271
- async send(batch) {
272
- if (!this.endpoint || !this.apiKey)
273
- return;
274
- try {
275
- await this.fetchImpl(this.endpoint, {
276
- method: "POST",
277
- headers: {
278
- "content-type": "application/json",
279
- "user-agent": `base-context/${VERSION}`,
280
- authorization: `Bearer ${this.apiKey}`,
281
- },
282
- redirect: "error",
283
- body: JSON.stringify(batch),
284
- signal: AbortSignal.timeout(this.requestTimeoutMs),
285
- });
286
- }
287
- catch {
288
- // Product analytics is best-effort and must never affect the agent.
289
- }
290
- }
291
- }
292
- function modelCategory(model) {
293
- const normalized = model.toLowerCase();
294
- const categories = [
295
- "claude",
296
- "gpt",
297
- "o1",
298
- "o3",
299
- "o4",
300
- "gemini",
301
- "glm",
302
- "kimi",
303
- "qwen",
304
- "deepseek",
305
- "llama",
306
- "mistral",
307
- ];
308
- return categories.find((category) => normalized.includes(category)) ?? "custom";
309
- }
310
- export function telemetryProviderCategory(provider) {
311
- if (!provider) {
312
- return "unknown";
313
- }
314
- const normalized = provider.toLowerCase();
315
- const categories = [
316
- "anthropic",
317
- "openai",
318
- "google",
319
- "prime",
320
- "openrouter",
321
- "bedrock",
322
- "vertex",
323
- "mistral",
324
- "groq",
325
- "xai",
326
- ];
327
- return categories.find((category) => normalized.includes(category)) ?? "custom";
328
- }
329
- export function telemetryAuthCategory(source, storedCredentialType) {
330
- switch (source) {
331
- case "stored":
332
- return storedCredentialType ?? "stored";
333
- case "runtime":
334
- return "runtime_api_key";
335
- case "environment":
336
- return "environment";
337
- case "prime_cli":
338
- return "prime_cli";
339
- case "models_json_key":
340
- case "models_json_command":
341
- return "models_json";
342
- case "fallback":
343
- return "fallback";
344
- case "stale":
345
- return "stale";
346
- default:
347
- return "none";
348
- }
349
- }
350
- function baseProperties(executionMode) {
351
- return {
352
- version: VERSION,
353
- os_family: platform(),
354
- architecture: arch(),
355
- install_method: detectInstallMethod(),
356
- execution_mode: executionMode,
357
- };
358
- }
359
- export async function captureOnboardingCompleted(options) {
360
- if (!isTelemetryEnabled(options.settingsManager)) {
361
- return;
362
- }
363
- const now = options.now ?? Date.now;
364
- const randomId = options.randomId ?? randomUUID;
365
- const sink = options.sink ??
366
- new TelemetryClient({
367
- agentDir: options.agentDir,
368
- now,
369
- randomId,
370
- });
371
- sink.capture("onboarding completed", {
372
- ...baseProperties("interactive"),
373
- duration_ms: Math.max(0, options.durationMs),
374
- outcome: options.outcome,
375
- auth_category: telemetryAuthCategory(options.authSource, options.storedCredentialType),
376
- provider_category: telemetryProviderCategory(options.provider),
377
- });
378
- await sink.flush();
379
- }
380
- export async function captureAgentCommandUsed(options) {
381
- if (!isBuiltinSlashCommandName(options.commandName) || !isTelemetryEnabled(options.settingsManager)) {
382
- return;
383
- }
384
- const commandName = resolveBuiltinSlashCommandName(options.commandName);
385
- const now = options.now ?? Date.now;
386
- const randomId = options.randomId ?? randomUUID;
387
- const sink = options.sink ??
388
- new TelemetryClient({
389
- agentDir: options.agentDir,
390
- now,
391
- randomId,
392
- });
393
- sink.capture("agent command used", {
394
- ...baseProperties("interactive"),
395
- command_name: commandName,
396
- });
397
- await sink.flush();
398
- }
399
- function errorCategory(message) {
400
- if (!message || message.stopReason !== "error") {
401
- return null;
402
- }
403
- const error = message.errorMessage?.toLowerCase() ?? "";
404
- if (/\b401\b|\b403\b|auth|api.?key|credential|unauthori[sz]ed|forbidden/.test(error)) {
405
- return "authentication";
406
- }
407
- if (/\b429\b|rate.?limit|quota/.test(error)) {
408
- return "rate_limit";
409
- }
410
- if (/timeout|timed out/.test(error)) {
411
- return "timeout";
412
- }
413
- if (/context|token.*limit|too long|maximum.*length/.test(error)) {
414
- return "context_limit";
415
- }
416
- if (/network|socket|connection|fetch/.test(error)) {
417
- return "network";
418
- }
419
- if (/\b5\d\d\b|overload|unavailable/.test(error)) {
420
- return "provider_unavailable";
421
- }
422
- return "other";
423
- }
424
- function runOutcome(message) {
425
- if (message?.stopReason === "aborted") {
426
- return "aborted";
427
- }
428
- if (!message || message.stopReason === "error") {
429
- return "error";
430
- }
431
- return "success";
432
- }
433
- function assistantMessage(event) {
434
- if (event.type !== "message_end" || event.message.role !== "assistant") {
435
- return undefined;
436
- }
437
- return event.message;
438
- }
439
- function createActiveRun(now) {
440
- return {
441
- startedAt: now(),
442
- agentEnded: false,
443
- modelLatencyMs: 0,
444
- maxModelLatencyMs: 0,
445
- turnCount: 0,
446
- toolCallCount: 0,
447
- toolErrorCount: 0,
448
- compactionCount: 0,
449
- retryCount: 0,
450
- usage: newUsageTotals(),
451
- };
452
- }
453
- export function installAgentTelemetry(session, options) {
454
- if (!isTelemetryEnabled(options.settingsManager)) {
455
- return;
456
- }
457
- const now = options.now ?? Date.now;
458
- const randomId = options.randomId ?? randomUUID;
459
- const sink = options.sink ??
460
- new TelemetryClient({
461
- agentDir: options.agentDir,
462
- now,
463
- randomId,
464
- });
465
- const sessionId = randomId();
466
- const sessionTotals = {
467
- startedAt: now(),
468
- runCount: 0,
469
- successfulRunCount: 0,
470
- failedRunCount: 0,
471
- abortedRunCount: 0,
472
- promptCount: 0,
473
- toolCallCount: 0,
474
- compactionCount: 0,
475
- usage: newUsageTotals(),
476
- };
477
- let activeRun;
478
- let turnActionActive = false;
479
- const commonProperties = () => ({
480
- ...baseProperties(options.executionMode ?? "unknown"),
481
- session_id: sessionId,
482
- });
483
- const finalizeRun = () => {
484
- const run = activeRun;
485
- if (!run) {
486
- return;
487
- }
488
- activeRun = undefined;
489
- const outcome = runOutcome(run.lastAssistant);
490
- sessionTotals.runCount++;
491
- sessionTotals.toolCallCount += run.toolCallCount;
492
- sessionTotals.compactionCount += run.compactionCount;
493
- if (outcome === "success") {
494
- sessionTotals.successfulRunCount++;
495
- }
496
- else if (outcome === "aborted") {
497
- sessionTotals.abortedRunCount++;
498
- }
499
- else {
500
- sessionTotals.failedRunCount++;
501
- }
502
- mergeUsage(sessionTotals.usage, run.usage);
503
- const lastAssistant = run.lastAssistant;
504
- sink.capture("agent run completed", {
505
- ...commonProperties(),
506
- outcome,
507
- duration_ms: Math.max(0, now() - run.startedAt),
508
- visible_ttft_ms: run.visibleTtftMs ?? null,
509
- first_model_event_ms: run.firstModelEventMs ?? null,
510
- model_latency_ms: run.modelLatencyMs,
511
- max_model_latency_ms: run.maxModelLatencyMs,
512
- model_call_count: run.usage.modelCallCount,
513
- turn_count: run.turnCount,
514
- tool_call_count: run.toolCallCount,
515
- tool_error_count: run.toolErrorCount,
516
- input_tokens: run.usage.input,
517
- output_tokens: run.usage.output,
518
- cache_read_tokens: run.usage.cacheRead,
519
- cache_write_tokens: run.usage.cacheWrite,
520
- total_tokens: run.usage.totalTokens,
521
- compaction_count: run.compactionCount,
522
- retry_count: run.retryCount,
523
- provider_category: telemetryProviderCategory(lastAssistant?.provider),
524
- model_category: lastAssistant ? modelCategory(lastAssistant.model) : "unknown",
525
- error_category: errorCategory(lastAssistant),
526
- });
527
- };
528
- sink.capture("agent started", commonProperties());
529
- const unsubscribe = session.subscribe((event) => {
530
- switch (event.type) {
531
- case "session_action_update":
532
- turnActionActive = event.actions.active?.kind === "turn";
533
- if (!turnActionActive && activeRun?.agentEnded) {
534
- finalizeRun();
535
- }
536
- break;
537
- case "agent_start":
538
- if (activeRun?.agentEnded && !turnActionActive) {
539
- finalizeRun();
540
- }
541
- activeRun ??= createActiveRun(now);
542
- activeRun.agentEnded = false;
543
- break;
544
- case "message_start":
545
- if (event.message.role === "user") {
546
- sessionTotals.promptCount++;
547
- }
548
- break;
549
- case "turn_start":
550
- if (activeRun) {
551
- activeRun.currentTurnStartedAt = now();
552
- activeRun.firstTurnStartedAt ??= activeRun.currentTurnStartedAt;
553
- activeRun.turnCount++;
554
- }
555
- break;
556
- case "message_update":
557
- if (!activeRun) {
558
- break;
559
- }
560
- if (activeRun.firstModelEventMs === undefined && activeRun.firstTurnStartedAt !== undefined) {
561
- activeRun.firstModelEventMs = Math.max(0, now() - activeRun.firstTurnStartedAt);
562
- }
563
- if (activeRun.visibleTtftMs === undefined &&
564
- activeRun.firstTurnStartedAt !== undefined &&
565
- event.assistantMessageEvent.type === "text_delta" &&
566
- event.assistantMessageEvent.delta.length > 0) {
567
- activeRun.visibleTtftMs = Math.max(0, now() - activeRun.firstTurnStartedAt);
568
- }
569
- break;
570
- case "message_end": {
571
- const message = assistantMessage(event);
572
- if (!activeRun || !message) {
573
- break;
574
- }
575
- activeRun.lastAssistant = message;
576
- addUsage(activeRun.usage, message.usage);
577
- if (activeRun.currentTurnStartedAt !== undefined) {
578
- const latency = Math.max(0, now() - activeRun.currentTurnStartedAt);
579
- activeRun.modelLatencyMs += latency;
580
- activeRun.maxModelLatencyMs = Math.max(activeRun.maxModelLatencyMs, latency);
581
- activeRun.currentTurnStartedAt = undefined;
582
- }
583
- break;
584
- }
585
- case "tool_execution_end":
586
- if (activeRun) {
587
- activeRun.toolCallCount++;
588
- if (event.isError) {
589
- activeRun.toolErrorCount++;
590
- }
591
- }
592
- break;
593
- case "compaction_end":
594
- if (activeRun && event.result && !event.aborted) {
595
- activeRun.compactionCount++;
596
- }
597
- break;
598
- case "auto_retry_start":
599
- if (activeRun) {
600
- activeRun.retryCount++;
601
- }
602
- break;
603
- case "agent_end":
604
- if (!activeRun) {
605
- break;
606
- }
607
- activeRun.agentEnded = true;
608
- if (!turnActionActive) {
609
- finalizeRun();
610
- }
611
- break;
612
- }
613
- });
614
- session.registerDisposeCallback(async () => {
615
- unsubscribe();
616
- finalizeRun();
617
- sink.capture("agent session ended", {
618
- ...commonProperties(),
619
- duration_ms: Math.max(0, now() - sessionTotals.startedAt),
620
- prompt_count: sessionTotals.promptCount,
621
- run_count: sessionTotals.runCount,
622
- successful_run_count: sessionTotals.successfulRunCount,
623
- failed_run_count: sessionTotals.failedRunCount,
624
- aborted_run_count: sessionTotals.abortedRunCount,
625
- tool_call_count: sessionTotals.toolCallCount,
626
- compaction_count: sessionTotals.compactionCount,
627
- model_call_count: sessionTotals.usage.modelCallCount,
628
- input_tokens: sessionTotals.usage.input,
629
- output_tokens: sessionTotals.usage.output,
630
- cache_read_tokens: sessionTotals.usage.cacheRead,
631
- cache_write_tokens: sessionTotals.usage.cacheWrite,
632
- total_tokens: sessionTotals.usage.totalTokens,
633
- });
634
- await sink.flush();
635
- });
636
- }
637
- //# sourceMappingURL=telemetry.js.map