@terreno/ai 57.9.0 → 57.11.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 (417) hide show
  1. package/README.md +1 -0
  2. package/bunfig.toml +1 -1
  3. package/dist/aiApp.d.ts +8 -2
  4. package/dist/aiApp.js +12 -6
  5. package/dist/harness/adminScreens.d.ts +6 -0
  6. package/dist/harness/adminScreens.js +11 -0
  7. package/dist/harness/agentLoop.d.ts +61 -0
  8. package/dist/harness/agentLoop.js +798 -0
  9. package/dist/harness/agentTaskNames.d.ts +4 -0
  10. package/dist/harness/agentTaskNames.js +7 -0
  11. package/dist/harness/approvalPolicy.d.ts +3 -0
  12. package/dist/harness/approvalPolicy.js +23 -0
  13. package/dist/harness/approvals.d.ts +101 -0
  14. package/dist/harness/approvals.js +468 -0
  15. package/dist/harness/commit.d.ts +238 -0
  16. package/dist/harness/commit.js +678 -0
  17. package/dist/harness/conversation.d.ts +75 -0
  18. package/dist/harness/conversation.js +397 -0
  19. package/dist/harness/defineAgent.d.ts +9 -0
  20. package/dist/harness/defineAgent.js +65 -0
  21. package/dist/harness/defineTask.d.ts +11 -0
  22. package/dist/harness/defineTask.js +59 -0
  23. package/dist/harness/defineTool.d.ts +7 -0
  24. package/dist/harness/defineTool.js +36 -0
  25. package/dist/harness/definitionError.d.ts +8 -0
  26. package/dist/harness/definitionError.js +15 -0
  27. package/dist/harness/errors.d.ts +133 -0
  28. package/dist/harness/errors.js +107 -0
  29. package/dist/harness/eventHub.d.ts +45 -0
  30. package/dist/harness/eventHub.js +135 -0
  31. package/dist/harness/events.d.ts +95 -0
  32. package/dist/harness/events.js +267 -0
  33. package/dist/harness/executionEnv.d.ts +31 -0
  34. package/dist/harness/executionEnv.js +2 -0
  35. package/dist/harness/extensions.d.ts +82 -0
  36. package/dist/harness/extensions.js +274 -0
  37. package/dist/harness/harness.d.ts +163 -0
  38. package/dist/harness/harness.js +522 -0
  39. package/dist/harness/harnessApp.d.ts +44 -0
  40. package/dist/harness/harnessApp.js +62 -0
  41. package/dist/harness/internalRuntime.d.ts +16 -0
  42. package/dist/harness/internalRuntime.js +18 -0
  43. package/dist/harness/leases.d.ts +35 -0
  44. package/dist/harness/leases.js +86 -0
  45. package/dist/harness/memo.d.ts +24 -0
  46. package/dist/harness/memo.js +78 -0
  47. package/dist/harness/modelCall.d.ts +45 -0
  48. package/dist/harness/modelCall.js +147 -0
  49. package/dist/harness/models/harnessApproval.d.ts +2 -0
  50. package/dist/harness/models/harnessApproval.js +100 -0
  51. package/dist/harness/models/harnessConversation.d.ts +2 -0
  52. package/dist/harness/models/harnessConversation.js +117 -0
  53. package/dist/harness/models/harnessEvent.d.ts +3 -0
  54. package/dist/harness/models/harnessEvent.js +73 -0
  55. package/dist/harness/models/harnessInboxEvent.d.ts +2 -0
  56. package/dist/harness/models/harnessInboxEvent.js +58 -0
  57. package/dist/harness/models/harnessMemo.d.ts +2 -0
  58. package/dist/harness/models/harnessMemo.js +42 -0
  59. package/dist/harness/models/harnessMessage.d.ts +2 -0
  60. package/dist/harness/models/harnessMessage.js +73 -0
  61. package/dist/harness/models/harnessOwner.d.ts +2 -0
  62. package/dist/harness/models/harnessOwner.js +36 -0
  63. package/dist/harness/models/harnessTask.d.ts +2 -0
  64. package/dist/harness/models/harnessTask.js +191 -0
  65. package/dist/harness/ownership.d.ts +89 -0
  66. package/dist/harness/ownership.js +438 -0
  67. package/dist/harness/registry.d.ts +29 -0
  68. package/dist/harness/registry.js +93 -0
  69. package/dist/harness/retryBackoff.d.ts +31 -0
  70. package/dist/harness/retryBackoff.js +50 -0
  71. package/dist/harness/routes/approvals.d.ts +13 -0
  72. package/dist/harness/routes/approvals.js +93 -0
  73. package/dist/harness/routes/conversations.d.ts +11 -0
  74. package/dist/harness/routes/conversations.js +66 -0
  75. package/dist/harness/routes/events.d.ts +19 -0
  76. package/dist/harness/routes/events.js +215 -0
  77. package/dist/harness/routes/tasks.d.ts +11 -0
  78. package/dist/harness/routes/tasks.js +108 -0
  79. package/dist/harness/runners/inProcessRunner.d.ts +64 -0
  80. package/dist/harness/runners/inProcessRunner.js +227 -0
  81. package/dist/harness/runners/jobsRunner.d.ts +62 -0
  82. package/dist/harness/runners/jobsRunner.js +212 -0
  83. package/dist/harness/runtime.d.ts +52 -0
  84. package/dist/harness/runtime.js +604 -0
  85. package/dist/harness/subagent.d.ts +53 -0
  86. package/dist/harness/subagent.js +134 -0
  87. package/dist/harness/suspend.d.ts +8 -0
  88. package/dist/harness/suspend.js +16 -0
  89. package/dist/harness/transaction.d.ts +3 -0
  90. package/dist/harness/transaction.js +22 -0
  91. package/dist/harness/waits.d.ts +100 -0
  92. package/dist/harness/waits.js +214 -0
  93. package/dist/index.d.ts +19 -1
  94. package/dist/index.js +45 -2
  95. package/dist/models/aiDataset.d.ts +2 -0
  96. package/dist/models/aiDataset.js +67 -0
  97. package/dist/models/aiRequest.js +1 -1
  98. package/dist/models/gptHistory.js +73 -0
  99. package/dist/observability/adminScreens.d.ts +5 -0
  100. package/dist/observability/adminScreens.js +32 -0
  101. package/dist/observability/compileTemplate.d.ts +1 -0
  102. package/dist/observability/compileTemplate.js +13 -0
  103. package/dist/observability/datasetImport.d.ts +11 -0
  104. package/dist/observability/datasetImport.js +185 -0
  105. package/dist/observability/evaluate.d.ts +31 -0
  106. package/dist/observability/evaluate.js +200 -0
  107. package/dist/observability/evaluatorTemplates.d.ts +17 -0
  108. package/dist/observability/evaluatorTemplates.js +111 -0
  109. package/dist/observability/jsonPath.d.ts +2 -0
  110. package/dist/observability/jsonPath.js +57 -0
  111. package/dist/observability/local/datasetStore.d.ts +87 -0
  112. package/dist/observability/local/datasetStore.js +326 -0
  113. package/dist/observability/local/evaluatorStore.d.ts +44 -0
  114. package/dist/observability/local/evaluatorStore.js +182 -0
  115. package/dist/observability/local/experimentRunner.d.ts +96 -0
  116. package/dist/observability/local/experimentRunner.js +419 -0
  117. package/dist/observability/local/localPlugin.d.ts +22 -0
  118. package/dist/observability/local/localPlugin.js +76 -0
  119. package/dist/observability/local/models/obsDataset.d.ts +2 -0
  120. package/dist/observability/local/models/obsDataset.js +37 -0
  121. package/dist/observability/local/models/obsDatasetItem.d.ts +2 -0
  122. package/dist/observability/local/models/obsDatasetItem.js +76 -0
  123. package/dist/observability/local/models/obsEvaluator.d.ts +2 -0
  124. package/dist/observability/local/models/obsEvaluator.js +98 -0
  125. package/dist/observability/local/models/obsExperiment.d.ts +2 -0
  126. package/dist/observability/local/models/obsExperiment.js +87 -0
  127. package/dist/observability/local/models/obsExperimentItem.d.ts +2 -0
  128. package/dist/observability/local/models/obsExperimentItem.js +45 -0
  129. package/dist/observability/local/models/obsPrompt.d.ts +2 -0
  130. package/dist/observability/local/models/obsPrompt.js +42 -0
  131. package/dist/observability/local/models/obsPromptLabel.d.ts +2 -0
  132. package/dist/observability/local/models/obsPromptLabel.js +39 -0
  133. package/dist/observability/local/models/obsPromptVersion.d.ts +2 -0
  134. package/dist/observability/local/models/obsPromptVersion.js +96 -0
  135. package/dist/observability/local/models/obsReviewItem.d.ts +2 -0
  136. package/dist/observability/local/models/obsReviewItem.js +75 -0
  137. package/dist/observability/local/models/obsScore.d.ts +2 -0
  138. package/dist/observability/local/models/obsScore.js +58 -0
  139. package/dist/observability/local/models/obsSpan.d.ts +2 -0
  140. package/dist/observability/local/models/obsSpan.js +64 -0
  141. package/dist/observability/local/models/obsTrace.d.ts +2 -0
  142. package/dist/observability/local/models/obsTrace.js +93 -0
  143. package/dist/observability/local/promptStore.d.ts +180 -0
  144. package/dist/observability/local/promptStore.js +444 -0
  145. package/dist/observability/local/reviewStore.d.ts +50 -0
  146. package/dist/observability/local/reviewStore.js +203 -0
  147. package/dist/observability/local/traceStore.d.ts +103 -0
  148. package/dist/observability/local/traceStore.js +325 -0
  149. package/dist/observability/observabilityApp.d.ts +24 -0
  150. package/dist/observability/observabilityApp.js +157 -0
  151. package/dist/observability/observabilityAppRegistry.d.ts +14 -0
  152. package/dist/observability/observabilityAppRegistry.js +16 -0
  153. package/dist/observability/permissions.d.ts +7 -0
  154. package/dist/observability/permissions.js +60 -0
  155. package/dist/observability/reviewPanels.d.ts +18 -0
  156. package/dist/observability/reviewPanels.js +46 -0
  157. package/dist/observability/routes/datasets.d.ts +7 -0
  158. package/dist/observability/routes/datasets.js +149 -0
  159. package/dist/observability/routes/evaluators.d.ts +7 -0
  160. package/dist/observability/routes/evaluators.js +123 -0
  161. package/dist/observability/routes/experiments.d.ts +7 -0
  162. package/dist/observability/routes/experiments.js +95 -0
  163. package/dist/observability/routes/observabilityRouteAccess.d.ts +13 -0
  164. package/dist/observability/routes/observabilityRouteAccess.js +67 -0
  165. package/dist/observability/routes/prompts.d.ts +12 -0
  166. package/dist/observability/routes/prompts.js +172 -0
  167. package/dist/observability/routes/requestAiService.d.ts +13 -0
  168. package/dist/observability/routes/requestAiService.js +21 -0
  169. package/dist/observability/routes/review.d.ts +7 -0
  170. package/dist/observability/routes/review.js +79 -0
  171. package/dist/observability/routes/status.d.ts +5 -0
  172. package/dist/observability/routes/status.js +31 -0
  173. package/dist/observability/routes/testMultiStage.d.ts +9 -0
  174. package/dist/observability/routes/testMultiStage.js +67 -0
  175. package/dist/observability/routes/traces.d.ts +7 -0
  176. package/dist/observability/routes/traces.js +129 -0
  177. package/dist/observability/schemaValidation.d.ts +5 -0
  178. package/dist/observability/schemaValidation.js +26 -0
  179. package/dist/observability/sopThresholds.d.ts +2 -0
  180. package/dist/observability/sopThresholds.js +21 -0
  181. package/dist/observability/status.d.ts +23 -0
  182. package/dist/observability/status.js +34 -0
  183. package/dist/observability/testMultiStageSchemas.d.ts +10 -0
  184. package/dist/observability/testMultiStageSchemas.js +79 -0
  185. package/dist/observability/testMultiStageWorkflow.d.ts +20 -0
  186. package/dist/observability/testMultiStageWorkflow.js +218 -0
  187. package/dist/observability/types.d.ts +159 -0
  188. package/dist/observability/types.js +67 -0
  189. package/dist/routes/files.js +18 -7
  190. package/dist/routes/gpt.js +79 -689
  191. package/dist/routes/gptActions.d.ts +9 -0
  192. package/dist/routes/gptActions.js +212 -0
  193. package/dist/routes/gptDatasets.d.ts +5 -0
  194. package/dist/routes/gptDatasets.js +94 -0
  195. package/dist/routes/gptHistories.d.ts +3 -1
  196. package/dist/routes/gptHistories.js +188 -2
  197. package/dist/service/aiDatasets.d.ts +57 -0
  198. package/dist/service/aiDatasets.js +253 -0
  199. package/dist/service/aiService.d.ts +50 -1
  200. package/dist/service/aiService.js +426 -77
  201. package/dist/service/askFiles.d.ts +49 -0
  202. package/dist/service/askFiles.js +162 -0
  203. package/dist/service/asks.d.ts +76 -0
  204. package/dist/service/asks.js +284 -0
  205. package/dist/service/chatTurn.d.ts +128 -0
  206. package/dist/service/chatTurn.js +1674 -0
  207. package/dist/service/fileStorage.d.ts +4 -0
  208. package/dist/service/fileStorage.js +7 -2
  209. package/dist/service/fillActionIds.d.ts +6 -0
  210. package/dist/service/fillActionIds.js +79 -0
  211. package/dist/service/jsonFenceModel.d.ts +7 -0
  212. package/dist/service/jsonFenceModel.js +45 -0
  213. package/dist/service/prompts.d.ts +52 -0
  214. package/dist/service/prompts.js +96 -1
  215. package/dist/service/sanitizeHtml.d.ts +11 -0
  216. package/dist/service/sanitizeHtml.js +108 -0
  217. package/dist/types/harness.d.ts +952 -0
  218. package/dist/types/harness.js +133 -0
  219. package/dist/types/index.d.ts +192 -14
  220. package/dist/types/index.js +2 -0
  221. package/dist/types/observability.d.ts +288 -0
  222. package/dist/types/observability.js +2 -0
  223. package/package.json +28 -5
  224. package/src/aiApp.test.ts +207 -1
  225. package/src/aiApp.ts +19 -6
  226. package/src/harness/adminScreens.test.ts +34 -0
  227. package/src/harness/adminScreens.ts +11 -0
  228. package/src/harness/agentLoop.test.ts +1034 -0
  229. package/src/harness/agentLoop.ts +1082 -0
  230. package/src/harness/agentRecovery.test.ts +380 -0
  231. package/src/harness/agentTaskNames.ts +4 -0
  232. package/src/harness/approvalPolicy.ts +28 -0
  233. package/src/harness/approvals.test.ts +1094 -0
  234. package/src/harness/approvals.ts +644 -0
  235. package/src/harness/commit.ts +1049 -0
  236. package/src/harness/concurrency.test.ts +155 -0
  237. package/src/harness/conversation.ts +503 -0
  238. package/src/harness/defineAgent.test.ts +92 -0
  239. package/src/harness/defineAgent.ts +82 -0
  240. package/src/harness/defineTask.test.ts +59 -0
  241. package/src/harness/defineTask.ts +76 -0
  242. package/src/harness/defineTool.ts +45 -0
  243. package/src/harness/definitionError.ts +13 -0
  244. package/src/harness/errors.test.ts +90 -0
  245. package/src/harness/errors.ts +125 -0
  246. package/src/harness/eventHub.test.ts +124 -0
  247. package/src/harness/eventHub.ts +159 -0
  248. package/src/harness/events.test.ts +769 -0
  249. package/src/harness/events.ts +384 -0
  250. package/src/harness/executionEnv.ts +34 -0
  251. package/src/harness/extensions.test.ts +1218 -0
  252. package/src/harness/extensions.ts +382 -0
  253. package/src/harness/harness.test.ts +693 -0
  254. package/src/harness/harness.ts +778 -0
  255. package/src/harness/harnessApp.ts +76 -0
  256. package/src/harness/internalRuntime.ts +44 -0
  257. package/src/harness/leases.ts +132 -0
  258. package/src/harness/memo.ts +107 -0
  259. package/src/harness/modelCall.test.ts +134 -0
  260. package/src/harness/modelCall.ts +200 -0
  261. package/src/harness/models/harnessApproval.ts +109 -0
  262. package/src/harness/models/harnessConversation.ts +142 -0
  263. package/src/harness/models/harnessEvent.ts +92 -0
  264. package/src/harness/models/harnessInboxEvent.ts +73 -0
  265. package/src/harness/models/harnessMemo.ts +43 -0
  266. package/src/harness/models/harnessMessage.ts +85 -0
  267. package/src/harness/models/harnessModels.test.ts +60 -0
  268. package/src/harness/models/harnessOwner.ts +40 -0
  269. package/src/harness/models/harnessTask.ts +206 -0
  270. package/src/harness/ownership.test.ts +1087 -0
  271. package/src/harness/ownership.ts +618 -0
  272. package/src/harness/recovery.test.ts +907 -0
  273. package/src/harness/registry.ts +124 -0
  274. package/src/harness/retries.test.ts +292 -0
  275. package/src/harness/retryBackoff.test.ts +100 -0
  276. package/src/harness/retryBackoff.ts +77 -0
  277. package/src/harness/routes/approvals.ts +123 -0
  278. package/src/harness/routes/conversations.test.ts +518 -0
  279. package/src/harness/routes/conversations.ts +102 -0
  280. package/src/harness/routes/events.ts +280 -0
  281. package/src/harness/routes/tasks.test.ts +198 -0
  282. package/src/harness/routes/tasks.ts +146 -0
  283. package/src/harness/runners/inProcessRunner.test.ts +530 -0
  284. package/src/harness/runners/inProcessRunner.ts +260 -0
  285. package/src/harness/runners/jobsRunner.test.ts +709 -0
  286. package/src/harness/runners/jobsRunner.ts +252 -0
  287. package/src/harness/runtime.ts +802 -0
  288. package/src/harness/subagent.test.ts +696 -0
  289. package/src/harness/subagent.ts +192 -0
  290. package/src/harness/suspend.ts +13 -0
  291. package/src/harness/transaction.ts +17 -0
  292. package/src/harness/versioning.test.ts +288 -0
  293. package/src/harness/waits.test.ts +693 -0
  294. package/src/harness/waits.ts +356 -0
  295. package/src/index.test.ts +13 -0
  296. package/src/index.ts +70 -0
  297. package/src/isolated/adminScreensConfig.isolated.ts +143 -0
  298. package/src/models/aiDataset.test.ts +34 -0
  299. package/src/models/aiDataset.ts +78 -0
  300. package/src/models/aiRequest.ts +2 -1
  301. package/src/models/gptHistory.test.ts +156 -0
  302. package/src/models/gptHistory.ts +89 -1
  303. package/src/observability/adminScreens.test.ts +71 -0
  304. package/src/observability/adminScreens.ts +42 -0
  305. package/src/observability/compileTemplate.test.ts +14 -0
  306. package/src/observability/compileTemplate.ts +10 -0
  307. package/src/observability/datasetImport.test.ts +95 -0
  308. package/src/observability/datasetImport.ts +220 -0
  309. package/src/observability/evaluate.test.ts +549 -0
  310. package/src/observability/evaluate.ts +254 -0
  311. package/src/observability/evaluatorTemplates.ts +124 -0
  312. package/src/observability/jsonPath.test.ts +52 -0
  313. package/src/observability/jsonPath.ts +60 -0
  314. package/src/observability/local/datasetStore.test.ts +230 -0
  315. package/src/observability/local/datasetStore.ts +452 -0
  316. package/src/observability/local/evaluatorStore.test.ts +138 -0
  317. package/src/observability/local/evaluatorStore.ts +224 -0
  318. package/src/observability/local/experimentRunner.test.ts +489 -0
  319. package/src/observability/local/experimentRunner.ts +548 -0
  320. package/src/observability/local/localPlugin.ts +88 -0
  321. package/src/observability/local/models/obsDataset.ts +38 -0
  322. package/src/observability/local/models/obsDatasetItem.ts +80 -0
  323. package/src/observability/local/models/obsEvaluator.ts +102 -0
  324. package/src/observability/local/models/obsExperiment.ts +91 -0
  325. package/src/observability/local/models/obsExperimentItem.ts +52 -0
  326. package/src/observability/local/models/obsPrompt.ts +43 -0
  327. package/src/observability/local/models/obsPromptLabel.ts +43 -0
  328. package/src/observability/local/models/obsPromptVersion.ts +107 -0
  329. package/src/observability/local/models/obsReviewItem.ts +79 -0
  330. package/src/observability/local/models/obsScore.ts +59 -0
  331. package/src/observability/local/models/obsSpan.ts +65 -0
  332. package/src/observability/local/models/obsTrace.ts +94 -0
  333. package/src/observability/local/obsModels.test.ts +119 -0
  334. package/src/observability/local/promptStore.test.ts +351 -0
  335. package/src/observability/local/promptStore.ts +665 -0
  336. package/src/observability/local/reviewStore.test.ts +164 -0
  337. package/src/observability/local/reviewStore.ts +264 -0
  338. package/src/observability/local/traceStore.test.ts +264 -0
  339. package/src/observability/local/traceStore.ts +440 -0
  340. package/src/observability/observabilityApp.test.ts +92 -0
  341. package/src/observability/observabilityApp.ts +185 -0
  342. package/src/observability/observabilityAppRegistry.test.ts +30 -0
  343. package/src/observability/observabilityAppRegistry.ts +35 -0
  344. package/src/observability/permissions.test.ts +96 -0
  345. package/src/observability/permissions.ts +78 -0
  346. package/src/observability/reviewPanels.test.ts +35 -0
  347. package/src/observability/reviewPanels.ts +64 -0
  348. package/src/observability/routes/datasets.test.ts +176 -0
  349. package/src/observability/routes/datasets.ts +252 -0
  350. package/src/observability/routes/evaluators.test.ts +119 -0
  351. package/src/observability/routes/evaluators.ts +207 -0
  352. package/src/observability/routes/experiments.test.ts +282 -0
  353. package/src/observability/routes/experiments.ts +152 -0
  354. package/src/observability/routes/observabilityRbac.test.ts +767 -0
  355. package/src/observability/routes/observabilityRouteAccess.ts +106 -0
  356. package/src/observability/routes/prompts.test.ts +320 -0
  357. package/src/observability/routes/prompts.ts +263 -0
  358. package/src/observability/routes/requestAiService.ts +36 -0
  359. package/src/observability/routes/review.test.ts +120 -0
  360. package/src/observability/routes/review.ts +125 -0
  361. package/src/observability/routes/status.test.ts +116 -0
  362. package/src/observability/routes/status.ts +47 -0
  363. package/src/observability/routes/testMultiStage.test.ts +214 -0
  364. package/src/observability/routes/testMultiStage.ts +96 -0
  365. package/src/observability/routes/traces.test.ts +167 -0
  366. package/src/observability/routes/traces.ts +172 -0
  367. package/src/observability/schemaValidation.ts +29 -0
  368. package/src/observability/sopThresholds.ts +20 -0
  369. package/src/observability/status.test.ts +38 -0
  370. package/src/observability/status.ts +56 -0
  371. package/src/observability/testMultiStageSchemas.ts +95 -0
  372. package/src/observability/testMultiStageWorkflow.test.ts +114 -0
  373. package/src/observability/testMultiStageWorkflow.ts +307 -0
  374. package/src/observability/types.ts +262 -0
  375. package/src/routes/files.test.ts +63 -9
  376. package/src/routes/files.ts +21 -7
  377. package/src/routes/gpt.test.ts +3279 -0
  378. package/src/routes/gpt.ts +91 -816
  379. package/src/routes/gptActions.test.ts +241 -0
  380. package/src/routes/gptActions.ts +255 -0
  381. package/src/routes/gptApprovals.test.ts +762 -0
  382. package/src/routes/gptDatasets.test.ts +86 -0
  383. package/src/routes/gptDatasets.ts +104 -0
  384. package/src/routes/gptFiles.test.ts +589 -0
  385. package/src/routes/gptHistories.test.ts +933 -0
  386. package/src/routes/gptHistories.ts +266 -5
  387. package/src/routes/routes.test.ts +222 -2
  388. package/src/service/aiDatasets.test.ts +123 -0
  389. package/src/service/aiDatasets.ts +316 -0
  390. package/src/service/aiService.test.ts +593 -0
  391. package/src/service/aiService.ts +568 -111
  392. package/src/service/askFiles.test.ts +96 -0
  393. package/src/service/askFiles.ts +222 -0
  394. package/src/service/asks.test.ts +946 -0
  395. package/src/service/asks.ts +423 -0
  396. package/src/service/chatTurn.ts +2287 -0
  397. package/src/service/fileStorage.test.ts +11 -0
  398. package/src/service/fileStorage.ts +10 -2
  399. package/src/service/fillActionIds.test.ts +65 -0
  400. package/src/service/fillActionIds.ts +98 -0
  401. package/src/service/jsonFenceModel.test.ts +47 -0
  402. package/src/service/jsonFenceModel.ts +53 -0
  403. package/src/service/prompts.test.ts +21 -0
  404. package/src/service/prompts.ts +147 -0
  405. package/src/service/sanitizeHtml.test.ts +61 -0
  406. package/src/service/sanitizeHtml.ts +109 -0
  407. package/src/tests/bunSetup.test.ts +7 -4
  408. package/src/tests/bunSetup.ts +7 -7
  409. package/src/tests/chatHarness.ts +478 -0
  410. package/src/tests/generateStream.ts +49 -0
  411. package/src/tests/harnessErrors.ts +22 -0
  412. package/src/tests/harnessStreaming.ts +227 -0
  413. package/src/tests/helpers.ts +9 -2
  414. package/src/types/harness.ts +1119 -0
  415. package/src/types/index.ts +209 -13
  416. package/src/types/observability.ts +344 -0
  417. package/src/types/sanitize-html.d.ts +25 -0
@@ -0,0 +1,1674 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.runBufferedChatTurn = exports.runChatTurn = exports.staleAskError = exports.resolveAiService = exports.DEMO_RESPONSE = void 0;
4
+ const node_crypto_1 = require("node:crypto");
5
+ const api_1 = require("@terreno/api");
6
+ const blocks_1 = require("@terreno/blocks");
7
+ const ai_1 = require("ai");
8
+ const luxon_1 = require("luxon");
9
+ const langfuseVercelAi_1 = require("../langfuseVercelAi");
10
+ const aiRequest_1 = require("../models/aiRequest");
11
+ const gptHistory_1 = require("../models/gptHistory");
12
+ const project_1 = require("../models/project");
13
+ const aiService_1 = require("./aiService");
14
+ const askFiles_1 = require("./askFiles");
15
+ const asks_1 = require("./asks");
16
+ const fileUploadsGate_1 = require("./fileUploadsGate");
17
+ const fillActionIds_1 = require("./fillActionIds");
18
+ const prompts_1 = require("./prompts");
19
+ const sanitizeHtml_1 = require("./sanitizeHtml");
20
+ exports.DEMO_RESPONSE = "This is demo mode. To use AI features, paste your Gemini API key in Settings.";
21
+ const DEFAULT_STREAM_PERSIST_INTERVAL_MS = 1000;
22
+ // Bump `updated` even without new text so long tool calls do not look stale to resumers.
23
+ const STREAM_HEARTBEAT_MS = 10_000;
24
+ const DURABLE_URL_PATTERN = /^(https?:|data:)/i;
25
+ const DATA_URL_PATTERN = /^data:([^;,]*)((?:;[^;,]*)*?)(;base64)?,(.*)$/s;
26
+ const isGeneratedImageFile = (value) => typeof value === "object" &&
27
+ value !== null &&
28
+ "base64" in value &&
29
+ typeof value.base64 === "string" &&
30
+ "mediaType" in value &&
31
+ typeof value.mediaType === "string" &&
32
+ value.mediaType.startsWith("image/");
33
+ const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
34
+ /** The tool name an ask's rows store: the host tool for an approval ask, else `ask_<kind>`. */
35
+ const askRowToolName = (ask) => ask.toolName ?? (0, asks_1.askToolName)(ask.kind);
36
+ /** The fields that mark an ask as an approval, or none for an ask the model made. */
37
+ const approvalFieldsOf = (ask) => ask.origin === "approval" ? { origin: ask.origin, toolName: ask.toolName } : {};
38
+ const trailingActionBlob = /\{[\s\S]*?"action"[\s\S]*?\}\s*$/;
39
+ // Drop a trailing JSON action blob the model leaked after its reply. A reply that is
40
+ // itself the object, including a JSON block document, stays intact.
41
+ const cleanStepText = (text) => {
42
+ const trimmed = text.trim();
43
+ const match = trailingActionBlob.exec(trimmed);
44
+ if (!match || match.index === undefined) {
45
+ return trimmed;
46
+ }
47
+ const before = trimmed.slice(0, match.index).trim();
48
+ if (!before || (0, blocks_1.parseBlocks)(match[0]).ok) {
49
+ return trimmed;
50
+ }
51
+ return before;
52
+ };
53
+ /**
54
+ * Resolve the AIService for a request. Priority:
55
+ * 1. Per-request API key via `x-ai-api-key` header (creates a temporary AIService via createModelFn)
56
+ * 2. Specific model requested + server-side model factory (creates a temporary AIService via createServerModelFn)
57
+ * 3. Pre-configured aiService from route options
58
+ * 4. undefined (triggers demo mode response)
59
+ */
60
+ const resolveAiService = (req, options, modelId) => {
61
+ const perRequestKey = req.headers["x-ai-api-key"];
62
+ if (perRequestKey && options.createModelFn) {
63
+ return new aiService_1.AIService({ model: options.createModelFn(perRequestKey, modelId) });
64
+ }
65
+ if (modelId && options.createServerModelFn) {
66
+ const serverModel = options.createServerModelFn(modelId);
67
+ if (serverModel) {
68
+ return new aiService_1.AIService({ model: serverModel });
69
+ }
70
+ }
71
+ return options.aiService;
72
+ };
73
+ exports.resolveAiService = resolveAiService;
74
+ /** Generate a short title for a conversation using a cheap model call. */
75
+ const generateTitle = async (prompt, response, aiService, options, perRequestApiKey) => {
76
+ try {
77
+ let titleService = aiService;
78
+ if (options.titleModelId) {
79
+ if (options.createModelFn && perRequestApiKey) {
80
+ titleService = new aiService_1.AIService({
81
+ model: options.createModelFn(perRequestApiKey, options.titleModelId),
82
+ });
83
+ }
84
+ else if (options.createServerModelFn) {
85
+ const titleModel = options.createServerModelFn(options.titleModelId);
86
+ if (titleModel) {
87
+ titleService = new aiService_1.AIService({ model: titleModel });
88
+ }
89
+ }
90
+ }
91
+ const conversationSnippet = `User: ${prompt}\nAssistant: ${response.substring(0, 500)}`;
92
+ const title = await titleService.generateText({
93
+ prompt: conversationSnippet,
94
+ skipTrace: true,
95
+ systemPrompt: prompts_1.TITLE_GENERATION_PROMPT,
96
+ temperature: 0.3,
97
+ });
98
+ return title.trim().replace(/^["']|["']$/g, "");
99
+ }
100
+ catch {
101
+ return undefined;
102
+ }
103
+ };
104
+ const parseAskAnswer = (value) => {
105
+ if (!isRecord(value)) {
106
+ throw new api_1.APIError({ status: 400, title: "askResponse must be an object" });
107
+ }
108
+ const { toolCallId, ...response } = value;
109
+ if (typeof toolCallId !== "string" || toolCallId.length === 0) {
110
+ throw new api_1.APIError({ status: 400, title: "askResponse.toolCallId is required" });
111
+ }
112
+ return { response, toolCallId };
113
+ };
114
+ /** Checks which kind of turn the body asks for. Throws a 400 when it is neither or both. */
115
+ const parseTurnBody = ({ askKinds, body, }) => {
116
+ const { askResponse, attachments, historyId, prompt } = body;
117
+ const hasPrompt = typeof prompt === "string" && prompt.length > 0;
118
+ const askAnswer = askKinds.length > 0 && askResponse !== undefined && askResponse !== null
119
+ ? parseAskAnswer(askResponse)
120
+ : undefined;
121
+ if (!askAnswer) {
122
+ if (!hasPrompt) {
123
+ throw new api_1.APIError({ status: 400, title: "prompt is required" });
124
+ }
125
+ return undefined;
126
+ }
127
+ if (hasPrompt) {
128
+ throw new api_1.APIError({ status: 400, title: "Send either prompt or askResponse, not both" });
129
+ }
130
+ if (Array.isArray(attachments) && attachments.length > 0) {
131
+ throw new api_1.APIError({ status: 400, title: "attachments cannot be sent with askResponse" });
132
+ }
133
+ if (!historyId) {
134
+ throw new api_1.APIError({ status: 400, title: "historyId is required with askResponse" });
135
+ }
136
+ return askAnswer;
137
+ };
138
+ /** The body's `surface`, `"full"` when it sends none. Throws a 400 for any other value. */
139
+ const parseSurface = (surface) => {
140
+ if (surface === undefined || surface === null) {
141
+ return "full";
142
+ }
143
+ const knownSurface = blocks_1.ASK_SURFACES.find((candidate) => candidate === surface);
144
+ if (!knownSurface) {
145
+ throw new api_1.APIError({
146
+ detail: `Send one of: ${blocks_1.ASK_SURFACES.join(", ")}.`,
147
+ status: 400,
148
+ title: "surface is not a known surface",
149
+ });
150
+ }
151
+ return knownSurface;
152
+ };
153
+ const loadHistory = async ({ historyId, projectId, userId, }) => {
154
+ if (!historyId) {
155
+ return new gptHistory_1.GptHistory({ prompts: [], userId, ...(projectId ? { projectId } : {}) });
156
+ }
157
+ const history = await gptHistory_1.GptHistory.findById(historyId);
158
+ if (!history) {
159
+ throw new api_1.APIError({ status: 404, title: "History not found" });
160
+ }
161
+ if (history.userId.toString() !== userId?.toString()) {
162
+ throw new api_1.APIError({ status: 403, title: "Not authorized to access this history" });
163
+ }
164
+ return history;
165
+ };
166
+ /** The 409 for an answer that names a tool call the conversation is not waiting on. */
167
+ const staleAskError = (toolCallId) => new api_1.APIError({
168
+ detail: `Tool call ${toolCallId} is not the ask this conversation is waiting on.`,
169
+ status: 409,
170
+ title: "This ask is no longer pending",
171
+ });
172
+ exports.staleAskError = staleAskError;
173
+ /*
174
+ * The ask updates below are pipeline updates because MongoDB rejects `$push` and a positional
175
+ * `$set` on the same array in one update. Values go in through `$literal` so a string that starts
176
+ * with `$` is not read as a field path.
177
+ */
178
+ /** Inside a `$map` over `prompts` as `row`: true for the ask's call row while it has `status`. */
179
+ const isAskCallRow = ({ status, toolCallId, }) => ({
180
+ $and: [
181
+ { $eq: ["$$row.type", "tool-call"] },
182
+ { $eq: ["$$row.toolCallId", { $literal: toolCallId }] },
183
+ { $eq: ["$$row.ask.status", { $literal: status }] },
184
+ ],
185
+ });
186
+ /** A `$map` over `rows` that sets the ask's call row from status `from` to status `to`. */
187
+ const withAskCallStatus = ({ from, rows, to, toolCallId, }) => {
188
+ const markedRow = {
189
+ $mergeObjects: ["$$row", { ask: { $mergeObjects: ["$$row.ask", { status: { $literal: to } }] } }],
190
+ };
191
+ return {
192
+ $map: {
193
+ as: "row",
194
+ in: { $cond: [isAskCallRow({ status: from, toolCallId }), markedRow, "$$row"] },
195
+ input: rows,
196
+ },
197
+ };
198
+ };
199
+ /** `prompts` with the ask's call row marked `status` and its result row appended. */
200
+ const promptsWithAskResult = ({ result, status, toolCallId, toolName, }) => {
201
+ const resultRow = {
202
+ result,
203
+ text: `Tool result: ${toolName}`,
204
+ toolCallId,
205
+ toolName,
206
+ type: "tool-result",
207
+ };
208
+ const markedRows = withAskCallStatus({
209
+ from: "pending",
210
+ rows: "$prompts",
211
+ to: status,
212
+ toolCallId,
213
+ });
214
+ return { $concatArrays: [markedRows, [{ $literal: resultRow }]] };
215
+ };
216
+ /**
217
+ * Clears the pending ask, stores its result row, and marks its call row in one atomic update, so
218
+ * an ask resolves once even when two requests answer it at the same time. Returns null when the
219
+ * conversation no longer waits on this ask.
220
+ */
221
+ const resolvePendingAsk = async ({ history, result, status, toolCallId, toolName, }) => gptHistory_1.GptHistory.findOneAndUpdate({ _id: history._id, "pendingAsk.toolCallId": toolCallId }, [
222
+ {
223
+ $set: {
224
+ prompts: promptsWithAskResult({ result, status, toolCallId, toolName }),
225
+ updated: { $literal: luxon_1.DateTime.now().toJSDate() },
226
+ },
227
+ },
228
+ { $unset: "pendingAsk" },
229
+ ], { returnDocument: "after", updatePipeline: true });
230
+ /**
231
+ * Undoes `resolvePendingAsk` for an answer whose turn failed before the client saw any output, so
232
+ * the user can answer again. Only applies while the conversation is as the answer left it: no
233
+ * ask is pending and it still has `rowCount` rows, the last being the answer's result row.
234
+ * Returns whether it applied.
235
+ */
236
+ const restorePendingAsk = async ({ answer, history, rowCount, }) => {
237
+ try {
238
+ const restored = await gptHistory_1.GptHistory.findOneAndUpdate({ _id: history._id, pendingAsk: null, prompts: { $size: rowCount } }, [
239
+ {
240
+ $set: {
241
+ pendingAsk: { $literal: answer.pendingAsk },
242
+ prompts: withAskCallStatus({
243
+ from: answer.status,
244
+ rows: { $slice: ["$prompts", rowCount - 1] },
245
+ to: "pending",
246
+ toolCallId: answer.toolCallId,
247
+ }),
248
+ },
249
+ },
250
+ ], { returnDocument: "after", updatePipeline: true });
251
+ return restored !== null;
252
+ }
253
+ catch (error) {
254
+ api_1.logger.error("Failed to put back an ask whose answer's turn failed", {
255
+ error: error instanceof Error ? error.message : String(error),
256
+ historyId: history._id.toString(),
257
+ toolCallId: answer.toolCallId,
258
+ });
259
+ return false;
260
+ }
261
+ };
262
+ /**
263
+ * Cancels an ask whose call row was saved pending by a turn that failed before making it the
264
+ * pending ask, so no row waits on an ask the conversation does not.
265
+ */
266
+ const cancelUnclaimedAsk = async ({ ask, history, }) => {
267
+ const { toolCallId } = ask;
268
+ try {
269
+ await gptHistory_1.GptHistory.findOneAndUpdate({
270
+ _id: history._id,
271
+ "pendingAsk.toolCallId": { $ne: toolCallId },
272
+ prompts: { $elemMatch: { "ask.status": "pending", toolCallId, type: "tool-call" } },
273
+ }, [
274
+ {
275
+ $set: {
276
+ prompts: promptsWithAskResult({
277
+ result: { action: "cancel" },
278
+ status: "cancelled",
279
+ toolCallId,
280
+ toolName: askRowToolName(ask),
281
+ }),
282
+ },
283
+ },
284
+ ], { updatePipeline: true });
285
+ }
286
+ catch (error) {
287
+ api_1.logger.error("Failed to cancel an ask a failed turn left pending", {
288
+ error: error instanceof Error ? error.message : String(error),
289
+ historyId: history._id.toString(),
290
+ toolCallId,
291
+ });
292
+ }
293
+ };
294
+ /** How many times a new message retries cancelling an ask that other requests keep replacing. */
295
+ const MAX_CANCEL_ATTEMPTS = 3;
296
+ /**
297
+ * Cancels the pending ask because the user sent a message instead of answering. When another
298
+ * request resolves the ask first, the message goes ahead as a normal prompt on the reloaded
299
+ * history, cancelling any ask that took its place.
300
+ */
301
+ const cancelPendingAsk = async (loaded) => {
302
+ let history = loaded;
303
+ for (let attempt = 0; attempt < MAX_CANCEL_ATTEMPTS && history.pendingAsk; attempt++) {
304
+ const pending = history.pendingAsk;
305
+ const { kind, toolCallId } = pending;
306
+ const cancelled = await resolvePendingAsk({
307
+ history,
308
+ result: { action: "cancel", reason: blocks_1.ASK_CANCEL_REASONS.userSentMessage },
309
+ status: "cancelled",
310
+ toolCallId,
311
+ toolName: askRowToolName(pending),
312
+ });
313
+ if (cancelled) {
314
+ return {
315
+ history: cancelled,
316
+ resolvedAsk: { action: "cancel", kind, toolCallId, ...approvalFieldsOf(pending) },
317
+ };
318
+ }
319
+ const reloaded = await gptHistory_1.GptHistory.findById(history._id);
320
+ if (!reloaded) {
321
+ throw new api_1.APIError({ status: 404, title: "History not found" });
322
+ }
323
+ history = reloaded;
324
+ }
325
+ if (history.pendingAsk) {
326
+ api_1.logger.warn("Sent a message without cancelling an ask that other requests kept replacing", {
327
+ historyId: history._id.toString(),
328
+ toolCallId: history.pendingAsk.toolCallId,
329
+ });
330
+ }
331
+ return { history };
332
+ };
333
+ /**
334
+ * Appends the turn's rows in one atomic `$push`, so rows another turn saved meanwhile are kept.
335
+ * A new conversation is inserted with its rows instead. Returns the saved history and the index
336
+ * of the turn's first row.
337
+ */
338
+ const appendTurnRows = async ({ history, isNewHistory, projectId, rows, }) => {
339
+ if (isNewHistory) {
340
+ history.prompts.push(...rows);
341
+ await history.save();
342
+ return { firstRowIndex: 0, history };
343
+ }
344
+ if (projectId && !history.projectId) {
345
+ await gptHistory_1.GptHistory.updateOne({ _id: history._id, projectId: null }, { $set: { projectId } });
346
+ }
347
+ if (rows.length === 0) {
348
+ return { firstRowIndex: history.prompts.length, history };
349
+ }
350
+ const saved = await gptHistory_1.GptHistory.findOneAndUpdate({ _id: history._id }, { $push: { prompts: { $each: rows } }, $set: { updated: luxon_1.DateTime.now().toJSDate() } }, { returnDocument: "after" });
351
+ if (!saved) {
352
+ throw new api_1.APIError({ status: 404, title: "History not found" });
353
+ }
354
+ return { firstRowIndex: saved.prompts.length - rows.length, history: saved };
355
+ };
356
+ /**
357
+ * Stores the generated title unless another turn titled the conversation first, and returns the
358
+ * title the conversation ends up with.
359
+ */
360
+ const saveTitle = async ({ history, title, }) => {
361
+ const titled = await gptHistory_1.GptHistory.findOneAndUpdate({ _id: history._id, title: { $in: [null, ""] } }, { $set: { title } }, { returnDocument: "after" });
362
+ if (titled) {
363
+ return titled.title;
364
+ }
365
+ const current = await gptHistory_1.GptHistory.findById(history._id);
366
+ return current?.title ?? undefined;
367
+ };
368
+ /**
369
+ * Makes the ask the conversation's pending ask in one atomic update, unless another turn's ask is
370
+ * already pending: then the ask is answered with `cancel` (`one_ask_at_a_time`) instead, so two
371
+ * turns that pause at the same time cannot overwrite each other. The ask's call row must already
372
+ * be saved. Returns whether the ask became the pending ask.
373
+ */
374
+ const claimPendingAsk = async ({ ask, history, promptIndex, responseMessages, }) => {
375
+ const { input, kind, simple, toolCallId } = ask;
376
+ const waitsOnNoAsk = { $eq: [{ $ifNull: ["$pendingAsk", null] }, null] };
377
+ const approval = approvalFieldsOf(ask);
378
+ const pendingAsk = {
379
+ created: { $literal: luxon_1.DateTime.now().toJSDate() },
380
+ input: { $literal: input },
381
+ kind: { $literal: kind },
382
+ promptIndex: { $literal: promptIndex },
383
+ responseMessages: { $literal: responseMessages },
384
+ simple: { $literal: simple },
385
+ toolCallId: { $literal: toolCallId },
386
+ ...(approval.origin
387
+ ? {
388
+ approvalId: { $literal: toolCallId },
389
+ origin: { $literal: approval.origin },
390
+ toolName: { $literal: approval.toolName },
391
+ }
392
+ : {}),
393
+ };
394
+ const cancelledPrompts = promptsWithAskResult({
395
+ result: { action: "cancel", reason: blocks_1.ASK_CANCEL_REASONS.oneAskAtATime },
396
+ status: "cancelled",
397
+ toolCallId,
398
+ toolName: askRowToolName(ask),
399
+ });
400
+ const saved = await gptHistory_1.GptHistory.findOneAndUpdate({ _id: history._id }, [
401
+ {
402
+ $set: {
403
+ pendingAsk: { $cond: [waitsOnNoAsk, pendingAsk, "$pendingAsk"] },
404
+ prompts: { $cond: [waitsOnNoAsk, "$prompts", cancelledPrompts] },
405
+ },
406
+ },
407
+ ], { returnDocument: "after", updatePipeline: true });
408
+ return saved?.pendingAsk?.toolCallId === toolCallId;
409
+ };
410
+ /**
411
+ * Reject attachment URLs that only exist on the client (blob:, file:, content:, ph:, ...).
412
+ * They cannot be read by the model provider or reloaded from history later.
413
+ */
414
+ const validateAttachments = (attachments) => {
415
+ if (attachments === undefined || attachments === null) {
416
+ return [];
417
+ }
418
+ if (!Array.isArray(attachments)) {
419
+ throw new api_1.APIError({ status: 400, title: "attachments must be an array" });
420
+ }
421
+ for (const attachment of attachments) {
422
+ if (attachment?.type !== "image" && attachment?.type !== "file") {
423
+ continue;
424
+ }
425
+ if (typeof attachment.url !== "string" || !DURABLE_URL_PATTERN.test(attachment.url)) {
426
+ const scheme = typeof attachment.url === "string" ? (attachment.url.split(":")[0] ?? "") : "missing";
427
+ throw new api_1.APIError({
428
+ detail: `Attachment "${attachment.filename ?? attachment.type}" uses a "${scheme}" URL. ` +
429
+ "Upload the file first (POST /files/upload) or send it as a data: URL.",
430
+ status: 400,
431
+ title: "Attachment URL must be an http(s) or data: URL",
432
+ });
433
+ }
434
+ }
435
+ return attachments;
436
+ };
437
+ const decodeDataUrl = (url) => {
438
+ const match = DATA_URL_PATTERN.exec(url);
439
+ if (!match) {
440
+ return undefined;
441
+ }
442
+ const [, mimeType, , base64Flag, payload] = match;
443
+ const buffer = base64Flag
444
+ ? Buffer.from(payload, "base64")
445
+ : Buffer.from(decodeURIComponent(payload), "utf8");
446
+ return { buffer, mimeType: mimeType || undefined };
447
+ };
448
+ /** Build model and history content parts, uploading data: attachments when storage is configured. */
449
+ const buildAttachmentParts = async ({ attachments, fileStorageService, userId, }) => {
450
+ const modelParts = [];
451
+ const storedParts = [];
452
+ for (const attachment of attachments) {
453
+ if (attachment.type !== "image" && attachment.type !== "file") {
454
+ continue;
455
+ }
456
+ const url = attachment.url;
457
+ const modelPart = attachment.type === "image"
458
+ ? { mimeType: attachment.mimeType, type: "image", url }
459
+ : {
460
+ filename: attachment.filename,
461
+ mimeType: attachment.mimeType,
462
+ type: "file",
463
+ url,
464
+ };
465
+ modelParts.push(modelPart);
466
+ const decoded = url.startsWith("data:") ? decodeDataUrl(url) : undefined;
467
+ if (!fileStorageService || !("upload" in fileStorageService) || !userId || !decoded) {
468
+ storedParts.push(modelPart);
469
+ continue;
470
+ }
471
+ const mimeType = attachment.mimeType ?? decoded.mimeType ?? "application/octet-stream";
472
+ const extension = mimeType.split("/")[1]?.split("+")[0] ?? "bin";
473
+ try {
474
+ const uploaded = await fileStorageService.upload({
475
+ buffer: decoded.buffer,
476
+ filename: attachment.filename ?? `attachment-${luxon_1.DateTime.now().toMillis()}.${extension}`,
477
+ mimeType,
478
+ userId,
479
+ });
480
+ storedParts.push({ ...modelPart, gcsKey: uploaded.gcsKey, url: uploaded.url });
481
+ }
482
+ catch (error) {
483
+ throw new api_1.APIError({
484
+ cause: error,
485
+ detail: error instanceof Error ? error.message : String(error),
486
+ status: 502,
487
+ title: "Attachment upload failed",
488
+ });
489
+ }
490
+ }
491
+ return { modelParts, storedParts };
492
+ };
493
+ const toPlainPrompt = (prompt) => {
494
+ const maybeSubdoc = prompt;
495
+ return typeof maybeSubdoc.toObject === "function" ? maybeSubdoc.toObject() : prompt;
496
+ };
497
+ /** Swap stored attachment references for short-lived signed URLs the model provider can fetch. */
498
+ const resolveStoredAttachmentUrls = async (prompts, fileStorageService) => {
499
+ if (!fileStorageService || !("getSignedUrl" in fileStorageService)) {
500
+ return prompts;
501
+ }
502
+ return Promise.all(prompts.map(async (prompt) => {
503
+ if (!prompt.content?.some((part) => part.type !== "text" && part.gcsKey)) {
504
+ return prompt;
505
+ }
506
+ const plain = toPlainPrompt(prompt);
507
+ const content = await Promise.all((plain.content ?? []).map(async (part) => {
508
+ if (part.type === "text" || !part.gcsKey) {
509
+ return part;
510
+ }
511
+ try {
512
+ return { ...part, url: await fileStorageService.getSignedUrl(part.gcsKey) };
513
+ }
514
+ catch (error) {
515
+ api_1.logger.warn("Could not sign stored attachment URL", {
516
+ error: error instanceof Error ? error.message : String(error),
517
+ gcsKey: part.gcsKey,
518
+ });
519
+ return part;
520
+ }
521
+ }));
522
+ return { ...plain, content };
523
+ }));
524
+ };
525
+ /**
526
+ * The answer as the conversation stores it, and the tool result the model sees this turn. An
527
+ * accepted `files` answer has its files loaded and checked: the model sees them, and the stored
528
+ * answer keeps only their metadata.
529
+ */
530
+ const prepareAnswer = async ({ answer, fileStorageService, kind, maxFileSizeBytes, userId, }) => {
531
+ if (kind !== "files" || answer.action !== "accept") {
532
+ return { storedAnswer: answer };
533
+ }
534
+ const files = await (0, askFiles_1.resolveAskFiles)({
535
+ fileStorageService,
536
+ files: answer.content.files,
537
+ maxFileSizeBytes,
538
+ userId,
539
+ });
540
+ return { answerOutput: (0, askFiles_1.askFilesModelOutput)(files), storedAnswer: (0, askFiles_1.storedFilesAnswer)(files) };
541
+ };
542
+ /**
543
+ * Loads the history and prepares the turn's messages. A new prompt first cancels any pending
544
+ * ask. An answer is checked and replays the paused turn with the answer appended; `runChatTurn`
545
+ * resolves the pending ask once the turn's tools and system prompt are ready.
546
+ */
547
+ const startTurn = async ({ aiService, askAnswer, body, options, req, userId, }) => {
548
+ const { attachments, historyId, projectId, prompt } = body;
549
+ const loaded = await loadHistory({ historyId, projectId, userId });
550
+ const isNewHistory = !historyId;
551
+ if (askAnswer) {
552
+ const history = loaded;
553
+ const pending = history.pendingAsk;
554
+ if (!pending || pending.toolCallId !== askAnswer.toolCallId) {
555
+ throw (0, exports.staleAskError)(askAnswer.toolCallId);
556
+ }
557
+ const maxFileSizeBytes = (0, asks_1.resolveMaxFileSizeBytes)(options.asks);
558
+ const fields = (0, blocks_1.validateAskResponse)({
559
+ input: pending.input,
560
+ kind: pending.kind,
561
+ maxFileSizeBytes,
562
+ response: askAnswer.response,
563
+ });
564
+ if (fields.length > 0) {
565
+ throw (0, askFiles_1.invalidAskResponseError)(fields);
566
+ }
567
+ const answer = askAnswer.response;
568
+ const { approvalId, kind, promptIndex, responseMessages, toolCallId } = pending;
569
+ const { answerOutput, storedAnswer } = await prepareAnswer({
570
+ answer,
571
+ fileStorageService: options.fileStorageService,
572
+ kind,
573
+ maxFileSizeBytes,
574
+ userId,
575
+ });
576
+ // `minimize: false` keeps the empty objects the replayed messages need, such as `input: {}`.
577
+ const storedPendingAsk = history.toObject({ minimize: false, virtuals: false })
578
+ .pendingAsk;
579
+ // Rows are only ever appended, and the paused turn's rows start at `promptIndex`, so these
580
+ // rows are the same once the ask is resolved.
581
+ const turnHistory = history.prompts.slice(0, promptIndex);
582
+ const replayedMessages = (0, asks_1.completePausedTurn)({
583
+ answer: storedAnswer,
584
+ approvalId,
585
+ responseMessages,
586
+ toolCallId,
587
+ });
588
+ const modelMessages = answerOutput
589
+ ? (0, asks_1.completePausedTurn)({
590
+ answer: storedAnswer,
591
+ answerOutput,
592
+ approvalId,
593
+ responseMessages,
594
+ toolCallId,
595
+ })
596
+ : replayedMessages;
597
+ return {
598
+ answer: {
599
+ pendingAsk: storedPendingAsk,
600
+ result: storedAnswer,
601
+ status: answer.action === "cancel" ? "cancelled" : "answered",
602
+ toolCallId,
603
+ toolName: askRowToolName(pending),
604
+ },
605
+ history,
606
+ isNewHistory: false,
607
+ logPrompt: JSON.stringify(storedAnswer),
608
+ messages: [...aiService.buildMessages(turnHistory), ...modelMessages],
609
+ promptIndex,
610
+ replayedMessages,
611
+ resolvedAsk: { action: answer.action, kind, toolCallId, ...approvalFieldsOf(pending) },
612
+ rows: [],
613
+ titlePrompt: lastUserText(turnHistory),
614
+ };
615
+ }
616
+ if (Array.isArray(attachments) && attachments.length > 0) {
617
+ await (0, fileUploadsGate_1.assertFileUploadsEnabled)(req, options.fileUploadsEnabled);
618
+ }
619
+ const validAttachments = validateAttachments(attachments);
620
+ const { history, resolvedAsk } = await cancelPendingAsk(loaded);
621
+ // If history doesn't have a projectId yet but one was provided, associate it
622
+ if (isNewHistory && projectId) {
623
+ history.projectId = projectId;
624
+ }
625
+ const promptText = prompt;
626
+ api_1.logger.debug("Processing attachments", {
627
+ count: validAttachments.length,
628
+ types: validAttachments.map((attachment) => ({
629
+ mimeType: attachment.mimeType,
630
+ type: attachment.type,
631
+ urlLength: attachment.url?.length ?? 0,
632
+ })),
633
+ });
634
+ const earlierPrompts = await resolveStoredAttachmentUrls(history.prompts, options.fileStorageService);
635
+ const { modelParts, storedParts } = await buildAttachmentParts({
636
+ attachments: validAttachments,
637
+ fileStorageService: options.fileStorageService,
638
+ userId,
639
+ });
640
+ const hasAttachments = storedParts.length > 0;
641
+ const userRow = {
642
+ text: promptText,
643
+ type: "user",
644
+ ...(hasAttachments ? { content: [{ text: promptText, type: "text" }, ...storedParts] } : {}),
645
+ };
646
+ const modelUserRow = hasAttachments
647
+ ? { ...userRow, content: [{ text: promptText, type: "text" }, ...modelParts] }
648
+ : userRow;
649
+ api_1.logger.debug("Building messages", {
650
+ attachmentCount: storedParts.length,
651
+ historyLength: history.prompts.length + 1,
652
+ });
653
+ const messages = aiService.buildMessages([...earlierPrompts, modelUserRow]);
654
+ api_1.logger.debug("Messages built", { messageCount: messages.length });
655
+ return {
656
+ history,
657
+ isNewHistory,
658
+ logPrompt: promptText,
659
+ messages,
660
+ replayedMessages: [],
661
+ resolvedAsk,
662
+ rows: [userRow],
663
+ titlePrompt: promptText,
664
+ };
665
+ };
666
+ const lastUserText = (prompts) => prompts.findLast((prompt) => prompt.type === "user")?.text ?? "";
667
+ /** The request's system prompt with project context and the Langfuse prompt prepended. */
668
+ const buildSystemPrompt = async ({ history, options, projectId, systemPrompt, userId, }) => {
669
+ const effectiveProjectId = projectId ?? history.projectId;
670
+ let effectiveSystemPrompt = systemPrompt;
671
+ if (effectiveProjectId) {
672
+ try {
673
+ const project = await project_1.Project.findById(effectiveProjectId);
674
+ if (project && project.userId.toString() === userId?.toString()) {
675
+ const parts = [];
676
+ if (project.systemContext) {
677
+ parts.push(project.systemContext);
678
+ }
679
+ if (project.memories.length > 0) {
680
+ parts.push(`## Relevant Memories\n${project.memories.map((m) => `- ${m.text}`).join("\n")}`);
681
+ }
682
+ if (parts.length > 0) {
683
+ const projectContext = parts.join("\n\n");
684
+ effectiveSystemPrompt = effectiveSystemPrompt
685
+ ? `${projectContext}\n\n${effectiveSystemPrompt}`
686
+ : projectContext;
687
+ }
688
+ }
689
+ }
690
+ catch {
691
+ // Project loading failure should not block the request
692
+ }
693
+ }
694
+ if (options.langfuseSystemPromptName) {
695
+ try {
696
+ const langfuseResult = await (0, langfuseVercelAi_1.preparePromptForAI)({
697
+ promptName: options.langfuseSystemPromptName,
698
+ userId: userId?.toString(),
699
+ });
700
+ const langfusePrompt = typeof langfuseResult.prompt === "string" ? langfuseResult.prompt : undefined;
701
+ if (langfusePrompt) {
702
+ effectiveSystemPrompt = effectiveSystemPrompt
703
+ ? `${langfusePrompt}\n\n${effectiveSystemPrompt}`
704
+ : langfusePrompt;
705
+ }
706
+ }
707
+ catch (err) {
708
+ api_1.logger.debug(`Langfuse system prompt skipped: ${err.message}`);
709
+ }
710
+ }
711
+ return effectiveSystemPrompt;
712
+ };
713
+ /** Route, per-request, and MCP tools, plus the ask tools when asks are offered. */
714
+ const collectTools = async ({ askKinds, options, req, supportsTools, surface, }) => {
715
+ const { createRequestTools, mcpService, tools: routeTools } = options;
716
+ const requestTools = createRequestTools ? createRequestTools(req) : undefined;
717
+ if (!supportsTools || (!routeTools && !requestTools && !mcpService && askKinds.length === 0)) {
718
+ return undefined;
719
+ }
720
+ const allTools = { ...(routeTools ?? {}), ...(requestTools ?? {}) };
721
+ if (mcpService) {
722
+ try {
723
+ const mcpTools = await mcpService.getTools();
724
+ Object.assign(allTools, mcpTools);
725
+ }
726
+ catch {
727
+ // MCP tool discovery failure should not block the request
728
+ }
729
+ }
730
+ if (askKinds.length === 0) {
731
+ return allTools;
732
+ }
733
+ return { ...(0, asks_1.withoutReservedToolNames)(allTools), ...(0, asks_1.createAskTools)({ kinds: askKinds, surface }) };
734
+ };
735
+ const resolveUiBlocks = (uiBlocks) => {
736
+ if (!uiBlocks) {
737
+ return undefined;
738
+ }
739
+ if (uiBlocks === true) {
740
+ return {};
741
+ }
742
+ return uiBlocks;
743
+ };
744
+ /**
745
+ * The system prompt plus the asks section, the blocks document section, and the compact line
746
+ * when those options are on. With none of them, it is the request's system prompt unchanged.
747
+ */
748
+ const withTurnSystemPrompt = ({ askKinds, blocksPrompt, surface, systemPrompt, }) => {
749
+ const sections = [
750
+ ...(askKinds.length > 0 ? [(0, asks_1.buildAsksSystemPrompt)({ kinds: askKinds, surface })] : []),
751
+ ...(blocksPrompt ? [blocksPrompt] : []),
752
+ ...(surface === "compact" ? [prompts_1.COMPACT_SURFACE_SYSTEM_PROMPT] : []),
753
+ ];
754
+ if (sections.length === 0) {
755
+ return systemPrompt;
756
+ }
757
+ return [systemPrompt, ...sections].filter(Boolean).join("\n\n");
758
+ };
759
+ const checkBlockDocument = (text, hostActions, allowHtml, imageHosts) => {
760
+ const parsed = (0, blocks_1.parseBlocks)(text);
761
+ if (!parsed.ok) {
762
+ return { errors: parsed.errors, ok: false, warnings: [] };
763
+ }
764
+ const validated = (0, blocks_1.validateBlocks)(parsed.value, {
765
+ ...(allowHtml ? { allowHtml: true } : {}),
766
+ ...(hostActions ? { hostActions } : {}),
767
+ ...(imageHosts ? { imageHosts } : {}),
768
+ });
769
+ return {
770
+ errors: validated.ok ? [] : validated.errors,
771
+ ok: validated.ok,
772
+ warnings: validated.warnings,
773
+ };
774
+ };
775
+ const storedBlockText = (text, errors) => {
776
+ if (errors.length === 0) {
777
+ return text;
778
+ }
779
+ const lines = errors.map((error) => `- ${error.path} ${error.code} ${error.message}`);
780
+ return `${text}\n\nBlock validation errors:\n${lines.join("\n")}`;
781
+ };
782
+ const repairBlockDocument = async ({ aiService, errors, text, }) => {
783
+ try {
784
+ const errorLines = errors.map((error) => `- ${error.path} ${error.code} ${error.message}`);
785
+ const repaired = await aiService.generateText({
786
+ prompt: `Document:\n${text}\n\nErrors:\n${errorLines.join("\n")}`,
787
+ systemPrompt: prompts_1.UI_BLOCKS_REPAIR_SYSTEM_PROMPT,
788
+ });
789
+ const trimmed = repaired.trim();
790
+ return trimmed.length > 0 ? trimmed : undefined;
791
+ }
792
+ catch (error) {
793
+ api_1.logger.warn("Block document repair failed", {
794
+ error: error instanceof Error ? error.message : String(error),
795
+ });
796
+ return undefined;
797
+ }
798
+ };
799
+ /**
800
+ * Forwards the model stream to the sink and records what it sent in `record`, which stays valid
801
+ * when the stream fails partway. Ask calls are collected, not forwarded: an invalid one goes back
802
+ * to the model as a tool error, and a valid one pauses the turn. So is each approval request for
803
+ * a host tool, as an approval ask; without `approvals` (asks are off) the tool just does not run.
804
+ * The collected asks keep the order of their tool calls, so the first one the model made pauses
805
+ * the turn. `deniedReasons` holds why each approval this turn resumes with was denied.
806
+ */
807
+ const consumeStream = async ({ approvals, askKinds, deniedReasons, holdAssistantText = false, onStreamStart, record, result, sink, }) => {
808
+ const { askCalls, generatedImages } = record;
809
+ const callOrder = new Map();
810
+ const askOrder = new Map();
811
+ // Stream file parts and result.files report the same images; emit each data URL once.
812
+ const sendGeneratedImage = (file) => {
813
+ const url = `data:${file.mediaType};base64,${file.base64}`;
814
+ if (generatedImages.some((img) => img.url === url)) {
815
+ return;
816
+ }
817
+ generatedImages.push({ mimeType: file.mediaType, url });
818
+ sink.emit({ image: { mimeType: file.mediaType, url } });
819
+ };
820
+ let partCount = 0;
821
+ let hasStreamStarted = false;
822
+ const startStream = async () => {
823
+ if (hasStreamStarted || !onStreamStart) {
824
+ return;
825
+ }
826
+ hasStreamStarted = true;
827
+ await onStreamStart();
828
+ };
829
+ // Buffer text per step so we can discard reasoning text when a tool call follows.
830
+ // A block document in that step is kept only when the turn would otherwise have no text:
831
+ // models often write the reply beside the tool call and then stop.
832
+ let stepTextBuffer = "";
833
+ let stepHasToolCall = false;
834
+ let toolStepDocument = "";
835
+ const publishStreamingText = () => {
836
+ record.streamingText = record.fullResponse + (stepHasToolCall ? "" : stepTextBuffer);
837
+ };
838
+ const commitStepText = () => {
839
+ const cleaned = cleanStepText(stepTextBuffer);
840
+ if (!stepHasToolCall) {
841
+ if (cleaned) {
842
+ toolStepDocument = "";
843
+ record.fullResponse += cleaned;
844
+ if (!holdAssistantText) {
845
+ record.textDelivered = true;
846
+ sink.emit({ text: cleaned });
847
+ }
848
+ }
849
+ return;
850
+ }
851
+ if (cleaned && (0, blocks_1.parseBlocks)(cleaned).ok) {
852
+ toolStepDocument = cleaned;
853
+ }
854
+ };
855
+ for await (const part of result.fullStream) {
856
+ // The AI SDK emits `start` before it calls the model. Saving there would run before
857
+ // a concurrent turn has loaded this history. The reply row waits for a model part.
858
+ if (part.type !== "start") {
859
+ await startStream();
860
+ }
861
+ partCount++;
862
+ if (partCount <= 5 || part.type === "error" || part.type === "file") {
863
+ api_1.logger.debug("Stream part", {
864
+ partCount,
865
+ type: part.type,
866
+ ...(part.type === "file" && isGeneratedImageFile(part.file)
867
+ ? { mediaType: part.file.mediaType }
868
+ : {}),
869
+ ...(part.type === "error" ? { error: String(part.error ?? part) } : {}),
870
+ });
871
+ }
872
+ // Track step boundaries to discard reasoning text from tool-call steps
873
+ if (part.type === "start-step") {
874
+ stepTextBuffer = "";
875
+ stepHasToolCall = false;
876
+ publishStreamingText();
877
+ continue;
878
+ }
879
+ if (part.type === "finish-step") {
880
+ commitStepText();
881
+ stepTextBuffer = "";
882
+ stepHasToolCall = false;
883
+ publishStreamingText();
884
+ continue;
885
+ }
886
+ if (part.type === "error") {
887
+ const errMsg = part.error instanceof Error
888
+ ? part.error.message
889
+ : String(part.error ?? "Unknown stream error");
890
+ api_1.logger.error("AI stream error part", { error: errMsg });
891
+ record.streamError = errMsg;
892
+ sink.emit({ error: errMsg });
893
+ continue;
894
+ }
895
+ if (part.type === "file") {
896
+ if (isGeneratedImageFile(part.file)) {
897
+ sendGeneratedImage(part.file);
898
+ api_1.logger.debug("Sent inline image from stream");
899
+ }
900
+ continue;
901
+ }
902
+ if (part.type === "text-delta") {
903
+ const textChunk = (part.text ?? "");
904
+ if (textChunk) {
905
+ stepTextBuffer += textChunk;
906
+ publishStreamingText();
907
+ }
908
+ }
909
+ else if (part.type === "tool-call") {
910
+ stepHasToolCall = true;
911
+ publishStreamingText();
912
+ const toolName = part.toolName;
913
+ const toolCallId = part.toolCallId;
914
+ const order = callOrder.size;
915
+ callOrder.set(toolCallId, order);
916
+ const askKind = (0, asks_1.askKindFromToolName)(toolName, askKinds);
917
+ if (askKind) {
918
+ if (part.invalid) {
919
+ api_1.logger.debug("Model sent an invalid ask; it receives the errors as a tool error", {
920
+ toolCallId,
921
+ toolName,
922
+ });
923
+ }
924
+ else {
925
+ askOrder.set(toolCallId, order);
926
+ askCalls.push({ ...(0, asks_1.parseAsk)({ input: part.input, kind: askKind }), toolCallId });
927
+ }
928
+ continue;
929
+ }
930
+ sink.emit({ toolCall: { args: part.input, toolCallId, toolName } });
931
+ record.rows.push({
932
+ args: part.input,
933
+ text: `Tool call: ${toolName}`,
934
+ toolCallId,
935
+ toolName,
936
+ type: "tool-call",
937
+ });
938
+ }
939
+ else if (part.type === "tool-approval-request") {
940
+ const approvalId = part.approvalId;
941
+ const toolCall = part.toolCall;
942
+ if (!approvals) {
943
+ api_1.logger.warn("A host tool needs approval, but asks are off, so it did not run", {
944
+ toolName: toolCall.toolName,
945
+ });
946
+ continue;
947
+ }
948
+ askOrder.set(approvalId, callOrder.get(toolCall.toolCallId) ?? callOrder.size);
949
+ askCalls.push({
950
+ input: (0, asks_1.approvalAskInput)({
951
+ asks: approvals.asks,
952
+ description: approvals.tools[toolCall.toolName]?.description,
953
+ input: toolCall.input,
954
+ surface: approvals.surface,
955
+ toolName: toolCall.toolName,
956
+ }),
957
+ kind: "confirm",
958
+ origin: "approval",
959
+ toolCallId: approvalId,
960
+ toolName: toolCall.toolName,
961
+ });
962
+ }
963
+ else if (part.type === "tool-output-denied") {
964
+ const toolName = part.toolName;
965
+ const toolCallId = part.toolCallId;
966
+ const reason = deniedReasons.get(toolCallId);
967
+ const denied = { approved: false, ...(reason ? { reason } : {}) };
968
+ sink.emit({ toolResult: { result: denied, toolCallId, toolName } });
969
+ record.rows.push({
970
+ result: denied,
971
+ text: `Tool result: ${toolName}`,
972
+ toolCallId,
973
+ toolName,
974
+ type: "tool-result",
975
+ });
976
+ }
977
+ else if (part.type === "tool-result") {
978
+ const toolResult = part.output;
979
+ // If the tool result contains file data, send it as a separate file SSE event
980
+ if (toolResult?.fileData && typeof toolResult.fileData === "string") {
981
+ sink.emit({
982
+ file: {
983
+ filename: toolResult.filename ?? "document",
984
+ mimeType: toolResult.mimeType ?? "application/octet-stream",
985
+ url: toolResult.fileData,
986
+ },
987
+ });
988
+ api_1.logger.debug("Sent generated file from tool result", {
989
+ filename: toolResult.filename,
990
+ mimeType: toolResult.mimeType,
991
+ });
992
+ }
993
+ // Strip fileData from result before sending/storing to avoid bloating the SSE and DB
994
+ const { fileData: _fileData, ...cleanResult } = toolResult ?? {};
995
+ const toolName = part.toolName;
996
+ const toolCallId = part.toolCallId;
997
+ sink.emit({ toolResult: { result: cleanResult, toolCallId, toolName } });
998
+ record.rows.push({
999
+ result: cleanResult,
1000
+ text: `Tool result: ${toolName}`,
1001
+ toolCallId,
1002
+ toolName,
1003
+ type: "tool-result",
1004
+ });
1005
+ }
1006
+ }
1007
+ await startStream();
1008
+ // Flush any remaining buffered text from the last step
1009
+ commitStepText();
1010
+ publishStreamingText();
1011
+ if (record.fullResponse.trim() === "" && toolStepDocument) {
1012
+ record.fullResponse = toolStepDocument;
1013
+ record.streamingText = toolStepDocument;
1014
+ if (!holdAssistantText) {
1015
+ record.textDelivered = true;
1016
+ sink.emit({ text: toolStepDocument });
1017
+ }
1018
+ }
1019
+ api_1.logger.debug("Stream completed", { fullResponseLength: record.fullResponse.length, partCount });
1020
+ askCalls.sort((a, b) => (askOrder.get(a.toolCallId) ?? askOrder.size) - (askOrder.get(b.toolCallId) ?? askOrder.size));
1021
+ // Check for generated images (e.g. from gemini-2.5-flash-image)
1022
+ try {
1023
+ const files = await result.files;
1024
+ if (files && files.length > 0) {
1025
+ for (const file of files) {
1026
+ if (isGeneratedImageFile(file)) {
1027
+ sendGeneratedImage(file);
1028
+ }
1029
+ }
1030
+ api_1.logger.debug("Sent generated images", { count: generatedImages.length });
1031
+ }
1032
+ }
1033
+ catch (fileErr) {
1034
+ api_1.logger.debug("No files in response", {
1035
+ error: fileErr instanceof Error ? fileErr.message : String(fileErr),
1036
+ });
1037
+ }
1038
+ };
1039
+ /** The assistant's reply row, or none when the turn produced no text or images. */
1040
+ const assistantRows = ({ fullResponse, generatedImages, modelId, }) => {
1041
+ if (!fullResponse && generatedImages.length === 0) {
1042
+ return [];
1043
+ }
1044
+ const contentParts = generatedImages.map((img) => ({
1045
+ mimeType: img.mimeType,
1046
+ type: "image",
1047
+ url: img.url,
1048
+ }));
1049
+ return [
1050
+ {
1051
+ model: modelId,
1052
+ text: fullResponse,
1053
+ type: "assistant",
1054
+ ...(contentParts.length > 0 ? { content: contentParts } : {}),
1055
+ },
1056
+ ];
1057
+ };
1058
+ const askCallRow = (call, status) => ({
1059
+ args: call.input,
1060
+ ask: { kind: call.kind, status, ...(call.origin ? { origin: call.origin } : {}) },
1061
+ text: `Tool call: ${askRowToolName(call)}`,
1062
+ toolCallId: call.toolCallId,
1063
+ toolName: askRowToolName(call),
1064
+ type: "tool-call",
1065
+ });
1066
+ const droppedAskResultRow = (call) => ({
1067
+ result: { action: "cancel", reason: blocks_1.ASK_CANCEL_REASONS.oneAskAtATime },
1068
+ text: `Tool result: ${askRowToolName(call)}`,
1069
+ toolCallId: call.toolCallId,
1070
+ toolName: askRowToolName(call),
1071
+ type: "tool-result",
1072
+ });
1073
+ const answeredAskMetadata = (resolvedAsk) => resolvedAsk
1074
+ ? {
1075
+ ask: {
1076
+ action: resolvedAsk.action,
1077
+ kind: resolvedAsk.kind,
1078
+ phase: "answered",
1079
+ toolCallId: resolvedAsk.toolCallId,
1080
+ ...approvalFieldsOf(resolvedAsk),
1081
+ },
1082
+ }
1083
+ : {};
1084
+ const toolSpansFromRows = ({ rows, startTime, }) => {
1085
+ const resultsByCallId = new Map(rows
1086
+ .filter((row) => row.type === "tool-result" && row.toolCallId)
1087
+ .map((row) => [row.toolCallId, row.result]));
1088
+ const endedAt = luxon_1.DateTime.now().toMillis();
1089
+ return rows
1090
+ .filter((row) => row.type === "tool-call" && row.toolCallId && row.toolName)
1091
+ .map((row, index) => ({
1092
+ durationMs: 0,
1093
+ endedAt: luxon_1.DateTime.fromMillis(endedAt, { zone: "utc" }).toISO() ?? "",
1094
+ id: `${row.toolCallId}-span`,
1095
+ input: row.args,
1096
+ kind: "TOOL",
1097
+ name: row.toolName,
1098
+ output: resultsByCallId.get(row.toolCallId),
1099
+ startedAt: luxon_1.DateTime.fromMillis(startTime + index, { zone: "utc" }).toISO() ?? "",
1100
+ startOffsetMs: index,
1101
+ status: "ok",
1102
+ }));
1103
+ };
1104
+ /**
1105
+ * Runs one chat turn: a new prompt, or the user's answer to the pending ask (`askResponse`).
1106
+ * `surface: "compact"` offers only the asks a small screen can show and asks for short replies.
1107
+ * Request errors throw an APIError before `sink.open()`; after that, failures are sent as an
1108
+ * `{error}` event. `/gpt/prompt` drives it with a server-sent events sink and the headless
1109
+ * `turn` action with `runBufferedChatTurn`.
1110
+ */
1111
+ const runChatTurn = async ({ body, options, req, sink, }) => {
1112
+ const { toolChoice, maxSteps } = options;
1113
+ const { model: requestModel, projectId, systemPrompt } = body;
1114
+ const userId = req.user?._id;
1115
+ const promptName = typeof body.promptName === "string" ? body.promptName : undefined;
1116
+ const promptLabel = typeof body.promptLabel === "string" ? body.promptLabel : undefined;
1117
+ const isAdmin = req.user?.admin === true;
1118
+ if ((promptName || promptLabel) && !isAdmin) {
1119
+ throw new api_1.APIError({ status: 403, title: "Prompt registry selection requires admin access" });
1120
+ }
1121
+ const sessionId = (typeof body.sessionId === "string" ? body.sessionId : undefined) ??
1122
+ req.header("x-ai-session-id") ??
1123
+ undefined;
1124
+ const askKinds = (0, asks_1.resolveAskKinds)(options.asks);
1125
+ const askAnswer = parseTurnBody({ askKinds, body });
1126
+ const surface = parseSurface(body.surface);
1127
+ // Resolve AI service (per-request key takes priority, then configured service)
1128
+ api_1.logger.debug("Resolving AI service", {
1129
+ hasConfiguredService: !!options.aiService,
1130
+ hasCreateModelFn: !!options.createModelFn,
1131
+ hasPerRequestKey: !!req.headers["x-ai-api-key"],
1132
+ });
1133
+ const aiService = (0, exports.resolveAiService)(req, options, requestModel);
1134
+ if (!aiService) {
1135
+ api_1.logger.debug("No AI service available, sending demo response");
1136
+ sink.open();
1137
+ sink.emit({ text: exports.DEMO_RESPONSE });
1138
+ sink.emit({ done: true });
1139
+ return;
1140
+ }
1141
+ const turn = await startTurn({ aiService, askAnswer, body, options, req, userId });
1142
+ const { answer, isNewHistory, logPrompt, messages, replayedMessages, resolvedAsk, titlePrompt } = turn;
1143
+ let { history } = turn;
1144
+ let effectiveSystemPrompt = await buildSystemPrompt({
1145
+ history,
1146
+ options,
1147
+ projectId,
1148
+ systemPrompt,
1149
+ userId,
1150
+ });
1151
+ const observability = await aiService.resolveGenerateObservability({
1152
+ promptLabel,
1153
+ promptName,
1154
+ sensitive: body.sensitive === true ? true : undefined,
1155
+ sessionId,
1156
+ systemPrompt: effectiveSystemPrompt,
1157
+ });
1158
+ effectiveSystemPrompt = observability.systemPrompt;
1159
+ // Some models (e.g. gemini-2.5-flash-image) don't support tool calling
1160
+ const modelId = aiService.modelId;
1161
+ const supportsTools = !modelId?.includes("image");
1162
+ const offeredAskKinds = supportsTools ? (0, blocks_1.askKindsForSurface)({ kinds: askKinds, surface }) : [];
1163
+ const allTools = await collectTools({
1164
+ askKinds: offeredAskKinds,
1165
+ options,
1166
+ req,
1167
+ supportsTools,
1168
+ surface,
1169
+ });
1170
+ const uiBlocks = resolveUiBlocks(options.uiBlocks);
1171
+ const system = withTurnSystemPrompt({
1172
+ askKinds: offeredAskKinds,
1173
+ blocksPrompt: uiBlocks
1174
+ ? (0, prompts_1.uiBlocksSystemPrompt)(Object.keys(uiBlocks.hostActions ?? {}), uiBlocks.html === true, uiBlocks.imageHosts ?? [])
1175
+ : undefined,
1176
+ surface,
1177
+ systemPrompt: effectiveSystemPrompt,
1178
+ });
1179
+ // Rows saved after the answer mean the turn moved on, so a failure keeps the answer.
1180
+ let answeredRowCount = 0;
1181
+ if (answer) {
1182
+ const resolved = await resolvePendingAsk({ history, ...answer });
1183
+ if (!resolved) {
1184
+ throw (0, exports.staleAskError)(answer.toolCallId);
1185
+ }
1186
+ history = resolved;
1187
+ answeredRowCount = resolved.prompts.length;
1188
+ }
1189
+ const startTime = luxon_1.DateTime.now().toMillis();
1190
+ const record = {
1191
+ askCalls: [],
1192
+ fullResponse: "",
1193
+ generatedImages: [],
1194
+ hasOutput: false,
1195
+ rows: [...turn.rows],
1196
+ streamingText: "",
1197
+ textDelivered: false,
1198
+ };
1199
+ const outputSink = {
1200
+ emit: (event) => {
1201
+ if (!("error" in event)) {
1202
+ record.hasOutput = true;
1203
+ }
1204
+ sink.emit(event);
1205
+ },
1206
+ open: sink.open,
1207
+ };
1208
+ let isSaved = false;
1209
+ let historyIsNew = isNewHistory;
1210
+ const saveRows = async (rows) => {
1211
+ const saved = await appendTurnRows({
1212
+ history,
1213
+ isNewHistory: historyIsNew,
1214
+ projectId,
1215
+ rows,
1216
+ });
1217
+ history = saved.history;
1218
+ historyIsNew = false;
1219
+ isSaved = true;
1220
+ return saved.firstRowIndex;
1221
+ };
1222
+ // An ask whose call row is saved pending but that is not yet the conversation's pending ask.
1223
+ let unclaimedAsk;
1224
+ const streamId = (0, node_crypto_1.randomUUID)();
1225
+ let pausedPromptIndex = turn.promptIndex;
1226
+ let persistTimer;
1227
+ let persistQueue = Promise.resolve();
1228
+ const stopPersisting = async () => {
1229
+ if (persistTimer !== undefined) {
1230
+ clearInterval(persistTimer);
1231
+ persistTimer = undefined;
1232
+ }
1233
+ await persistQueue;
1234
+ };
1235
+ const removeStreamingPlaceholder = async () => {
1236
+ await gptHistory_1.GptHistory.updateOne({ _id: history._id }, { $pull: { prompts: { streamId } } });
1237
+ };
1238
+ // Open before the model runs so a failure that yields no part is still an SSE error.
1239
+ // The reply row is saved on the first part, after concurrent turns have loaded this history.
1240
+ sink.open();
1241
+ let lastPersistedText = "";
1242
+ let lastPersistedAt = luxon_1.DateTime.now();
1243
+ const persistIntervalMs = options.streamPersistIntervalMs ?? DEFAULT_STREAM_PERSIST_INTERVAL_MS;
1244
+ const beginStreaming = async () => {
1245
+ const earlyRows = await saveRows([
1246
+ ...record.rows,
1247
+ {
1248
+ model: aiService.modelId,
1249
+ status: "streaming",
1250
+ streamId,
1251
+ text: "",
1252
+ type: "assistant",
1253
+ },
1254
+ ]);
1255
+ if (pausedPromptIndex === undefined) {
1256
+ pausedPromptIndex = earlyRows + 1;
1257
+ }
1258
+ record.rows = [];
1259
+ sink.emit({ historyId: history._id.toString(), started: true, streamId });
1260
+ persistTimer = setInterval(() => {
1261
+ const text = record.streamingText;
1262
+ const isHeartbeatDue = luxon_1.DateTime.now().diff(lastPersistedAt).toMillis() >= STREAM_HEARTBEAT_MS;
1263
+ if (text === lastPersistedText && !isHeartbeatDue) {
1264
+ return;
1265
+ }
1266
+ lastPersistedText = text;
1267
+ lastPersistedAt = luxon_1.DateTime.now();
1268
+ persistQueue = persistQueue
1269
+ .then(async () => {
1270
+ await gptHistory_1.GptHistory.updateOne({ _id: history._id, "prompts.streamId": streamId }, {
1271
+ $set: {
1272
+ "prompts.$.text": text,
1273
+ updated: luxon_1.DateTime.now().toJSDate(),
1274
+ },
1275
+ });
1276
+ })
1277
+ .catch((persistErr) => {
1278
+ api_1.logger.warn("Failed to persist partial GPT response", {
1279
+ error: persistErr instanceof Error ? persistErr.message : String(persistErr),
1280
+ });
1281
+ });
1282
+ }, persistIntervalMs);
1283
+ };
1284
+ /**
1285
+ * Ends a failed turn with `{error}` then `{done}`. An answer's turn that failed before the
1286
+ * client saw any output puts the ask back, so the user can answer again.
1287
+ */
1288
+ const endFailedTurn = async ({ errorMessage, isErrorSent, }) => {
1289
+ await stopPersisting();
1290
+ // The streaming placeholder is not part of the answer. Remove it before putting the ask
1291
+ // back, which only applies while the row count is still what the answer stored.
1292
+ if (!record.hasOutput && record.streamingText === "" && record.rows.length === 0) {
1293
+ try {
1294
+ await removeStreamingPlaceholder();
1295
+ }
1296
+ catch (saveErr) {
1297
+ api_1.logger.error("Failed to remove a streaming placeholder", {
1298
+ error: saveErr instanceof Error ? saveErr.message : String(saveErr),
1299
+ historyId: history._id.toString(),
1300
+ });
1301
+ }
1302
+ }
1303
+ const isAskRestored = answer && !record.hasOutput
1304
+ ? await restorePendingAsk({ answer, history, rowCount: answeredRowCount })
1305
+ : false;
1306
+ const metadata = isAskRestored ? {} : answeredAskMetadata(resolvedAsk);
1307
+ try {
1308
+ await aiRequest_1.AIRequest.logRequest({
1309
+ aiModel: modelId ?? "unknown",
1310
+ error: errorMessage,
1311
+ prompt: logPrompt,
1312
+ requestType: "general",
1313
+ responseTime: luxon_1.DateTime.now().toMillis() - startTime,
1314
+ userId: userId ?? undefined,
1315
+ ...(Object.keys(metadata).length > 0 ? { metadata } : {}),
1316
+ });
1317
+ }
1318
+ catch (logErr) {
1319
+ api_1.logger.warn("Failed to log AIRequest error", {
1320
+ error: logErr instanceof Error ? logErr.message : String(logErr),
1321
+ });
1322
+ }
1323
+ if (!isErrorSent) {
1324
+ sink.emit({ error: errorMessage });
1325
+ }
1326
+ if (unclaimedAsk) {
1327
+ await cancelUnclaimedAsk({ ask: unclaimedAsk, history });
1328
+ }
1329
+ // Keep what the user saw before the failure. Asks are dropped: the turn cannot pause on them.
1330
+ try {
1331
+ if (isSaved) {
1332
+ const partial = record.streamingText;
1333
+ if (partial) {
1334
+ await gptHistory_1.GptHistory.updateOne({ _id: history._id, "prompts.streamId": streamId }, {
1335
+ $set: {
1336
+ "prompts.$.status": "error",
1337
+ "prompts.$.text": partial,
1338
+ updated: luxon_1.DateTime.now().toJSDate(),
1339
+ },
1340
+ });
1341
+ }
1342
+ else {
1343
+ await removeStreamingPlaceholder();
1344
+ }
1345
+ if (record.rows.length > 0) {
1346
+ await saveRows(record.rows);
1347
+ record.rows = [];
1348
+ }
1349
+ }
1350
+ else if (!isAskRestored) {
1351
+ await saveRows([
1352
+ ...record.rows,
1353
+ ...assistantRows({
1354
+ fullResponse: record.fullResponse,
1355
+ generatedImages: record.generatedImages,
1356
+ modelId: aiService.modelId,
1357
+ }),
1358
+ ]);
1359
+ }
1360
+ }
1361
+ catch (saveErr) {
1362
+ api_1.logger.error("Failed to save a failed turn's rows", {
1363
+ error: saveErr instanceof Error ? saveErr.message : String(saveErr),
1364
+ historyId: history._id.toString(),
1365
+ });
1366
+ }
1367
+ sink.emit({
1368
+ done: true,
1369
+ ...(isSaved || !isNewHistory ? { historyId: history._id.toString() } : {}),
1370
+ ...(history.title ? { title: history.title } : {}),
1371
+ ...(isAskRestored && answer ? { pendingAsk: { toolCallId: answer.toolCallId } } : {}),
1372
+ });
1373
+ };
1374
+ try {
1375
+ if (resolvedAsk) {
1376
+ sink.emit({ askResolved: { action: resolvedAsk.action, toolCallId: resolvedAsk.toolCallId } });
1377
+ }
1378
+ api_1.logger.debug("Starting streamText", { model: modelId, supportsTools });
1379
+ const telemetry = (0, langfuseVercelAi_1.createTelemetryConfig)({
1380
+ functionId: "gpt-prompt",
1381
+ metadata: {
1382
+ ...(options.langfuseSystemPromptName
1383
+ ? { langfusePromptName: options.langfuseSystemPromptName }
1384
+ : {}),
1385
+ },
1386
+ userId: userId?.toString(),
1387
+ });
1388
+ const result = (0, ai_1.streamText)({
1389
+ experimental_telemetry: telemetry,
1390
+ messages,
1391
+ model: aiService.model,
1392
+ providerOptions: !supportsTools
1393
+ ? {
1394
+ google: { responseModalities: ["TEXT", "IMAGE"] },
1395
+ vertex: { responseModalities: ["TEXT", "IMAGE"] },
1396
+ }
1397
+ : undefined,
1398
+ stopWhen: allTools ? (0, ai_1.stepCountIs)(maxSteps ?? 5) : (0, ai_1.stepCountIs)(1),
1399
+ system: system ?? undefined,
1400
+ temperature: aiService.defaultTemperature,
1401
+ toolChoice: allTools ? (toolChoice ?? "auto") : undefined,
1402
+ tools: allTools,
1403
+ });
1404
+ await consumeStream({
1405
+ approvals: askKinds.length > 0 && allTools
1406
+ ? { asks: options.asks, surface, tools: allTools }
1407
+ : undefined,
1408
+ askKinds: offeredAskKinds,
1409
+ deniedReasons: (0, asks_1.deniedApprovalReasons)(messages),
1410
+ holdAssistantText: uiBlocks !== undefined,
1411
+ onStreamStart: beginStreaming,
1412
+ record,
1413
+ result,
1414
+ sink: outputSink,
1415
+ });
1416
+ if (answer && !record.hasOutput && record.streamError !== undefined) {
1417
+ await endFailedTurn({ errorMessage: record.streamError, isErrorSent: true });
1418
+ return;
1419
+ }
1420
+ const { askCalls, generatedImages } = record;
1421
+ let { fullResponse } = record;
1422
+ const streamedResponse = fullResponse;
1423
+ const hostActionNames = uiBlocks?.hostActions === undefined ? undefined : Object.keys(uiBlocks.hostActions);
1424
+ const allowHtml = uiBlocks?.html === true;
1425
+ const imageHosts = uiBlocks?.imageHosts;
1426
+ if (uiBlocks && fullResponse.trim() !== "") {
1427
+ const filled = (0, fillActionIds_1.fillMissingActionIds)(fullResponse);
1428
+ if (filled !== undefined) {
1429
+ fullResponse = filled;
1430
+ }
1431
+ }
1432
+ let blocksCheck = uiBlocks && fullResponse.trim() !== ""
1433
+ ? checkBlockDocument(fullResponse, hostActionNames, allowHtml, imageHosts)
1434
+ : undefined;
1435
+ let repaired = false;
1436
+ if (blocksCheck && !blocksCheck.ok && uiBlocks?.repair === true) {
1437
+ const next = await repairBlockDocument({
1438
+ aiService,
1439
+ errors: blocksCheck.errors,
1440
+ text: fullResponse,
1441
+ });
1442
+ if (next !== undefined) {
1443
+ fullResponse = next;
1444
+ repaired = true;
1445
+ blocksCheck = checkBlockDocument(fullResponse, hostActionNames, allowHtml, imageHosts);
1446
+ }
1447
+ }
1448
+ if (allowHtml && blocksCheck?.ok) {
1449
+ const sanitized = (0, sanitizeHtml_1.sanitizeBlocksText)(fullResponse);
1450
+ if (sanitized.changed) {
1451
+ fullResponse = sanitized.text;
1452
+ blocksCheck = checkBlockDocument(fullResponse, hostActionNames, allowHtml, imageHosts);
1453
+ }
1454
+ }
1455
+ const storedResponse = blocksCheck && !blocksCheck.ok
1456
+ ? storedBlockText(fullResponse, blocksCheck.errors)
1457
+ : fullResponse;
1458
+ // Send the document the client should paint before `{ask}`. With uiBlocks the stream held
1459
+ // the draft, so this is the first `{text}` and it already includes id-fill, repair, and
1460
+ // sanitizing. A draft that was already sent is replaced instead.
1461
+ if (fullResponse.trim() !== "") {
1462
+ if (record.textDelivered) {
1463
+ if (fullResponse !== streamedResponse) {
1464
+ outputSink.emit({ replace: "text", text: fullResponse });
1465
+ }
1466
+ }
1467
+ else if (uiBlocks) {
1468
+ outputSink.emit({ text: fullResponse });
1469
+ record.textDelivered = true;
1470
+ }
1471
+ }
1472
+ // The first ask in the step pauses the turn. Any other ask the model made in that step is
1473
+ // cancelled now; any other approval is denied when the turn resumes, so its tool never runs.
1474
+ const [asked, ...otherAsks] = askCalls;
1475
+ const droppedAsks = otherAsks.filter((call) => call.origin !== "approval");
1476
+ const ask = asked ? { ...asked, simple: (0, blocks_1.toSimpleCard)(asked) } : undefined;
1477
+ await stopPersisting();
1478
+ const finalRows = [
1479
+ ...record.rows,
1480
+ ...assistantRows({
1481
+ fullResponse: storedResponse,
1482
+ generatedImages,
1483
+ modelId: aiService.modelId,
1484
+ }).map((row) => ({ ...row, status: "complete", streamId })),
1485
+ ...(ask
1486
+ ? [
1487
+ askCallRow(ask, "pending"),
1488
+ ...droppedAsks.map((call) => askCallRow(call, "cancelled")),
1489
+ ...droppedAsks.map(droppedAskResultRow),
1490
+ ]
1491
+ : []),
1492
+ ];
1493
+ // A text reply replaces the placeholder in place, so a concurrent turn cannot land between
1494
+ // the user message and this reply. Tool and ask rows are appended after the placeholder goes.
1495
+ const [onlyRow] = finalRows;
1496
+ const replacesPlaceholder = finalRows.length === 1 && onlyRow?.type === "assistant" && onlyRow.streamId === streamId;
1497
+ if (replacesPlaceholder) {
1498
+ await gptHistory_1.GptHistory.updateOne({ _id: history._id, "prompts.streamId": streamId }, {
1499
+ $set: {
1500
+ "prompts.$.model": onlyRow.model,
1501
+ "prompts.$.status": "complete",
1502
+ "prompts.$.text": onlyRow.text,
1503
+ ...(onlyRow.content ? { "prompts.$.content": onlyRow.content } : {}),
1504
+ updated: luxon_1.DateTime.now().toJSDate(),
1505
+ },
1506
+ });
1507
+ }
1508
+ else {
1509
+ await removeStreamingPlaceholder();
1510
+ if (finalRows.length > 0) {
1511
+ await saveRows(finalRows);
1512
+ }
1513
+ }
1514
+ unclaimedAsk = ask;
1515
+ const isPaused = ask
1516
+ ? await claimPendingAsk({
1517
+ ask,
1518
+ history,
1519
+ // A prompt turn's history ends with its own user message, the first row it appended.
1520
+ promptIndex: pausedPromptIndex ?? 1,
1521
+ responseMessages: (0, asks_1.toStoredMessages)([
1522
+ ...replayedMessages,
1523
+ ...(await result.response).messages,
1524
+ ]),
1525
+ })
1526
+ : false;
1527
+ unclaimedAsk = undefined;
1528
+ const pausedAsk = isPaused ? ask : undefined;
1529
+ if (ask && !pausedAsk) {
1530
+ api_1.logger.warn("Cancelled an ask because another turn's ask is already pending", {
1531
+ historyId: history._id.toString(),
1532
+ toolCallId: ask.toolCallId,
1533
+ });
1534
+ }
1535
+ if (pausedAsk) {
1536
+ sink.emit({ ask: pausedAsk, historyId: history._id.toString() });
1537
+ }
1538
+ const metadata = {
1539
+ ...answeredAskMetadata(resolvedAsk),
1540
+ ...(pausedAsk
1541
+ ? {
1542
+ [resolvedAsk ? "nextAsk" : "ask"]: {
1543
+ kind: pausedAsk.kind,
1544
+ phase: "asked",
1545
+ toolCallId: pausedAsk.toolCallId,
1546
+ ...approvalFieldsOf(pausedAsk),
1547
+ },
1548
+ }
1549
+ : {}),
1550
+ ...(blocksCheck
1551
+ ? {
1552
+ uiBlocks: {
1553
+ errorCodes: blocksCheck.errors.map((error) => error.code),
1554
+ ok: blocksCheck.ok,
1555
+ repaired,
1556
+ warningCodes: blocksCheck.warnings.map((error) => error.code),
1557
+ },
1558
+ }
1559
+ : {}),
1560
+ };
1561
+ try {
1562
+ await aiService.recordGenerate({
1563
+ childSpans: toolSpansFromRows({ rows: record.rows, startTime }),
1564
+ metadata,
1565
+ observability,
1566
+ prompt: logPrompt,
1567
+ requestType: "general",
1568
+ response: storedResponse,
1569
+ responseTime: luxon_1.DateTime.now().toMillis() - startTime,
1570
+ startTime,
1571
+ userId: userId ?? undefined,
1572
+ });
1573
+ }
1574
+ catch (logErr) {
1575
+ api_1.logger.warn("Failed to log AIRequest", {
1576
+ error: logErr instanceof Error ? logErr.message : String(logErr),
1577
+ });
1578
+ }
1579
+ // Generate a title for new conversations using a cheap model call
1580
+ let title = history.title;
1581
+ if (!title && fullResponse) {
1582
+ const perRequestApiKey = req.headers["x-ai-api-key"];
1583
+ const generatedTitle = await generateTitle(titlePrompt, fullResponse, aiService, options, perRequestApiKey);
1584
+ if (generatedTitle) {
1585
+ title = await saveTitle({ history, title: generatedTitle });
1586
+ }
1587
+ }
1588
+ api_1.logger.debug("Sending done event", {
1589
+ fullResponseLength: fullResponse.length,
1590
+ historyId: history._id.toString(),
1591
+ });
1592
+ if (blocksCheck) {
1593
+ sink.emit({ blocks: blocksCheck });
1594
+ }
1595
+ sink.emit({
1596
+ done: true,
1597
+ historyId: history._id.toString(),
1598
+ ...(title ? { title } : {}),
1599
+ ...(pausedAsk ? { pendingAsk: { toolCallId: pausedAsk.toolCallId } } : {}),
1600
+ });
1601
+ }
1602
+ catch (error) {
1603
+ const errorMessage = error instanceof Error ? error.message : String(error);
1604
+ api_1.logger.error("Error in GPT stream", { error: errorMessage });
1605
+ try {
1606
+ await aiService.recordGenerate({
1607
+ childSpans: toolSpansFromRows({ rows: record.rows, startTime }),
1608
+ error: errorMessage,
1609
+ observability,
1610
+ prompt: logPrompt,
1611
+ requestType: "general",
1612
+ response: record.fullResponse || undefined,
1613
+ responseTime: luxon_1.DateTime.now().toMillis() - startTime,
1614
+ startTime,
1615
+ userId: userId ?? undefined,
1616
+ });
1617
+ }
1618
+ catch (logError) {
1619
+ api_1.logger.warn("Failed to log AIRequest error", {
1620
+ error: logError instanceof Error ? logError.message : String(logError),
1621
+ });
1622
+ }
1623
+ await endFailedTurn({
1624
+ errorMessage: error instanceof Error ? error.message : "Unknown error",
1625
+ isErrorSent: false,
1626
+ });
1627
+ }
1628
+ };
1629
+ exports.runChatTurn = runChatTurn;
1630
+ /**
1631
+ * Runs a turn to completion and returns it as one JSON result, for clients that do not read
1632
+ * server-sent events. Nothing is written to the response while the turn runs, so a client that
1633
+ * disconnects does not stop it: the turn still finishes and saves.
1634
+ */
1635
+ const runBufferedChatTurn = async ({ body, options, req, }) => {
1636
+ const turn = { errors: [], texts: [] };
1637
+ await (0, exports.runChatTurn)({
1638
+ body,
1639
+ options,
1640
+ req,
1641
+ sink: {
1642
+ emit: (event) => {
1643
+ if ("replace" in event && event.replace === "text") {
1644
+ turn.texts = [event.text];
1645
+ }
1646
+ else if ("text" in event) {
1647
+ turn.texts.push(event.text);
1648
+ }
1649
+ else if ("error" in event) {
1650
+ turn.errors.push(event.error);
1651
+ }
1652
+ else if ("ask" in event) {
1653
+ turn.ask = event.ask;
1654
+ }
1655
+ else if ("done" in event) {
1656
+ turn.done = event;
1657
+ }
1658
+ },
1659
+ open: () => { },
1660
+ },
1661
+ });
1662
+ const { done, errors, texts } = turn;
1663
+ const historyId = done?.historyId ?? body.historyId;
1664
+ // A failed answer's turn puts its ask back without streaming it again.
1665
+ const ask = turn.ask ?? (done?.pendingAsk ? (await gptHistory_1.GptHistory.findById(historyId))?.pendingAsk : undefined);
1666
+ return {
1667
+ ...(errors.length > 0 ? { error: errors.join("\n") } : {}),
1668
+ historyId,
1669
+ ...(ask ? { pendingAsk: { kind: ask.kind, simple: ask.simple, toolCallId: ask.toolCallId } } : {}),
1670
+ text: texts.join(""),
1671
+ ...(done?.title ? { title: done.title } : {}),
1672
+ };
1673
+ };
1674
+ exports.runBufferedChatTurn = runBufferedChatTurn;