@superblocksteam/vite-plugin-file-sync 2.0.175-next.0 → 2.0.176-next.0

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 (286) hide show
  1. package/dist/ai-service/agent/prompts/build-base-system-prompt.d.ts.map +1 -1
  2. package/dist/ai-service/agent/prompts/build-base-system-prompt.js +1 -11
  3. package/dist/ai-service/agent/prompts/build-base-system-prompt.js.map +1 -1
  4. package/dist/ai-service/agent/tools/apis/get-api-docs.d.ts +3 -3
  5. package/dist/ai-service/agent/tools/apis/test-api.d.ts +13 -78
  6. package/dist/ai-service/agent/tools/apis/test-api.d.ts.map +1 -1
  7. package/dist/ai-service/agent/tools/apis/test-api.js +50 -52
  8. package/dist/ai-service/agent/tools/apis/test-api.js.map +1 -1
  9. package/dist/ai-service/agent/tools/build-capture-screenshot.d.ts +3 -11
  10. package/dist/ai-service/agent/tools/build-capture-screenshot.d.ts.map +1 -1
  11. package/dist/ai-service/agent/tools/build-capture-screenshot.js +8 -13
  12. package/dist/ai-service/agent/tools/build-capture-screenshot.js.map +1 -1
  13. package/dist/ai-service/agent/tools/build-navigate-preview.d.ts +0 -6
  14. package/dist/ai-service/agent/tools/build-navigate-preview.d.ts.map +1 -1
  15. package/dist/ai-service/agent/tools/build-navigate-preview.js +4 -5
  16. package/dist/ai-service/agent/tools/build-navigate-preview.js.map +1 -1
  17. package/dist/ai-service/agent/tools/databases/destructive-migration-prompt.d.ts +1 -4
  18. package/dist/ai-service/agent/tools/databases/destructive-migration-prompt.d.ts.map +1 -1
  19. package/dist/ai-service/agent/tools/databases/destructive-migration-prompt.js +3 -6
  20. package/dist/ai-service/agent/tools/databases/destructive-migration-prompt.js.map +1 -1
  21. package/dist/ai-service/agent/tools/databases/dev-database-tasks.d.ts +4 -11
  22. package/dist/ai-service/agent/tools/databases/dev-database-tasks.d.ts.map +1 -1
  23. package/dist/ai-service/agent/tools/databases/dev-database-tasks.js +3 -21
  24. package/dist/ai-service/agent/tools/databases/dev-database-tasks.js.map +1 -1
  25. package/dist/ai-service/agent/tools/databases/dev-database-tools.d.ts.map +1 -1
  26. package/dist/ai-service/agent/tools/databases/dev-database-tools.js +5 -32
  27. package/dist/ai-service/agent/tools/databases/dev-database-tools.js.map +1 -1
  28. package/dist/ai-service/agent/tools/get-logs.d.ts +2 -5
  29. package/dist/ai-service/agent/tools/get-logs.d.ts.map +1 -1
  30. package/dist/ai-service/agent/tools/get-logs.js +3 -6
  31. package/dist/ai-service/agent/tools/get-logs.js.map +1 -1
  32. package/dist/ai-service/agent/tools/get-runtime-errors.d.ts +0 -2
  33. package/dist/ai-service/agent/tools/get-runtime-errors.d.ts.map +1 -1
  34. package/dist/ai-service/agent/tools/get-runtime-errors.js +2 -3
  35. package/dist/ai-service/agent/tools/get-runtime-errors.js.map +1 -1
  36. package/dist/ai-service/agent/tools/get-security-findings.js +1 -1
  37. package/dist/ai-service/agent/tools/integrations/execute-request.d.ts +5 -5
  38. package/dist/ai-service/agent/tools/integrations/metadata.d.ts +1 -1
  39. package/dist/ai-service/agent/tools/integrations/metadata.js.map +1 -1
  40. package/dist/ai-service/agent/tools/report-security-findings.d.ts +3 -3
  41. package/dist/ai-service/agent/tools.d.ts +5 -1
  42. package/dist/ai-service/agent/tools.d.ts.map +1 -1
  43. package/dist/ai-service/agent/tools.js +42 -7
  44. package/dist/ai-service/agent/tools.js.map +1 -1
  45. package/dist/ai-service/agent/tools2/tools/grep-metadata.d.ts +3 -4
  46. package/dist/ai-service/agent/tools2/tools/grep-metadata.d.ts.map +1 -1
  47. package/dist/ai-service/agent/tools2/tools/grep-metadata.js +2 -4
  48. package/dist/ai-service/agent/tools2/tools/grep-metadata.js.map +1 -1
  49. package/dist/ai-service/agent/tools2/tools/grep.d.ts.map +1 -1
  50. package/dist/ai-service/agent/tools2/tools/grep.js +5 -27
  51. package/dist/ai-service/agent/tools2/tools/grep.js.map +1 -1
  52. package/dist/ai-service/app-interface/file-system-interface.d.ts +1 -1
  53. package/dist/ai-service/app-interface/filesystem/handlers/skill-handler.d.ts +1 -2
  54. package/dist/ai-service/app-interface/filesystem/handlers/skill-handler.d.ts.map +1 -1
  55. package/dist/ai-service/app-interface/filesystem/handlers/skill-handler.js +2 -9
  56. package/dist/ai-service/app-interface/filesystem/handlers/skill-handler.js.map +1 -1
  57. package/dist/ai-service/app-interface/filesystem/virtual-file-system.d.ts +0 -1
  58. package/dist/ai-service/app-interface/filesystem/virtual-file-system.d.ts.map +1 -1
  59. package/dist/ai-service/app-interface/filesystem/virtual-file-system.js +1 -1
  60. package/dist/ai-service/app-interface/filesystem/virtual-file-system.js.map +1 -1
  61. package/dist/ai-service/app-interface/npm-registry.d.ts +0 -1
  62. package/dist/ai-service/app-interface/npm-registry.d.ts.map +1 -1
  63. package/dist/ai-service/app-interface/npm-registry.js +6 -45
  64. package/dist/ai-service/app-interface/npm-registry.js.map +1 -1
  65. package/dist/ai-service/app-interface/shell.d.ts +1 -4
  66. package/dist/ai-service/app-interface/shell.d.ts.map +1 -1
  67. package/dist/ai-service/app-interface/shell.js +8 -26
  68. package/dist/ai-service/app-interface/shell.js.map +1 -1
  69. package/dist/ai-service/chat/chat-session-store.d.ts +1 -10
  70. package/dist/ai-service/chat/chat-session-store.d.ts.map +1 -1
  71. package/dist/ai-service/chat/chat-session-store.js +1 -30
  72. package/dist/ai-service/chat/chat-session-store.js.map +1 -1
  73. package/dist/ai-service/dev-database-client.d.ts +0 -1
  74. package/dist/ai-service/dev-database-client.d.ts.map +1 -1
  75. package/dist/ai-service/dev-database-client.js +0 -1
  76. package/dist/ai-service/dev-database-client.js.map +1 -1
  77. package/dist/ai-service/dev-database-file-sync.d.ts +1 -3
  78. package/dist/ai-service/dev-database-file-sync.d.ts.map +1 -1
  79. package/dist/ai-service/dev-database-file-sync.js.map +1 -1
  80. package/dist/ai-service/features.d.ts +8 -15
  81. package/dist/ai-service/features.d.ts.map +1 -1
  82. package/dist/ai-service/features.js +8 -15
  83. package/dist/ai-service/features.js.map +1 -1
  84. package/dist/ai-service/index.d.ts +19 -34
  85. package/dist/ai-service/index.d.ts.map +1 -1
  86. package/dist/ai-service/index.js +738 -474
  87. package/dist/ai-service/index.js.map +1 -1
  88. package/dist/ai-service/llm/context-v2/context-metrics.js +2 -2
  89. package/dist/ai-service/llm/context-v2/manager.d.ts +1 -3
  90. package/dist/ai-service/llm/context-v2/manager.d.ts.map +1 -1
  91. package/dist/ai-service/llm/context-v2/manager.js +0 -5
  92. package/dist/ai-service/llm/context-v2/manager.js.map +1 -1
  93. package/dist/ai-service/llm/context-v2/types.d.ts +1 -1
  94. package/dist/ai-service/llm/context-v2/types.d.ts.map +1 -1
  95. package/dist/ai-service/llm/context-v2/types.js.map +1 -1
  96. package/dist/ai-service/llm/stream/config.d.ts +1 -1
  97. package/dist/ai-service/llm/stream/config.d.ts.map +1 -1
  98. package/dist/ai-service/llm/stream/config.js.map +1 -1
  99. package/dist/ai-service/llm/stream/errors.d.ts +1 -4
  100. package/dist/ai-service/llm/stream/errors.d.ts.map +1 -1
  101. package/dist/ai-service/llm/stream/errors.js +2 -8
  102. package/dist/ai-service/llm/stream/errors.js.map +1 -1
  103. package/dist/ai-service/llm/stream/orchestrator.d.ts.map +1 -1
  104. package/dist/ai-service/llm/stream/orchestrator.js +6 -3
  105. package/dist/ai-service/llm/stream/orchestrator.js.map +1 -1
  106. package/dist/ai-service/llmobs/tracer.d.ts.map +1 -1
  107. package/dist/ai-service/llmobs/tracer.js +21 -4
  108. package/dist/ai-service/llmobs/tracer.js.map +1 -1
  109. package/dist/ai-service/managed-native-db-availability.d.ts +1 -1
  110. package/dist/ai-service/managed-native-db-availability.d.ts.map +1 -1
  111. package/dist/ai-service/managed-native-db-availability.js +2 -4
  112. package/dist/ai-service/managed-native-db-availability.js.map +1 -1
  113. package/dist/ai-service/policy-gate-remediation-usage.d.ts +1 -2
  114. package/dist/ai-service/policy-gate-remediation-usage.d.ts.map +1 -1
  115. package/dist/ai-service/policy-gate-remediation-usage.js +1 -2
  116. package/dist/ai-service/policy-gate-remediation-usage.js.map +1 -1
  117. package/dist/ai-service/skills/system/materialized.d.ts +1 -3
  118. package/dist/ai-service/skills/system/materialized.d.ts.map +1 -1
  119. package/dist/ai-service/skills/system/materialized.js +3 -3
  120. package/dist/ai-service/skills/system/materialized.js.map +1 -1
  121. package/dist/ai-service/state-machine/handlers/llm-generating.d.ts.map +1 -1
  122. package/dist/ai-service/state-machine/handlers/llm-generating.js +4 -6
  123. package/dist/ai-service/state-machine/handlers/llm-generating.js.map +1 -1
  124. package/dist/ai-service/state-machine/helpers/policy-gate-usage-metrics.d.ts +1 -1
  125. package/dist/ai-service/state-machine/helpers/policy-gate-usage-metrics.d.ts.map +1 -1
  126. package/dist/ai-service/state-machine/helpers/policy-gate-usage-metrics.js +1 -1
  127. package/dist/ai-service/state-machine/helpers/policy-gate-usage-metrics.js.map +1 -1
  128. package/dist/ai-service/state-machine/helpers/user-response-message.d.ts.map +1 -1
  129. package/dist/ai-service/state-machine/helpers/user-response-message.js +2 -4
  130. package/dist/ai-service/state-machine/helpers/user-response-message.js.map +1 -1
  131. package/dist/ai-service/types.d.ts +0 -3
  132. package/dist/ai-service/types.d.ts.map +1 -1
  133. package/dist/ai-service/types.js.map +1 -1
  134. package/dist/extract-api-integrations.d.ts +91 -52
  135. package/dist/extract-api-integrations.d.ts.map +1 -1
  136. package/dist/extract-api-integrations.js +487 -512
  137. package/dist/extract-api-integrations.js.map +1 -1
  138. package/dist/file-sync-vite-plugin.d.ts.map +1 -1
  139. package/dist/file-sync-vite-plugin.js +5 -53
  140. package/dist/file-sync-vite-plugin.js.map +1 -1
  141. package/dist/file-system-helpers.d.ts.map +1 -1
  142. package/dist/file-system-helpers.js +2 -3
  143. package/dist/file-system-helpers.js.map +1 -1
  144. package/dist/git-service/index.d.ts +0 -1
  145. package/dist/git-service/index.d.ts.map +1 -1
  146. package/dist/git-service/index.js +38 -6
  147. package/dist/git-service/index.js.map +1 -1
  148. package/dist/git-service/managed-excludes.d.ts.map +1 -1
  149. package/dist/git-service/managed-excludes.js +0 -2
  150. package/dist/git-service/managed-excludes.js.map +1 -1
  151. package/dist/index.d.ts +0 -1
  152. package/dist/index.d.ts.map +1 -1
  153. package/dist/index.js +0 -1
  154. package/dist/index.js.map +1 -1
  155. package/dist/injected-index.d.ts +3 -1
  156. package/dist/injected-index.d.ts.map +1 -1
  157. package/dist/injected-index.js +8 -3
  158. package/dist/injected-index.js.map +1 -1
  159. package/dist/lock-service/index.d.ts +0 -3
  160. package/dist/lock-service/index.d.ts.map +1 -1
  161. package/dist/lock-service/index.js +0 -7
  162. package/dist/lock-service/index.js.map +1 -1
  163. package/dist/migration/get-fullstack-template-dir.d.ts +5 -2
  164. package/dist/migration/get-fullstack-template-dir.d.ts.map +1 -1
  165. package/dist/migration/get-fullstack-template-dir.js +8 -5
  166. package/dist/migration/get-fullstack-template-dir.js.map +1 -1
  167. package/dist/migration-templates/app-fullstack/.gitignore +50 -0
  168. package/dist/plugin-options.d.ts +1 -1
  169. package/dist/plugin-options.d.ts.map +1 -1
  170. package/dist/policy-gate-runner.d.ts +7 -32
  171. package/dist/policy-gate-runner.d.ts.map +1 -1
  172. package/dist/policy-gate-runner.js +6 -23
  173. package/dist/policy-gate-runner.js.map +1 -1
  174. package/dist/socket-manager.d.ts +2 -9
  175. package/dist/socket-manager.d.ts.map +1 -1
  176. package/dist/socket-manager.js +19 -160
  177. package/dist/socket-manager.js.map +1 -1
  178. package/dist/sync-service/draft-helpers.d.ts +0 -9
  179. package/dist/sync-service/draft-helpers.d.ts.map +1 -1
  180. package/dist/sync-service/draft-helpers.js +0 -135
  181. package/dist/sync-service/draft-helpers.js.map +1 -1
  182. package/dist/sync-service/index.d.ts +1 -24
  183. package/dist/sync-service/index.d.ts.map +1 -1
  184. package/dist/sync-service/index.js +9 -197
  185. package/dist/sync-service/index.js.map +1 -1
  186. package/dist/sync-service/list-dir.d.ts +1 -3
  187. package/dist/sync-service/list-dir.d.ts.map +1 -1
  188. package/dist/sync-service/list-dir.js +1 -7
  189. package/dist/sync-service/list-dir.js.map +1 -1
  190. package/dist/util/log-sanitizer.d.ts +0 -4
  191. package/dist/util/log-sanitizer.d.ts.map +1 -1
  192. package/dist/util/log-sanitizer.js +0 -7
  193. package/dist/util/log-sanitizer.js.map +1 -1
  194. package/package.json +52 -57
  195. package/dist/agent-tool-executor/codex-drafts.d.ts +0 -12
  196. package/dist/agent-tool-executor/codex-drafts.d.ts.map +0 -1
  197. package/dist/agent-tool-executor/codex-drafts.js +0 -48
  198. package/dist/agent-tool-executor/codex-drafts.js.map +0 -1
  199. package/dist/agent-tool-executor/codex-exec-server.d.ts +0 -42
  200. package/dist/agent-tool-executor/codex-exec-server.d.ts.map +0 -1
  201. package/dist/agent-tool-executor/codex-exec-server.js +0 -405
  202. package/dist/agent-tool-executor/codex-exec-server.js.map +0 -1
  203. package/dist/agent-tool-executor/codex-execution-policy.d.ts +0 -12
  204. package/dist/agent-tool-executor/codex-execution-policy.d.ts.map +0 -1
  205. package/dist/agent-tool-executor/codex-execution-policy.js +0 -201
  206. package/dist/agent-tool-executor/codex-execution-policy.js.map +0 -1
  207. package/dist/agent-tool-executor/native-dotenv-watch-scope.d.ts +0 -12
  208. package/dist/agent-tool-executor/native-dotenv-watch-scope.d.ts.map +0 -1
  209. package/dist/agent-tool-executor/native-dotenv-watch-scope.js +0 -74
  210. package/dist/agent-tool-executor/native-dotenv-watch-scope.js.map +0 -1
  211. package/dist/ai-service/agent/security-scan-tools.d.ts +0 -12
  212. package/dist/ai-service/agent/security-scan-tools.d.ts.map +0 -1
  213. package/dist/ai-service/agent/security-scan-tools.js +0 -44
  214. package/dist/ai-service/agent/security-scan-tools.js.map +0 -1
  215. package/dist/ai-service/builder-conversation.d.ts +0 -70
  216. package/dist/ai-service/builder-conversation.d.ts.map +0 -1
  217. package/dist/ai-service/builder-conversation.js +0 -660
  218. package/dist/ai-service/builder-conversation.js.map +0 -1
  219. package/dist/ai-service/builder-inference-auth.d.ts +0 -11
  220. package/dist/ai-service/builder-inference-auth.d.ts.map +0 -1
  221. package/dist/ai-service/builder-inference-auth.js +0 -48
  222. package/dist/ai-service/builder-inference-auth.js.map +0 -1
  223. package/dist/ai-service/builder-runtime.d.ts +0 -181
  224. package/dist/ai-service/builder-runtime.d.ts.map +0 -1
  225. package/dist/ai-service/builder-runtime.js +0 -337
  226. package/dist/ai-service/builder-runtime.js.map +0 -1
  227. package/dist/ai-service/builder-session-store.d.ts +0 -26
  228. package/dist/ai-service/builder-session-store.d.ts.map +0 -1
  229. package/dist/ai-service/builder-session-store.js +0 -66
  230. package/dist/ai-service/builder-session-store.js.map +0 -1
  231. package/dist/ai-service/builder-tool-callbacks.d.ts +0 -26
  232. package/dist/ai-service/builder-tool-callbacks.d.ts.map +0 -1
  233. package/dist/ai-service/builder-tool-callbacks.js +0 -114
  234. package/dist/ai-service/builder-tool-callbacks.js.map +0 -1
  235. package/dist/ai-service/builder-tools.d.ts +0 -46
  236. package/dist/ai-service/builder-tools.d.ts.map +0 -1
  237. package/dist/ai-service/builder-tools.js +0 -181
  238. package/dist/ai-service/builder-tools.js.map +0 -1
  239. package/dist/ai-service/chat/switch-chat-seed.d.ts +0 -8
  240. package/dist/ai-service/chat/switch-chat-seed.d.ts.map +0 -1
  241. package/dist/ai-service/chat/switch-chat-seed.js +0 -47
  242. package/dist/ai-service/chat/switch-chat-seed.js.map +0 -1
  243. package/dist/ai-service/llm/request-settings.d.ts +0 -6
  244. package/dist/ai-service/llm/request-settings.d.ts.map +0 -1
  245. package/dist/ai-service/llm/request-settings.js +0 -21
  246. package/dist/ai-service/llm/request-settings.js.map +0 -1
  247. package/dist/ai-service/native-callback-order.d.ts +0 -7
  248. package/dist/ai-service/native-callback-order.d.ts.map +0 -1
  249. package/dist/ai-service/native-callback-order.js +0 -39
  250. package/dist/ai-service/native-callback-order.js.map +0 -1
  251. package/dist/ai-service/native-secret-redactor.d.ts +0 -4
  252. package/dist/ai-service/native-secret-redactor.d.ts.map +0 -1
  253. package/dist/ai-service/native-secret-redactor.js +0 -34
  254. package/dist/ai-service/native-secret-redactor.js.map +0 -1
  255. package/dist/ai-service/policy-gate-review.d.ts +0 -42
  256. package/dist/ai-service/policy-gate-review.d.ts.map +0 -1
  257. package/dist/ai-service/policy-gate-review.js +0 -664
  258. package/dist/ai-service/policy-gate-review.js.map +0 -1
  259. package/dist/ai-service/skills/system/native.d.ts +0 -7
  260. package/dist/ai-service/skills/system/native.d.ts.map +0 -1
  261. package/dist/ai-service/skills/system/native.js +0 -25
  262. package/dist/ai-service/skills/system/native.js.map +0 -1
  263. package/dist/ai-service/skills/system/paths.d.ts +0 -2
  264. package/dist/ai-service/skills/system/paths.d.ts.map +0 -1
  265. package/dist/ai-service/skills/system/paths.js +0 -2
  266. package/dist/ai-service/skills/system/paths.js.map +0 -1
  267. package/dist/git-service/credential-scope.d.ts +0 -16
  268. package/dist/git-service/credential-scope.d.ts.map +0 -1
  269. package/dist/git-service/credential-scope.js +0 -91
  270. package/dist/git-service/credential-scope.js.map +0 -1
  271. package/dist/review-runner.d.ts +0 -25
  272. package/dist/review-runner.d.ts.map +0 -1
  273. package/dist/review-runner.js +0 -196
  274. package/dist/review-runner.js.map +0 -1
  275. package/dist/sync-service/draft-mutation.d.ts +0 -11
  276. package/dist/sync-service/draft-mutation.d.ts.map +0 -1
  277. package/dist/sync-service/draft-mutation.js +0 -121
  278. package/dist/sync-service/draft-mutation.js.map +0 -1
  279. package/dist/sync-service/native-draft-session.d.ts +0 -22
  280. package/dist/sync-service/native-draft-session.d.ts.map +0 -1
  281. package/dist/sync-service/native-draft-session.js +0 -229
  282. package/dist/sync-service/native-draft-session.js.map +0 -1
  283. package/dist/sync-service/safe-directory-reader.d.ts +0 -24
  284. package/dist/sync-service/safe-directory-reader.d.ts.map +0 -1
  285. package/dist/sync-service/safe-directory-reader.js +0 -124
  286. package/dist/sync-service/safe-directory-reader.js.map +0 -1
@@ -1,660 +0,0 @@
1
- import { randomUUID } from "node:crypto";
2
- import { z } from "zod";
3
- import { AiGenerationState, AiMode, promptCostTokensFrom, } from "@superblocksteam/library-shared/types";
4
- import { getAiQuotaPaywallReasonFromMessage, } from "@superblocksteam/shared";
5
- import { BuilderRuntimeError, } from "./builder-runtime.js";
6
- import { BuilderSessionStore } from "./builder-session-store.js";
7
- import { NativeCallbackOrder } from "./native-callback-order.js";
8
- import { RequestDeduplicator } from "./request-deduplicator.js";
9
- import { paywallStopAdvice } from "./state-machine/helpers/prompt-failure.js";
10
- export class BuilderConversation {
11
- deps;
12
- requests = new RequestDeduplicator();
13
- sessions;
14
- turn;
15
- completion = Promise.resolve();
16
- snapshot = {
17
- state: AiGenerationState.IDLE,
18
- };
19
- pendingPermission;
20
- pendingQuestion;
21
- constructor(deps) {
22
- this.deps = deps;
23
- this.sessions = deps.sessionStore ?? new BuilderSessionStore();
24
- }
25
- get isBusy() {
26
- return this.turn !== undefined;
27
- }
28
- get generationId() {
29
- return this.snapshot.generationId;
30
- }
31
- get state() {
32
- return this.snapshot;
33
- }
34
- get activeInteractiveMessage() {
35
- return this.pendingQuestion?.message;
36
- }
37
- hasRequest(requestId) {
38
- return requestId !== undefined && this.requests.has(requestId);
39
- }
40
- get isAwaitingUser() {
41
- return (this.snapshot.state === AiGenerationState.AWAITING_USER_INPUT ||
42
- this.snapshot.state === AiGenerationState.COMPLETED);
43
- }
44
- async start(request) {
45
- if (request.requestId && this.requests.has(request.requestId))
46
- return this.generationId;
47
- if (request.responseMetadata?.type === "multi_choice_response") {
48
- const pending = this.pendingQuestion;
49
- if (!pending ||
50
- pending.message.id !== request.responseMetadata.responseToMessageId)
51
- throw new Error("No matching pending native question");
52
- const answers = request.responseMetadata.selectedChoiceIndices.map((index) => {
53
- const choice = pending.message.choices[index];
54
- if (choice === undefined)
55
- throw new Error("Invalid native question answer");
56
- return typeof choice === "string" ? choice : choice.text;
57
- });
58
- if (request.prompt.trim())
59
- answers.push(request.prompt.trim());
60
- if (!answers.length)
61
- throw new Error("An answer is required");
62
- const turn = this.turn;
63
- if (!turn || turn.controller.signal.aborted)
64
- throw new DOMException("Builder turn ended", "AbortError");
65
- if (pending.responding)
66
- throw new Error("Question response is already being applied");
67
- pending.responding = true;
68
- if (request.requestId)
69
- this.requests.markIfNew(request.requestId);
70
- await this.publish({
71
- role: "user",
72
- type: "multi_choice_response",
73
- content: request.prompt,
74
- responseToMessageId: pending.message.id,
75
- selectedChoiceIndices: request.responseMetadata.selectedChoiceIndices,
76
- });
77
- turn.controller.signal.throwIfAborted();
78
- await this.publishState(AiGenerationState.GENERATING, turn.id);
79
- turn.controller.signal.throwIfAborted();
80
- this.pendingQuestion = undefined;
81
- pending.resolve(answers);
82
- return this.generationId;
83
- }
84
- if (request.responseMetadata?.type === "tool_permission_response") {
85
- await this.respondToPermission({
86
- messageId: request.responseMetadata.responseToMessageId,
87
- approved: request.responseMetadata.approved === true,
88
- requestId: request.requestId,
89
- });
90
- return this.generationId;
91
- }
92
- if (this.turn)
93
- throw new Error("Service is busy");
94
- if (request.promptContext?.attachments?.length)
95
- throw new Error("Native builder does not yet support image or file attachments");
96
- if (request.requestId)
97
- this.requests.markIfNew(request.requestId);
98
- const turn = {
99
- id: randomUUID(),
100
- controller: new AbortController(),
101
- completion: Promise.resolve(),
102
- events: Promise.resolve(),
103
- calls: new Set(),
104
- results: new Set(),
105
- plans: new Set(),
106
- startedAt: new Date().toISOString(),
107
- model: this.deps.runtime.model,
108
- callbacks: new NativeCallbackOrder(),
109
- };
110
- this.turn = turn;
111
- this.snapshot = {
112
- state: AiGenerationState.GENERATING,
113
- generationId: turn.id,
114
- };
115
- let userPublished = false;
116
- try {
117
- const prepared = await this.deps.prepare(request, turn.controller.signal, turn.id);
118
- turn.controller.signal.throwIfAborted();
119
- const provider = this.deps.runtime.providerFor(this.deps.runtime.model);
120
- const scope = `${prepared.scope}:${provider}:${this.deps.runtime.model}`;
121
- let session = prepared.conversationId
122
- ? {
123
- conversationId: prepared.conversationId,
124
- sessionId: prepared.nativeSessionId,
125
- }
126
- : await this.sessions.get(scope);
127
- if (!session) {
128
- session = {
129
- conversationId: prepared.conversationId ?? BuilderSessionStore.identity(scope),
130
- };
131
- await this.sessions.set(scope, session);
132
- }
133
- await this.publishUser(request);
134
- userPublished = true;
135
- await this.publishState(AiGenerationState.GENERATING, turn.id);
136
- const tools = await this.deps.tools.prepare({
137
- provider,
138
- request,
139
- abortSignal: turn.controller.signal,
140
- peer: this.deps.peer,
141
- requestPermission: (permission) => this.requestPermission(permission),
142
- });
143
- turn.controller.signal.throwIfAborted();
144
- turn.completion = this.run(turn, {
145
- ...tools,
146
- callbackTools: this.callbackTools(turn, request),
147
- mode: request.mode === AiMode.PLAN ? "plan" : "build",
148
- runId: request.requestId
149
- ? BuilderSessionStore.identity(`${session.conversationId}:${request.requestId}`)
150
- : turn.id,
151
- conversationId: session.conversationId,
152
- actorId: prepared.actorId,
153
- inference: prepared.inference,
154
- sessionId: session.sessionId,
155
- model: this.deps.runtime.model,
156
- prompt: request.prompt,
157
- signal: turn.controller.signal,
158
- onEvent: (event) => {
159
- if (this.turn !== turn || turn.controller.signal.aborted)
160
- return;
161
- turn.events = turn.events
162
- .then(async () => {
163
- if (turn.controller.signal.aborted)
164
- return;
165
- if (event.type === "session") {
166
- if (prepared.saveSession)
167
- await this.persistSession(turn, prepared.saveSession(event.sessionId));
168
- session.sessionId = event.sessionId;
169
- await this.sessions.set(scope, session);
170
- }
171
- else
172
- await this.event(turn, event);
173
- })
174
- .catch((error) => {
175
- turn.controller.abort(error);
176
- this.deps.onError?.(error);
177
- });
178
- return turn.events;
179
- },
180
- });
181
- this.completion = turn.completion;
182
- return turn.id;
183
- }
184
- catch (error) {
185
- const paywallReason = error instanceof Error
186
- ? getAiQuotaPaywallReasonFromMessage(error.message)
187
- : undefined;
188
- if (paywallReason && !turn.controller.signal.aborted) {
189
- if (!userPublished)
190
- await this.publishUser(request);
191
- await this.publish({
192
- role: "assistant",
193
- type: "text",
194
- content: paywallStopAdvice(paywallReason),
195
- });
196
- await this.settle(turn, error, paywallReason);
197
- return turn.id;
198
- }
199
- if (request.requestId)
200
- this.requests.forget(request.requestId);
201
- await this.settle(turn, error);
202
- throw error;
203
- }
204
- }
205
- async persistSession(turn, saved) {
206
- const signal = turn.controller.signal;
207
- signal.throwIfAborted();
208
- let onAbort = () => { };
209
- let timeout;
210
- const interrupted = new Promise((_, reject) => {
211
- onAbort = () => reject(signal.reason);
212
- signal.addEventListener("abort", onAbort, { once: true });
213
- timeout = setTimeout(() => reject(new Error("Native session persistence timed out")), 30_000);
214
- timeout.unref();
215
- });
216
- try {
217
- await Promise.race([saved, interrupted]);
218
- }
219
- finally {
220
- signal.removeEventListener("abort", onAbort);
221
- clearTimeout(timeout);
222
- }
223
- }
224
- async run(turn, invocation) {
225
- let failure;
226
- try {
227
- await this.deps.runtime.invoke(invocation);
228
- }
229
- catch (error) {
230
- failure = error;
231
- if (!turn.controller.signal.aborted) {
232
- this.deps.onError?.(error);
233
- await this.publish({
234
- role: "assistant",
235
- type: "error",
236
- content: error instanceof BuilderRuntimeError
237
- ? error.message
238
- : "The native builder stopped before completing this request. Its outcome may be incomplete; inspect the app before sending another prompt.",
239
- });
240
- }
241
- }
242
- finally {
243
- try {
244
- await this.deps.afterRun?.();
245
- }
246
- catch (error) {
247
- failure ??= error;
248
- this.deps.onError?.(error);
249
- }
250
- try {
251
- await turn.events;
252
- await this.flushText(turn);
253
- await this.deps.chat.flushPendingPushes();
254
- }
255
- catch (error) {
256
- failure ??= error;
257
- this.deps.onError?.(error);
258
- }
259
- this.clearPermission(new DOMException("Builder turn ended", "AbortError"));
260
- await this.settle(turn, failure);
261
- }
262
- }
263
- async cancel() {
264
- const turn = this.turn;
265
- if (!turn)
266
- return;
267
- turn.controller.abort(new DOMException("Builder turn cancelled", "AbortError"));
268
- this.clearPermission(turn.controller.signal.reason);
269
- await turn.completion;
270
- await this.settle(turn, turn.controller.signal.reason);
271
- }
272
- async settle(turn, failure, paywallReason) {
273
- if (turn.settled)
274
- return;
275
- turn.settled = true;
276
- if (this.turn === turn) {
277
- this.turn = undefined;
278
- await this.publishState(failure || turn.controller.signal.aborted
279
- ? AiGenerationState.CANCELLED
280
- : AiGenerationState.COMPLETED, turn.id, !!failure && !turn.controller.signal.aborted && !paywallReason, paywallReason);
281
- }
282
- this.deps.onSettled(turn.id, failure ?? turn.controller.signal.reason);
283
- }
284
- waitForCompletion() {
285
- return this.completion;
286
- }
287
- async reset() {
288
- await this.cancel();
289
- await this.deps.chat.newChat();
290
- await this.sessions.clear();
291
- this.snapshot = { state: AiGenerationState.IDLE, chatSessionReset: true };
292
- await this.push(() => this.deps.peer.call.aiSetGenerationState(this.snapshot));
293
- }
294
- async requestPermission(request) {
295
- const turn = this.turn;
296
- if (!turn || turn.controller.signal.aborted)
297
- throw new DOMException("Builder turn ended", "AbortError");
298
- if (this.pendingPermission)
299
- throw new Error("A tool permission request is already pending");
300
- const result = new Promise((resolve, reject) => {
301
- this.pendingPermission = { request, resolve, reject };
302
- });
303
- void result.catch(() => { });
304
- await this.flushText(turn);
305
- await this.publishState(AiGenerationState.AWAITING_USER_INPUT, turn.id);
306
- await this.deliverPermission(request);
307
- return result;
308
- }
309
- async respondToPermission(response) {
310
- if (response.requestId && this.requests.has(response.requestId))
311
- return;
312
- const pending = this.pendingPermission;
313
- if (!pending || pending.request.toolCallId !== response.messageId)
314
- throw new Error("No matching pending tool permission");
315
- if (!this.turn || this.turn.controller.signal.aborted)
316
- throw new Error("Builder turn ended");
317
- const turn = this.turn;
318
- if (pending.responding)
319
- throw new Error("Permission response is already being applied");
320
- pending.responding = true;
321
- if (response.requestId)
322
- this.requests.markIfNew(response.requestId);
323
- await this.push(() => this.deps.peer.call.aiClearToolPermission());
324
- turn.controller.signal.throwIfAborted();
325
- await this.publishState(AiGenerationState.GENERATING, turn.id);
326
- turn.controller.signal.throwIfAborted();
327
- this.pendingPermission = undefined;
328
- pending.resolve(response.approved);
329
- }
330
- async replay() {
331
- await this.push(() => this.deps.peer.call.aiSetGenerationState(this.snapshot));
332
- if (this.pendingPermission)
333
- await this.deliverPermission(this.pendingPermission.request);
334
- else
335
- await this.push(() => this.deps.peer.call.aiClearToolPermission());
336
- if (this.pendingQuestion)
337
- await this.push(() => this.deps.peer.call.aiPushMessage(this.pendingQuestion.message));
338
- const text = this.turn?.text;
339
- if (text)
340
- await this.push(() => this.deps.peer.call.aiPushMessage({
341
- id: text.id,
342
- role: "assistant",
343
- type: "text",
344
- textBlockId: text.id,
345
- content: text.value,
346
- }));
347
- }
348
- async event(turn, event) {
349
- switch (event.type) {
350
- case "callback-ready":
351
- await this.flushText(turn);
352
- turn.callbacks.arrive(event.id);
353
- break;
354
- case "text-delta": {
355
- turn.text ??= { id: randomUUID(), value: "" };
356
- turn.text.value += event.text;
357
- await this.push(() => this.deps.peer.call.aiPushMessage({
358
- id: randomUUID(),
359
- role: "assistant",
360
- type: "text-delta",
361
- textBlockId: turn.text.id,
362
- content: event.text,
363
- }));
364
- break;
365
- }
366
- case "plan":
367
- if (turn.plans.has(event.id))
368
- return;
369
- turn.plans.add(event.id);
370
- await this.flushText(turn);
371
- await this.publish({
372
- role: "assistant",
373
- type: "plan",
374
- content: event.text,
375
- title: "Plan",
376
- streaming: false,
377
- });
378
- break;
379
- case "tool-call":
380
- if (turn.calls.has(event.toolCallId))
381
- return;
382
- turn.calls.add(event.toolCallId);
383
- await this.flushText(turn);
384
- await this.publish({
385
- role: "assistant",
386
- type: "tool",
387
- tool: event.toolName,
388
- args: event.input,
389
- toolCallId: event.toolCallId,
390
- content: "",
391
- id: event.toolCallId,
392
- });
393
- break;
394
- case "tool-result":
395
- if (turn.results.has(event.toolCallId))
396
- return;
397
- turn.results.add(event.toolCallId);
398
- await this.flushText(turn);
399
- await this.publish(event.isError
400
- ? {
401
- role: "assistant",
402
- type: "tool-error",
403
- toolCallId: event.toolCallId,
404
- toolName: event.toolName,
405
- error: String(event.output),
406
- content: "",
407
- }
408
- : {
409
- role: "assistant",
410
- type: "tool-result",
411
- toolCallId: event.toolCallId,
412
- toolName: event.toolName,
413
- output: event.output,
414
- content: "",
415
- });
416
- break;
417
- case "finish":
418
- if (turn.usageRecorded)
419
- return;
420
- turn.usageRecorded = true;
421
- await this.push(() => this.deps.peer.call.aiPushTokenUsage({
422
- requestId: turn.id,
423
- inputTokens: event.usage.inputTokens,
424
- outputTokens: event.usage.outputTokens,
425
- totalTokens: event.usage.inputTokens + event.usage.outputTokens,
426
- cachedInputTokens: event.usage.cacheReadTokens,
427
- model: turn.model,
428
- startTime: turn.startedAt,
429
- endTime: new Date().toISOString(),
430
- }));
431
- try {
432
- await this.deps.onUsage?.(turn.id, {
433
- model: turn.model,
434
- provider: this.deps.runtime.providerFor(turn.model),
435
- ...promptCostTokensFrom({
436
- inputTokens: event.usage.inputTokens,
437
- outputTokens: event.usage.outputTokens,
438
- inputTokenDetails: {
439
- cacheReadTokens: event.usage.cacheReadTokens,
440
- cacheWriteTokens: event.usage.cacheWriteTokens,
441
- },
442
- }),
443
- });
444
- }
445
- catch (error) {
446
- this.deps.onError?.(error);
447
- }
448
- break;
449
- case "session":
450
- case "error":
451
- break;
452
- }
453
- }
454
- async flushText(turn) {
455
- const text = turn.text;
456
- if (!text)
457
- return;
458
- turn.text = undefined;
459
- await this.deps.chat.recordAssistant({
460
- id: text.id,
461
- role: "assistant",
462
- type: "text",
463
- textBlockId: text.id,
464
- content: text.value,
465
- });
466
- }
467
- async publish(message) {
468
- const payload = {
469
- ...message,
470
- id: message.id ?? randomUUID(),
471
- timestamp: message.timestamp ?? Date.now(),
472
- };
473
- await this.deps.chat.recordAssistant(payload);
474
- await this.push(() => this.deps.peer.call.aiPushMessage(payload));
475
- }
476
- async publishUser(request) {
477
- const response = request.responseMetadata;
478
- const message = {
479
- id: request.requestId ?? randomUUID(),
480
- role: "user",
481
- content: request.prompt,
482
- mode: request.mode,
483
- timestamp: Date.now(),
484
- ...(response?.type === "plan_response"
485
- ? {
486
- type: response.type,
487
- responseToMessageId: response.responseToMessageId,
488
- approved: response.approved ?? true,
489
- enableTesting: response.enableTesting,
490
- }
491
- : { type: "text" }),
492
- };
493
- if (response?.type === "plan_response") {
494
- await this.deps.chat.recordUserResponse(request.prompt, response.type, {
495
- mode: request.mode,
496
- messageId: message.id,
497
- responseToMessageId: response.responseToMessageId,
498
- approved: response.approved ?? true,
499
- enableTesting: response.enableTesting,
500
- });
501
- }
502
- else {
503
- await this.deps.chat.recordUser(request.prompt, {
504
- mode: request.mode,
505
- messageId: message.id,
506
- });
507
- }
508
- await this.push(() => this.deps.peer.call.aiPushMessage(message));
509
- }
510
- async publishState(state, generationId, hasError = false, paywallReason) {
511
- this.snapshot = {
512
- state,
513
- generationId,
514
- ...(hasError ? { hasError: true } : {}),
515
- ...(paywallReason ? { paywallReason } : {}),
516
- };
517
- await this.push(() => this.deps.peer.call.aiSetGenerationState(this.snapshot));
518
- }
519
- deliverPermission(request) {
520
- return this.push(() => this.deps.peer.call.aiRequestToolPermission({
521
- messageId: request.toolCallId,
522
- toolName: request.toolName,
523
- actionName: request.actionName,
524
- formattedInput: request.formattedInput,
525
- rawInput: request.input,
526
- permissions: request.permissions,
527
- }));
528
- }
529
- clearPermission(error) {
530
- const pending = this.pendingPermission;
531
- this.pendingPermission = undefined;
532
- pending?.reject(error);
533
- this.pendingQuestion?.reject(error);
534
- this.pendingQuestion = undefined;
535
- if (pending)
536
- void this.push(() => this.deps.peer.call.aiClearToolPermission());
537
- }
538
- callbackTools(turn, request) {
539
- return {
540
- __runtime_approval: {
541
- inputSchema: z.object({
542
- method: z.enum([
543
- "item/commandExecution/requestApproval",
544
- "item/fileChange/requestApproval",
545
- ]),
546
- params: z.record(z.unknown()),
547
- }),
548
- execute: async ({ method, params }, options) => {
549
- if (request.mode === AiMode.PLAN)
550
- return { decision: "decline" };
551
- const approved = await this.requestPermission({
552
- toolCallId: options.toolCallId,
553
- toolName: method === "item/fileChange/requestApproval"
554
- ? "apply_patch"
555
- : typeof params.toolName === "string"
556
- ? params.toolName
557
- : "exec_command",
558
- input: params,
559
- formattedInput: {
560
- targets: [
561
- typeof params.command === "string"
562
- ? params.command
563
- : "Native builder action",
564
- ],
565
- },
566
- permissions: [],
567
- });
568
- return { decision: approved ? "accept" : "decline" };
569
- },
570
- },
571
- __runtime_user_input: {
572
- inputSchema: z.object({
573
- method: z.enum(["item/tool/requestUserInput"]),
574
- afterEventId: z.string().uuid().optional(),
575
- params: z
576
- .object({
577
- questions: z
578
- .array(z.object({
579
- id: z.string().optional(),
580
- question: z.string(),
581
- header: z.string().optional(),
582
- multiSelect: z.boolean().optional(),
583
- options: z
584
- .array(z.object({
585
- label: z.string(),
586
- description: z.string().optional(),
587
- }))
588
- .optional(),
589
- }))
590
- .min(1),
591
- })
592
- .passthrough(),
593
- }),
594
- execute: async ({ params, afterEventId }, options) => {
595
- const signal = options.abortSignal ?? turn.controller.signal;
596
- if (afterEventId) {
597
- if (afterEventId !== options.toolCallId)
598
- throw new Error("Native callback does not match its event");
599
- await turn.callbacks.wait(afterEventId, signal);
600
- }
601
- const answers = {};
602
- for (const [index, question] of params.questions.entries()) {
603
- await turn.events;
604
- signal.throwIfAborted();
605
- await this.flushText(turn);
606
- signal.throwIfAborted();
607
- const message = {
608
- role: "assistant",
609
- type: "multi_choice",
610
- content: question.question,
611
- subtitle: question.header,
612
- id: `${options.toolCallId}:${index}`,
613
- choices: question.options?.map((option) => ({
614
- text: option.label,
615
- subText: option.description,
616
- })) ?? [],
617
- selectionType: question.multiSelect ? "multiple" : "single",
618
- };
619
- const result = new Promise((resolve, reject) => {
620
- this.pendingQuestion = { message, resolve, reject };
621
- });
622
- void result.catch(() => { });
623
- await this.publishState(AiGenerationState.AWAITING_USER_INPUT, turn.id);
624
- signal.throwIfAborted();
625
- await this.publish(message);
626
- const values = await result;
627
- answers[question.id ?? String(index)] = { answers: values };
628
- }
629
- return { answers };
630
- },
631
- },
632
- };
633
- }
634
- async push(send) {
635
- const signal = this.turn?.controller.signal;
636
- const sent = Promise.resolve()
637
- .then(send)
638
- .catch((error) => this.deps.onError?.(error));
639
- if (!signal || signal.aborted) {
640
- void sent;
641
- return;
642
- }
643
- let onAbort;
644
- let timeout;
645
- const interrupted = new Promise((resolve) => {
646
- onAbort = resolve;
647
- signal.addEventListener("abort", onAbort, { once: true });
648
- timeout = setTimeout(resolve, 5_000);
649
- timeout.unref();
650
- });
651
- try {
652
- await Promise.race([sent, interrupted]);
653
- }
654
- finally {
655
- signal.removeEventListener("abort", onAbort);
656
- clearTimeout(timeout);
657
- }
658
- }
659
- }
660
- //# sourceMappingURL=builder-conversation.js.map