@mastra/code-sdk 0.0.0-aggregate-trace-20261015182851

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 (749) hide show
  1. package/LICENSE.md +32 -0
  2. package/README.md +284 -0
  3. package/dist/acp/agent.d.ts +48 -0
  4. package/dist/acp/agent.d.ts.map +1 -0
  5. package/dist/acp/agent.js +416 -0
  6. package/dist/acp/agent.js.map +1 -0
  7. package/dist/acp/errors.d.ts +3 -0
  8. package/dist/acp/errors.d.ts.map +1 -0
  9. package/dist/acp/errors.js +14 -0
  10. package/dist/acp/errors.js.map +1 -0
  11. package/dist/acp/event-mapper.d.ts +28 -0
  12. package/dist/acp/event-mapper.d.ts.map +1 -0
  13. package/dist/acp/event-mapper.js +279 -0
  14. package/dist/acp/event-mapper.js.map +1 -0
  15. package/dist/acp/index.d.ts +9 -0
  16. package/dist/acp/index.d.ts.map +1 -0
  17. package/dist/acp/index.js +24 -0
  18. package/dist/acp/index.js.map +1 -0
  19. package/dist/acp/runtime.d.ts +5 -0
  20. package/dist/acp/runtime.d.ts.map +1 -0
  21. package/dist/acp/runtime.js +91 -0
  22. package/dist/acp/runtime.js.map +1 -0
  23. package/dist/acp/server.d.ts +4 -0
  24. package/dist/acp/server.d.ts.map +1 -0
  25. package/dist/acp/server.js +38 -0
  26. package/dist/acp/server.js.map +1 -0
  27. package/dist/acp/skills.d.ts +6 -0
  28. package/dist/acp/skills.d.ts.map +1 -0
  29. package/dist/acp/skills.js +33 -0
  30. package/dist/acp/skills.js.map +1 -0
  31. package/dist/acp.d.ts +2 -0
  32. package/dist/acp.d.ts.map +1 -0
  33. package/dist/acp.js +2 -0
  34. package/dist/agent-connections/messaging-processor.d.ts +17 -0
  35. package/dist/agent-connections/messaging-processor.d.ts.map +1 -0
  36. package/dist/agent-connections/messaging-processor.js +134 -0
  37. package/dist/agent-connections/messaging-processor.js.map +1 -0
  38. package/dist/agent-connections/ownership.d.ts +24 -0
  39. package/dist/agent-connections/ownership.d.ts.map +1 -0
  40. package/dist/agent-connections/ownership.js +99 -0
  41. package/dist/agent-connections/ownership.js.map +1 -0
  42. package/dist/agent-connections/registry.d.ts +20 -0
  43. package/dist/agent-connections/registry.d.ts.map +1 -0
  44. package/dist/agent-connections/registry.js +170 -0
  45. package/dist/agent-connections/registry.js.map +1 -0
  46. package/dist/agent-connections/session-advertisement.d.ts +27 -0
  47. package/dist/agent-connections/session-advertisement.d.ts.map +1 -0
  48. package/dist/agent-connections/session-advertisement.js +89 -0
  49. package/dist/agent-connections/session-advertisement.js.map +1 -0
  50. package/dist/agent-connections/signal-provider.d.ts +141 -0
  51. package/dist/agent-connections/signal-provider.d.ts.map +1 -0
  52. package/dist/agent-connections/signal-provider.js +38 -0
  53. package/dist/agent-connections/signal-provider.js.map +1 -0
  54. package/dist/agent-connections/state-processor.d.ts +23 -0
  55. package/dist/agent-connections/state-processor.d.ts.map +1 -0
  56. package/dist/agent-connections/state-processor.js +273 -0
  57. package/dist/agent-connections/state-processor.js.map +1 -0
  58. package/dist/agent-connections/thread-state.d.ts +41 -0
  59. package/dist/agent-connections/thread-state.d.ts.map +1 -0
  60. package/dist/agent-connections/thread-state.js +160 -0
  61. package/dist/agent-connections/thread-state.js.map +1 -0
  62. package/dist/agent-connections/tools.d.ts +133 -0
  63. package/dist/agent-connections/tools.d.ts.map +1 -0
  64. package/dist/agent-connections/tools.js +552 -0
  65. package/dist/agent-connections/tools.js.map +1 -0
  66. package/dist/agent-connections/types.d.ts +99 -0
  67. package/dist/agent-connections/types.d.ts.map +1 -0
  68. package/dist/agent-connections/types.js +7 -0
  69. package/dist/agent-connections/types.js.map +1 -0
  70. package/dist/agent-connections/untrusted-text.d.ts +5 -0
  71. package/dist/agent-connections/untrusted-text.d.ts.map +1 -0
  72. package/dist/agent-connections/untrusted-text.js +22 -0
  73. package/dist/agent-connections/untrusted-text.js.map +1 -0
  74. package/dist/agents/background-completion-events.d.ts +16 -0
  75. package/dist/agents/background-completion-events.d.ts.map +1 -0
  76. package/dist/agents/background-completion-events.js +19 -0
  77. package/dist/agents/background-completion-events.js.map +1 -0
  78. package/dist/agents/background-completion.d.ts +9 -0
  79. package/dist/agents/background-completion.d.ts.map +1 -0
  80. package/dist/agents/background-completion.js +59 -0
  81. package/dist/agents/background-completion.js.map +1 -0
  82. package/dist/agents/context-audit.d.ts +84 -0
  83. package/dist/agents/context-audit.d.ts.map +1 -0
  84. package/dist/agents/context-audit.js +118 -0
  85. package/dist/agents/context-audit.js.map +1 -0
  86. package/dist/agents/credential-resolver.d.ts +67 -0
  87. package/dist/agents/credential-resolver.d.ts.map +1 -0
  88. package/dist/agents/credential-resolver.js +136 -0
  89. package/dist/agents/credential-resolver.js.map +1 -0
  90. package/dist/agents/custom-provider-source.d.ts +35 -0
  91. package/dist/agents/custom-provider-source.d.ts.map +1 -0
  92. package/dist/agents/custom-provider-source.js +25 -0
  93. package/dist/agents/custom-provider-source.js.map +1 -0
  94. package/dist/agents/instructions.d.ts +40 -0
  95. package/dist/agents/instructions.d.ts.map +1 -0
  96. package/dist/agents/instructions.js +63 -0
  97. package/dist/agents/instructions.js.map +1 -0
  98. package/dist/agents/mastracode-gateway.d.ts +76 -0
  99. package/dist/agents/mastracode-gateway.d.ts.map +1 -0
  100. package/dist/agents/mastracode-gateway.js +434 -0
  101. package/dist/agents/mastracode-gateway.js.map +1 -0
  102. package/dist/agents/memory.d.ts +39 -0
  103. package/dist/agents/memory.d.ts.map +1 -0
  104. package/dist/agents/memory.js +266 -0
  105. package/dist/agents/memory.js.map +1 -0
  106. package/dist/agents/model.d.ts +123 -0
  107. package/dist/agents/model.d.ts.map +1 -0
  108. package/dist/agents/model.js +295 -0
  109. package/dist/agents/model.js.map +1 -0
  110. package/dist/agents/modes/build.d.ts +12 -0
  111. package/dist/agents/modes/build.d.ts.map +1 -0
  112. package/dist/agents/modes/build.js +36 -0
  113. package/dist/agents/modes/build.js.map +1 -0
  114. package/dist/agents/modes/explore.d.ts +10 -0
  115. package/dist/agents/modes/explore.d.ts.map +1 -0
  116. package/dist/agents/modes/explore.js +28 -0
  117. package/dist/agents/modes/explore.js.map +1 -0
  118. package/dist/agents/modes/plan.d.ts +8 -0
  119. package/dist/agents/modes/plan.d.ts.map +1 -0
  120. package/dist/agents/modes/plan.js +46 -0
  121. package/dist/agents/modes/plan.js.map +1 -0
  122. package/dist/agents/prompts/agent-instructions.d.ts +99 -0
  123. package/dist/agents/prompts/agent-instructions.d.ts.map +1 -0
  124. package/dist/agents/prompts/agent-instructions.js +275 -0
  125. package/dist/agents/prompts/agent-instructions.js.map +1 -0
  126. package/dist/agents/prompts/build.d.ts +12 -0
  127. package/dist/agents/prompts/build.d.ts.map +1 -0
  128. package/dist/agents/prompts/build.js +76 -0
  129. package/dist/agents/prompts/build.js.map +1 -0
  130. package/dist/agents/prompts/fast.d.ts +5 -0
  131. package/dist/agents/prompts/fast.d.ts.map +1 -0
  132. package/dist/agents/prompts/fast.js +26 -0
  133. package/dist/agents/prompts/fast.js.map +1 -0
  134. package/dist/agents/prompts/index.d.ts +54 -0
  135. package/dist/agents/prompts/index.d.ts.map +1 -0
  136. package/dist/agents/prompts/index.js +127 -0
  137. package/dist/agents/prompts/index.js.map +1 -0
  138. package/dist/agents/prompts/model.d.ts +5 -0
  139. package/dist/agents/prompts/model.d.ts.map +1 -0
  140. package/dist/agents/prompts/model.js +23 -0
  141. package/dist/agents/prompts/model.js.map +1 -0
  142. package/dist/agents/prompts/plan.d.ts +8 -0
  143. package/dist/agents/prompts/plan.d.ts.map +1 -0
  144. package/dist/agents/prompts/plan.js +129 -0
  145. package/dist/agents/prompts/plan.js.map +1 -0
  146. package/dist/agents/prompts/tool-guidance.d.ts +18 -0
  147. package/dist/agents/prompts/tool-guidance.d.ts.map +1 -0
  148. package/dist/agents/prompts/tool-guidance.js +181 -0
  149. package/dist/agents/prompts/tool-guidance.js.map +1 -0
  150. package/dist/agents/sandbox-filesystem.d.ts +151 -0
  151. package/dist/agents/sandbox-filesystem.d.ts.map +1 -0
  152. package/dist/agents/sandbox-filesystem.js +581 -0
  153. package/dist/agents/sandbox-filesystem.js.map +1 -0
  154. package/dist/agents/subagents/execute.d.ts +3 -0
  155. package/dist/agents/subagents/execute.d.ts.map +1 -0
  156. package/dist/agents/subagents/execute.js +27 -0
  157. package/dist/agents/subagents/execute.js.map +1 -0
  158. package/dist/agents/subagents/explore.d.ts +3 -0
  159. package/dist/agents/subagents/explore.d.ts.map +1 -0
  160. package/dist/agents/subagents/explore.js +18 -0
  161. package/dist/agents/subagents/explore.js.map +1 -0
  162. package/dist/agents/subagents/plan.d.ts +3 -0
  163. package/dist/agents/subagents/plan.d.ts.map +1 -0
  164. package/dist/agents/subagents/plan.js +18 -0
  165. package/dist/agents/subagents/plan.js.map +1 -0
  166. package/dist/agents/thread-caveman-state.d.ts +17 -0
  167. package/dist/agents/thread-caveman-state.d.ts.map +1 -0
  168. package/dist/agents/thread-caveman-state.js +107 -0
  169. package/dist/agents/thread-caveman-state.js.map +1 -0
  170. package/dist/agents/tool-availability.d.ts +12 -0
  171. package/dist/agents/tool-availability.d.ts.map +1 -0
  172. package/dist/agents/tool-availability.js +129 -0
  173. package/dist/agents/tool-availability.js.map +1 -0
  174. package/dist/agents/tools.d.ts +37 -0
  175. package/dist/agents/tools.d.ts.map +1 -0
  176. package/dist/agents/tools.js +131 -0
  177. package/dist/agents/tools.js.map +1 -0
  178. package/dist/agents/workflow-builder-agent.d.ts +2 -0
  179. package/dist/agents/workflow-builder-agent.d.ts.map +1 -0
  180. package/dist/agents/workflow-builder-agent.js +82 -0
  181. package/dist/agents/workflow-builder-agent.js.map +1 -0
  182. package/dist/agents/workspace.d.ts +33 -0
  183. package/dist/agents/workspace.d.ts.map +1 -0
  184. package/dist/agents/workspace.js +208 -0
  185. package/dist/agents/workspace.js.map +1 -0
  186. package/dist/analytics.d.ts +18 -0
  187. package/dist/analytics.d.ts.map +1 -0
  188. package/dist/analytics.js +162 -0
  189. package/dist/analytics.js.map +1 -0
  190. package/dist/auth/account-rotation-processor.d.ts +182 -0
  191. package/dist/auth/account-rotation-processor.d.ts.map +1 -0
  192. package/dist/auth/account-rotation-processor.js +609 -0
  193. package/dist/auth/account-rotation-processor.js.map +1 -0
  194. package/dist/auth/account-routing-context.d.ts +13 -0
  195. package/dist/auth/account-routing-context.d.ts.map +1 -0
  196. package/dist/auth/account-routing-context.js +41 -0
  197. package/dist/auth/account-routing-context.js.map +1 -0
  198. package/dist/auth/authorization-input.d.ts +16 -0
  199. package/dist/auth/authorization-input.d.ts.map +1 -0
  200. package/dist/auth/authorization-input.js +42 -0
  201. package/dist/auth/authorization-input.js.map +1 -0
  202. package/dist/auth/device-code.d.ts +93 -0
  203. package/dist/auth/device-code.d.ts.map +1 -0
  204. package/dist/auth/device-code.js +129 -0
  205. package/dist/auth/device-code.js.map +1 -0
  206. package/dist/auth/index.d.ts +13 -0
  207. package/dist/auth/index.d.ts.map +1 -0
  208. package/dist/auth/index.js +9 -0
  209. package/dist/auth/pkce.d.ts +13 -0
  210. package/dist/auth/pkce.d.ts.map +1 -0
  211. package/dist/auth/pkce.js +32 -0
  212. package/dist/auth/pkce.js.map +1 -0
  213. package/dist/auth/provider-auth-error.d.ts +12 -0
  214. package/dist/auth/provider-auth-error.d.ts.map +1 -0
  215. package/dist/auth/provider-auth-error.js +16 -0
  216. package/dist/auth/provider-auth-error.js.map +1 -0
  217. package/dist/auth/providers/anthropic.d.ts +40 -0
  218. package/dist/auth/providers/anthropic.d.ts.map +1 -0
  219. package/dist/auth/providers/anthropic.js +120 -0
  220. package/dist/auth/providers/anthropic.js.map +1 -0
  221. package/dist/auth/providers/github-copilot.d.ts +154 -0
  222. package/dist/auth/providers/github-copilot.d.ts.map +1 -0
  223. package/dist/auth/providers/github-copilot.js +362 -0
  224. package/dist/auth/providers/github-copilot.js.map +1 -0
  225. package/dist/auth/providers/kimi-coding.d.ts +38 -0
  226. package/dist/auth/providers/kimi-coding.d.ts.map +1 -0
  227. package/dist/auth/providers/kimi-coding.js +228 -0
  228. package/dist/auth/providers/kimi-coding.js.map +1 -0
  229. package/dist/auth/providers/openai-codex.d.ts +141 -0
  230. package/dist/auth/providers/openai-codex.d.ts.map +1 -0
  231. package/dist/auth/providers/openai-codex.js +533 -0
  232. package/dist/auth/providers/openai-codex.js.map +1 -0
  233. package/dist/auth/providers/xai.d.ts +64 -0
  234. package/dist/auth/providers/xai.d.ts.map +1 -0
  235. package/dist/auth/providers/xai.js +226 -0
  236. package/dist/auth/providers/xai.js.map +1 -0
  237. package/dist/auth/storage.d.ts +214 -0
  238. package/dist/auth/storage.d.ts.map +1 -0
  239. package/dist/auth/storage.js +774 -0
  240. package/dist/auth/storage.js.map +1 -0
  241. package/dist/auth/types.d.ts +164 -0
  242. package/dist/auth/types.d.ts.map +1 -0
  243. package/dist/auth/types.js +1 -0
  244. package/dist/clipboard/index.d.ts +26 -0
  245. package/dist/clipboard/index.d.ts.map +1 -0
  246. package/dist/clipboard/index.js +226 -0
  247. package/dist/clipboard/index.js.map +1 -0
  248. package/dist/constants.d.ts +12 -0
  249. package/dist/constants.d.ts.map +1 -0
  250. package/dist/constants.js +20 -0
  251. package/dist/constants.js.map +1 -0
  252. package/dist/error-classification.d.ts +10 -0
  253. package/dist/error-classification.d.ts.map +1 -0
  254. package/dist/error-classification.js +22 -0
  255. package/dist/error-classification.js.map +1 -0
  256. package/dist/evals/context-builder.d.ts +41 -0
  257. package/dist/evals/context-builder.d.ts.map +1 -0
  258. package/dist/evals/context-builder.js +161 -0
  259. package/dist/evals/context-builder.js.map +1 -0
  260. package/dist/evals/index.d.ts +4 -0
  261. package/dist/evals/index.d.ts.map +1 -0
  262. package/dist/evals/index.js +5 -0
  263. package/dist/evals/scorers/classify-command.d.ts +31 -0
  264. package/dist/evals/scorers/classify-command.d.ts.map +1 -0
  265. package/dist/evals/scorers/classify-command.js +73 -0
  266. package/dist/evals/scorers/classify-command.js.map +1 -0
  267. package/dist/evals/scorers/efficiency.d.ts +44 -0
  268. package/dist/evals/scorers/efficiency.d.ts.map +1 -0
  269. package/dist/evals/scorers/efficiency.js +259 -0
  270. package/dist/evals/scorers/efficiency.js.map +1 -0
  271. package/dist/evals/scorers/extract-tools.d.ts +25 -0
  272. package/dist/evals/scorers/extract-tools.d.ts.map +1 -0
  273. package/dist/evals/scorers/extract-tools.js +60 -0
  274. package/dist/evals/scorers/extract-tools.js.map +1 -0
  275. package/dist/evals/scorers/index.d.ts +3 -0
  276. package/dist/evals/scorers/index.d.ts.map +1 -0
  277. package/dist/evals/scorers/index.js +3 -0
  278. package/dist/evals/scorers/outcome.d.ts +49 -0
  279. package/dist/evals/scorers/outcome.d.ts.map +1 -0
  280. package/dist/evals/scorers/outcome.js +347 -0
  281. package/dist/evals/scorers/outcome.js.map +1 -0
  282. package/dist/experimental-agent.d.ts +11 -0
  283. package/dist/experimental-agent.d.ts.map +1 -0
  284. package/dist/experimental-agent.js +36 -0
  285. package/dist/experimental-agent.js.map +1 -0
  286. package/dist/goal-manager.d.ts +114 -0
  287. package/dist/goal-manager.d.ts.map +1 -0
  288. package/dist/goal-manager.js +354 -0
  289. package/dist/goal-manager.js.map +1 -0
  290. package/dist/goal-signal.d.ts +14 -0
  291. package/dist/goal-signal.d.ts.map +1 -0
  292. package/dist/goal-signal.js +17 -0
  293. package/dist/goal-signal.js.map +1 -0
  294. package/dist/headless/cli.d.ts +48 -0
  295. package/dist/headless/cli.d.ts.map +1 -0
  296. package/dist/headless/cli.js +239 -0
  297. package/dist/headless/cli.js.map +1 -0
  298. package/dist/headless/flags.d.ts +33 -0
  299. package/dist/headless/flags.d.ts.map +1 -0
  300. package/dist/headless/flags.js +203 -0
  301. package/dist/headless/flags.js.map +1 -0
  302. package/dist/headless/format.d.ts +39 -0
  303. package/dist/headless/format.d.ts.map +1 -0
  304. package/dist/headless/format.js +85 -0
  305. package/dist/headless/format.js.map +1 -0
  306. package/dist/headless/index.d.ts +25 -0
  307. package/dist/headless/index.d.ts.map +1 -0
  308. package/dist/headless/index.js +6 -0
  309. package/dist/headless/policy.d.ts +17 -0
  310. package/dist/headless/policy.d.ts.map +1 -0
  311. package/dist/headless/policy.js +42 -0
  312. package/dist/headless/policy.js.map +1 -0
  313. package/dist/headless/run-mc.d.ts +7 -0
  314. package/dist/headless/run-mc.d.ts.map +1 -0
  315. package/dist/headless/run-mc.js +408 -0
  316. package/dist/headless/run-mc.js.map +1 -0
  317. package/dist/headless/types.d.ts +174 -0
  318. package/dist/headless/types.d.ts.map +1 -0
  319. package/dist/headless/types.js +19 -0
  320. package/dist/headless/types.js.map +1 -0
  321. package/dist/hooks/config.d.ts +11 -0
  322. package/dist/hooks/config.d.ts.map +1 -0
  323. package/dist/hooks/config.js +80 -0
  324. package/dist/hooks/config.js.map +1 -0
  325. package/dist/hooks/executor.d.ts +13 -0
  326. package/dist/hooks/executor.d.ts.map +1 -0
  327. package/dist/hooks/executor.js +129 -0
  328. package/dist/hooks/executor.js.map +1 -0
  329. package/dist/hooks/index.d.ts +6 -0
  330. package/dist/hooks/index.d.ts.map +1 -0
  331. package/dist/hooks/index.js +5 -0
  332. package/dist/hooks/manager.d.ts +54 -0
  333. package/dist/hooks/manager.d.ts.map +1 -0
  334. package/dist/hooks/manager.js +241 -0
  335. package/dist/hooks/manager.js.map +1 -0
  336. package/dist/hooks/types.d.ts +172 -0
  337. package/dist/hooks/types.d.ts.map +1 -0
  338. package/dist/hooks/types.js +8 -0
  339. package/dist/hooks/types.js.map +1 -0
  340. package/dist/index.d.ts +463 -0
  341. package/dist/index.d.ts.map +1 -0
  342. package/dist/index.js +1305 -0
  343. package/dist/index.js.map +1 -0
  344. package/dist/ipc/ipc-reporter.d.ts +14 -0
  345. package/dist/ipc/ipc-reporter.d.ts.map +1 -0
  346. package/dist/ipc/ipc-reporter.js +21 -0
  347. package/dist/ipc/ipc-reporter.js.map +1 -0
  348. package/dist/knowledge-inspector.d.ts +123 -0
  349. package/dist/knowledge-inspector.d.ts.map +1 -0
  350. package/dist/knowledge-inspector.js +516 -0
  351. package/dist/knowledge-inspector.js.map +1 -0
  352. package/dist/knowledge-scope.d.ts +35 -0
  353. package/dist/knowledge-scope.d.ts.map +1 -0
  354. package/dist/knowledge-scope.js +47 -0
  355. package/dist/knowledge-scope.js.map +1 -0
  356. package/dist/lsp/client.d.ts +56 -0
  357. package/dist/lsp/client.d.ts.map +1 -0
  358. package/dist/lsp/client.js +251 -0
  359. package/dist/lsp/client.js.map +1 -0
  360. package/dist/lsp/index.d.ts +4 -0
  361. package/dist/lsp/index.d.ts.map +1 -0
  362. package/dist/lsp/index.js +4 -0
  363. package/dist/lsp/language.d.ts +9 -0
  364. package/dist/lsp/language.d.ts.map +1 -0
  365. package/dist/lsp/language.js +44 -0
  366. package/dist/lsp/language.js.map +1 -0
  367. package/dist/lsp/manager.d.ts +34 -0
  368. package/dist/lsp/manager.d.ts.map +1 -0
  369. package/dist/lsp/manager.js +82 -0
  370. package/dist/lsp/manager.js.map +1 -0
  371. package/dist/lsp/server.d.ts +27 -0
  372. package/dist/lsp/server.d.ts.map +1 -0
  373. package/dist/lsp/server.js +159 -0
  374. package/dist/lsp/server.js.map +1 -0
  375. package/dist/lsp/workspace.d.ts +9 -0
  376. package/dist/lsp/workspace.d.ts.map +1 -0
  377. package/dist/lsp/workspace.js +55 -0
  378. package/dist/lsp/workspace.js.map +1 -0
  379. package/dist/mcp/config.d.ts +77 -0
  380. package/dist/mcp/config.d.ts.map +1 -0
  381. package/dist/mcp/config.js +316 -0
  382. package/dist/mcp/config.js.map +1 -0
  383. package/dist/mcp/index.d.ts +6 -0
  384. package/dist/mcp/index.d.ts.map +1 -0
  385. package/dist/mcp/index.js +3 -0
  386. package/dist/mcp/manager.d.ts +102 -0
  387. package/dist/mcp/manager.d.ts.map +1 -0
  388. package/dist/mcp/manager.js +650 -0
  389. package/dist/mcp/manager.js.map +1 -0
  390. package/dist/mcp/state.d.ts +35 -0
  391. package/dist/mcp/state.d.ts.map +1 -0
  392. package/dist/mcp/state.js +97 -0
  393. package/dist/mcp/state.js.map +1 -0
  394. package/dist/mcp/types.d.ts +139 -0
  395. package/dist/mcp/types.d.ts.map +1 -0
  396. package/dist/mcp/types.js +1 -0
  397. package/dist/onboarding/custom-packs.d.ts +7 -0
  398. package/dist/onboarding/custom-packs.d.ts.map +1 -0
  399. package/dist/onboarding/custom-packs.js +20 -0
  400. package/dist/onboarding/custom-packs.js.map +1 -0
  401. package/dist/onboarding/custom-providers.d.ts +4 -0
  402. package/dist/onboarding/custom-providers.d.ts.map +1 -0
  403. package/dist/onboarding/custom-providers.js +30 -0
  404. package/dist/onboarding/custom-providers.js.map +1 -0
  405. package/dist/onboarding/index.d.ts +5 -0
  406. package/dist/onboarding/index.d.ts.map +1 -0
  407. package/dist/onboarding/index.js +3 -0
  408. package/dist/onboarding/om-settings.d.ts +47 -0
  409. package/dist/onboarding/om-settings.d.ts.map +1 -0
  410. package/dist/onboarding/om-settings.js +83 -0
  411. package/dist/onboarding/om-settings.js.map +1 -0
  412. package/dist/onboarding/pack-activation.d.ts +52 -0
  413. package/dist/onboarding/pack-activation.d.ts.map +1 -0
  414. package/dist/onboarding/pack-activation.js +108 -0
  415. package/dist/onboarding/pack-activation.js.map +1 -0
  416. package/dist/onboarding/packs.d.ts +102 -0
  417. package/dist/onboarding/packs.d.ts.map +1 -0
  418. package/dist/onboarding/packs.js +278 -0
  419. package/dist/onboarding/packs.js.map +1 -0
  420. package/dist/onboarding/settings.d.ts +538 -0
  421. package/dist/onboarding/settings.d.ts.map +1 -0
  422. package/dist/onboarding/settings.js +1060 -0
  423. package/dist/onboarding/settings.js.map +1 -0
  424. package/dist/permissions.d.ts +55 -0
  425. package/dist/permissions.d.ts.map +1 -0
  426. package/dist/permissions.js +142 -0
  427. package/dist/permissions.js.map +1 -0
  428. package/dist/plugin.d.ts +156 -0
  429. package/dist/plugin.d.ts.map +1 -0
  430. package/dist/plugin.js +30 -0
  431. package/dist/plugin.js.map +1 -0
  432. package/dist/plugins/dependencies.d.ts +9 -0
  433. package/dist/plugins/dependencies.d.ts.map +1 -0
  434. package/dist/plugins/dependencies.js +87 -0
  435. package/dist/plugins/dependencies.js.map +1 -0
  436. package/dist/plugins/install.d.ts +21 -0
  437. package/dist/plugins/install.d.ts.map +1 -0
  438. package/dist/plugins/install.js +190 -0
  439. package/dist/plugins/install.js.map +1 -0
  440. package/dist/plugins/loader.d.ts +19 -0
  441. package/dist/plugins/loader.d.ts.map +1 -0
  442. package/dist/plugins/loader.js +260 -0
  443. package/dist/plugins/loader.js.map +1 -0
  444. package/dist/plugins/manager.d.ts +98 -0
  445. package/dist/plugins/manager.d.ts.map +1 -0
  446. package/dist/plugins/manager.js +456 -0
  447. package/dist/plugins/manager.js.map +1 -0
  448. package/dist/plugins/manifest.d.ts +14 -0
  449. package/dist/plugins/manifest.d.ts.map +1 -0
  450. package/dist/plugins/manifest.js +50 -0
  451. package/dist/plugins/manifest.js.map +1 -0
  452. package/dist/plugins/package-link.d.ts +3 -0
  453. package/dist/plugins/package-link.d.ts.map +1 -0
  454. package/dist/plugins/package-link.js +61 -0
  455. package/dist/plugins/package-link.js.map +1 -0
  456. package/dist/plugins/paths.d.ts +10 -0
  457. package/dist/plugins/paths.d.ts.map +1 -0
  458. package/dist/plugins/paths.js +25 -0
  459. package/dist/plugins/paths.js.map +1 -0
  460. package/dist/plugins/registry.d.ts +8 -0
  461. package/dist/plugins/registry.d.ts.map +1 -0
  462. package/dist/plugins/registry.js +112 -0
  463. package/dist/plugins/registry.js.map +1 -0
  464. package/dist/plugins/scaffold.d.ts +10 -0
  465. package/dist/plugins/scaffold.d.ts.map +1 -0
  466. package/dist/plugins/scaffold.js +116 -0
  467. package/dist/plugins/scaffold.js.map +1 -0
  468. package/dist/plugins/signal-lane.d.ts +58 -0
  469. package/dist/plugins/signal-lane.d.ts.map +1 -0
  470. package/dist/plugins/signal-lane.js +166 -0
  471. package/dist/plugins/signal-lane.js.map +1 -0
  472. package/dist/plugins/types.d.ts +81 -0
  473. package/dist/plugins/types.d.ts.map +1 -0
  474. package/dist/plugins/types.js +1 -0
  475. package/dist/process-memory-diagnostics.d.ts +135 -0
  476. package/dist/process-memory-diagnostics.d.ts.map +1 -0
  477. package/dist/process-memory-diagnostics.js +499 -0
  478. package/dist/process-memory-diagnostics.js.map +1 -0
  479. package/dist/processors/plan-rejection-abort.d.ts +12 -0
  480. package/dist/processors/plan-rejection-abort.d.ts.map +1 -0
  481. package/dist/processors/plan-rejection-abort.js +54 -0
  482. package/dist/processors/plan-rejection-abort.js.map +1 -0
  483. package/dist/providers/amazon-bedrock-gateway.d.ts +47 -0
  484. package/dist/providers/amazon-bedrock-gateway.d.ts.map +1 -0
  485. package/dist/providers/amazon-bedrock-gateway.js +144 -0
  486. package/dist/providers/amazon-bedrock-gateway.js.map +1 -0
  487. package/dist/providers/amazon-bedrock.d.ts +28 -0
  488. package/dist/providers/amazon-bedrock.d.ts.map +1 -0
  489. package/dist/providers/amazon-bedrock.js +102 -0
  490. package/dist/providers/amazon-bedrock.js.map +1 -0
  491. package/dist/providers/anthropic-prompt-cache.d.ts +19 -0
  492. package/dist/providers/anthropic-prompt-cache.d.ts.map +1 -0
  493. package/dist/providers/anthropic-prompt-cache.js +11 -0
  494. package/dist/providers/anthropic-prompt-cache.js.map +1 -0
  495. package/dist/providers/anthropic-thinking.d.ts +5 -0
  496. package/dist/providers/anthropic-thinking.d.ts.map +1 -0
  497. package/dist/providers/anthropic-thinking.js +25 -0
  498. package/dist/providers/anthropic-thinking.js.map +1 -0
  499. package/dist/providers/claude-max.d.ts +67 -0
  500. package/dist/providers/claude-max.d.ts.map +1 -0
  501. package/dist/providers/claude-max.js +226 -0
  502. package/dist/providers/claude-max.js.map +1 -0
  503. package/dist/providers/github-copilot-gateway.d.ts +35 -0
  504. package/dist/providers/github-copilot-gateway.d.ts.map +1 -0
  505. package/dist/providers/github-copilot-gateway.js +61 -0
  506. package/dist/providers/github-copilot-gateway.js.map +1 -0
  507. package/dist/providers/github-copilot.d.ts +60 -0
  508. package/dist/providers/github-copilot.d.ts.map +1 -0
  509. package/dist/providers/github-copilot.js +318 -0
  510. package/dist/providers/github-copilot.js.map +1 -0
  511. package/dist/providers/google-thinking.d.ts +31 -0
  512. package/dist/providers/google-thinking.d.ts.map +1 -0
  513. package/dist/providers/google-thinking.js +67 -0
  514. package/dist/providers/google-thinking.js.map +1 -0
  515. package/dist/providers/kimi-coding.d.ts +14 -0
  516. package/dist/providers/kimi-coding.d.ts.map +1 -0
  517. package/dist/providers/kimi-coding.js +78 -0
  518. package/dist/providers/kimi-coding.js.map +1 -0
  519. package/dist/providers/model-ids.d.ts +30 -0
  520. package/dist/providers/model-ids.d.ts.map +1 -0
  521. package/dist/providers/model-ids.js +79 -0
  522. package/dist/providers/model-ids.js.map +1 -0
  523. package/dist/providers/openai-codex.d.ts +84 -0
  524. package/dist/providers/openai-codex.d.ts.map +1 -0
  525. package/dist/providers/openai-codex.js +347 -0
  526. package/dist/providers/openai-codex.js.map +1 -0
  527. package/dist/providers/provider-thinking.d.ts +23 -0
  528. package/dist/providers/provider-thinking.d.ts.map +1 -0
  529. package/dist/providers/provider-thinking.js +64 -0
  530. package/dist/providers/provider-thinking.js.map +1 -0
  531. package/dist/providers/thinking-request.d.ts +14 -0
  532. package/dist/providers/thinking-request.d.ts.map +1 -0
  533. package/dist/providers/thinking-request.js +90 -0
  534. package/dist/providers/thinking-request.js.map +1 -0
  535. package/dist/providers/xai.d.ts +33 -0
  536. package/dist/providers/xai.d.ts.map +1 -0
  537. package/dist/providers/xai.js +71 -0
  538. package/dist/providers/xai.js.map +1 -0
  539. package/dist/schedules/index.d.ts +10 -0
  540. package/dist/schedules/index.d.ts.map +1 -0
  541. package/dist/schedules/index.js +6 -0
  542. package/dist/schedules/interval.d.ts +49 -0
  543. package/dist/schedules/interval.d.ts.map +1 -0
  544. package/dist/schedules/interval.js +215 -0
  545. package/dist/schedules/interval.js.map +1 -0
  546. package/dist/schedules/prompt.d.ts +36 -0
  547. package/dist/schedules/prompt.d.ts.map +1 -0
  548. package/dist/schedules/prompt.js +39 -0
  549. package/dist/schedules/prompt.js.map +1 -0
  550. package/dist/schedules/run-script.d.ts +8 -0
  551. package/dist/schedules/run-script.d.ts.map +1 -0
  552. package/dist/schedules/run-script.js +83 -0
  553. package/dist/schedules/run-script.js.map +1 -0
  554. package/dist/schedules/scheduler.d.ts +101 -0
  555. package/dist/schedules/scheduler.d.ts.map +1 -0
  556. package/dist/schedules/scheduler.js +169 -0
  557. package/dist/schedules/scheduler.js.map +1 -0
  558. package/dist/schedules/tools.d.ts +57 -0
  559. package/dist/schedules/tools.d.ts.map +1 -0
  560. package/dist/schedules/tools.js +220 -0
  561. package/dist/schedules/tools.js.map +1 -0
  562. package/dist/schema.d.ts +499 -0
  563. package/dist/schema.d.ts.map +1 -0
  564. package/dist/schema.js +134 -0
  565. package/dist/schema.js.map +1 -0
  566. package/dist/theme-palette.d.ts +17 -0
  567. package/dist/theme-palette.d.ts.map +1 -0
  568. package/dist/theme-palette.js +21 -0
  569. package/dist/theme-palette.js.map +1 -0
  570. package/dist/thinking-catalog.d.ts +4 -0
  571. package/dist/thinking-catalog.d.ts.map +1 -0
  572. package/dist/thinking-catalog.js +13 -0
  573. package/dist/thinking-catalog.js.map +1 -0
  574. package/dist/thinking.d.ts +37 -0
  575. package/dist/thinking.d.ts.map +1 -0
  576. package/dist/thinking.js +137 -0
  577. package/dist/thinking.js.map +1 -0
  578. package/dist/tool-names.d.ts +77 -0
  579. package/dist/tool-names.d.ts.map +1 -0
  580. package/dist/tool-names.js +56 -0
  581. package/dist/tool-names.js.map +1 -0
  582. package/dist/tools/index.d.ts +6 -0
  583. package/dist/tools/index.d.ts.map +1 -0
  584. package/dist/tools/index.js +3 -0
  585. package/dist/tools/request-sandbox-access.d.ts +6 -0
  586. package/dist/tools/request-sandbox-access.d.ts.map +1 -0
  587. package/dist/tools/request-sandbox-access.js +80 -0
  588. package/dist/tools/request-sandbox-access.js.map +1 -0
  589. package/dist/tools/utils.d.ts +20 -0
  590. package/dist/tools/utils.d.ts.map +1 -0
  591. package/dist/tools/utils.js +32 -0
  592. package/dist/tools/utils.js.map +1 -0
  593. package/dist/tools/web-search.d.ts +114 -0
  594. package/dist/tools/web-search.d.ts.map +1 -0
  595. package/dist/tools/web-search.js +155 -0
  596. package/dist/tools/web-search.js.map +1 -0
  597. package/dist/tools/workflows/create-workflow.d.ts +7 -0
  598. package/dist/tools/workflows/create-workflow.d.ts.map +1 -0
  599. package/dist/tools/workflows/create-workflow.js +81 -0
  600. package/dist/tools/workflows/create-workflow.js.map +1 -0
  601. package/dist/tools/workflows/delete-workflow.d.ts +7 -0
  602. package/dist/tools/workflows/delete-workflow.d.ts.map +1 -0
  603. package/dist/tools/workflows/delete-workflow.js +21 -0
  604. package/dist/tools/workflows/delete-workflow.js.map +1 -0
  605. package/dist/tools/workflows/ephemeral-memory.d.ts +28 -0
  606. package/dist/tools/workflows/ephemeral-memory.d.ts.map +1 -0
  607. package/dist/tools/workflows/ephemeral-memory.js +44 -0
  608. package/dist/tools/workflows/ephemeral-memory.js.map +1 -0
  609. package/dist/tools/workflows/extract-json-schema.d.ts +12 -0
  610. package/dist/tools/workflows/extract-json-schema.d.ts.map +1 -0
  611. package/dist/tools/workflows/extract-json-schema.js +22 -0
  612. package/dist/tools/workflows/extract-json-schema.js.map +1 -0
  613. package/dist/tools/workflows/get-workflow.d.ts +11 -0
  614. package/dist/tools/workflows/get-workflow.d.ts.map +1 -0
  615. package/dist/tools/workflows/get-workflow.js +34 -0
  616. package/dist/tools/workflows/get-workflow.js.map +1 -0
  617. package/dist/tools/workflows/list-available-agents.d.ts +8 -0
  618. package/dist/tools/workflows/list-available-agents.d.ts.map +1 -0
  619. package/dist/tools/workflows/list-available-agents.js +32 -0
  620. package/dist/tools/workflows/list-available-agents.js.map +1 -0
  621. package/dist/tools/workflows/list-available-classifiers.d.ts +19 -0
  622. package/dist/tools/workflows/list-available-classifiers.d.ts.map +1 -0
  623. package/dist/tools/workflows/list-available-classifiers.js +56 -0
  624. package/dist/tools/workflows/list-available-classifiers.js.map +1 -0
  625. package/dist/tools/workflows/list-available-tools.d.ts +9 -0
  626. package/dist/tools/workflows/list-available-tools.d.ts.map +1 -0
  627. package/dist/tools/workflows/list-available-tools.js +40 -0
  628. package/dist/tools/workflows/list-available-tools.js.map +1 -0
  629. package/dist/tools/workflows/list-available-workflows.d.ts +9 -0
  630. package/dist/tools/workflows/list-available-workflows.d.ts.map +1 -0
  631. package/dist/tools/workflows/list-available-workflows.js +32 -0
  632. package/dist/tools/workflows/list-available-workflows.js.map +1 -0
  633. package/dist/tools/workflows/list-workflows.d.ts +9 -0
  634. package/dist/tools/workflows/list-workflows.d.ts.map +1 -0
  635. package/dist/tools/workflows/list-workflows.js +33 -0
  636. package/dist/tools/workflows/list-workflows.js.map +1 -0
  637. package/dist/tools/workflows/run-workflow.d.ts +9 -0
  638. package/dist/tools/workflows/run-workflow.d.ts.map +1 -0
  639. package/dist/tools/workflows/run-workflow.js +52 -0
  640. package/dist/tools/workflows/run-workflow.js.map +1 -0
  641. package/dist/tools/workflows/save-workflow.d.ts +1125 -0
  642. package/dist/tools/workflows/save-workflow.d.ts.map +1 -0
  643. package/dist/tools/workflows/save-workflow.js +28 -0
  644. package/dist/tools/workflows/save-workflow.js.map +1 -0
  645. package/dist/tools/workflows/tool-ids.d.ts +16 -0
  646. package/dist/tools/workflows/tool-ids.d.ts.map +1 -0
  647. package/dist/tools/workflows/tool-ids.js +20 -0
  648. package/dist/tools/workflows/tool-ids.js.map +1 -0
  649. package/dist/utils/binaries.d.ts +7 -0
  650. package/dist/utils/binaries.d.ts.map +1 -0
  651. package/dist/utils/binaries.js +78 -0
  652. package/dist/utils/binaries.js.map +1 -0
  653. package/dist/utils/debug-log.d.ts +12 -0
  654. package/dist/utils/debug-log.d.ts.map +1 -0
  655. package/dist/utils/debug-log.js +59 -0
  656. package/dist/utils/debug-log.js.map +1 -0
  657. package/dist/utils/discarding-scores-storage.d.ts +33 -0
  658. package/dist/utils/discarding-scores-storage.d.ts.map +1 -0
  659. package/dist/utils/discarding-scores-storage.js +54 -0
  660. package/dist/utils/discarding-scores-storage.js.map +1 -0
  661. package/dist/utils/errors.d.ts +36 -0
  662. package/dist/utils/errors.d.ts.map +1 -0
  663. package/dist/utils/errors.js +196 -0
  664. package/dist/utils/errors.js.map +1 -0
  665. package/dist/utils/gateway-sync.d.ts +26 -0
  666. package/dist/utils/gateway-sync.d.ts.map +1 -0
  667. package/dist/utils/gateway-sync.js +61 -0
  668. package/dist/utils/gateway-sync.js.map +1 -0
  669. package/dist/utils/maintenance-lock.d.ts +42 -0
  670. package/dist/utils/maintenance-lock.d.ts.map +1 -0
  671. package/dist/utils/maintenance-lock.js +199 -0
  672. package/dist/utils/maintenance-lock.js.map +1 -0
  673. package/dist/utils/notification-dispatch.d.ts +59 -0
  674. package/dist/utils/notification-dispatch.d.ts.map +1 -0
  675. package/dist/utils/notification-dispatch.js +109 -0
  676. package/dist/utils/notification-dispatch.js.map +1 -0
  677. package/dist/utils/open-url.d.ts +10 -0
  678. package/dist/utils/open-url.d.ts.map +1 -0
  679. package/dist/utils/open-url.js +30 -0
  680. package/dist/utils/open-url.js.map +1 -0
  681. package/dist/utils/path-security.d.ts +2 -0
  682. package/dist/utils/path-security.d.ts.map +1 -0
  683. package/dist/utils/path-security.js +10 -0
  684. package/dist/utils/path-security.js.map +1 -0
  685. package/dist/utils/plan-diff.d.ts +25 -0
  686. package/dist/utils/plan-diff.d.ts.map +1 -0
  687. package/dist/utils/plan-diff.js +62 -0
  688. package/dist/utils/plan-diff.js.map +1 -0
  689. package/dist/utils/plans.d.ts +59 -0
  690. package/dist/utils/plans.d.ts.map +1 -0
  691. package/dist/utils/plans.js +117 -0
  692. package/dist/utils/plans.js.map +1 -0
  693. package/dist/utils/project.d.ts +160 -0
  694. package/dist/utils/project.d.ts.map +1 -0
  695. package/dist/utils/project.js +364 -0
  696. package/dist/utils/project.js.map +1 -0
  697. package/dist/utils/prune-cli.d.ts +24 -0
  698. package/dist/utils/prune-cli.d.ts.map +1 -0
  699. package/dist/utils/prune-cli.js +125 -0
  700. package/dist/utils/prune-cli.js.map +1 -0
  701. package/dist/utils/signals-pubsub.d.ts +101 -0
  702. package/dist/utils/signals-pubsub.d.ts.map +1 -0
  703. package/dist/utils/signals-pubsub.js +410 -0
  704. package/dist/utils/signals-pubsub.js.map +1 -0
  705. package/dist/utils/slash-command-loader.d.ts +52 -0
  706. package/dist/utils/slash-command-loader.d.ts.map +1 -0
  707. package/dist/utils/slash-command-loader.js +142 -0
  708. package/dist/utils/slash-command-loader.js.map +1 -0
  709. package/dist/utils/slash-command-processor.d.ts +14 -0
  710. package/dist/utils/slash-command-processor.d.ts.map +1 -0
  711. package/dist/utils/slash-command-processor.js +109 -0
  712. package/dist/utils/slash-command-processor.js.map +1 -0
  713. package/dist/utils/stdin-pipe.d.ts +33 -0
  714. package/dist/utils/stdin-pipe.d.ts.map +1 -0
  715. package/dist/utils/stdin-pipe.js +96 -0
  716. package/dist/utils/stdin-pipe.js.map +1 -0
  717. package/dist/utils/storage-factory.d.ts +50 -0
  718. package/dist/utils/storage-factory.d.ts.map +1 -0
  719. package/dist/utils/storage-factory.js +121 -0
  720. package/dist/utils/storage-factory.js.map +1 -0
  721. package/dist/utils/storage-maintenance.d.ts +121 -0
  722. package/dist/utils/storage-maintenance.d.ts.map +1 -0
  723. package/dist/utils/storage-maintenance.js +355 -0
  724. package/dist/utils/storage-maintenance.js.map +1 -0
  725. package/dist/utils/thread-lock.d.ts +26 -0
  726. package/dist/utils/thread-lock.d.ts.map +1 -0
  727. package/dist/utils/thread-lock.js +112 -0
  728. package/dist/utils/thread-lock.js.map +1 -0
  729. package/dist/utils/token-estimator.d.ts +3 -0
  730. package/dist/utils/token-estimator.d.ts.map +1 -0
  731. package/dist/utils/token-estimator.js +21 -0
  732. package/dist/utils/token-estimator.js.map +1 -0
  733. package/dist/utils/update-check.d.ts +151 -0
  734. package/dist/utils/update-check.d.ts.map +1 -0
  735. package/dist/utils/update-check.js +432 -0
  736. package/dist/utils/update-check.js.map +1 -0
  737. package/dist/voice/stt-registry.d.ts +64 -0
  738. package/dist/voice/stt-registry.d.ts.map +1 -0
  739. package/dist/voice/stt-registry.js +144 -0
  740. package/dist/voice/stt-registry.js.map +1 -0
  741. package/dist/workflows/register-primitives.d.ts +52 -0
  742. package/dist/workflows/register-primitives.d.ts.map +1 -0
  743. package/dist/workflows/register-primitives.js +40 -0
  744. package/dist/workflows/register-primitives.js.map +1 -0
  745. package/dist/workflows/service.d.ts +64 -0
  746. package/dist/workflows/service.d.ts.map +1 -0
  747. package/dist/workflows/service.js +53 -0
  748. package/dist/workflows/service.js.map +1 -0
  749. package/package.json +107 -0
@@ -0,0 +1,774 @@
1
+ import { getAppDataDir } from "../utils/project.js";
2
+ import { anthropicOAuthProvider } from "./providers/anthropic.js";
3
+ import { githubCopilotOAuthProvider } from "./providers/github-copilot.js";
4
+ import { kimiCodingOAuthProvider } from "./providers/kimi-coding.js";
5
+ import { openaiCodexOAuthProvider } from "./providers/openai-codex.js";
6
+ import { xaiOAuthProvider } from "./providers/xai.js";
7
+ import { dirname, join } from "path";
8
+ import { createHash } from "crypto";
9
+ import { chmodSync, existsSync, mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from "fs";
10
+ //#region src/auth/storage.ts
11
+ /**
12
+ * Credential storage for API keys and OAuth tokens.
13
+ * Handles loading, saving, and refreshing credentials from auth.json.
14
+ */
15
+ /**
16
+ * Best/default models for each OAuth provider.
17
+ * Used when auto-selecting a model after login.
18
+ */
19
+ const PROVIDER_DEFAULT_MODELS = {
20
+ anthropic: "anthropic/claude-fable-5",
21
+ "openai-codex": "openai/gpt-5.6-sol",
22
+ "github-copilot": "github-copilot/gpt-4.1",
23
+ "kimi-for-coding": "kimi-code-plan-cn/kimi-for-coding",
24
+ xai: "xai/grok-4.5"
25
+ };
26
+ const oauthProviderRegistry = /* @__PURE__ */ new Map([
27
+ [anthropicOAuthProvider.id, anthropicOAuthProvider],
28
+ [openaiCodexOAuthProvider.id, openaiCodexOAuthProvider],
29
+ [githubCopilotOAuthProvider.id, githubCopilotOAuthProvider],
30
+ [kimiCodingOAuthProvider.id, kimiCodingOAuthProvider],
31
+ [xaiOAuthProvider.id, xaiOAuthProvider]
32
+ ]);
33
+ /**
34
+ * Get an OAuth provider by ID
35
+ */
36
+ function getOAuthProvider(id) {
37
+ return oauthProviderRegistry.get(id);
38
+ }
39
+ /**
40
+ * Get all registered OAuth providers
41
+ */
42
+ function getOAuthProviders() {
43
+ return Array.from(oauthProviderRegistry.values());
44
+ }
45
+ /**
46
+ * Mint an account instance id: `${providerId}:${randomUUID()}`.
47
+ *
48
+ * Assigned once, when the account is created, and never derived from
49
+ * credentials. Deriving it from the refresh token (the pre-A13 scheme) looked
50
+ * stable but was not: `persistRefreshedCredential` keeps an entry's key while
51
+ * writing rotated tokens onto it, so every refresh left the id describing a
52
+ * credential the account no longer had — and re-adding that same subscription
53
+ * then failed to match its own entry and registered a second one for it.
54
+ */
55
+ function mintAccountId(providerId) {
56
+ return `${providerId}:${globalThis.crypto.randomUUID()}`;
57
+ }
58
+ /**
59
+ * The pre-A13 id scheme: `${providerId}:${sha256(refresh).slice(0,8)}`.
60
+ *
61
+ * No longer used to assign ids, but still needed to recognize registries
62
+ * written before A13 — both to match a re-authorized account onto its existing
63
+ * entry, and to recognize entries whose fields the A13 migration must backfill.
64
+ */
65
+ function legacyAccountIdFor(providerId, refreshToken) {
66
+ return `${providerId}:${createHash("sha256").update(refreshToken).digest("hex").slice(0, 8)}`;
67
+ }
68
+ /**
69
+ * Stable identity for the *subscription* behind an account, when the provider
70
+ * exposes one (an account id, an email — anything that survives a refresh and
71
+ * a re-authorization). Used to decide whether an added account is one we
72
+ * already have. Providers whose credentials carry no such identifier return
73
+ * undefined; for those, a re-authorization is indistinguishable from a new
74
+ * account without spending a request.
75
+ */
76
+ function providerIdentity(provider, creds) {
77
+ try {
78
+ const identity = provider?.getAccountIdentity?.(creds);
79
+ return typeof identity === "string" && identity.length > 0 ? identity : void 0;
80
+ } catch {
81
+ return;
82
+ }
83
+ }
84
+ function isOAuthAccountRecord(value) {
85
+ if (!value || typeof value !== "object") return false;
86
+ const v = value;
87
+ return v.type === "oauth-account" && typeof v.id === "string" && typeof v.label === "string" && typeof v.addedAt === "string" && typeof v.active === "boolean" && typeof v.refresh === "string" && typeof v.access === "string" && typeof v.expires === "number";
88
+ }
89
+ /** The credential fields of an account record — everything but registry identity. */
90
+ function credentialFieldsOf(record) {
91
+ const { type: _type, id: _id, label: _label, addedAt: _addedAt, active: _active, identity: _identity, ...creds } = record;
92
+ return creds;
93
+ }
94
+ /**
95
+ * Credential storage backed by a JSON file.
96
+ */
97
+ var AuthStorage = class {
98
+ authPath;
99
+ data = {};
100
+ refreshPromises = /* @__PURE__ */ new Map();
101
+ constructor(authPath = join(getAppDataDir(), "auth.json")) {
102
+ this.authPath = authPath;
103
+ this.reload();
104
+ }
105
+ /**
106
+ * Reload credentials from disk.
107
+ */
108
+ reload() {
109
+ if (!existsSync(this.authPath)) {
110
+ this.data = {};
111
+ return;
112
+ }
113
+ try {
114
+ this.data = JSON.parse(readFileSync(this.authPath, "utf-8"));
115
+ } catch {
116
+ this.data = {};
117
+ }
118
+ this.migrate();
119
+ }
120
+ /**
121
+ * Bring auth.json up to the multi-account registry format. Runs on every
122
+ * load; saves only when something actually changed.
123
+ *
124
+ * 1. A legacy OAuth slot with no registry gets one adopted active entry
125
+ * (the slot's tokens stay put — the entry mirrors them).
126
+ * 2. When the slot's refresh token differs from the active entry's, an
127
+ * external writer (older build, another worktree) owns the slot — the
128
+ * slot wins and its tokens are adopted onto the active entry.
129
+ * 3. A registry with no active entry (hand-edited) self-heals to its first
130
+ * entry; malformed `accounts:` values are skipped, never fatal.
131
+ * 4. A registry whose legacy slot disappeared (partial write, an external
132
+ * hand-edit) self-heals by mirroring the active entry's tokens back into
133
+ * the slot, so `isLoggedIn()`/`getOAuthCredential()` keep agreeing with
134
+ * `listAccounts()`. `remove()`/`logout()` clear the registry with the
135
+ * slot, so they never leave the shape this heals.
136
+ */
137
+ migrate() {
138
+ let changed = false;
139
+ for (const key of Object.keys(this.data)) {
140
+ const slot = this.data[key];
141
+ if (!slot || slot.type !== "oauth") continue;
142
+ const providerId = key;
143
+ const entries = this.accountEntries(providerId);
144
+ if (entries.length === 0) {
145
+ this.adoptSlot(providerId, slot);
146
+ changed = true;
147
+ continue;
148
+ }
149
+ const active = entries.find((entry) => entry.active);
150
+ if (!active) {
151
+ this.data[this.accountKeyFor(entries[0].id)] = {
152
+ ...entries[0],
153
+ active: true
154
+ };
155
+ changed = true;
156
+ continue;
157
+ }
158
+ if (active.refresh !== slot.refresh) {
159
+ const matching = entries.find((entry) => entry.id !== active.id && entry.refresh === slot.refresh);
160
+ if (matching) {
161
+ const { type: _t, ...slotCreds } = slot;
162
+ this.data[this.accountKeyFor(matching.id)] = {
163
+ ...matching,
164
+ ...slotCreds,
165
+ type: "oauth-account",
166
+ active: true
167
+ };
168
+ this.data[this.accountKeyFor(active.id)] = {
169
+ ...active,
170
+ active: false
171
+ };
172
+ changed = true;
173
+ } else {
174
+ const { type: _type, ...slotCreds } = slot;
175
+ this.data[this.accountKeyFor(active.id)] = {
176
+ ...active,
177
+ ...slotCreds,
178
+ type: "oauth-account"
179
+ };
180
+ changed = true;
181
+ }
182
+ }
183
+ }
184
+ const registeredProviders = /* @__PURE__ */ new Set();
185
+ for (const key of Object.keys(this.data)) {
186
+ if (!key.startsWith("accounts:")) continue;
187
+ const providerId = key.slice(9).split(":")[0];
188
+ if (providerId) registeredProviders.add(providerId);
189
+ }
190
+ for (const providerId of registeredProviders) {
191
+ if (this.data[providerId]?.type === "oauth") continue;
192
+ const entries = this.accountEntries(providerId);
193
+ if (entries.length === 0) continue;
194
+ let active = entries.find((entry) => entry.active);
195
+ if (!active) {
196
+ active = entries[0];
197
+ this.data[this.accountKeyFor(active.id)] = {
198
+ ...active,
199
+ active: true
200
+ };
201
+ }
202
+ this.data[providerId] = {
203
+ type: "oauth",
204
+ ...credentialFieldsOf(active)
205
+ };
206
+ changed = true;
207
+ }
208
+ for (const providerId of registeredProviders) {
209
+ const provider = getOAuthProvider(providerId);
210
+ if (!provider?.getAccountIdentity) continue;
211
+ for (const entry of this.accountEntries(providerId)) {
212
+ if (entry.identity) continue;
213
+ const identity = providerIdentity(provider, entry);
214
+ if (!identity) continue;
215
+ this.data[this.accountKeyFor(entry.id)] = {
216
+ ...entry,
217
+ identity
218
+ };
219
+ changed = true;
220
+ }
221
+ }
222
+ if (changed) this.save();
223
+ }
224
+ /**
225
+ * Save credentials to disk.
226
+ */
227
+ save() {
228
+ const dir = dirname(this.authPath);
229
+ if (!existsSync(dir)) mkdirSync(dir, {
230
+ recursive: true,
231
+ mode: 448
232
+ });
233
+ const tempPath = `${this.authPath}.${process.pid}.${globalThis.crypto.randomUUID()}.tmp`;
234
+ try {
235
+ writeFileSync(tempPath, JSON.stringify(this.data, null, 2), {
236
+ encoding: "utf-8",
237
+ mode: 384
238
+ });
239
+ chmodSync(tempPath, 384);
240
+ renameSync(tempPath, this.authPath);
241
+ } finally {
242
+ rmSync(tempPath, { force: true });
243
+ }
244
+ }
245
+ /**
246
+ * Get credential for a provider. Registry entries (type 'oauth-account')
247
+ * live under `accounts:` keys and never surface here.
248
+ */
249
+ get(provider) {
250
+ const cred = this.data[provider];
251
+ return cred !== void 0 && cred.type !== "oauth-account" ? cred : void 0;
252
+ }
253
+ /**
254
+ * Set credential for a provider.
255
+ */
256
+ set(provider, credential) {
257
+ this.reload();
258
+ this.data[provider] = credential;
259
+ this.save();
260
+ }
261
+ /**
262
+ * Remove credential for a provider, including its account registry.
263
+ * Clearing only the slot would leave the registry behind, and the next
264
+ * load's migration would heal the slot back from it — resurrecting the
265
+ * provider the caller just signed out.
266
+ */
267
+ remove(provider) {
268
+ this.reload();
269
+ delete this.data[provider];
270
+ const prefix = this.accountPrefixFor(provider);
271
+ for (const key of Object.keys(this.data)) if (key.startsWith(prefix)) delete this.data[key];
272
+ this.save();
273
+ }
274
+ /**
275
+ * List all providers with credentials.
276
+ */
277
+ list() {
278
+ return Object.keys(this.data);
279
+ }
280
+ /**
281
+ * Check if credentials exist for a provider.
282
+ */
283
+ has(provider) {
284
+ return provider in this.data;
285
+ }
286
+ /**
287
+ * Check if logged in via OAuth for a provider.
288
+ */
289
+ isLoggedIn(provider) {
290
+ return this.data[provider]?.type === "oauth";
291
+ }
292
+ /**
293
+ * Check if a stored API key exists for a provider.
294
+ * Keys are stored under `apikey:<provider>` in auth.json.
295
+ */
296
+ hasStoredApiKey(provider) {
297
+ const cred = this.data[`apikey:${provider}`];
298
+ return cred?.type === "api_key" && cred.key.length > 0;
299
+ }
300
+ /**
301
+ * Get a stored API key for a provider, if any.
302
+ */
303
+ getStoredApiKey(provider) {
304
+ const cred = this.data[`apikey:${provider}`];
305
+ return cred?.type === "api_key" && cred.key.length > 0 ? cred.key : void 0;
306
+ }
307
+ /**
308
+ * Store an API key for a provider.
309
+ * Also sets the corresponding environment variable so model resolution can find it.
310
+ */
311
+ setStoredApiKey(provider, key, envVar) {
312
+ this.set(`apikey:${provider}`, {
313
+ type: "api_key",
314
+ key
315
+ });
316
+ if (envVar) process.env[envVar] = key;
317
+ }
318
+ /**
319
+ * Load all stored API keys into process.env.
320
+ * Called at startup so model resolution can find stored keys.
321
+ * Only sets env vars that aren't already set (env vars take precedence).
322
+ */
323
+ loadStoredApiKeysIntoEnv(providerEnvVars) {
324
+ for (const [key, cred] of Object.entries(this.data)) {
325
+ if (!key.startsWith("apikey:") || cred.type !== "api_key" || !cred.key) continue;
326
+ const envVar = providerEnvVars[key.substring(7)];
327
+ if (envVar && !process.env[envVar]) process.env[envVar] = cred.key;
328
+ }
329
+ }
330
+ /**
331
+ * Login to an OAuth provider. Resolves to the registered account record.
332
+ * Pass `activate: false` to add the account without making it active (the
333
+ * first account for a provider is always activated — a provider with
334
+ * accounts must have an active one).
335
+ */
336
+ async login(providerId, callbacks, opts) {
337
+ const provider = getOAuthProvider(providerId);
338
+ if (!provider) throw new Error(`Unknown OAuth provider: ${providerId}`);
339
+ const credentials = await provider.login(callbacks);
340
+ return this.addAccount(providerId, credentials, opts);
341
+ }
342
+ /**
343
+ * Logout from a provider: remove the legacy slot and every registered account.
344
+ */
345
+ logout(provider) {
346
+ this.remove(provider);
347
+ }
348
+ accountPrefixFor(providerId) {
349
+ return `accounts:${providerId}:`;
350
+ }
351
+ accountKeyFor(id) {
352
+ return `accounts:${id}`;
353
+ }
354
+ /** Registry entries for a provider in JSON insertion order (malformed values skipped). */
355
+ accountEntries(providerId) {
356
+ const prefix = this.accountPrefixFor(providerId);
357
+ const out = [];
358
+ for (const [key, value] of Object.entries(this.data)) if (key.startsWith(prefix) && isOAuthAccountRecord(value)) out.push(value);
359
+ return out;
360
+ }
361
+ /** Adopt a legacy slot credential as the registry's first active entry. */
362
+ adoptSlot(providerId, slot) {
363
+ const id = mintAccountId(providerId);
364
+ const provider = getOAuthProvider(providerId);
365
+ const identity = providerIdentity(provider, slot);
366
+ const record = {
367
+ ...slot,
368
+ type: "oauth-account",
369
+ id,
370
+ ...identity ? { identity } : {},
371
+ label: `${provider?.name ?? providerId} account 1`,
372
+ addedAt: (/* @__PURE__ */ new Date()).toISOString(),
373
+ active: true
374
+ };
375
+ this.data[this.accountKeyFor(id)] = record;
376
+ return record;
377
+ }
378
+ /**
379
+ * Registered OAuth accounts for a provider, in insertion order. Copies —
380
+ * callers cannot corrupt unsaved storage state by mutating a record.
381
+ */
382
+ listAccounts(providerId) {
383
+ return this.accountEntries(providerId).map((entry) => ({ ...entry }));
384
+ }
385
+ /**
386
+ * The provider's active registry entry, if a registry exists. A copy.
387
+ */
388
+ getActiveAccount(providerId) {
389
+ const active = this.accountEntries(providerId).find((entry) => entry.active);
390
+ return active ? { ...active } : void 0;
391
+ }
392
+ /**
393
+ * Register OAuth credentials as an account for a provider, making it active
394
+ * unless `activate: false` is passed.
395
+ *
396
+ * With `replaceAccountId` (re-authentication of a picked account), the
397
+ * target entry's tokens are replaced in place — id re-keyed to the new
398
+ * refresh-token hash, label/position/addedAt preserved — because providers
399
+ * rotate refresh tokens per authorization, so the picked account's old id
400
+ * never matches the new token hash. Re-authentication keeps the target's
401
+ * active state: an inactive account stays inactive and the currently active
402
+ * account keeps the legacy slot. Otherwise, when the new credentials
403
+ * hash to an existing entry's id (same refresh token), that entry is
404
+ * updated in place; a genuinely new account is appended and activated.
405
+ *
406
+ * `activate: false` only applies to the plain add path: the new/updated entry
407
+ * keeps the active state it had, and the previously active account stays
408
+ * active. The first account of a provider is activated regardless.
409
+ */
410
+ async addAccount(providerId, creds, opts) {
411
+ this.reload();
412
+ const entries = this.accountEntries(providerId);
413
+ if (opts?.replaceAccountId) {
414
+ const target = entries.find((entry) => entry.id === opts.replaceAccountId);
415
+ if (!target) throw new Error(`No account ${opts.replaceAccountId} for provider ${providerId}`);
416
+ const identity = providerIdentity(getOAuthProvider(providerId), creds) ?? target.identity;
417
+ const legacyId = legacyAccountIdFor(providerId, creds.refresh);
418
+ const collided = entries.find((entry) => entry.id !== target.id && (identity !== void 0 && entry.identity === identity || entry.id === legacyId || entry.refresh === creds.refresh));
419
+ const replacement = {
420
+ ...target,
421
+ ...collided ? credentialFieldsOf(collided) : {},
422
+ ...creds,
423
+ type: "oauth-account",
424
+ ...identity ? { identity } : {},
425
+ label: opts.label ?? target.label
426
+ };
427
+ const rebuilt = {};
428
+ for (const [key, value] of Object.entries(this.data)) if (key === this.accountKeyFor(target.id)) rebuilt[key] = collided?.active ? {
429
+ ...replacement,
430
+ active: true
431
+ } : replacement;
432
+ else if (collided && key === this.accountKeyFor(collided.id)) continue;
433
+ else rebuilt[key] = value;
434
+ this.data = rebuilt;
435
+ if (target.active === true || collided?.active === true || entries.some((entry) => entry.active && entry.id !== target.id) === false) {
436
+ const activated = this.activateInMemory(providerId, target.id);
437
+ if (!activated) throw new Error(`Failed to activate account ${target.id} for provider ${providerId}`);
438
+ this.save();
439
+ return activated;
440
+ }
441
+ const replacementEntry = this.accountEntries(providerId).find((entry) => entry.id === target.id);
442
+ if (!replacementEntry) throw new Error(`Failed to store account ${target.id} for provider ${providerId}`);
443
+ this.save();
444
+ return replacementEntry;
445
+ }
446
+ const provider = getOAuthProvider(providerId);
447
+ const identity = providerIdentity(provider, creds);
448
+ const label = opts?.label ?? await provider?.getAccountLabel?.(creds) ?? null;
449
+ this.reload();
450
+ const freshEntries = this.accountEntries(providerId);
451
+ const legacyId = legacyAccountIdFor(providerId, creds.refresh);
452
+ const existing = (identity ? freshEntries.find((entry) => entry.identity === identity) : void 0) ?? freshEntries.find((entry) => entry.id === legacyId) ?? freshEntries.find((entry) => entry.refresh === creds.refresh);
453
+ const id = existing?.id ?? mintAccountId(providerId);
454
+ if (existing) this.data[this.accountKeyFor(id)] = {
455
+ ...existing,
456
+ ...creds,
457
+ type: "oauth-account",
458
+ id,
459
+ ...identity ? { identity } : {},
460
+ active: existing.active
461
+ };
462
+ else {
463
+ const resolvedLabel = label ?? `${provider?.name ?? providerId} account ${freshEntries.length + 1}`;
464
+ this.data[this.accountKeyFor(id)] = {
465
+ type: "oauth-account",
466
+ id,
467
+ ...identity ? { identity } : {},
468
+ label: resolvedLabel,
469
+ addedAt: (/* @__PURE__ */ new Date()).toISOString(),
470
+ active: false,
471
+ ...creds
472
+ };
473
+ }
474
+ if (opts?.activate === false && freshEntries.length > 0 && !existing?.active) {
475
+ this.save();
476
+ return { ...this.data[this.accountKeyFor(id)] };
477
+ }
478
+ const activated = this.activateInMemory(providerId, id);
479
+ if (!activated) throw new Error(`Failed to activate account ${id} for provider ${providerId}`);
480
+ this.save();
481
+ return activated;
482
+ }
483
+ /**
484
+ * Activate an account: move the active tokens out of the legacy slot back
485
+ * into the previously active entry, and the target's tokens into the slot
486
+ * (never copy — one home per token set). With no `instanceId`, rotate to
487
+ * the next entry in insertion order, wrapping once to the front; returns
488
+ * undefined when there is no other entry to rotate to.
489
+ */
490
+ activateAccount(providerId, instanceId) {
491
+ this.reload();
492
+ const activated = this.activateInMemory(providerId, instanceId);
493
+ if (activated) this.save();
494
+ return activated;
495
+ }
496
+ /**
497
+ * Activate an account in memory only — the caller owns reload/save. Move the
498
+ * active tokens out of the legacy slot back into the previously active
499
+ * entry, and the target's tokens into the slot (never copy — one home per
500
+ * token set). With no `instanceId`, rotate to the next entry in insertion
501
+ * order, wrapping once to the front; returns undefined when there is no
502
+ * other entry to rotate to.
503
+ */
504
+ activateInMemory(providerId, instanceId) {
505
+ const entries = this.accountEntries(providerId);
506
+ if (entries.length === 0) return void 0;
507
+ let target;
508
+ if (instanceId !== void 0) {
509
+ target = entries.find((entry) => entry.id === instanceId);
510
+ if (!target) return void 0;
511
+ } else {
512
+ if (entries.length <= 1) return void 0;
513
+ const currentIdx = entries.findIndex((entry) => entry.active);
514
+ target = (currentIdx >= 0 ? [...entries.slice(currentIdx + 1), ...entries.slice(0, currentIdx)] : [...entries])[0];
515
+ }
516
+ if (!target) return void 0;
517
+ const slot = this.data[providerId];
518
+ const current = entries.find((entry) => entry.active && entry.id !== target.id);
519
+ if (current && slot?.type === "oauth") {
520
+ const { type: _type, ...slotCreds } = slot;
521
+ this.data[this.accountKeyFor(current.id)] = {
522
+ ...current,
523
+ ...slotCreds,
524
+ type: "oauth-account",
525
+ active: false
526
+ };
527
+ }
528
+ this.data[providerId] = {
529
+ ...credentialFieldsOf(target),
530
+ type: "oauth"
531
+ };
532
+ this.data[this.accountKeyFor(target.id)] = {
533
+ ...target,
534
+ active: true
535
+ };
536
+ for (const entry of entries) {
537
+ if (entry.id === target.id || entry.id === current?.id) continue;
538
+ if (entry.active) this.data[this.accountKeyFor(entry.id)] = {
539
+ ...entry,
540
+ active: false
541
+ };
542
+ }
543
+ return {
544
+ ...target,
545
+ active: true
546
+ };
547
+ }
548
+ /**
549
+ * Remove an account from the registry. If it was active, the next entry in
550
+ * insertion order is activated; when it was the last one, the legacy slot
551
+ * is removed too (full sign-out for that provider).
552
+ */
553
+ removeAccount(providerId, instanceId) {
554
+ this.reload();
555
+ const entries = this.accountEntries(providerId);
556
+ const target = entries.find((entry) => entry.id === instanceId);
557
+ if (!target) return;
558
+ const wasActive = target.active;
559
+ delete this.data[this.accountKeyFor(instanceId)];
560
+ if (wasActive) {
561
+ const next = entries[entries.indexOf(target) + 1] ?? entries.find((entry) => entry.id !== instanceId);
562
+ if (next) {
563
+ this.data[providerId] = {
564
+ type: "oauth",
565
+ ...credentialFieldsOf(next)
566
+ };
567
+ this.data[this.accountKeyFor(next.id)] = {
568
+ ...next,
569
+ active: true
570
+ };
571
+ for (const entry of entries) {
572
+ if (entry.id === next.id || entry.id === instanceId) continue;
573
+ if (entry.active) this.data[this.accountKeyFor(entry.id)] = {
574
+ ...entry,
575
+ active: false
576
+ };
577
+ }
578
+ } else delete this.data[providerId];
579
+ }
580
+ this.save();
581
+ }
582
+ /**
583
+ * Rename an account's label.
584
+ */
585
+ renameAccount(providerId, instanceId, label) {
586
+ this.reload();
587
+ const key = this.accountKeyFor(instanceId);
588
+ const entry = this.data[key];
589
+ if (!isOAuthAccountRecord(entry)) return;
590
+ this.data[key] = {
591
+ ...entry,
592
+ label
593
+ };
594
+ this.save();
595
+ }
596
+ /**
597
+ * Persist refreshed credentials for the account that initiated the refresh.
598
+ * The legacy slot is updated only if that account is still active when the
599
+ * refresh completes; otherwise a delayed refresh could overwrite a newer
600
+ * activation.
601
+ */
602
+ persistRefreshedCredential(providerId, instanceId, creds) {
603
+ this.reload();
604
+ if (instanceId) {
605
+ const key = this.accountKeyFor(instanceId);
606
+ const entry = this.data[key];
607
+ if (!isOAuthAccountRecord(entry)) return;
608
+ this.data[key] = {
609
+ ...entry,
610
+ ...creds,
611
+ type: "oauth-account"
612
+ };
613
+ if (entry.active) {
614
+ const slot = this.data[providerId];
615
+ this.data[providerId] = slot?.type === "oauth" ? {
616
+ ...slot,
617
+ ...creds,
618
+ type: "oauth"
619
+ } : {
620
+ type: "oauth",
621
+ ...creds
622
+ };
623
+ }
624
+ } else {
625
+ const slot = this.data[providerId];
626
+ this.data[providerId] = slot?.type === "oauth" ? {
627
+ ...slot,
628
+ ...creds,
629
+ type: "oauth"
630
+ } : {
631
+ type: "oauth",
632
+ ...creds
633
+ };
634
+ }
635
+ this.save();
636
+ }
637
+ /**
638
+ * Refresh one account instance through the per-instance dedupe map. Used
639
+ * for sibling refreshes during the rotation walk, so a concurrent
640
+ * `getApiKey` that reloads storage mid-walk (after the candidate was
641
+ * activated but before its refresh resolves) joins the same refresh
642
+ * instead of double-spending a single-use refresh token.
643
+ */
644
+ async refreshInstance(providerId, instanceId, creds) {
645
+ const provider = getOAuthProvider(providerId);
646
+ if (!provider) return void 0;
647
+ const refreshKey = `${providerId}:${instanceId}`;
648
+ const pending = this.refreshPromises.get(refreshKey);
649
+ if (pending) return pending;
650
+ const refresh = (async () => {
651
+ try {
652
+ const fresh = await provider.refreshToken(creds);
653
+ this.persistRefreshedCredential(providerId, instanceId, fresh);
654
+ return fresh;
655
+ } catch {
656
+ return;
657
+ }
658
+ })();
659
+ this.refreshPromises.set(refreshKey, refresh);
660
+ try {
661
+ return await refresh;
662
+ } finally {
663
+ this.refreshPromises.delete(refreshKey);
664
+ }
665
+ }
666
+ /**
667
+ * Get a ready-to-use OAuth credential snapshot, auto-refreshing if needed.
668
+ * A failed refresh leaves the active account unchanged and returns undefined;
669
+ * the account-rotation processor owns switching so it can persist a visible
670
+ * account-switch notice with the request that triggered the change.
671
+ */
672
+ async getOAuthCredential(providerId, accountInstanceId) {
673
+ this.reload();
674
+ const activeEntry = this.getActiveAccount(providerId);
675
+ const selectedEntry = accountInstanceId ? this.accountEntries(providerId).find((entry) => entry.id === accountInstanceId) : activeEntry;
676
+ const slot = this.data[providerId];
677
+ let credential;
678
+ if (selectedEntry) {
679
+ const { type: _type, id: _id, label: _label, addedAt: _addedAt, active: _active, ...accountCredential } = selectedEntry;
680
+ credential = accountCredential;
681
+ if (selectedEntry.active && slot?.type === "oauth" && slot.refresh === selectedEntry.refresh) {
682
+ const { type: _slotType, ...slotCredential } = slot;
683
+ credential = {
684
+ ...credential,
685
+ ...slotCredential
686
+ };
687
+ }
688
+ } else if (!accountInstanceId && slot?.type === "oauth") credential = slot;
689
+ if (!credential) return void 0;
690
+ const provider = getOAuthProvider(providerId);
691
+ if (!provider) return void 0;
692
+ const selectedInstanceId = selectedEntry?.id;
693
+ const toSnapshot = (credentials) => ({
694
+ type: "oauth",
695
+ ...credentials,
696
+ accountInstanceId: selectedInstanceId
697
+ });
698
+ if (Date.now() < credential.expires) return toSnapshot(credential);
699
+ if (selectedInstanceId) {
700
+ const refreshed = await this.refreshInstance(providerId, selectedInstanceId, credential);
701
+ return refreshed ? toSnapshot(refreshed) : void 0;
702
+ }
703
+ const pendingRefresh = this.refreshPromises.get(providerId);
704
+ const refresh = pendingRefresh ?? (async () => {
705
+ try {
706
+ const fresh = await provider.refreshToken(credential);
707
+ this.persistRefreshedCredential(providerId, void 0, fresh);
708
+ return fresh;
709
+ } catch {
710
+ return;
711
+ }
712
+ })();
713
+ if (!pendingRefresh) this.refreshPromises.set(providerId, refresh);
714
+ try {
715
+ const refreshed = await refresh;
716
+ return refreshed ? toSnapshot(refreshed) : void 0;
717
+ } finally {
718
+ if (!pendingRefresh) this.refreshPromises.delete(providerId);
719
+ }
720
+ }
721
+ /** Get API key for a provider, refreshing OAuth tokens if needed. */
722
+ async getApiKey(providerId, accountInstanceId) {
723
+ this.reload();
724
+ const cred = this.data[providerId];
725
+ if (accountInstanceId === void 0 && cred?.type === "api_key") return cred.key;
726
+ const oauth = await this.getOAuthCredential(providerId, accountInstanceId);
727
+ const provider = oauth ? getOAuthProvider(providerId) : void 0;
728
+ return oauth && provider ? provider.getApiKey(oauth) : void 0;
729
+ }
730
+ /**
731
+ * Force one refresh of the active OAuth account's tokens, regardless of
732
+ * expiry. The account-rotation error processor calls this when a provider
733
+ * rejects a not-yet-expired token (401/403) — server-side clock skew and
734
+ * refresh-token races surface that way. Returns the fresh access token, or
735
+ * undefined when the refresh fails or there is no OAuth credential.
736
+ * Shares the per-instance refresh dedupe with `getApiKey`.
737
+ */
738
+ async forceRefreshActiveAccount(providerId, accountInstanceId) {
739
+ this.reload();
740
+ const activeEntry = this.getActiveAccount(providerId);
741
+ const selectedEntry = accountInstanceId ? this.accountEntries(providerId).find((entry) => entry.id === accountInstanceId) : activeEntry;
742
+ const slot = this.get(providerId);
743
+ let cred;
744
+ if (selectedEntry) {
745
+ const { type: _type, id: _id, label: _label, addedAt: _addedAt, active: _active, ...accountCredential } = selectedEntry;
746
+ cred = accountCredential;
747
+ } else if (!accountInstanceId && slot?.type === "oauth") cred = slot;
748
+ if (!cred) return void 0;
749
+ const provider = getOAuthProvider(providerId);
750
+ if (!provider) return void 0;
751
+ const refreshKey = selectedEntry ? `${providerId}:${selectedEntry.id}` : providerId;
752
+ const pending = this.refreshPromises.get(refreshKey);
753
+ const refresh = pending ?? (async () => {
754
+ try {
755
+ const fresh = await provider.refreshToken(cred);
756
+ this.persistRefreshedCredential(providerId, selectedEntry?.id, fresh);
757
+ return fresh;
758
+ } catch {
759
+ return;
760
+ }
761
+ })();
762
+ if (!pending) this.refreshPromises.set(refreshKey, refresh);
763
+ try {
764
+ const creds = await refresh;
765
+ return creds ? provider.getApiKey(creds) : void 0;
766
+ } finally {
767
+ if (!pending) this.refreshPromises.delete(refreshKey);
768
+ }
769
+ }
770
+ };
771
+ //#endregion
772
+ export { AuthStorage, PROVIDER_DEFAULT_MODELS, getOAuthProvider, getOAuthProviders };
773
+
774
+ //# sourceMappingURL=storage.js.map