@alumbwe/anvil 1.0.20 → 1.0.22

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 (328) hide show
  1. package/package.json +74 -70
  2. package/src/agents/bundled-agents.generated.ts +4 -4
  3. package/src/chat.tsx +1703 -1698
  4. package/src/cli-args.ts +134 -134
  5. package/src/components/status-bar.tsx +331 -313
  6. package/src/hooks/helpers/send-message.ts +663 -654
  7. package/src/hooks/use-send-message.ts +806 -773
  8. package/src/index.tsx +473 -473
  9. package/src/state/chat-store.ts +553 -544
  10. package/src/utils/create-run-config.ts +121 -121
  11. package/src/utils/format-compact-number.ts +37 -0
  12. package/vendor/@anvil/common/package.json +27 -0
  13. package/vendor/@anvil/common/src/__tests__/agent-validation.test.ts +846 -0
  14. package/vendor/@anvil/common/src/__tests__/anvil-model-availability.test.ts +101 -0
  15. package/vendor/@anvil/common/src/__tests__/anvil-models.test.ts +1408 -0
  16. package/vendor/@anvil/common/src/__tests__/anvil-public-data-use-copy.test.ts +258 -0
  17. package/vendor/@anvil/common/src/__tests__/anvil-referral-tiers.test.ts +48 -0
  18. package/vendor/@anvil/common/src/__tests__/anvil-spend-ceilings.test.ts +282 -0
  19. package/vendor/@anvil/common/src/__tests__/anvil-trust.test.ts +526 -0
  20. package/vendor/@anvil/common/src/__tests__/deepseek-direct.test.ts +68 -0
  21. package/vendor/@anvil/common/src/__tests__/disposable-email.test.ts +132 -0
  22. package/vendor/@anvil/common/src/__tests__/dynamic-agent-template-schema.test.ts +422 -0
  23. package/vendor/@anvil/common/src/__tests__/env-ci.test.ts +167 -0
  24. package/vendor/@anvil/common/src/__tests__/env-process.test.ts +145 -0
  25. package/vendor/@anvil/common/src/__tests__/foreign-client-shipped-agents.test.ts +234 -0
  26. package/vendor/@anvil/common/src/__tests__/foreign-client-signals.test.ts +379 -0
  27. package/vendor/@anvil/common/src/__tests__/free-agents.test.ts +807 -0
  28. package/vendor/@anvil/common/src/__tests__/gravity-capi.test.ts +154 -0
  29. package/vendor/@anvil/common/src/__tests__/handlesteps-parsing.test.ts +246 -0
  30. package/vendor/@anvil/common/src/__tests__/kimi-k3-god-only.test.ts +77 -0
  31. package/vendor/@anvil/common/src/__tests__/model-config.test.ts +89 -0
  32. package/vendor/@anvil/common/src/__tests__/project-file-tree.test.ts +298 -0
  33. package/vendor/@anvil/common/src/__tests__/provisioned-model-tiers.test.ts +116 -0
  34. package/vendor/@anvil/common/src/__tests__/reasoning-effort.test.ts +167 -0
  35. package/vendor/@anvil/common/src/__tests__/reddit-capi.test.ts +132 -0
  36. package/vendor/@anvil/common/src/__tests__/response-ad-positions.test.ts +72 -0
  37. package/vendor/@anvil/common/src/__tests__/user-state.test.ts +30 -0
  38. package/vendor/@anvil/common/src/actions.ts +215 -0
  39. package/vendor/@anvil/common/src/analytics-core.ts +69 -0
  40. package/vendor/@anvil/common/src/analytics.ts +93 -0
  41. package/vendor/@anvil/common/src/api-keys/constants.ts +26 -0
  42. package/vendor/@anvil/common/src/browser-actions.ts +413 -0
  43. package/vendor/@anvil/common/src/constants/__tests__/anvil-onboarding-gate.test.ts +63 -0
  44. package/vendor/@anvil/common/src/constants/__tests__/anvil-onboarding.test.ts +246 -0
  45. package/vendor/@anvil/common/src/constants/__tests__/cf-worker-signals.test.ts +106 -0
  46. package/vendor/@anvil/common/src/constants/agents.ts +99 -0
  47. package/vendor/@anvil/common/src/constants/analytics-events.ts +435 -0
  48. package/vendor/@anvil/common/src/constants/anthropic.ts +73 -0
  49. package/vendor/@anvil/common/src/constants/anvil-data-use.ts +81 -0
  50. package/vendor/@anvil/common/src/constants/anvil-errors.ts +7 -0
  51. package/vendor/@anvil/common/src/constants/anvil-gemini-thinker.ts +21 -0
  52. package/vendor/@anvil/common/src/constants/anvil-model-ids.ts +11 -0
  53. package/vendor/@anvil/common/src/constants/anvil-models.ts +2130 -0
  54. package/vendor/@anvil/common/src/constants/anvil-onboarding-gate.ts +55 -0
  55. package/vendor/@anvil/common/src/constants/anvil-onboarding.ts +307 -0
  56. package/vendor/@anvil/common/src/constants/anvil-referral-tiers.ts +87 -0
  57. package/vendor/@anvil/common/src/constants/anvil-signup-block.ts +68 -0
  58. package/vendor/@anvil/common/src/constants/anvil-spend-ceilings.ts +450 -0
  59. package/vendor/@anvil/common/src/constants/anvil-trust.ts +976 -0
  60. package/vendor/@anvil/common/src/constants/auth.ts +18 -0
  61. package/vendor/@anvil/common/src/constants/byok.ts +2 -0
  62. package/vendor/@anvil/common/src/constants/cf-worker-signals.ts +132 -0
  63. package/vendor/@anvil/common/src/constants/composio.ts +34 -0
  64. package/vendor/@anvil/common/src/constants/deepseek-direct.ts +55 -0
  65. package/vendor/@anvil/common/src/constants/feedback.ts +13 -0
  66. package/vendor/@anvil/common/src/constants/foreign-client-signals.ts +272 -0
  67. package/vendor/@anvil/common/src/constants/free-agents.ts +777 -0
  68. package/vendor/@anvil/common/src/constants/gemini.ts +15 -0
  69. package/vendor/@anvil/common/src/constants/grant-priorities.ts +13 -0
  70. package/vendor/@anvil/common/src/constants/hosts.ts +6 -0
  71. package/vendor/@anvil/common/src/constants/images.ts +51 -0
  72. package/vendor/@anvil/common/src/constants/index.ts +7 -0
  73. package/vendor/@anvil/common/src/constants/knowledge.ts +35 -0
  74. package/vendor/@anvil/common/src/constants/limits.ts +23 -0
  75. package/vendor/@anvil/common/src/constants/model-config.ts +277 -0
  76. package/vendor/@anvil/common/src/constants/openrouter-attribution.ts +8 -0
  77. package/vendor/@anvil/common/src/constants/paths.ts +69 -0
  78. package/vendor/@anvil/common/src/constants/provider-routes.ts +317 -0
  79. package/vendor/@anvil/common/src/constants/reasoning-effort.ts +79 -0
  80. package/vendor/@anvil/common/src/constants/skills.ts +60 -0
  81. package/vendor/@anvil/common/src/constants/subscription-plans.ts +49 -0
  82. package/vendor/@anvil/common/src/constants/ui.ts +25 -0
  83. package/vendor/@anvil/common/src/env-ci.ts +36 -0
  84. package/vendor/@anvil/common/src/env-process.ts +95 -0
  85. package/vendor/@anvil/common/src/env-schema.ts +95 -0
  86. package/vendor/@anvil/common/src/env.ts +24 -0
  87. package/vendor/@anvil/common/src/gravity-capi.ts +207 -0
  88. package/vendor/@anvil/common/src/mcp/client.ts +233 -0
  89. package/vendor/@anvil/common/src/old-constants.ts +10 -0
  90. package/vendor/@anvil/common/src/project-file-tree.ts +355 -0
  91. package/vendor/@anvil/common/src/reddit-capi.ts +254 -0
  92. package/vendor/@anvil/common/src/schemas/feedback.ts +52 -0
  93. package/vendor/@anvil/common/src/schemas/logs.ts +66 -0
  94. package/vendor/@anvil/common/src/templates/agent-validation.ts +392 -0
  95. package/vendor/@anvil/common/src/templates/initial-agents-dir/LICENSE +202 -0
  96. package/vendor/@anvil/common/src/templates/initial-agents-dir/README.md +294 -0
  97. package/vendor/@anvil/common/src/templates/initial-agents-dir/examples/01-basic-diff-reviewer.ts +17 -0
  98. package/vendor/@anvil/common/src/templates/initial-agents-dir/examples/02-intermediate-git-committer.ts +78 -0
  99. package/vendor/@anvil/common/src/templates/initial-agents-dir/examples/03-advanced-file-explorer.ts +73 -0
  100. package/vendor/@anvil/common/src/templates/initial-agents-dir/my-custom-agent.ts +40 -0
  101. package/vendor/@anvil/common/src/templates/initial-agents-dir/package.json +6 -0
  102. package/vendor/@anvil/common/src/templates/initial-agents-dir/skills/README.md +65 -0
  103. package/vendor/@anvil/common/src/templates/initial-agents-dir/skills/example-skill/SKILL.md +29 -0
  104. package/vendor/@anvil/common/src/templates/initial-agents-dir/types/agent-definition.ts +497 -0
  105. package/vendor/@anvil/common/src/templates/initial-agents-dir/types/tools.ts +444 -0
  106. package/vendor/@anvil/common/src/templates/initial-agents-dir/types/util-types.ts +178 -0
  107. package/vendor/@anvil/common/src/testing/TESTING_PATTERNS.md +351 -0
  108. package/vendor/@anvil/common/src/testing/anvil-offer-invariants.ts +169 -0
  109. package/vendor/@anvil/common/src/testing/errors.ts +33 -0
  110. package/vendor/@anvil/common/src/testing/fixtures/agent-runtime.ts +334 -0
  111. package/vendor/@anvil/common/src/testing/impl/agent-runtime.ts +6 -0
  112. package/vendor/@anvil/common/src/testing/index.ts +84 -0
  113. package/vendor/@anvil/common/src/testing/mock-modules.ts +53 -0
  114. package/vendor/@anvil/common/src/testing/mock-types.ts +123 -0
  115. package/vendor/@anvil/common/src/testing/mocks/analytics.ts +261 -0
  116. package/vendor/@anvil/common/src/testing/mocks/child-process.ts +93 -0
  117. package/vendor/@anvil/common/src/testing/mocks/crypto.ts +218 -0
  118. package/vendor/@anvil/common/src/testing/mocks/database.ts +337 -0
  119. package/vendor/@anvil/common/src/testing/mocks/fetch.ts +219 -0
  120. package/vendor/@anvil/common/src/testing/mocks/filesystem.ts +166 -0
  121. package/vendor/@anvil/common/src/testing/mocks/index.ts +101 -0
  122. package/vendor/@anvil/common/src/testing/mocks/logger.ts +135 -0
  123. package/vendor/@anvil/common/src/testing/mocks/stream.ts +313 -0
  124. package/vendor/@anvil/common/src/testing/mocks/timers.ts +132 -0
  125. package/vendor/@anvil/common/src/testing/mocks/tree-sitter.ts +127 -0
  126. package/vendor/@anvil/common/src/testing/setup.ts +282 -0
  127. package/vendor/@anvil/common/src/testing-env-ci.ts +15 -0
  128. package/vendor/@anvil/common/src/testing-env-process.ts +78 -0
  129. package/vendor/@anvil/common/src/tools/__tests__/compile-tool-definitions.test.ts +34 -0
  130. package/vendor/@anvil/common/src/tools/compile-tool-definitions.ts +157 -0
  131. package/vendor/@anvil/common/src/tools/constants.ts +117 -0
  132. package/vendor/@anvil/common/src/tools/list.ts +190 -0
  133. package/vendor/@anvil/common/src/tools/params/__tests__/coerce-to-array.test.ts +213 -0
  134. package/vendor/@anvil/common/src/tools/params/__tests__/read-docs.test.ts +47 -0
  135. package/vendor/@anvil/common/src/tools/params/__tests__/render-ui.test.ts +65 -0
  136. package/vendor/@anvil/common/src/tools/params/tool/__tests__/run-terminal-command-timeout.test.ts +49 -0
  137. package/vendor/@anvil/common/src/tools/params/tool/add-message.ts +39 -0
  138. package/vendor/@anvil/common/src/tools/params/tool/add-subgoal.ts +57 -0
  139. package/vendor/@anvil/common/src/tools/params/tool/apply-patch.ts +110 -0
  140. package/vendor/@anvil/common/src/tools/params/tool/ask-user.ts +181 -0
  141. package/vendor/@anvil/common/src/tools/params/tool/browser-logs.ts +85 -0
  142. package/vendor/@anvil/common/src/tools/params/tool/cloud-plan-ready.ts +94 -0
  143. package/vendor/@anvil/common/src/tools/params/tool/code-search.ts +159 -0
  144. package/vendor/@anvil/common/src/tools/params/tool/composio.ts +131 -0
  145. package/vendor/@anvil/common/src/tools/params/tool/create-plan.ts +80 -0
  146. package/vendor/@anvil/common/src/tools/params/tool/end-turn.ts +57 -0
  147. package/vendor/@anvil/common/src/tools/params/tool/find-files.ts +60 -0
  148. package/vendor/@anvil/common/src/tools/params/tool/glob.ts +80 -0
  149. package/vendor/@anvil/common/src/tools/params/tool/gravity-index.ts +93 -0
  150. package/vendor/@anvil/common/src/tools/params/tool/list-directory.ts +58 -0
  151. package/vendor/@anvil/common/src/tools/params/tool/lookup-agent-info.ts +37 -0
  152. package/vendor/@anvil/common/src/tools/params/tool/propose-str-replace.ts +103 -0
  153. package/vendor/@anvil/common/src/tools/params/tool/propose-write-file.ts +71 -0
  154. package/vendor/@anvil/common/src/tools/params/tool/read-docs.ts +90 -0
  155. package/vendor/@anvil/common/src/tools/params/tool/read-files.ts +109 -0
  156. package/vendor/@anvil/common/src/tools/params/tool/read-subtree.ts +79 -0
  157. package/vendor/@anvil/common/src/tools/params/tool/read-url.ts +81 -0
  158. package/vendor/@anvil/common/src/tools/params/tool/render-ui.ts +168 -0
  159. package/vendor/@anvil/common/src/tools/params/tool/run-file-change-hooks.ts +57 -0
  160. package/vendor/@anvil/common/src/tools/params/tool/run-terminal-command.ts +211 -0
  161. package/vendor/@anvil/common/src/tools/params/tool/screenshot.ts +97 -0
  162. package/vendor/@anvil/common/src/tools/params/tool/set-messages.ts +44 -0
  163. package/vendor/@anvil/common/src/tools/params/tool/set-output.ts +61 -0
  164. package/vendor/@anvil/common/src/tools/params/tool/skill.ts +59 -0
  165. package/vendor/@anvil/common/src/tools/params/tool/spawn-agent-inline.ts +56 -0
  166. package/vendor/@anvil/common/src/tools/params/tool/spawn-agents.ts +154 -0
  167. package/vendor/@anvil/common/src/tools/params/tool/str-replace.ts +107 -0
  168. package/vendor/@anvil/common/src/tools/params/tool/suggest-followups.ts +97 -0
  169. package/vendor/@anvil/common/src/tools/params/tool/task-completed.ts +61 -0
  170. package/vendor/@anvil/common/src/tools/params/tool/think-deeply.ts +56 -0
  171. package/vendor/@anvil/common/src/tools/params/tool/update-subgoal.ts +89 -0
  172. package/vendor/@anvil/common/src/tools/params/tool/vision-analyze.ts +82 -0
  173. package/vendor/@anvil/common/src/tools/params/tool/web-search.ts +73 -0
  174. package/vendor/@anvil/common/src/tools/params/tool/write-file.ts +71 -0
  175. package/vendor/@anvil/common/src/tools/params/tool/write-todos.ts +67 -0
  176. package/vendor/@anvil/common/src/tools/params/utils.ts +150 -0
  177. package/vendor/@anvil/common/src/tools/utils.ts +24 -0
  178. package/vendor/@anvil/common/src/types/__tests__/dynamic-agent-template.test.ts +20 -0
  179. package/vendor/@anvil/common/src/types/agent-template.ts +215 -0
  180. package/vendor/@anvil/common/src/types/anvil-session.ts +535 -0
  181. package/vendor/@anvil/common/src/types/anvil-streak.ts +6 -0
  182. package/vendor/@anvil/common/src/types/anvil-usage.ts +64 -0
  183. package/vendor/@anvil/common/src/types/api/agents/publish.ts +61 -0
  184. package/vendor/@anvil/common/src/types/bun-test.d.ts +5 -0
  185. package/vendor/@anvil/common/src/types/contracts/agent-runtime.ts +75 -0
  186. package/vendor/@anvil/common/src/types/contracts/analytics.ts +9 -0
  187. package/vendor/@anvil/common/src/types/contracts/bigquery.ts +55 -0
  188. package/vendor/@anvil/common/src/types/contracts/billing.ts +46 -0
  189. package/vendor/@anvil/common/src/types/contracts/client.ts +53 -0
  190. package/vendor/@anvil/common/src/types/contracts/database.ts +112 -0
  191. package/vendor/@anvil/common/src/types/contracts/env.ts +203 -0
  192. package/vendor/@anvil/common/src/types/contracts/llm.ts +191 -0
  193. package/vendor/@anvil/common/src/types/contracts/logger.ts +14 -0
  194. package/vendor/@anvil/common/src/types/contracts/logs.ts +34 -0
  195. package/vendor/@anvil/common/src/types/contracts/trace.ts +21 -0
  196. package/vendor/@anvil/common/src/types/dynamic-agent-template.ts +328 -0
  197. package/vendor/@anvil/common/src/types/filesystem.ts +10 -0
  198. package/vendor/@anvil/common/src/types/function-params.ts +33 -0
  199. package/vendor/@anvil/common/src/types/grant.ts +20 -0
  200. package/vendor/@anvil/common/src/types/gravity-index.ts +170 -0
  201. package/vendor/@anvil/common/src/types/json.ts +29 -0
  202. package/vendor/@anvil/common/src/types/mcp.ts +24 -0
  203. package/vendor/@anvil/common/src/types/messages/anvil-message.ts +58 -0
  204. package/vendor/@anvil/common/src/types/messages/content-part.ts +59 -0
  205. package/vendor/@anvil/common/src/types/messages/data-content.ts +14 -0
  206. package/vendor/@anvil/common/src/types/messages/provider-metadata.ts +13 -0
  207. package/vendor/@anvil/common/src/types/organization.ts +118 -0
  208. package/vendor/@anvil/common/src/types/print-mode.ts +121 -0
  209. package/vendor/@anvil/common/src/types/publisher.ts +67 -0
  210. package/vendor/@anvil/common/src/types/session-state.ts +149 -0
  211. package/vendor/@anvil/common/src/types/skill.ts +86 -0
  212. package/vendor/@anvil/common/src/types/source.ts +11 -0
  213. package/vendor/@anvil/common/src/types/spawn.ts +13 -0
  214. package/vendor/@anvil/common/src/types/subscription.ts +67 -0
  215. package/vendor/@anvil/common/src/types/usage.ts +16 -0
  216. package/vendor/@anvil/common/src/types/util.ts +3 -0
  217. package/vendor/@anvil/common/src/util/__tests__/ad-user-agent.test.ts +26 -0
  218. package/vendor/@anvil/common/src/util/__tests__/analytics-dispatcher.test.ts +122 -0
  219. package/vendor/@anvil/common/src/util/__tests__/analytics-log.test.ts +102 -0
  220. package/vendor/@anvil/common/src/util/__tests__/analytics-sampling.test.ts +143 -0
  221. package/vendor/@anvil/common/src/util/__tests__/anvil-streak.test.ts +202 -0
  222. package/vendor/@anvil/common/src/util/__tests__/anvil-usage-summary.test.ts +193 -0
  223. package/vendor/@anvil/common/src/util/__tests__/axiom-only-log.test.ts +140 -0
  224. package/vendor/@anvil/common/src/util/__tests__/client-user-agent.test.ts +148 -0
  225. package/vendor/@anvil/common/src/util/__tests__/engagement-tracker.test.ts +115 -0
  226. package/vendor/@anvil/common/src/util/__tests__/env-file-path.test.ts +57 -0
  227. package/vendor/@anvil/common/src/util/__tests__/error-abort.test.ts +774 -0
  228. package/vendor/@anvil/common/src/util/__tests__/error-api-details.test.ts +206 -0
  229. package/vendor/@anvil/common/src/util/__tests__/file-read-limits.test.ts +173 -0
  230. package/vendor/@anvil/common/src/util/__tests__/format-code-search.test.ts +60 -0
  231. package/vendor/@anvil/common/src/util/__tests__/log-mirror.test.ts +31 -0
  232. package/vendor/@anvil/common/src/util/__tests__/messages.test.ts +1252 -0
  233. package/vendor/@anvil/common/src/util/__tests__/partial-json-delta.test.ts +505 -0
  234. package/vendor/@anvil/common/src/util/__tests__/path.test.ts +22 -0
  235. package/vendor/@anvil/common/src/util/__tests__/project-ignore.test.ts +90 -0
  236. package/vendor/@anvil/common/src/util/__tests__/promise.test.ts +325 -0
  237. package/vendor/@anvil/common/src/util/__tests__/rate-limit.test.ts +28 -0
  238. package/vendor/@anvil/common/src/util/__tests__/reddit-anvil-retention.test.ts +130 -0
  239. package/vendor/@anvil/common/src/util/__tests__/saxy.test.ts +1008 -0
  240. package/vendor/@anvil/common/src/util/__tests__/split-data.test.ts +289 -0
  241. package/vendor/@anvil/common/src/util/__tests__/string.test.ts +239 -0
  242. package/vendor/@anvil/common/src/util/__tests__/thread-title.test.ts +70 -0
  243. package/vendor/@anvil/common/src/util/__tests__/tool-result-media-order.test.ts +108 -0
  244. package/vendor/@anvil/common/src/util/__tests__/ttft-histogram.test.ts +180 -0
  245. package/vendor/@anvil/common/src/util/__tests__/with-timeout.test.ts +24 -0
  246. package/vendor/@anvil/common/src/util/__tests__/zoned-time.test.ts +88 -0
  247. package/vendor/@anvil/common/src/util/ad-user-agent.ts +25 -0
  248. package/vendor/@anvil/common/src/util/agent-file-utils.ts +110 -0
  249. package/vendor/@anvil/common/src/util/agent-id-parsing.ts +136 -0
  250. package/vendor/@anvil/common/src/util/agent-name-normalization.ts +38 -0
  251. package/vendor/@anvil/common/src/util/agent-name-resolver.ts +86 -0
  252. package/vendor/@anvil/common/src/util/analytics-dispatcher.ts +82 -0
  253. package/vendor/@anvil/common/src/util/analytics-log.ts +78 -0
  254. package/vendor/@anvil/common/src/util/analytics-sampling.ts +255 -0
  255. package/vendor/@anvil/common/src/util/anvil-model-availability.ts +102 -0
  256. package/vendor/@anvil/common/src/util/anvil-privacy.ts +82 -0
  257. package/vendor/@anvil/common/src/util/anvil-streak-line.ts +103 -0
  258. package/vendor/@anvil/common/src/util/anvil-streak.ts +156 -0
  259. package/vendor/@anvil/common/src/util/anvil-usage-summary.ts +125 -0
  260. package/vendor/@anvil/common/src/util/array.ts +31 -0
  261. package/vendor/@anvil/common/src/util/axiom-only-log.ts +124 -0
  262. package/vendor/@anvil/common/src/util/cache-debug.ts +171 -0
  263. package/vendor/@anvil/common/src/util/client-user-agent.ts +114 -0
  264. package/vendor/@anvil/common/src/util/credentials.ts +26 -0
  265. package/vendor/@anvil/common/src/util/currency.ts +25 -0
  266. package/vendor/@anvil/common/src/util/dates.ts +81 -0
  267. package/vendor/@anvil/common/src/util/disposable-email.ts +277 -0
  268. package/vendor/@anvil/common/src/util/engagement-tracker.ts +129 -0
  269. package/vendor/@anvil/common/src/util/env-file-path.ts +40 -0
  270. package/vendor/@anvil/common/src/util/error.ts +562 -0
  271. package/vendor/@anvil/common/src/util/file-read-limits.ts +236 -0
  272. package/vendor/@anvil/common/src/util/file.ts +339 -0
  273. package/vendor/@anvil/common/src/util/format-code-search.ts +115 -0
  274. package/vendor/@anvil/common/src/util/lazy-response-ads.ts +92 -0
  275. package/vendor/@anvil/common/src/util/log-data.ts +57 -0
  276. package/vendor/@anvil/common/src/util/log-ingest.ts +58 -0
  277. package/vendor/@anvil/common/src/util/log-mirror.ts +34 -0
  278. package/vendor/@anvil/common/src/util/lru-cache.ts +67 -0
  279. package/vendor/@anvil/common/src/util/messages.ts +685 -0
  280. package/vendor/@anvil/common/src/util/min-heap.ts +87 -0
  281. package/vendor/@anvil/common/src/util/model-utils.ts +4 -0
  282. package/vendor/@anvil/common/src/util/object.ts +128 -0
  283. package/vendor/@anvil/common/src/util/partial-json-delta.ts +89 -0
  284. package/vendor/@anvil/common/src/util/path.ts +15 -0
  285. package/vendor/@anvil/common/src/util/project-ignore.ts +119 -0
  286. package/vendor/@anvil/common/src/util/promise.ts +73 -0
  287. package/vendor/@anvil/common/src/util/random.ts +15 -0
  288. package/vendor/@anvil/common/src/util/rate-limit.ts +56 -0
  289. package/vendor/@anvil/common/src/util/reddit-anvil-retention.ts +60 -0
  290. package/vendor/@anvil/common/src/util/reddit-capi-events.ts +36 -0
  291. package/vendor/@anvil/common/src/util/response-ad-positions.ts +54 -0
  292. package/vendor/@anvil/common/src/util/saxy.ts +741 -0
  293. package/vendor/@anvil/common/src/util/skills.test.ts +44 -0
  294. package/vendor/@anvil/common/src/util/skills.ts +36 -0
  295. package/vendor/@anvil/common/src/util/split-data.ts +259 -0
  296. package/vendor/@anvil/common/src/util/stop-sequence.ts +61 -0
  297. package/vendor/@anvil/common/src/util/string.ts +426 -0
  298. package/vendor/@anvil/common/src/util/system-info.ts +53 -0
  299. package/vendor/@anvil/common/src/util/thread-title.ts +46 -0
  300. package/vendor/@anvil/common/src/util/ttft-histogram.ts +73 -0
  301. package/vendor/@anvil/common/src/util/xml-parser.ts +27 -0
  302. package/vendor/@anvil/common/src/util/xml.ts +17 -0
  303. package/vendor/@anvil/common/src/util/zod-schema.ts +25 -0
  304. package/vendor/@anvil/common/src/util/zoned-time.ts +136 -0
  305. package/vendor/@anvil/common/src/utils/ask-user-bridge.ts +44 -0
  306. package/vendor/@anvil/sdk/dist/index.cjs +52297 -0
  307. package/vendor/@anvil/sdk/dist/index.cjs.map +440 -0
  308. package/vendor/@anvil/sdk/dist/index.d.ts +4277 -0
  309. package/vendor/@anvil/sdk/dist/index.mjs +52290 -0
  310. package/vendor/@anvil/sdk/dist/index.mjs.map +440 -0
  311. package/vendor/@anvil/sdk/dist/vendor/ai.cjs +30886 -0
  312. package/vendor/@anvil/sdk/dist/vendor/ripgrep/arm64-darwin/rg +0 -0
  313. package/vendor/@anvil/sdk/dist/vendor/ripgrep/arm64-linux/rg +0 -0
  314. package/vendor/@anvil/sdk/dist/vendor/ripgrep/x64-darwin/rg +0 -0
  315. package/vendor/@anvil/sdk/dist/vendor/ripgrep/x64-linux/rg +0 -0
  316. package/vendor/@anvil/sdk/dist/vendor/ripgrep/x64-win32/rg.exe +0 -0
  317. package/vendor/@anvil/sdk/dist/wasm/tree-sitter-c-sharp.wasm +0 -0
  318. package/vendor/@anvil/sdk/dist/wasm/tree-sitter-cpp.wasm +0 -0
  319. package/vendor/@anvil/sdk/dist/wasm/tree-sitter-go.wasm +0 -0
  320. package/vendor/@anvil/sdk/dist/wasm/tree-sitter-java.wasm +0 -0
  321. package/vendor/@anvil/sdk/dist/wasm/tree-sitter-javascript.wasm +0 -0
  322. package/vendor/@anvil/sdk/dist/wasm/tree-sitter-python.wasm +0 -0
  323. package/vendor/@anvil/sdk/dist/wasm/tree-sitter-ruby.wasm +0 -0
  324. package/vendor/@anvil/sdk/dist/wasm/tree-sitter-rust.wasm +0 -0
  325. package/vendor/@anvil/sdk/dist/wasm/tree-sitter-tsx.wasm +0 -0
  326. package/vendor/@anvil/sdk/dist/wasm/tree-sitter-typescript.wasm +0 -0
  327. package/vendor/@anvil/sdk/dist/wasm/tree-sitter.wasm +0 -0
  328. package/vendor/@anvil/sdk/package.json +21 -0
@@ -1,544 +1,553 @@
1
- import { castDraft, enableMapSet } from 'immer'
2
- import { create } from 'zustand'
3
- import { immer } from 'zustand/middleware/immer'
4
-
5
- import { AGENT_MODES, IS_ANVIL } from '../utils/constants'
6
- import { clamp } from '../utils/math'
7
- import { loadModePreference, saveModePreference } from '../utils/settings'
8
-
9
- import type { ChatMessage, ContentBlock } from '../types/chat'
10
- import type { AgentMode } from '../utils/constants'
11
- import type { InputMode } from '../utils/input-modes'
12
- import type { RunState } from '@anvil/sdk'
13
-
14
- // Import types from the types/store module to avoid circular dependencies
15
- import type {
16
- TopBannerType,
17
- InputValue,
18
- AskUserQuestion,
19
- AnswerState,
20
- AskUserState,
21
- PendingImageStatus,
22
- PendingImageAttachment,
23
- PendingTextAttachment,
24
- PendingFileAttachment,
25
- PendingAttachment,
26
- PendingImage,
27
- PendingBashMessage,
28
- SuggestedFollowup,
29
- SuggestedFollowupsState,
30
- ClickedFollowupsMap,
31
- } from '../types/store'
32
-
33
- // Re-export types from the types/store module to maintain backwards compatibility
34
- export type {
35
- TopBannerType,
36
- InputValue,
37
- AskUserQuestion,
38
- AnswerState,
39
- AskUserState,
40
- PendingImageStatus,
41
- PendingImageAttachment,
42
- PendingTextAttachment,
43
- PendingFileAttachment,
44
- PendingAttachment,
45
- PendingImage,
46
- PendingBashMessage,
47
- SuggestedFollowup,
48
- SuggestedFollowupsState,
49
- ClickedFollowupsMap,
50
- }
51
-
52
- // Drafts Sets (streamingAgents, activeSubagents) through immer; see
53
- // message-block-store for why the store enables the plugin itself.
54
- enableMapSet()
55
-
56
- export type ChatStoreState = {
57
- /** Unique ID for this chat session, regenerated on /new */
58
- chatSessionId: string
59
- messages: ChatMessage[]
60
- streamingAgents: Set<string>
61
- focusedAgentId: string | null
62
- inputValue: string
63
- cursorPosition: number
64
- lastEditDueToNav: boolean
65
- inputFocused: boolean
66
- isFocusSupported: boolean
67
- activeSubagents: Set<string>
68
- isChainInProgress: boolean
69
- slashSelectedIndex: number
70
- agentSelectedIndex: number
71
- agentMode: AgentMode
72
- hasReceivedPlanResponse: boolean
73
- lastMessageMode: AgentMode | null
74
- sessionCreditsUsed: number
75
- runState: RunState | null
76
- /** The currently active top banner, or null if none */
77
- activeTopBanner: TopBannerType
78
- inputMode: InputMode
79
- isRetrying: boolean
80
- /** True while the current retry wait is a server capacity deferral (free
81
- * mode shed under high demand) rather than a stream recovery the status
82
- * bar says "high demand" instead of a generic "retrying". Cleared with
83
- * isRetrying. */
84
- isCapacityWait: boolean
85
- askUserState: AskUserState
86
- pendingAttachments: PendingAttachment[]
87
- pendingBashMessages: PendingBashMessage[]
88
- suggestedFollowups: SuggestedFollowupsState | null
89
- /** Persisted clicked indices per toolCallId */
90
- clickedFollowupsMap: ClickedFollowupsMap
91
- }
92
-
93
- const findLatestFollowupInBlocks = (
94
- blocks: ContentBlock[] | undefined,
95
- ): string | null => {
96
- if (!blocks) return null
97
-
98
- for (let i = blocks.length - 1; i >= 0; i--) {
99
- const block = blocks[i]
100
- if (block.type === 'tool' && block.toolName === 'suggest_followups') {
101
- return block.toolCallId
102
- }
103
- if (block.type === 'agent') {
104
- const nested = findLatestFollowupInBlocks(block.blocks)
105
- if (nested) return nested
106
- }
107
- }
108
-
109
- return null
110
- }
111
-
112
- export const getLatestFollowupToolCallId = (
113
- messages: ChatMessage[],
114
- ): string | null => {
115
- for (let i = messages.length - 1; i >= 0; i--) {
116
- const latest = findLatestFollowupInBlocks(messages[i]?.blocks)
117
- if (latest) return latest
118
- }
119
- return null
120
- }
121
-
122
- type ChatStoreActions = {
123
- setMessages: (
124
- value: ChatMessage[] | ((prev: ChatMessage[]) => ChatMessage[]),
125
- ) => void
126
- setStreamingAgents: (
127
- value: Set<string> | ((prev: Set<string>) => Set<string>),
128
- ) => void
129
- setFocusedAgentId: (
130
- value: string | null | ((prev: string | null) => string | null),
131
- ) => void
132
- setInputValue: (
133
- value: InputValue | ((prev: InputValue) => InputValue),
134
- ) => void
135
- setInputFocused: (focused: boolean) => void
136
- setIsFocusSupported: (supported: boolean) => void
137
- setActiveSubagents: (
138
- value: Set<string> | ((prev: Set<string>) => Set<string>),
139
- ) => void
140
- setIsChainInProgress: (active: boolean) => void
141
- setSlashSelectedIndex: (value: number | ((prev: number) => number)) => void
142
- setAgentSelectedIndex: (value: number | ((prev: number) => number)) => void
143
- setAgentMode: (mode: AgentMode) => void
144
- toggleAgentMode: () => void
145
- setHasReceivedPlanResponse: (value: boolean) => void
146
- setLastMessageMode: (mode: AgentMode | null) => void
147
- addSessionCredits: (credits: number) => void
148
- setRunState: (runState: RunState | null) => void
149
- setActiveTopBanner: (banner: TopBannerType) => void
150
- closeTopBanner: () => void
151
- setInputMode: (mode: InputMode) => void
152
- setIsRetrying: (retrying: boolean) => void
153
- /** Mark the current wait as a free-mode capacity deferral (implies
154
- * isRetrying). Cleared by setIsRetrying(false). */
155
- noteCapacityDeferral: () => void
156
- setAskUserState: (state: AskUserState) => void
157
- updateAskUserAnswer: (questionIndex: number, optionIndex: number) => void
158
- updateAskUserOtherText: (questionIndex: number, text: string) => void
159
- addPendingAttachment: (attachment: PendingAttachment) => void
160
- removePendingAttachment: (id: string) => void
161
- clearPendingAttachments: () => void
162
- // Convenience aliases for backwards compatibility
163
- addPendingImage: (image: Omit<PendingImageAttachment, 'kind'>) => void
164
- removePendingImage: (path: string) => void
165
- clearPendingImages: () => void
166
- addPendingTextAttachment: (attachment: Omit<PendingTextAttachment, 'kind'>) => void
167
- removePendingTextAttachment: (id: string) => void
168
- clearPendingTextAttachments: () => void
169
- addPendingFileAttachment: (attachment: Omit<PendingFileAttachment, 'kind'>) => void
170
- addPendingBashMessage: (message: PendingBashMessage) => void
171
- updatePendingBashMessage: (
172
- id: string,
173
- updates: Partial<PendingBashMessage>,
174
- ) => void
175
- removePendingBashMessage: (id: string) => void
176
- clearPendingBashMessages: () => void
177
- setSuggestedFollowups: (state: SuggestedFollowupsState | null) => void
178
- markFollowupClicked: (toolCallId: string, index: number) => void
179
- reset: () => void
180
- }
181
-
182
- type ChatStore = ChatStoreState & ChatStoreActions
183
-
184
- const generateSessionId = () => crypto.randomUUID()
185
-
186
- const initialState: ChatStoreState = {
187
- chatSessionId: generateSessionId(),
188
- messages: [],
189
- streamingAgents: new Set<string>(),
190
- focusedAgentId: null,
191
- inputValue: '',
192
- cursorPosition: 0,
193
- lastEditDueToNav: false,
194
- inputFocused: true, // Cursor visible by default
195
- isFocusSupported: false, // Don't blink until terminal support is detected
196
- activeSubagents: new Set<string>(),
197
- isChainInProgress: false,
198
- slashSelectedIndex: 0,
199
- agentSelectedIndex: 0,
200
- agentMode: IS_ANVIL ? ('LITE' as const) : loadModePreference(),
201
- hasReceivedPlanResponse: false,
202
- lastMessageMode: null,
203
- sessionCreditsUsed: 0,
204
- runState: null,
205
- activeTopBanner: null,
206
- inputMode: 'default' as InputMode,
207
- isRetrying: false,
208
- isCapacityWait: false,
209
- askUserState: null,
210
- pendingAttachments: [],
211
- pendingBashMessages: [],
212
- suggestedFollowups: null,
213
- clickedFollowupsMap: new Map<string, Set<number>>(),
214
- }
215
-
216
- export const useChatStore = create<ChatStore>()(
217
- immer((set) => ({
218
- ...initialState,
219
-
220
- setMessages: (value) =>
221
- set((state) => {
222
- state.messages =
223
- typeof value === 'function' ? value(state.messages) : value
224
- }),
225
-
226
- setStreamingAgents: (value) =>
227
- set((state) => {
228
- state.streamingAgents =
229
- typeof value === 'function' ? value(state.streamingAgents) : value
230
- }),
231
-
232
- setFocusedAgentId: (value) =>
233
- set((state) => {
234
- state.focusedAgentId =
235
- typeof value === 'function' ? value(state.focusedAgentId) : value
236
- }),
237
-
238
- setInputValue: (value) =>
239
- set((state) => {
240
- const { text, cursorPosition, lastEditDueToNav } =
241
- typeof value === 'function'
242
- ? value({
243
- text: state.inputValue,
244
- cursorPosition: state.cursorPosition,
245
- lastEditDueToNav: state.lastEditDueToNav,
246
- })
247
- : value
248
- state.inputValue = text
249
- state.cursorPosition = clamp(cursorPosition, 0, text.length)
250
- state.lastEditDueToNav = lastEditDueToNav
251
- }),
252
-
253
- setInputFocused: (focused) =>
254
- set((state) => {
255
- state.inputFocused = focused
256
- }),
257
-
258
- setIsFocusSupported: (supported) =>
259
- set((state) => {
260
- state.isFocusSupported = supported
261
- }),
262
-
263
- setActiveSubagents: (value) =>
264
- set((state) => {
265
- state.activeSubagents =
266
- typeof value === 'function' ? value(state.activeSubagents) : value
267
- }),
268
-
269
- setIsChainInProgress: (active) =>
270
- set((state) => {
271
- state.isChainInProgress = active
272
- }),
273
-
274
- setSlashSelectedIndex: (value) =>
275
- set((state) => {
276
- state.slashSelectedIndex =
277
- typeof value === 'function' ? value(state.slashSelectedIndex) : value
278
- }),
279
-
280
- setAgentSelectedIndex: (value) =>
281
- set((state) => {
282
- state.agentSelectedIndex =
283
- typeof value === 'function' ? value(state.agentSelectedIndex) : value
284
- }),
285
-
286
- setAgentMode: (mode) =>
287
- set((state) => {
288
- if (IS_ANVIL) return
289
- state.agentMode = mode
290
- saveModePreference(mode)
291
- }),
292
-
293
- toggleAgentMode: () =>
294
- set((state) => {
295
- if (IS_ANVIL) return
296
- const currentIndex = AGENT_MODES.indexOf(state.agentMode)
297
- const nextIndex = (currentIndex + 1) % AGENT_MODES.length
298
- state.agentMode = AGENT_MODES[nextIndex]
299
- saveModePreference(state.agentMode)
300
- }),
301
-
302
- setHasReceivedPlanResponse: (value) =>
303
- set((state) => {
304
- state.hasReceivedPlanResponse = value
305
- }),
306
-
307
- setLastMessageMode: (mode) =>
308
- set((state) => {
309
- state.lastMessageMode = mode
310
- }),
311
-
312
- addSessionCredits: (credits) =>
313
- set((state) => {
314
- state.sessionCreditsUsed += credits
315
- }),
316
-
317
- setRunState: (runState) =>
318
- set((state) => {
319
- state.runState = runState ? castDraft(runState) : null
320
- }),
321
-
322
- setActiveTopBanner: (banner) =>
323
- set((state) => {
324
- state.activeTopBanner = banner
325
- }),
326
-
327
- closeTopBanner: () =>
328
- set((state) => {
329
- state.activeTopBanner = null
330
- }),
331
-
332
- setInputMode: (mode) =>
333
- set((state) => {
334
- state.inputMode = mode
335
- }),
336
-
337
- setIsRetrying: (retrying) =>
338
- set((state) => {
339
- state.isRetrying = retrying
340
- // Any transition (fresh generic retry, or the wait ending) supersedes
341
- // a capacity flavor; only noteCapacityDeferral re-establishes it.
342
- state.isCapacityWait = false
343
- }),
344
-
345
- noteCapacityDeferral: () =>
346
- set((state) => {
347
- state.isRetrying = true
348
- state.isCapacityWait = true
349
- }),
350
-
351
- setAskUserState: (askUserState) =>
352
- set((state) => {
353
- state.askUserState = askUserState
354
- }),
355
-
356
- addPendingAttachment: (attachment) =>
357
- set((state) => {
358
- // Don't add duplicates — use path for image/file, id for text
359
- const id = attachment.kind === 'text' ? attachment.id : attachment.path
360
- const isDuplicate = state.pendingAttachments.some((a) =>
361
- a.kind === 'text' ? a.id === id : a.path === id,
362
- )
363
- if (!isDuplicate) {
364
- state.pendingAttachments.push(attachment)
365
- }
366
- }),
367
-
368
- removePendingAttachment: (id) =>
369
- set((state) => {
370
- state.pendingAttachments = state.pendingAttachments.filter((a) =>
371
- a.kind === 'text' ? a.id !== id : a.path !== id,
372
- )
373
- }),
374
-
375
- clearPendingAttachments: () =>
376
- set((state) => {
377
- state.pendingAttachments = []
378
- }),
379
-
380
- // Backwards-compatible convenience methods that delegate to canonical functions
381
- addPendingImage: (image) => {
382
- useChatStore.getState().addPendingAttachment({ ...image, kind: 'image' })
383
- },
384
-
385
- removePendingImage: (path) => {
386
- // Clear any auto-remove timer to prevent memory leaks
387
- // Import dynamically to avoid circular dependency
388
- import('../utils/pending-attachments')
389
- .then(({ clearErrorImageTimer }) => {
390
- clearErrorImageTimer(path)
391
- })
392
- .catch(() => {
393
- // Silently ignore import errors - timer cleanup is best-effort
394
- })
395
- useChatStore.getState().removePendingAttachment(path)
396
- },
397
-
398
- clearPendingImages: () =>
399
- set((state) => {
400
- state.pendingAttachments = state.pendingAttachments.filter(
401
- (a) => a.kind !== 'image',
402
- )
403
- }),
404
-
405
- addPendingTextAttachment: (attachment) => {
406
- useChatStore.getState().addPendingAttachment({ ...attachment, kind: 'text' })
407
- },
408
-
409
- removePendingTextAttachment: (id) => {
410
- useChatStore.getState().removePendingAttachment(id)
411
- },
412
-
413
- clearPendingTextAttachments: () =>
414
- set((state) => {
415
- state.pendingAttachments = state.pendingAttachments.filter(
416
- (a) => a.kind !== 'text',
417
- )
418
- }),
419
-
420
- addPendingFileAttachment: (attachment) => {
421
- useChatStore.getState().addPendingAttachment({ ...attachment, kind: 'file' })
422
- },
423
-
424
- updateAskUserAnswer: (questionIndex, optionIndex) =>
425
- set((state) => {
426
- if (!state.askUserState) return
427
-
428
- const question = state.askUserState.questions[questionIndex]
429
- const currentAnswer = state.askUserState.selectedAnswers[questionIndex]
430
-
431
- if (question?.multiSelect) {
432
- // Multi-select: toggle option in array
433
- const selected = Array.isArray(currentAnswer) ? currentAnswer : []
434
- const newSelected = selected.includes(optionIndex)
435
- ? selected.filter((i) => i !== optionIndex) // Remove if already selected
436
- : [...selected, optionIndex] // Add if not selected
437
-
438
- state.askUserState.selectedAnswers[questionIndex] = newSelected
439
- } else {
440
- // Single-select: set option index
441
- state.askUserState.selectedAnswers[questionIndex] = optionIndex
442
- }
443
-
444
- // Clear other text when any option is selected (mutually exclusive)
445
- state.askUserState.otherTexts[questionIndex] = ''
446
- }),
447
-
448
- updateAskUserOtherText: (questionIndex, text) =>
449
- set((state) => {
450
- if (!state.askUserState) return
451
-
452
- state.askUserState.otherTexts[questionIndex] = text
453
-
454
- // Clear selected option(s) when text is entered (mutually exclusive)
455
- if (text) {
456
- const question = state.askUserState.questions[questionIndex]
457
- if (question?.multiSelect) {
458
- state.askUserState.selectedAnswers[questionIndex] = []
459
- } else {
460
- state.askUserState.selectedAnswers[questionIndex] = -1
461
- }
462
- }
463
- }),
464
-
465
- addPendingBashMessage: (message) =>
466
- set((state) => {
467
- state.pendingBashMessages.push(message)
468
- }),
469
-
470
- updatePendingBashMessage: (id, updates) =>
471
- set((state) => {
472
- const msg = state.pendingBashMessages.find((m) => m.id === id)
473
- if (msg) {
474
- Object.assign(msg, updates)
475
- }
476
- }),
477
-
478
- removePendingBashMessage: (id) =>
479
- set((state) => {
480
- state.pendingBashMessages = state.pendingBashMessages.filter(
481
- (m) => m.id !== id,
482
- )
483
- }),
484
-
485
- clearPendingBashMessages: () =>
486
- set((state) => {
487
- state.pendingBashMessages = []
488
- }),
489
-
490
- setSuggestedFollowups: (suggestedFollowups) =>
491
- set((state) => {
492
- state.suggestedFollowups = suggestedFollowups
493
- }),
494
-
495
- markFollowupClicked: (toolCallId: string, index: number) =>
496
- set((state) => {
497
- // Store in the persistent map
498
- if (!state.clickedFollowupsMap.has(toolCallId)) {
499
- state.clickedFollowupsMap.set(toolCallId, new Set<number>())
500
- }
501
- state.clickedFollowupsMap.get(toolCallId)!.add(index)
502
-
503
- // Also update the current suggestedFollowups if it matches
504
- if (state.suggestedFollowups?.toolCallId === toolCallId) {
505
- state.suggestedFollowups.clickedIndices.add(index)
506
- }
507
- }),
508
-
509
- reset: () =>
510
- set((state) => {
511
- state.chatSessionId = generateSessionId()
512
- state.messages = initialState.messages.slice()
513
- state.streamingAgents = new Set(initialState.streamingAgents)
514
- state.focusedAgentId = initialState.focusedAgentId
515
- state.inputValue = initialState.inputValue
516
- state.cursorPosition = initialState.cursorPosition
517
- state.lastEditDueToNav = initialState.lastEditDueToNav
518
- // Terminal capabilities and focus outlive a chat. Resetting these can
519
- // re-enable animation while the app is still unfocused, and focus
520
- // support would stay false because the mounted detector only reports
521
- // support once per subscription.
522
- state.activeSubagents = new Set(initialState.activeSubagents)
523
- state.isChainInProgress = initialState.isChainInProgress
524
- state.slashSelectedIndex = initialState.slashSelectedIndex
525
- state.agentSelectedIndex = initialState.agentSelectedIndex
526
- state.agentMode = initialState.agentMode
527
- state.hasReceivedPlanResponse = initialState.hasReceivedPlanResponse
528
- state.lastMessageMode = initialState.lastMessageMode
529
- state.sessionCreditsUsed = initialState.sessionCreditsUsed
530
- state.runState = initialState.runState
531
- ? castDraft(initialState.runState)
532
- : null
533
- state.activeTopBanner = initialState.activeTopBanner
534
- state.inputMode = initialState.inputMode
535
- state.isRetrying = initialState.isRetrying
536
- state.isCapacityWait = initialState.isCapacityWait
537
- state.askUserState = initialState.askUserState
538
- state.pendingAttachments = []
539
- state.pendingBashMessages = []
540
- state.suggestedFollowups = null
541
- state.clickedFollowupsMap = new Map<string, Set<number>>()
542
- }),
543
- })),
544
- )
1
+ import { castDraft, enableMapSet } from 'immer'
2
+ import { create } from 'zustand'
3
+ import { immer } from 'zustand/middleware/immer'
4
+
5
+ import { AGENT_MODES, IS_ANVIL } from '../utils/constants'
6
+ import { clamp } from '../utils/math'
7
+ import { loadModePreference, saveModePreference } from '../utils/settings'
8
+
9
+ import type { ChatMessage, ContentBlock } from '../types/chat'
10
+ import type { AgentMode } from '../utils/constants'
11
+ import type { InputMode } from '../utils/input-modes'
12
+ import type { RunState } from '@anvil/sdk'
13
+
14
+ // Import types from the types/store module to avoid circular dependencies
15
+ import type {
16
+ TopBannerType,
17
+ InputValue,
18
+ AskUserQuestion,
19
+ AnswerState,
20
+ AskUserState,
21
+ PendingImageStatus,
22
+ PendingImageAttachment,
23
+ PendingTextAttachment,
24
+ PendingFileAttachment,
25
+ PendingAttachment,
26
+ PendingImage,
27
+ PendingBashMessage,
28
+ SuggestedFollowup,
29
+ SuggestedFollowupsState,
30
+ ClickedFollowupsMap,
31
+ } from '../types/store'
32
+
33
+ // Re-export types from the types/store module to maintain backwards compatibility
34
+ export type {
35
+ TopBannerType,
36
+ InputValue,
37
+ AskUserQuestion,
38
+ AnswerState,
39
+ AskUserState,
40
+ PendingImageStatus,
41
+ PendingImageAttachment,
42
+ PendingTextAttachment,
43
+ PendingFileAttachment,
44
+ PendingAttachment,
45
+ PendingImage,
46
+ PendingBashMessage,
47
+ SuggestedFollowup,
48
+ SuggestedFollowupsState,
49
+ ClickedFollowupsMap,
50
+ }
51
+
52
+ // Drafts Sets (streamingAgents, activeSubagents) through immer; see
53
+ // message-block-store for why the store enables the plugin itself.
54
+ enableMapSet()
55
+
56
+ export type ChatStoreState = {
57
+ /** Unique ID for this chat session, regenerated on /new */
58
+ chatSessionId: string
59
+ messages: ChatMessage[]
60
+ streamingAgents: Set<string>
61
+ focusedAgentId: string | null
62
+ inputValue: string
63
+ cursorPosition: number
64
+ lastEditDueToNav: boolean
65
+ inputFocused: boolean
66
+ isFocusSupported: boolean
67
+ activeSubagents: Set<string>
68
+ isChainInProgress: boolean
69
+ slashSelectedIndex: number
70
+ agentSelectedIndex: number
71
+ agentMode: AgentMode
72
+ hasReceivedPlanResponse: boolean
73
+ lastMessageMode: AgentMode | null
74
+ sessionCreditsUsed: number
75
+ contextTokensUsed: number
76
+ runState: RunState | null
77
+ /** The currently active top banner, or null if none */
78
+ activeTopBanner: TopBannerType
79
+ inputMode: InputMode
80
+ isRetrying: boolean
81
+ /** True while the current retry wait is a server capacity deferral (free
82
+ * mode shed under high demand) rather than a stream recovery the status
83
+ * bar says "high demand" instead of a generic "retrying". Cleared with
84
+ * isRetrying. */
85
+ isCapacityWait: boolean
86
+ askUserState: AskUserState
87
+ pendingAttachments: PendingAttachment[]
88
+ pendingBashMessages: PendingBashMessage[]
89
+ suggestedFollowups: SuggestedFollowupsState | null
90
+ /** Persisted clicked indices per toolCallId */
91
+ clickedFollowupsMap: ClickedFollowupsMap
92
+ }
93
+
94
+ const findLatestFollowupInBlocks = (
95
+ blocks: ContentBlock[] | undefined,
96
+ ): string | null => {
97
+ if (!blocks) return null
98
+
99
+ for (let i = blocks.length - 1; i >= 0; i--) {
100
+ const block = blocks[i]
101
+ if (block.type === 'tool' && block.toolName === 'suggest_followups') {
102
+ return block.toolCallId
103
+ }
104
+ if (block.type === 'agent') {
105
+ const nested = findLatestFollowupInBlocks(block.blocks)
106
+ if (nested) return nested
107
+ }
108
+ }
109
+
110
+ return null
111
+ }
112
+
113
+ export const getLatestFollowupToolCallId = (
114
+ messages: ChatMessage[],
115
+ ): string | null => {
116
+ for (let i = messages.length - 1; i >= 0; i--) {
117
+ const latest = findLatestFollowupInBlocks(messages[i]?.blocks)
118
+ if (latest) return latest
119
+ }
120
+ return null
121
+ }
122
+
123
+ type ChatStoreActions = {
124
+ setMessages: (
125
+ value: ChatMessage[] | ((prev: ChatMessage[]) => ChatMessage[]),
126
+ ) => void
127
+ setStreamingAgents: (
128
+ value: Set<string> | ((prev: Set<string>) => Set<string>),
129
+ ) => void
130
+ setFocusedAgentId: (
131
+ value: string | null | ((prev: string | null) => string | null),
132
+ ) => void
133
+ setInputValue: (
134
+ value: InputValue | ((prev: InputValue) => InputValue),
135
+ ) => void
136
+ setInputFocused: (focused: boolean) => void
137
+ setIsFocusSupported: (supported: boolean) => void
138
+ setActiveSubagents: (
139
+ value: Set<string> | ((prev: Set<string>) => Set<string>),
140
+ ) => void
141
+ setIsChainInProgress: (active: boolean) => void
142
+ setSlashSelectedIndex: (value: number | ((prev: number) => number)) => void
143
+ setAgentSelectedIndex: (value: number | ((prev: number) => number)) => void
144
+ setAgentMode: (mode: AgentMode) => void
145
+ toggleAgentMode: () => void
146
+ setHasReceivedPlanResponse: (value: boolean) => void
147
+ setLastMessageMode: (mode: AgentMode | null) => void
148
+ addSessionCredits: (credits: number) => void
149
+ setContextTokensUsed: (tokens: number) => void
150
+ setRunState: (runState: RunState | null) => void
151
+ setActiveTopBanner: (banner: TopBannerType) => void
152
+ closeTopBanner: () => void
153
+ setInputMode: (mode: InputMode) => void
154
+ setIsRetrying: (retrying: boolean) => void
155
+ /** Mark the current wait as a free-mode capacity deferral (implies
156
+ * isRetrying). Cleared by setIsRetrying(false). */
157
+ noteCapacityDeferral: () => void
158
+ setAskUserState: (state: AskUserState) => void
159
+ updateAskUserAnswer: (questionIndex: number, optionIndex: number) => void
160
+ updateAskUserOtherText: (questionIndex: number, text: string) => void
161
+ addPendingAttachment: (attachment: PendingAttachment) => void
162
+ removePendingAttachment: (id: string) => void
163
+ clearPendingAttachments: () => void
164
+ // Convenience aliases for backwards compatibility
165
+ addPendingImage: (image: Omit<PendingImageAttachment, 'kind'>) => void
166
+ removePendingImage: (path: string) => void
167
+ clearPendingImages: () => void
168
+ addPendingTextAttachment: (attachment: Omit<PendingTextAttachment, 'kind'>) => void
169
+ removePendingTextAttachment: (id: string) => void
170
+ clearPendingTextAttachments: () => void
171
+ addPendingFileAttachment: (attachment: Omit<PendingFileAttachment, 'kind'>) => void
172
+ addPendingBashMessage: (message: PendingBashMessage) => void
173
+ updatePendingBashMessage: (
174
+ id: string,
175
+ updates: Partial<PendingBashMessage>,
176
+ ) => void
177
+ removePendingBashMessage: (id: string) => void
178
+ clearPendingBashMessages: () => void
179
+ setSuggestedFollowups: (state: SuggestedFollowupsState | null) => void
180
+ markFollowupClicked: (toolCallId: string, index: number) => void
181
+ reset: () => void
182
+ }
183
+
184
+ type ChatStore = ChatStoreState & ChatStoreActions
185
+
186
+ const generateSessionId = () => crypto.randomUUID()
187
+
188
+ const initialState: ChatStoreState = {
189
+ chatSessionId: generateSessionId(),
190
+ messages: [],
191
+ streamingAgents: new Set<string>(),
192
+ focusedAgentId: null,
193
+ inputValue: '',
194
+ cursorPosition: 0,
195
+ lastEditDueToNav: false,
196
+ inputFocused: true, // Cursor visible by default
197
+ isFocusSupported: false, // Don't blink until terminal support is detected
198
+ activeSubagents: new Set<string>(),
199
+ isChainInProgress: false,
200
+ slashSelectedIndex: 0,
201
+ agentSelectedIndex: 0,
202
+ agentMode: IS_ANVIL ? ('LITE' as const) : loadModePreference(),
203
+ hasReceivedPlanResponse: false,
204
+ lastMessageMode: null,
205
+ sessionCreditsUsed: 0,
206
+ contextTokensUsed: 0,
207
+ runState: null,
208
+ activeTopBanner: null,
209
+ inputMode: 'default' as InputMode,
210
+ isRetrying: false,
211
+ isCapacityWait: false,
212
+ askUserState: null,
213
+ pendingAttachments: [],
214
+ pendingBashMessages: [],
215
+ suggestedFollowups: null,
216
+ clickedFollowupsMap: new Map<string, Set<number>>(),
217
+ }
218
+
219
+ export const useChatStore = create<ChatStore>()(
220
+ immer((set) => ({
221
+ ...initialState,
222
+
223
+ setMessages: (value) =>
224
+ set((state) => {
225
+ state.messages =
226
+ typeof value === 'function' ? value(state.messages) : value
227
+ }),
228
+
229
+ setStreamingAgents: (value) =>
230
+ set((state) => {
231
+ state.streamingAgents =
232
+ typeof value === 'function' ? value(state.streamingAgents) : value
233
+ }),
234
+
235
+ setFocusedAgentId: (value) =>
236
+ set((state) => {
237
+ state.focusedAgentId =
238
+ typeof value === 'function' ? value(state.focusedAgentId) : value
239
+ }),
240
+
241
+ setInputValue: (value) =>
242
+ set((state) => {
243
+ const { text, cursorPosition, lastEditDueToNav } =
244
+ typeof value === 'function'
245
+ ? value({
246
+ text: state.inputValue,
247
+ cursorPosition: state.cursorPosition,
248
+ lastEditDueToNav: state.lastEditDueToNav,
249
+ })
250
+ : value
251
+ state.inputValue = text
252
+ state.cursorPosition = clamp(cursorPosition, 0, text.length)
253
+ state.lastEditDueToNav = lastEditDueToNav
254
+ }),
255
+
256
+ setInputFocused: (focused) =>
257
+ set((state) => {
258
+ state.inputFocused = focused
259
+ }),
260
+
261
+ setIsFocusSupported: (supported) =>
262
+ set((state) => {
263
+ state.isFocusSupported = supported
264
+ }),
265
+
266
+ setActiveSubagents: (value) =>
267
+ set((state) => {
268
+ state.activeSubagents =
269
+ typeof value === 'function' ? value(state.activeSubagents) : value
270
+ }),
271
+
272
+ setIsChainInProgress: (active) =>
273
+ set((state) => {
274
+ state.isChainInProgress = active
275
+ }),
276
+
277
+ setSlashSelectedIndex: (value) =>
278
+ set((state) => {
279
+ state.slashSelectedIndex =
280
+ typeof value === 'function' ? value(state.slashSelectedIndex) : value
281
+ }),
282
+
283
+ setAgentSelectedIndex: (value) =>
284
+ set((state) => {
285
+ state.agentSelectedIndex =
286
+ typeof value === 'function' ? value(state.agentSelectedIndex) : value
287
+ }),
288
+
289
+ setAgentMode: (mode) =>
290
+ set((state) => {
291
+ if (IS_ANVIL) return
292
+ state.agentMode = mode
293
+ saveModePreference(mode)
294
+ }),
295
+
296
+ toggleAgentMode: () =>
297
+ set((state) => {
298
+ if (IS_ANVIL) return
299
+ const currentIndex = AGENT_MODES.indexOf(state.agentMode)
300
+ const nextIndex = (currentIndex + 1) % AGENT_MODES.length
301
+ state.agentMode = AGENT_MODES[nextIndex]
302
+ saveModePreference(state.agentMode)
303
+ }),
304
+
305
+ setHasReceivedPlanResponse: (value) =>
306
+ set((state) => {
307
+ state.hasReceivedPlanResponse = value
308
+ }),
309
+
310
+ setLastMessageMode: (mode) =>
311
+ set((state) => {
312
+ state.lastMessageMode = mode
313
+ }),
314
+
315
+ addSessionCredits: (credits) =>
316
+ set((state) => {
317
+ state.sessionCreditsUsed += credits
318
+ }),
319
+
320
+ setContextTokensUsed: (tokens) =>
321
+ set((state) => {
322
+ state.contextTokensUsed = tokens
323
+ }),
324
+
325
+ setRunState: (runState) =>
326
+ set((state) => {
327
+ state.runState = runState ? castDraft(runState) : null
328
+ }),
329
+
330
+ setActiveTopBanner: (banner) =>
331
+ set((state) => {
332
+ state.activeTopBanner = banner
333
+ }),
334
+
335
+ closeTopBanner: () =>
336
+ set((state) => {
337
+ state.activeTopBanner = null
338
+ }),
339
+
340
+ setInputMode: (mode) =>
341
+ set((state) => {
342
+ state.inputMode = mode
343
+ }),
344
+
345
+ setIsRetrying: (retrying) =>
346
+ set((state) => {
347
+ state.isRetrying = retrying
348
+ // Any transition (fresh generic retry, or the wait ending) supersedes
349
+ // a capacity flavor; only noteCapacityDeferral re-establishes it.
350
+ state.isCapacityWait = false
351
+ }),
352
+
353
+ noteCapacityDeferral: () =>
354
+ set((state) => {
355
+ state.isRetrying = true
356
+ state.isCapacityWait = true
357
+ }),
358
+
359
+ setAskUserState: (askUserState) =>
360
+ set((state) => {
361
+ state.askUserState = askUserState
362
+ }),
363
+
364
+ addPendingAttachment: (attachment) =>
365
+ set((state) => {
366
+ // Don't add duplicates — use path for image/file, id for text
367
+ const id = attachment.kind === 'text' ? attachment.id : attachment.path
368
+ const isDuplicate = state.pendingAttachments.some((a) =>
369
+ a.kind === 'text' ? a.id === id : a.path === id,
370
+ )
371
+ if (!isDuplicate) {
372
+ state.pendingAttachments.push(attachment)
373
+ }
374
+ }),
375
+
376
+ removePendingAttachment: (id) =>
377
+ set((state) => {
378
+ state.pendingAttachments = state.pendingAttachments.filter((a) =>
379
+ a.kind === 'text' ? a.id !== id : a.path !== id,
380
+ )
381
+ }),
382
+
383
+ clearPendingAttachments: () =>
384
+ set((state) => {
385
+ state.pendingAttachments = []
386
+ }),
387
+
388
+ // Backwards-compatible convenience methods that delegate to canonical functions
389
+ addPendingImage: (image) => {
390
+ useChatStore.getState().addPendingAttachment({ ...image, kind: 'image' })
391
+ },
392
+
393
+ removePendingImage: (path) => {
394
+ // Clear any auto-remove timer to prevent memory leaks
395
+ // Import dynamically to avoid circular dependency
396
+ import('../utils/pending-attachments')
397
+ .then(({ clearErrorImageTimer }) => {
398
+ clearErrorImageTimer(path)
399
+ })
400
+ .catch(() => {
401
+ // Silently ignore import errors - timer cleanup is best-effort
402
+ })
403
+ useChatStore.getState().removePendingAttachment(path)
404
+ },
405
+
406
+ clearPendingImages: () =>
407
+ set((state) => {
408
+ state.pendingAttachments = state.pendingAttachments.filter(
409
+ (a) => a.kind !== 'image',
410
+ )
411
+ }),
412
+
413
+ addPendingTextAttachment: (attachment) => {
414
+ useChatStore.getState().addPendingAttachment({ ...attachment, kind: 'text' })
415
+ },
416
+
417
+ removePendingTextAttachment: (id) => {
418
+ useChatStore.getState().removePendingAttachment(id)
419
+ },
420
+
421
+ clearPendingTextAttachments: () =>
422
+ set((state) => {
423
+ state.pendingAttachments = state.pendingAttachments.filter(
424
+ (a) => a.kind !== 'text',
425
+ )
426
+ }),
427
+
428
+ addPendingFileAttachment: (attachment) => {
429
+ useChatStore.getState().addPendingAttachment({ ...attachment, kind: 'file' })
430
+ },
431
+
432
+ updateAskUserAnswer: (questionIndex, optionIndex) =>
433
+ set((state) => {
434
+ if (!state.askUserState) return
435
+
436
+ const question = state.askUserState.questions[questionIndex]
437
+ const currentAnswer = state.askUserState.selectedAnswers[questionIndex]
438
+
439
+ if (question?.multiSelect) {
440
+ // Multi-select: toggle option in array
441
+ const selected = Array.isArray(currentAnswer) ? currentAnswer : []
442
+ const newSelected = selected.includes(optionIndex)
443
+ ? selected.filter((i) => i !== optionIndex) // Remove if already selected
444
+ : [...selected, optionIndex] // Add if not selected
445
+
446
+ state.askUserState.selectedAnswers[questionIndex] = newSelected
447
+ } else {
448
+ // Single-select: set option index
449
+ state.askUserState.selectedAnswers[questionIndex] = optionIndex
450
+ }
451
+
452
+ // Clear other text when any option is selected (mutually exclusive)
453
+ state.askUserState.otherTexts[questionIndex] = ''
454
+ }),
455
+
456
+ updateAskUserOtherText: (questionIndex, text) =>
457
+ set((state) => {
458
+ if (!state.askUserState) return
459
+
460
+ state.askUserState.otherTexts[questionIndex] = text
461
+
462
+ // Clear selected option(s) when text is entered (mutually exclusive)
463
+ if (text) {
464
+ const question = state.askUserState.questions[questionIndex]
465
+ if (question?.multiSelect) {
466
+ state.askUserState.selectedAnswers[questionIndex] = []
467
+ } else {
468
+ state.askUserState.selectedAnswers[questionIndex] = -1
469
+ }
470
+ }
471
+ }),
472
+
473
+ addPendingBashMessage: (message) =>
474
+ set((state) => {
475
+ state.pendingBashMessages.push(message)
476
+ }),
477
+
478
+ updatePendingBashMessage: (id, updates) =>
479
+ set((state) => {
480
+ const msg = state.pendingBashMessages.find((m) => m.id === id)
481
+ if (msg) {
482
+ Object.assign(msg, updates)
483
+ }
484
+ }),
485
+
486
+ removePendingBashMessage: (id) =>
487
+ set((state) => {
488
+ state.pendingBashMessages = state.pendingBashMessages.filter(
489
+ (m) => m.id !== id,
490
+ )
491
+ }),
492
+
493
+ clearPendingBashMessages: () =>
494
+ set((state) => {
495
+ state.pendingBashMessages = []
496
+ }),
497
+
498
+ setSuggestedFollowups: (suggestedFollowups) =>
499
+ set((state) => {
500
+ state.suggestedFollowups = suggestedFollowups
501
+ }),
502
+
503
+ markFollowupClicked: (toolCallId: string, index: number) =>
504
+ set((state) => {
505
+ // Store in the persistent map
506
+ if (!state.clickedFollowupsMap.has(toolCallId)) {
507
+ state.clickedFollowupsMap.set(toolCallId, new Set<number>())
508
+ }
509
+ state.clickedFollowupsMap.get(toolCallId)!.add(index)
510
+
511
+ // Also update the current suggestedFollowups if it matches
512
+ if (state.suggestedFollowups?.toolCallId === toolCallId) {
513
+ state.suggestedFollowups.clickedIndices.add(index)
514
+ }
515
+ }),
516
+
517
+ reset: () =>
518
+ set((state) => {
519
+ state.chatSessionId = generateSessionId()
520
+ state.messages = initialState.messages.slice()
521
+ state.streamingAgents = new Set(initialState.streamingAgents)
522
+ state.focusedAgentId = initialState.focusedAgentId
523
+ state.inputValue = initialState.inputValue
524
+ state.cursorPosition = initialState.cursorPosition
525
+ state.lastEditDueToNav = initialState.lastEditDueToNav
526
+ // Terminal capabilities and focus outlive a chat. Resetting these can
527
+ // re-enable animation while the app is still unfocused, and focus
528
+ // support would stay false because the mounted detector only reports
529
+ // support once per subscription.
530
+ state.activeSubagents = new Set(initialState.activeSubagents)
531
+ state.isChainInProgress = initialState.isChainInProgress
532
+ state.slashSelectedIndex = initialState.slashSelectedIndex
533
+ state.agentSelectedIndex = initialState.agentSelectedIndex
534
+ state.agentMode = initialState.agentMode
535
+ state.hasReceivedPlanResponse = initialState.hasReceivedPlanResponse
536
+ state.lastMessageMode = initialState.lastMessageMode
537
+ state.sessionCreditsUsed = initialState.sessionCreditsUsed
538
+ state.contextTokensUsed = initialState.contextTokensUsed
539
+ state.runState = initialState.runState
540
+ ? castDraft(initialState.runState)
541
+ : null
542
+ state.activeTopBanner = initialState.activeTopBanner
543
+ state.inputMode = initialState.inputMode
544
+ state.isRetrying = initialState.isRetrying
545
+ state.isCapacityWait = initialState.isCapacityWait
546
+ state.askUserState = initialState.askUserState
547
+ state.pendingAttachments = []
548
+ state.pendingBashMessages = []
549
+ state.suggestedFollowups = null
550
+ state.clickedFollowupsMap = new Map<string, Set<number>>()
551
+ }),
552
+ })),
553
+ )