@terreno/ai 57.8.0 → 57.10.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (425) hide show
  1. package/.cursorrules +2 -2
  2. package/README.md +2 -1
  3. package/bunfig.toml +1 -1
  4. package/dist/aiApp.d.ts +16 -5
  5. package/dist/aiApp.js +17 -7
  6. package/dist/harness/adminScreens.d.ts +6 -0
  7. package/dist/harness/adminScreens.js +11 -0
  8. package/dist/harness/agentLoop.d.ts +61 -0
  9. package/dist/harness/agentLoop.js +798 -0
  10. package/dist/harness/agentTaskNames.d.ts +4 -0
  11. package/dist/harness/agentTaskNames.js +7 -0
  12. package/dist/harness/approvalPolicy.d.ts +3 -0
  13. package/dist/harness/approvalPolicy.js +23 -0
  14. package/dist/harness/approvals.d.ts +101 -0
  15. package/dist/harness/approvals.js +468 -0
  16. package/dist/harness/commit.d.ts +238 -0
  17. package/dist/harness/commit.js +678 -0
  18. package/dist/harness/conversation.d.ts +75 -0
  19. package/dist/harness/conversation.js +397 -0
  20. package/dist/harness/defineAgent.d.ts +9 -0
  21. package/dist/harness/defineAgent.js +65 -0
  22. package/dist/harness/defineTask.d.ts +11 -0
  23. package/dist/harness/defineTask.js +59 -0
  24. package/dist/harness/defineTool.d.ts +7 -0
  25. package/dist/harness/defineTool.js +36 -0
  26. package/dist/harness/definitionError.d.ts +8 -0
  27. package/dist/harness/definitionError.js +15 -0
  28. package/dist/harness/errors.d.ts +127 -0
  29. package/dist/harness/errors.js +101 -0
  30. package/dist/harness/eventHub.d.ts +45 -0
  31. package/dist/harness/eventHub.js +135 -0
  32. package/dist/harness/events.d.ts +95 -0
  33. package/dist/harness/events.js +267 -0
  34. package/dist/harness/executionEnv.d.ts +31 -0
  35. package/dist/harness/executionEnv.js +2 -0
  36. package/dist/harness/extensions.d.ts +82 -0
  37. package/dist/harness/extensions.js +274 -0
  38. package/dist/harness/harness.d.ts +163 -0
  39. package/dist/harness/harness.js +520 -0
  40. package/dist/harness/harnessApp.d.ts +44 -0
  41. package/dist/harness/harnessApp.js +62 -0
  42. package/dist/harness/internalRuntime.d.ts +16 -0
  43. package/dist/harness/internalRuntime.js +18 -0
  44. package/dist/harness/leases.d.ts +35 -0
  45. package/dist/harness/leases.js +86 -0
  46. package/dist/harness/memo.d.ts +24 -0
  47. package/dist/harness/memo.js +78 -0
  48. package/dist/harness/modelCall.d.ts +45 -0
  49. package/dist/harness/modelCall.js +147 -0
  50. package/dist/harness/models/harnessApproval.d.ts +2 -0
  51. package/dist/harness/models/harnessApproval.js +100 -0
  52. package/dist/harness/models/harnessConversation.d.ts +2 -0
  53. package/dist/harness/models/harnessConversation.js +117 -0
  54. package/dist/harness/models/harnessEvent.d.ts +3 -0
  55. package/dist/harness/models/harnessEvent.js +73 -0
  56. package/dist/harness/models/harnessInboxEvent.d.ts +2 -0
  57. package/dist/harness/models/harnessInboxEvent.js +58 -0
  58. package/dist/harness/models/harnessMemo.d.ts +2 -0
  59. package/dist/harness/models/harnessMemo.js +42 -0
  60. package/dist/harness/models/harnessMessage.d.ts +2 -0
  61. package/dist/harness/models/harnessMessage.js +73 -0
  62. package/dist/harness/models/harnessOwner.d.ts +2 -0
  63. package/dist/harness/models/harnessOwner.js +36 -0
  64. package/dist/harness/models/harnessTask.d.ts +2 -0
  65. package/dist/harness/models/harnessTask.js +186 -0
  66. package/dist/harness/ownership.d.ts +89 -0
  67. package/dist/harness/ownership.js +438 -0
  68. package/dist/harness/registry.d.ts +29 -0
  69. package/dist/harness/registry.js +93 -0
  70. package/dist/harness/retryBackoff.d.ts +31 -0
  71. package/dist/harness/retryBackoff.js +50 -0
  72. package/dist/harness/routes/approvals.d.ts +13 -0
  73. package/dist/harness/routes/approvals.js +93 -0
  74. package/dist/harness/routes/conversations.d.ts +11 -0
  75. package/dist/harness/routes/conversations.js +66 -0
  76. package/dist/harness/routes/events.d.ts +19 -0
  77. package/dist/harness/routes/events.js +215 -0
  78. package/dist/harness/routes/tasks.d.ts +11 -0
  79. package/dist/harness/routes/tasks.js +108 -0
  80. package/dist/harness/runners/inProcessRunner.d.ts +57 -0
  81. package/dist/harness/runners/inProcessRunner.js +196 -0
  82. package/dist/harness/runtime.d.ts +34 -0
  83. package/dist/harness/runtime.js +551 -0
  84. package/dist/harness/subagent.d.ts +53 -0
  85. package/dist/harness/subagent.js +134 -0
  86. package/dist/harness/suspend.d.ts +8 -0
  87. package/dist/harness/suspend.js +16 -0
  88. package/dist/harness/transaction.d.ts +3 -0
  89. package/dist/harness/transaction.js +22 -0
  90. package/dist/harness/waits.d.ts +100 -0
  91. package/dist/harness/waits.js +214 -0
  92. package/dist/index.d.ts +19 -1
  93. package/dist/index.js +45 -2
  94. package/dist/models/aiDataset.d.ts +2 -0
  95. package/dist/models/aiDataset.js +67 -0
  96. package/dist/models/aiRequest.js +1 -1
  97. package/dist/models/gptHistory.js +88 -2
  98. package/dist/observability/adminScreens.d.ts +5 -0
  99. package/dist/observability/adminScreens.js +32 -0
  100. package/dist/observability/compileTemplate.d.ts +1 -0
  101. package/dist/observability/compileTemplate.js +13 -0
  102. package/dist/observability/datasetImport.d.ts +11 -0
  103. package/dist/observability/datasetImport.js +185 -0
  104. package/dist/observability/evaluate.d.ts +31 -0
  105. package/dist/observability/evaluate.js +200 -0
  106. package/dist/observability/evaluatorTemplates.d.ts +17 -0
  107. package/dist/observability/evaluatorTemplates.js +111 -0
  108. package/dist/observability/jsonPath.d.ts +2 -0
  109. package/dist/observability/jsonPath.js +57 -0
  110. package/dist/observability/local/datasetStore.d.ts +87 -0
  111. package/dist/observability/local/datasetStore.js +326 -0
  112. package/dist/observability/local/evaluatorStore.d.ts +44 -0
  113. package/dist/observability/local/evaluatorStore.js +182 -0
  114. package/dist/observability/local/experimentRunner.d.ts +96 -0
  115. package/dist/observability/local/experimentRunner.js +419 -0
  116. package/dist/observability/local/localPlugin.d.ts +22 -0
  117. package/dist/observability/local/localPlugin.js +76 -0
  118. package/dist/observability/local/models/obsDataset.d.ts +2 -0
  119. package/dist/observability/local/models/obsDataset.js +37 -0
  120. package/dist/observability/local/models/obsDatasetItem.d.ts +2 -0
  121. package/dist/observability/local/models/obsDatasetItem.js +76 -0
  122. package/dist/observability/local/models/obsEvaluator.d.ts +2 -0
  123. package/dist/observability/local/models/obsEvaluator.js +98 -0
  124. package/dist/observability/local/models/obsExperiment.d.ts +2 -0
  125. package/dist/observability/local/models/obsExperiment.js +87 -0
  126. package/dist/observability/local/models/obsExperimentItem.d.ts +2 -0
  127. package/dist/observability/local/models/obsExperimentItem.js +45 -0
  128. package/dist/observability/local/models/obsPrompt.d.ts +2 -0
  129. package/dist/observability/local/models/obsPrompt.js +42 -0
  130. package/dist/observability/local/models/obsPromptLabel.d.ts +2 -0
  131. package/dist/observability/local/models/obsPromptLabel.js +39 -0
  132. package/dist/observability/local/models/obsPromptVersion.d.ts +2 -0
  133. package/dist/observability/local/models/obsPromptVersion.js +96 -0
  134. package/dist/observability/local/models/obsReviewItem.d.ts +2 -0
  135. package/dist/observability/local/models/obsReviewItem.js +75 -0
  136. package/dist/observability/local/models/obsScore.d.ts +2 -0
  137. package/dist/observability/local/models/obsScore.js +58 -0
  138. package/dist/observability/local/models/obsSpan.d.ts +2 -0
  139. package/dist/observability/local/models/obsSpan.js +64 -0
  140. package/dist/observability/local/models/obsTrace.d.ts +2 -0
  141. package/dist/observability/local/models/obsTrace.js +93 -0
  142. package/dist/observability/local/promptStore.d.ts +180 -0
  143. package/dist/observability/local/promptStore.js +444 -0
  144. package/dist/observability/local/reviewStore.d.ts +50 -0
  145. package/dist/observability/local/reviewStore.js +203 -0
  146. package/dist/observability/local/traceStore.d.ts +103 -0
  147. package/dist/observability/local/traceStore.js +325 -0
  148. package/dist/observability/observabilityApp.d.ts +24 -0
  149. package/dist/observability/observabilityApp.js +157 -0
  150. package/dist/observability/observabilityAppRegistry.d.ts +14 -0
  151. package/dist/observability/observabilityAppRegistry.js +16 -0
  152. package/dist/observability/permissions.d.ts +7 -0
  153. package/dist/observability/permissions.js +60 -0
  154. package/dist/observability/reviewPanels.d.ts +18 -0
  155. package/dist/observability/reviewPanels.js +46 -0
  156. package/dist/observability/routes/datasets.d.ts +7 -0
  157. package/dist/observability/routes/datasets.js +149 -0
  158. package/dist/observability/routes/evaluators.d.ts +7 -0
  159. package/dist/observability/routes/evaluators.js +123 -0
  160. package/dist/observability/routes/experiments.d.ts +7 -0
  161. package/dist/observability/routes/experiments.js +95 -0
  162. package/dist/observability/routes/observabilityRouteAccess.d.ts +13 -0
  163. package/dist/observability/routes/observabilityRouteAccess.js +67 -0
  164. package/dist/observability/routes/prompts.d.ts +12 -0
  165. package/dist/observability/routes/prompts.js +172 -0
  166. package/dist/observability/routes/requestAiService.d.ts +13 -0
  167. package/dist/observability/routes/requestAiService.js +21 -0
  168. package/dist/observability/routes/review.d.ts +7 -0
  169. package/dist/observability/routes/review.js +79 -0
  170. package/dist/observability/routes/status.d.ts +5 -0
  171. package/dist/observability/routes/status.js +31 -0
  172. package/dist/observability/routes/testMultiStage.d.ts +9 -0
  173. package/dist/observability/routes/testMultiStage.js +67 -0
  174. package/dist/observability/routes/traces.d.ts +7 -0
  175. package/dist/observability/routes/traces.js +129 -0
  176. package/dist/observability/schemaValidation.d.ts +5 -0
  177. package/dist/observability/schemaValidation.js +26 -0
  178. package/dist/observability/sopThresholds.d.ts +2 -0
  179. package/dist/observability/sopThresholds.js +21 -0
  180. package/dist/observability/status.d.ts +23 -0
  181. package/dist/observability/status.js +34 -0
  182. package/dist/observability/testMultiStageSchemas.d.ts +10 -0
  183. package/dist/observability/testMultiStageSchemas.js +79 -0
  184. package/dist/observability/testMultiStageWorkflow.d.ts +20 -0
  185. package/dist/observability/testMultiStageWorkflow.js +218 -0
  186. package/dist/observability/types.d.ts +159 -0
  187. package/dist/observability/types.js +67 -0
  188. package/dist/routes/files.js +20 -7
  189. package/dist/routes/gpt.js +179 -488
  190. package/dist/routes/gptActions.d.ts +9 -0
  191. package/dist/routes/gptActions.js +212 -0
  192. package/dist/routes/gptDatasets.d.ts +5 -0
  193. package/dist/routes/gptDatasets.js +94 -0
  194. package/dist/routes/gptHistories.d.ts +3 -1
  195. package/dist/routes/gptHistories.js +188 -2
  196. package/dist/service/aiDatasets.d.ts +57 -0
  197. package/dist/service/aiDatasets.js +253 -0
  198. package/dist/service/aiService.d.ts +50 -1
  199. package/dist/service/aiService.js +431 -77
  200. package/dist/service/askFiles.d.ts +49 -0
  201. package/dist/service/askFiles.js +162 -0
  202. package/dist/service/asks.d.ts +76 -0
  203. package/dist/service/asks.js +284 -0
  204. package/dist/service/chatTurn.d.ts +128 -0
  205. package/dist/service/chatTurn.js +1674 -0
  206. package/dist/service/fileStorage.d.ts +4 -0
  207. package/dist/service/fileStorage.js +7 -2
  208. package/dist/service/fileUploadsGate.d.ts +8 -0
  209. package/dist/service/fileUploadsGate.js +18 -0
  210. package/dist/service/fillActionIds.d.ts +6 -0
  211. package/dist/service/fillActionIds.js +79 -0
  212. package/dist/service/gemini.d.ts +3 -3
  213. package/dist/service/gemini.js +3 -3
  214. package/dist/service/jsonFenceModel.d.ts +7 -0
  215. package/dist/service/jsonFenceModel.js +45 -0
  216. package/dist/service/prompts.d.ts +52 -0
  217. package/dist/service/prompts.js +96 -1
  218. package/dist/service/sanitizeHtml.d.ts +11 -0
  219. package/dist/service/sanitizeHtml.js +108 -0
  220. package/dist/service/vertex.d.ts +6 -6
  221. package/dist/service/vertex.js +5 -5
  222. package/dist/types/harness.d.ts +934 -0
  223. package/dist/types/harness.js +133 -0
  224. package/dist/types/index.d.ts +219 -13
  225. package/dist/types/index.js +2 -0
  226. package/dist/types/observability.d.ts +288 -0
  227. package/dist/types/observability.js +2 -0
  228. package/package.json +17 -5
  229. package/src/aiApp.test.ts +207 -1
  230. package/src/aiApp.ts +31 -8
  231. package/src/harness/adminScreens.test.ts +34 -0
  232. package/src/harness/adminScreens.ts +11 -0
  233. package/src/harness/agentLoop.test.ts +1034 -0
  234. package/src/harness/agentLoop.ts +1082 -0
  235. package/src/harness/agentRecovery.test.ts +380 -0
  236. package/src/harness/agentTaskNames.ts +4 -0
  237. package/src/harness/approvalPolicy.ts +28 -0
  238. package/src/harness/approvals.test.ts +1094 -0
  239. package/src/harness/approvals.ts +644 -0
  240. package/src/harness/commit.ts +1049 -0
  241. package/src/harness/conversation.ts +503 -0
  242. package/src/harness/defineAgent.test.ts +92 -0
  243. package/src/harness/defineAgent.ts +82 -0
  244. package/src/harness/defineTask.test.ts +59 -0
  245. package/src/harness/defineTask.ts +76 -0
  246. package/src/harness/defineTool.ts +45 -0
  247. package/src/harness/definitionError.ts +13 -0
  248. package/src/harness/errors.test.ts +90 -0
  249. package/src/harness/errors.ts +119 -0
  250. package/src/harness/eventHub.test.ts +124 -0
  251. package/src/harness/eventHub.ts +159 -0
  252. package/src/harness/events.test.ts +757 -0
  253. package/src/harness/events.ts +384 -0
  254. package/src/harness/executionEnv.ts +34 -0
  255. package/src/harness/extensions.test.ts +1218 -0
  256. package/src/harness/extensions.ts +382 -0
  257. package/src/harness/harness.test.ts +693 -0
  258. package/src/harness/harness.ts +767 -0
  259. package/src/harness/harnessApp.ts +76 -0
  260. package/src/harness/internalRuntime.ts +44 -0
  261. package/src/harness/leases.ts +132 -0
  262. package/src/harness/memo.ts +107 -0
  263. package/src/harness/modelCall.test.ts +134 -0
  264. package/src/harness/modelCall.ts +200 -0
  265. package/src/harness/models/harnessApproval.ts +109 -0
  266. package/src/harness/models/harnessConversation.ts +142 -0
  267. package/src/harness/models/harnessEvent.ts +92 -0
  268. package/src/harness/models/harnessInboxEvent.ts +73 -0
  269. package/src/harness/models/harnessMemo.ts +43 -0
  270. package/src/harness/models/harnessMessage.ts +85 -0
  271. package/src/harness/models/harnessModels.test.ts +60 -0
  272. package/src/harness/models/harnessOwner.ts +40 -0
  273. package/src/harness/models/harnessTask.ts +201 -0
  274. package/src/harness/ownership.test.ts +1087 -0
  275. package/src/harness/ownership.ts +618 -0
  276. package/src/harness/recovery.test.ts +912 -0
  277. package/src/harness/registry.ts +124 -0
  278. package/src/harness/retries.test.ts +292 -0
  279. package/src/harness/retryBackoff.test.ts +100 -0
  280. package/src/harness/retryBackoff.ts +77 -0
  281. package/src/harness/routes/approvals.ts +123 -0
  282. package/src/harness/routes/conversations.test.ts +518 -0
  283. package/src/harness/routes/conversations.ts +102 -0
  284. package/src/harness/routes/events.ts +280 -0
  285. package/src/harness/routes/tasks.test.ts +198 -0
  286. package/src/harness/routes/tasks.ts +146 -0
  287. package/src/harness/runners/inProcessRunner.test.ts +402 -0
  288. package/src/harness/runners/inProcessRunner.ts +220 -0
  289. package/src/harness/runtime.ts +714 -0
  290. package/src/harness/subagent.test.ts +696 -0
  291. package/src/harness/subagent.ts +192 -0
  292. package/src/harness/suspend.ts +13 -0
  293. package/src/harness/transaction.ts +17 -0
  294. package/src/harness/versioning.test.ts +288 -0
  295. package/src/harness/waits.test.ts +693 -0
  296. package/src/harness/waits.ts +356 -0
  297. package/src/index.test.ts +13 -0
  298. package/src/index.ts +70 -0
  299. package/src/isolated/adminScreensConfig.isolated.ts +143 -0
  300. package/src/models/aiDataset.test.ts +34 -0
  301. package/src/models/aiDataset.ts +78 -0
  302. package/src/models/aiRequest.test.ts +2 -2
  303. package/src/models/aiRequest.ts +2 -1
  304. package/src/models/gptHistory.test.ts +156 -0
  305. package/src/models/gptHistory.ts +106 -4
  306. package/src/observability/adminScreens.test.ts +71 -0
  307. package/src/observability/adminScreens.ts +42 -0
  308. package/src/observability/compileTemplate.test.ts +14 -0
  309. package/src/observability/compileTemplate.ts +10 -0
  310. package/src/observability/datasetImport.test.ts +95 -0
  311. package/src/observability/datasetImport.ts +220 -0
  312. package/src/observability/evaluate.test.ts +549 -0
  313. package/src/observability/evaluate.ts +254 -0
  314. package/src/observability/evaluatorTemplates.ts +124 -0
  315. package/src/observability/jsonPath.test.ts +52 -0
  316. package/src/observability/jsonPath.ts +60 -0
  317. package/src/observability/local/datasetStore.test.ts +230 -0
  318. package/src/observability/local/datasetStore.ts +452 -0
  319. package/src/observability/local/evaluatorStore.test.ts +138 -0
  320. package/src/observability/local/evaluatorStore.ts +224 -0
  321. package/src/observability/local/experimentRunner.test.ts +489 -0
  322. package/src/observability/local/experimentRunner.ts +548 -0
  323. package/src/observability/local/localPlugin.ts +88 -0
  324. package/src/observability/local/models/obsDataset.ts +38 -0
  325. package/src/observability/local/models/obsDatasetItem.ts +80 -0
  326. package/src/observability/local/models/obsEvaluator.ts +102 -0
  327. package/src/observability/local/models/obsExperiment.ts +91 -0
  328. package/src/observability/local/models/obsExperimentItem.ts +52 -0
  329. package/src/observability/local/models/obsPrompt.ts +43 -0
  330. package/src/observability/local/models/obsPromptLabel.ts +43 -0
  331. package/src/observability/local/models/obsPromptVersion.ts +107 -0
  332. package/src/observability/local/models/obsReviewItem.ts +79 -0
  333. package/src/observability/local/models/obsScore.ts +59 -0
  334. package/src/observability/local/models/obsSpan.ts +65 -0
  335. package/src/observability/local/models/obsTrace.ts +94 -0
  336. package/src/observability/local/obsModels.test.ts +119 -0
  337. package/src/observability/local/promptStore.test.ts +351 -0
  338. package/src/observability/local/promptStore.ts +665 -0
  339. package/src/observability/local/reviewStore.test.ts +164 -0
  340. package/src/observability/local/reviewStore.ts +264 -0
  341. package/src/observability/local/traceStore.test.ts +264 -0
  342. package/src/observability/local/traceStore.ts +440 -0
  343. package/src/observability/observabilityApp.test.ts +92 -0
  344. package/src/observability/observabilityApp.ts +185 -0
  345. package/src/observability/observabilityAppRegistry.test.ts +30 -0
  346. package/src/observability/observabilityAppRegistry.ts +35 -0
  347. package/src/observability/permissions.test.ts +96 -0
  348. package/src/observability/permissions.ts +78 -0
  349. package/src/observability/reviewPanels.test.ts +35 -0
  350. package/src/observability/reviewPanels.ts +64 -0
  351. package/src/observability/routes/datasets.test.ts +176 -0
  352. package/src/observability/routes/datasets.ts +252 -0
  353. package/src/observability/routes/evaluators.test.ts +119 -0
  354. package/src/observability/routes/evaluators.ts +207 -0
  355. package/src/observability/routes/experiments.test.ts +282 -0
  356. package/src/observability/routes/experiments.ts +152 -0
  357. package/src/observability/routes/observabilityRbac.test.ts +767 -0
  358. package/src/observability/routes/observabilityRouteAccess.ts +106 -0
  359. package/src/observability/routes/prompts.test.ts +320 -0
  360. package/src/observability/routes/prompts.ts +263 -0
  361. package/src/observability/routes/requestAiService.ts +36 -0
  362. package/src/observability/routes/review.test.ts +120 -0
  363. package/src/observability/routes/review.ts +125 -0
  364. package/src/observability/routes/status.test.ts +116 -0
  365. package/src/observability/routes/status.ts +47 -0
  366. package/src/observability/routes/testMultiStage.test.ts +214 -0
  367. package/src/observability/routes/testMultiStage.ts +96 -0
  368. package/src/observability/routes/traces.test.ts +167 -0
  369. package/src/observability/routes/traces.ts +172 -0
  370. package/src/observability/schemaValidation.ts +29 -0
  371. package/src/observability/sopThresholds.ts +20 -0
  372. package/src/observability/status.test.ts +38 -0
  373. package/src/observability/status.ts +56 -0
  374. package/src/observability/testMultiStageSchemas.ts +95 -0
  375. package/src/observability/testMultiStageWorkflow.test.ts +114 -0
  376. package/src/observability/testMultiStageWorkflow.ts +307 -0
  377. package/src/observability/types.ts +262 -0
  378. package/src/routes/files.test.ts +84 -9
  379. package/src/routes/files.ts +23 -7
  380. package/src/routes/gpt.test.ts +3279 -0
  381. package/src/routes/gpt.ts +214 -552
  382. package/src/routes/gptActions.test.ts +241 -0
  383. package/src/routes/gptActions.ts +255 -0
  384. package/src/routes/gptApprovals.test.ts +762 -0
  385. package/src/routes/gptDatasets.test.ts +86 -0
  386. package/src/routes/gptDatasets.ts +104 -0
  387. package/src/routes/gptFiles.test.ts +589 -0
  388. package/src/routes/gptHistories.test.ts +933 -0
  389. package/src/routes/gptHistories.ts +266 -5
  390. package/src/routes/routes.test.ts +676 -5
  391. package/src/service/aiDatasets.test.ts +123 -0
  392. package/src/service/aiDatasets.ts +316 -0
  393. package/src/service/aiService.test.ts +614 -0
  394. package/src/service/aiService.ts +575 -111
  395. package/src/service/askFiles.test.ts +96 -0
  396. package/src/service/askFiles.ts +222 -0
  397. package/src/service/asks.test.ts +946 -0
  398. package/src/service/asks.ts +423 -0
  399. package/src/service/chatTurn.ts +2287 -0
  400. package/src/service/fileStorage.test.ts +11 -0
  401. package/src/service/fileStorage.ts +10 -2
  402. package/src/service/fileUploadsGate.ts +23 -0
  403. package/src/service/fillActionIds.test.ts +65 -0
  404. package/src/service/fillActionIds.ts +98 -0
  405. package/src/service/gemini.test.ts +16 -10
  406. package/src/service/gemini.ts +3 -3
  407. package/src/service/jsonFenceModel.test.ts +47 -0
  408. package/src/service/jsonFenceModel.ts +53 -0
  409. package/src/service/prompts.test.ts +21 -0
  410. package/src/service/prompts.ts +147 -0
  411. package/src/service/sanitizeHtml.test.ts +61 -0
  412. package/src/service/sanitizeHtml.ts +109 -0
  413. package/src/service/vertex.test.ts +49 -49
  414. package/src/service/vertex.ts +7 -8
  415. package/src/tests/bunSetup.test.ts +7 -4
  416. package/src/tests/bunSetup.ts +7 -7
  417. package/src/tests/chatHarness.ts +478 -0
  418. package/src/tests/generateStream.ts +49 -0
  419. package/src/tests/harnessErrors.ts +22 -0
  420. package/src/tests/harnessStreaming.ts +227 -0
  421. package/src/tests/helpers.ts +9 -2
  422. package/src/types/harness.ts +1095 -0
  423. package/src/types/index.ts +237 -12
  424. package/src/types/observability.ts +344 -0
  425. package/src/types/sanitize-html.d.ts +25 -0
package/src/routes/gpt.ts CHANGED
@@ -3,115 +3,91 @@ import {
3
3
  asyncHandler,
4
4
  authenticateMiddleware,
5
5
  createOpenApiBuilder,
6
+ isAPIError,
6
7
  logger,
8
+ type OpenApiSchemaProperty,
7
9
  } from "@terreno/api";
8
- import type {Tool} from "ai";
9
- import {stepCountIs, streamText} from "ai";
10
+ import {ASK_SURFACES, askSurfaceSchema} from "@terreno/blocks";
10
11
  import type express from "express";
11
12
  import {DateTime} from "luxon";
12
13
  import type mongoose from "mongoose";
13
- import {createTelemetryConfig, preparePromptForAI} from "../langfuseVercelAi";
14
14
 
15
- import {AIRequest} from "../models/aiRequest";
16
15
  import {GptHistory} from "../models/gptHistory";
17
- import {Project} from "../models/project";
18
- import {AIService} from "../service/aiService";
19
- import {TITLE_GENERATION_PROMPT} from "../service/prompts";
20
- import type {
21
- GptHistoryDocument,
22
- GptHistoryPrompt,
23
- GptRouteOptions,
24
- MessageContentPart,
25
- } from "../types";
26
-
27
- interface GeneratedImageFile {
28
- base64: string;
29
- mediaType: string;
30
- }
31
-
32
- const isGeneratedImageFile = (value: unknown): value is GeneratedImageFile =>
33
- typeof value === "object" &&
34
- value !== null &&
35
- "base64" in value &&
36
- typeof value.base64 === "string" &&
37
- "mediaType" in value &&
38
- typeof value.mediaType === "string" &&
39
- value.mediaType.startsWith("image/");
40
-
41
- const DEMO_RESPONSE =
42
- "This is demo mode. To use AI features, paste your Gemini API key in Settings.";
43
-
44
- /** Send a canned SSE demo response when no AI service is available. */
45
- const sendDemoResponse = (res: express.Response, historyId?: string): void => {
46
- res.setHeader("Content-Type", "text/event-stream");
47
- res.setHeader("Cache-Control", "no-cache");
48
- res.setHeader("Connection", "keep-alive");
49
- res.write(`data: ${JSON.stringify({text: DEMO_RESPONSE})}\n\n`);
50
- res.write(`data: ${JSON.stringify({done: true, ...(historyId ? {historyId} : {})})}\n\n`);
51
- res.end();
16
+ import {configureAiDatasets} from "../service/aiDatasets";
17
+ import {assertNoReservedToolNames, resolveAskKinds} from "../service/asks";
18
+ import {type ChatTurnSink, DEMO_RESPONSE, resolveAiService, runChatTurn} from "../service/chatTurn";
19
+ import type {GptRouteOptions} from "../types";
20
+ import {addGptActionRoutes} from "./gptActions";
21
+ import {addGptDatasetRoutes} from "./gptDatasets";
22
+
23
+ const ASK_RESPONSE_BODY: OpenApiSchemaProperty = {
24
+ description:
25
+ "The user's answer to the conversation's pending ask. Send it with historyId instead of prompt.",
26
+ properties: {
27
+ action: {enum: ["accept", "decline", "cancel"], type: "string"},
28
+ content: {description: "The answer, when action is accept", type: "object"},
29
+ reason: {description: "Why the ask was cancelled, when action is cancel", type: "string"},
30
+ toolCallId: {description: "The pending ask's toolCallId", type: "string"},
31
+ },
32
+ type: "object",
52
33
  };
53
34
 
54
- /**
55
- * Resolve the AIService for a request. Priority:
56
- * 1. Per-request API key via `x-ai-api-key` header (creates a temporary AIService via createModelFn)
57
- * 2. Specific model requested + server-side model factory (creates a temporary AIService via createServerModelFn)
58
- * 3. Pre-configured aiService from route options
59
- * 4. undefined (triggers demo mode response)
60
- */
61
- const resolveAiService = (
62
- req: express.Request,
63
- options: GptRouteOptions,
64
- modelId?: string
65
- ): AIService | undefined => {
66
- const perRequestKey = req.headers["x-ai-api-key"] as string | undefined;
67
- if (perRequestKey && options.createModelFn) {
68
- return new AIService({model: options.createModelFn(perRequestKey, modelId)});
69
- }
70
- if (modelId && options.createServerModelFn) {
71
- const serverModel = options.createServerModelFn(modelId);
72
- if (serverModel) {
73
- return new AIService({model: serverModel});
74
- }
75
- }
76
- return options.aiService;
35
+ const SURFACE_BODY: OpenApiSchemaProperty = {
36
+ description: askSurfaceSchema.description,
37
+ enum: [...ASK_SURFACES],
38
+ type: "string",
77
39
  };
78
40
 
79
- /** Generate a short title for a conversation using a cheap model call. */
80
- const generateTitle = async (
81
- prompt: string,
82
- response: string,
83
- aiService: AIService,
84
- options: GptRouteOptions,
85
- perRequestApiKey?: string
86
- ): Promise<string | undefined> => {
87
- try {
88
- let titleService = aiService;
89
- if (options.titleModelId) {
90
- if (options.createModelFn && perRequestApiKey) {
91
- titleService = new AIService({
92
- model: options.createModelFn(perRequestApiKey, options.titleModelId),
93
- });
94
- } else if (options.createServerModelFn) {
95
- const titleModel = options.createServerModelFn(options.titleModelId);
96
- if (titleModel) {
97
- titleService = new AIService({model: titleModel});
98
- }
99
- }
100
- }
101
- const conversationSnippet = `User: ${prompt}\nAssistant: ${response.substring(0, 500)}`;
102
- const title = await titleService.generateText({
103
- prompt: conversationSnippet,
104
- systemPrompt: TITLE_GENERATION_PROMPT,
105
- temperature: 0.3,
106
- });
107
- return title.trim().replace(/^["']|["']$/g, "");
108
- } catch {
109
- return undefined;
110
- }
41
+ interface SseSink extends ChatTurnSink {
42
+ isOpen: () => boolean;
43
+ }
44
+
45
+ const DEFAULT_STREAM_RESUME_POLL_INTERVAL_MS = 500;
46
+ const DEFAULT_STREAM_STALE_AFTER_MS = 60_000;
47
+
48
+ const sseEvent = (payload: Record<string, unknown>): string =>
49
+ `data: ${JSON.stringify(payload)}\n\n`;
50
+
51
+ const sleep = (ms: number): Promise<void> =>
52
+ new Promise((resolve) => {
53
+ setTimeout(resolve, ms);
54
+ });
55
+
56
+ /** Writes turn events as server-sent events. Headers go out when the turn opens the sink. */
57
+ const createSseSink = (res: express.Response): SseSink => {
58
+ let isOpen = false;
59
+ return {
60
+ emit: (event) => {
61
+ res.write(`data: ${JSON.stringify(event)}\n\n`);
62
+ },
63
+ isOpen: () => isOpen,
64
+ open: () => {
65
+ res.setHeader("Content-Type", "text/event-stream");
66
+ res.setHeader("Cache-Control", "no-cache");
67
+ res.setHeader("Connection", "keep-alive");
68
+ isOpen = true;
69
+ },
70
+ };
111
71
  };
112
72
 
113
73
  export const addGptRoutes = (router: express.Router, options: GptRouteOptions): void => {
114
- const {mcpService, tools: routeTools, createRequestTools, toolChoice, maxSteps} = options;
74
+ const {mcpService, tools: routeTools, createRequestTools} = options;
75
+ const askKinds = resolveAskKinds(options.asks);
76
+ if (options.uiBlocks) {
77
+ const uiBlocks = options.uiBlocks === true ? {} : options.uiBlocks;
78
+ configureAiDatasets({
79
+ datasetMaxRows: uiBlocks.datasetMaxRows,
80
+ datasetTtlDays: uiBlocks.datasetTtlDays,
81
+ });
82
+ addGptDatasetRoutes(router, {openApiOptions: options.openApiOptions});
83
+ addGptActionRoutes(router, options);
84
+ }
85
+ if (askKinds.length > 0) {
86
+ assertNoReservedToolNames(routeTools);
87
+ }
88
+ const resumePollIntervalMs =
89
+ options.streamResumePollIntervalMs ?? DEFAULT_STREAM_RESUME_POLL_INTERVAL_MS;
90
+ const staleAfterMs = options.streamStaleAfterMs ?? DEFAULT_STREAM_STALE_AFTER_MS;
115
91
 
116
92
  router.post(
117
93
  "/gpt/prompt",
@@ -121,6 +97,7 @@ export const addGptRoutes = (router: express.Router, options: GptRouteOptions):
121
97
  .withTags(["gpt"])
122
98
  .withSummary("Stream a GPT chat response")
123
99
  .withRequestBody({
100
+ ...(askKinds.length > 0 ? {askResponse: ASK_RESPONSE_BODY} : {}),
124
101
  attachments: {
125
102
  items: {
126
103
  properties: {
@@ -137,6 +114,7 @@ export const addGptRoutes = (router: express.Router, options: GptRouteOptions):
137
114
  model: {type: "string"},
138
115
  projectId: {type: "string"},
139
116
  prompt: {type: "string"},
117
+ surface: SURFACE_BODY,
140
118
  systemPrompt: {type: "string"},
141
119
  })
142
120
  .withResponse(200, {data: {type: "string"}})
@@ -145,476 +123,155 @@ export const addGptRoutes = (router: express.Router, options: GptRouteOptions):
145
123
  // Use a raw handler instead of asyncHandler so we can control error responses
146
124
  // for both pre-stream (JSON) and mid-stream (SSE) errors.
147
125
  async (req: express.Request, res: express.Response) => {
148
- let sseStarted = false;
126
+ const sink = createSseSink(res);
149
127
  try {
150
- const {
151
- prompt,
152
- historyId,
153
- systemPrompt,
154
- attachments,
155
- model: requestModel,
156
- projectId,
157
- } = req.body;
158
- const userId = (req.user as {_id?: mongoose.Types.ObjectId} | undefined)?._id;
159
-
160
- if (!prompt || typeof prompt !== "string") {
161
- throw new APIError({status: 400, title: "prompt is required"});
162
- }
163
-
164
- // Resolve AI service (per-request key takes priority, then configured service)
165
- const hasPerRequestKey = !!req.headers["x-ai-api-key"];
166
- const hasCreateModelFn = !!options.createModelFn;
167
- const hasConfiguredService = !!options.aiService;
168
- logger.debug("Resolving AI service", {
169
- hasConfiguredService,
170
- hasCreateModelFn,
171
- hasPerRequestKey,
172
- });
128
+ await runChatTurn({body: req.body ?? {}, options, req, sink});
129
+ res.end();
130
+ } catch (error) {
131
+ // Catch-all for errors thrown before or after SSE streaming
132
+ const message = error instanceof Error ? error.message : String(error);
133
+ const status = isAPIError(error) ? error.status : 500;
134
+ const stack = error instanceof Error ? error.stack : undefined;
135
+ logger.error("GPT prompt handler error", {error: message, stack, status});
173
136
 
174
- const aiService = resolveAiService(req, options, requestModel);
175
- if (!aiService) {
176
- logger.debug("No AI service available, sending demo response");
177
- return sendDemoResponse(res);
137
+ if (sink.isOpen()) {
138
+ // Already sent SSE headers — send error as SSE event
139
+ res.write(`data: ${JSON.stringify({error: message})}\n\n`);
140
+ res.end();
141
+ return;
178
142
  }
143
+ // Haven't started SSE — send a normal JSON error response
144
+ const fields = isAPIError(error) ? error.meta?.fields : undefined;
145
+ res.status(status).json({
146
+ detail: (isAPIError(error) ? error.detail : undefined) ?? message,
147
+ ...(fields ? {fields} : {}),
148
+ status,
149
+ title: isAPIError(error) ? error.title : "Internal server error",
150
+ });
151
+ }
152
+ }
153
+ );
179
154
 
180
- // Load or create history
181
- let history: GptHistoryDocument | null;
182
- if (historyId) {
183
- history = await GptHistory.findById(historyId);
184
- if (!history) {
185
- throw new APIError({status: 404, title: "History not found"});
186
- }
187
- if (history.userId.toString() !== userId?.toString()) {
188
- throw new APIError({status: 403, title: "Not authorized to access this history"});
189
- }
190
- } else {
191
- history = new GptHistory({prompts: [], userId, ...(projectId ? {projectId} : {})});
192
- }
155
+ // Re-attach to a reply that /gpt/prompt is still streaming (e.g. after a reload or remount).
156
+ // Polls the persisted partial output, so it works across server instances.
157
+ router.get(
158
+ "/gpt/histories/:id/stream",
159
+ [
160
+ authenticateMiddleware(),
161
+ createOpenApiBuilder(options.openApiOptions ?? {})
162
+ .withTags(["gpt"])
163
+ .withSummary("Resume an in-flight GPT reply as SSE")
164
+ .withPathParameter("id", {type: "string"})
165
+ .withQueryParameter(
166
+ "streamId",
167
+ {type: "string"},
168
+ {description: "Reply to follow. Defaults to the latest streaming reply."}
169
+ )
170
+ .withQueryParameter(
171
+ "offset",
172
+ {type: "number"},
173
+ {description: "Characters of the reply the client already shows. Defaults to 0."}
174
+ )
175
+ .withResponse(200, {data: {type: "string"}})
176
+ .build(),
177
+ ],
178
+ asyncHandler(async (req: express.Request, res: express.Response) => {
179
+ const {id} = req.params;
180
+ const userId = (req.user as {_id?: mongoose.Types.ObjectId} | undefined)?._id;
181
+ const requestedStreamId =
182
+ typeof req.query.streamId === "string" ? req.query.streamId : undefined;
183
+ const parsedOffset = Number(req.query.offset ?? 0);
184
+ const offset = Number.isFinite(parsedOffset) && parsedOffset > 0 ? parsedOffset : 0;
193
185
 
194
- // If history doesn't have a projectId yet but one was provided, associate it
195
- if (projectId && !history.projectId) {
196
- history.projectId = projectId;
197
- }
186
+ const history = await GptHistory.findById(id);
187
+ if (!history) {
188
+ throw new APIError({status: 404, title: "History not found"});
189
+ }
190
+ if (history.userId.toString() !== userId?.toString()) {
191
+ throw new APIError({status: 403, title: "Not authorized to access this history"});
192
+ }
198
193
 
199
- // Load project context if a projectId is provided (or inherited from history)
200
- const effectiveProjectId = projectId ?? history.projectId;
201
- let effectiveSystemPrompt = systemPrompt;
202
- if (effectiveProjectId) {
203
- try {
204
- const project = await Project.findById(effectiveProjectId);
205
- if (project && project.userId.toString() === userId?.toString()) {
206
- const parts: string[] = [];
207
- if (project.systemContext) {
208
- parts.push(project.systemContext);
209
- }
210
- if (project.memories.length > 0) {
211
- parts.push(
212
- `## Relevant Memories\n${project.memories.map((m) => `- ${m.text}`).join("\n")}`
213
- );
214
- }
215
- if (parts.length > 0) {
216
- const projectContext = parts.join("\n\n");
217
- effectiveSystemPrompt = effectiveSystemPrompt
218
- ? `${projectContext}\n\n${effectiveSystemPrompt}`
219
- : projectContext;
220
- }
221
- }
222
- } catch {
223
- // Project loading failure should not block the request
224
- }
225
- }
194
+ const reply = requestedStreamId
195
+ ? history.prompts.find((p) => p.streamId === requestedStreamId)
196
+ : history.prompts.findLast((p) => p.status === "streaming");
197
+ const streamId = reply?.streamId;
226
198
 
227
- // Load system prompt from Langfuse if configured
228
- if (options.langfuseSystemPromptName) {
229
- try {
230
- const langfuseResult = await preparePromptForAI({
231
- promptName: options.langfuseSystemPromptName,
232
- userId: userId?.toString(),
233
- });
234
- const langfusePrompt =
235
- typeof langfuseResult.prompt === "string" ? langfuseResult.prompt : undefined;
236
- if (langfusePrompt) {
237
- effectiveSystemPrompt = effectiveSystemPrompt
238
- ? `${langfusePrompt}\n\n${effectiveSystemPrompt}`
239
- : langfusePrompt;
240
- }
241
- } catch (err) {
242
- logger.debug(`Langfuse system prompt skipped: ${(err as Error).message}`);
243
- }
244
- }
199
+ res.setHeader("Content-Type", "text/event-stream");
200
+ res.setHeader("Cache-Control", "no-cache");
201
+ res.setHeader("Connection", "keep-alive");
202
+ res.write(sseEvent({historyId: id, resumed: true, ...(streamId ? {streamId} : {})}));
245
203
 
246
- // Build content parts from attachments
247
- const contentParts: MessageContentPart[] = [{text: prompt, type: "text"}];
248
- if (attachments && Array.isArray(attachments)) {
249
- logger.debug("Processing attachments", {
250
- count: attachments.length,
251
- types: attachments.map((a: {mimeType?: string; type?: string; url?: string}) => ({
252
- mimeType: a.mimeType,
253
- type: a.type,
254
- urlLength: a.url?.length ?? 0,
255
- })),
256
- });
257
- for (const attachment of attachments) {
258
- if (attachment.type === "image") {
259
- contentParts.push({
260
- mimeType: attachment.mimeType,
261
- type: "image",
262
- url: attachment.url,
263
- });
264
- } else if (attachment.type === "file") {
265
- contentParts.push({
266
- filename: attachment.filename,
267
- mimeType: attachment.mimeType,
268
- type: "file",
269
- url: attachment.url,
270
- });
271
- }
272
- }
273
- }
204
+ let isClosed = false;
205
+ req.on("close", () => {
206
+ isClosed = true;
207
+ });
274
208
 
275
- // Add user prompt to history
276
- const hasAttachments = contentParts.length > 1;
277
- const userPrompt: GptHistoryPrompt = {
278
- text: prompt,
279
- type: "user",
280
- ...(hasAttachments ? {content: contentParts} : {}),
281
- };
282
- history.prompts.push(userPrompt);
283
-
284
- // Build messages from history using AIService helper
285
- logger.debug("Building messages", {
286
- attachmentCount: contentParts.length - 1,
287
- historyLength: history.prompts.length,
288
- });
289
- const messages = aiService.buildMessages(history.prompts);
290
- logger.debug("Messages built", {messageCount: messages.length});
291
-
292
- // Some models (e.g. gemini-2.5-flash-image) don't support tool calling
293
- const modelId = aiService.modelId;
294
- const supportsTools = !modelId?.includes("image");
295
-
296
- // Merge tools from route config, per-request tools, and MCP service
297
- let allTools: Record<string, Tool> | undefined;
298
- const requestTools = createRequestTools ? createRequestTools(req) : undefined;
299
- if (supportsTools && (routeTools || requestTools || mcpService)) {
300
- allTools = {...(routeTools ?? {}), ...(requestTools ?? {})};
301
- if (mcpService) {
302
- try {
303
- const mcpTools = await mcpService.getTools();
304
- Object.assign(allTools, mcpTools);
305
- } catch {
306
- // MCP tool discovery failure should not block the request
307
- }
209
+ let isInitialPoll = true;
210
+ let sentText: string | undefined;
211
+ let sentLength = offset;
212
+ try {
213
+ while (!isClosed) {
214
+ const current = streamId ? await GptHistory.findById(id) : history;
215
+ if (!current) {
216
+ res.write(sseEvent({error: "History not found"}));
217
+ break;
308
218
  }
309
- }
310
-
311
- // Stream response via SSE
312
- res.setHeader("Content-Type", "text/event-stream");
313
- res.setHeader("Cache-Control", "no-cache");
314
- res.setHeader("Connection", "keep-alive");
315
- sseStarted = true;
316
-
317
- let fullResponse = "";
318
- const generatedImages: Array<{mimeType: string; url: string}> = [];
319
- // Stream file parts and result.files report the same images; emit each data URL once.
320
- const sendGeneratedImage = (file: GeneratedImageFile): void => {
321
- const url = `data:${file.mediaType};base64,${file.base64}`;
322
- if (generatedImages.some((img) => img.url === url)) {
323
- return;
219
+ const currentReply = streamId
220
+ ? current.prompts.find((p) => p.streamId === streamId)
221
+ : undefined;
222
+ const text = currentReply?.text ?? "";
223
+
224
+ if (isInitialPoll && offset > 0) {
225
+ // The client can have loaded a stale placeholder before reconnecting.
226
+ res.write(sseEvent({replace: true, text}));
227
+ } else if (sentText !== undefined && !text.startsWith(sentText)) {
228
+ // Text from a step that turned into a tool call was discarded; resend the reply
229
+ res.write(sseEvent({replace: true, text}));
230
+ } else if (text.length > sentLength) {
231
+ res.write(sseEvent({text: text.slice(sentLength)}));
324
232
  }
325
- generatedImages.push({mimeType: file.mediaType, url});
326
- res.write(`data: ${JSON.stringify({image: {mimeType: file.mediaType, url}})}\n\n`);
327
- };
328
- const startTime = DateTime.now().toMillis();
329
- try {
330
- logger.debug("Starting streamText", {model: modelId, supportsTools});
331
- const telemetry = createTelemetryConfig({
332
- functionId: "gpt-prompt",
333
- metadata: {
334
- ...(options.langfuseSystemPromptName
335
- ? {langfusePromptName: options.langfuseSystemPromptName}
336
- : {}),
337
- },
338
- userId: userId?.toString(),
339
- });
340
- const result = streamText({
341
- experimental_telemetry: telemetry,
342
- messages,
343
- model: aiService.model,
344
- providerOptions: !supportsTools
345
- ? {
346
- google: {responseModalities: ["TEXT", "IMAGE"]},
347
- vertex: {responseModalities: ["TEXT", "IMAGE"]},
348
- }
349
- : undefined,
350
- stopWhen: allTools ? stepCountIs(maxSteps ?? 5) : stepCountIs(1),
351
- system: effectiveSystemPrompt ?? undefined,
352
- temperature: aiService.defaultTemperature,
353
- toolChoice: allTools ? (toolChoice ?? "auto") : undefined,
354
- tools: allTools,
355
- });
356
-
357
- let partCount = 0;
358
- // Buffer text per step so we can discard reasoning text when a tool call follows
359
- let stepTextBuffer = "";
360
- let stepHasToolCall = false;
361
-
362
- for await (const part of result.fullStream as AsyncIterable<{
363
- type: string;
364
- [key: string]: unknown;
365
- }>) {
366
- partCount++;
367
- if (partCount <= 5 || part.type === "error" || part.type === "file") {
368
- logger.debug("Stream part", {
369
- partCount,
370
- type: part.type,
371
- ...(part.type === "file" && isGeneratedImageFile(part.file)
372
- ? {mediaType: part.file.mediaType}
373
- : {}),
374
- ...(part.type === "error" ? {error: String(part.error ?? part)} : {}),
375
- });
376
- }
377
-
378
- // Track step boundaries to discard reasoning text from tool-call steps
379
- if (part.type === "start-step") {
380
- stepTextBuffer = "";
381
- stepHasToolCall = false;
382
- continue;
383
- }
384
- if (part.type === "finish-step") {
385
- // Only emit buffered text if no tool call happened in this step
386
- if (!stepHasToolCall && stepTextBuffer) {
387
- // Strip model reasoning that leaks as JSON action blobs
388
- const cleaned = stepTextBuffer
389
- .replace(/\{[\s\S]*?"action"[\s\S]*?\}\s*$/g, "")
390
- .trim();
391
- if (cleaned) {
392
- fullResponse += cleaned;
393
- res.write(`data: ${JSON.stringify({text: cleaned})}\n\n`);
394
- }
395
- }
396
- stepTextBuffer = "";
397
- stepHasToolCall = false;
398
- continue;
399
- }
400
-
401
- if (part.type === "error") {
402
- const errMsg =
403
- part.error instanceof Error
404
- ? part.error.message
405
- : String(part.error ?? "Unknown stream error");
406
- logger.error("AI stream error part", {error: errMsg});
407
- res.write(`data: ${JSON.stringify({error: errMsg})}\n\n`);
408
- continue;
409
- }
410
- if (part.type === "file") {
411
- if (isGeneratedImageFile(part.file)) {
412
- sendGeneratedImage(part.file);
413
- logger.debug("Sent inline image from stream");
414
- }
415
- continue;
416
- }
417
- if (part.type === "text-delta") {
418
- const textChunk = (part.text ?? "") as string;
419
- if (textChunk) {
420
- stepTextBuffer += textChunk;
421
- }
422
- } else if (part.type === "tool-call") {
423
- stepHasToolCall = true;
424
- res.write(
425
- `data: ${JSON.stringify({
426
- toolCall: {
427
- args: part.input,
428
- toolCallId: part.toolCallId,
429
- toolName: part.toolName,
430
- },
431
- })}\n\n`
432
- );
433
- // Persist tool call in history
434
- history.prompts.push({
435
- args: part.input as Record<string, unknown>,
436
- text: `Tool call: ${part.toolName as string}`,
437
- toolCallId: part.toolCallId as string,
438
- toolName: part.toolName as string,
439
- type: "tool-call",
440
- });
441
- } else if (part.type === "tool-result") {
442
- const toolResult = part.output as Record<string, unknown> | undefined;
443
-
444
- // If the tool result contains file data, send it as a separate file SSE event
445
- if (toolResult?.fileData && typeof toolResult.fileData === "string") {
446
- res.write(
447
- `data: ${JSON.stringify({
448
- file: {
449
- filename: toolResult.filename ?? "document",
450
- mimeType: toolResult.mimeType ?? "application/octet-stream",
451
- url: toolResult.fileData,
452
- },
453
- })}\n\n`
454
- );
455
- logger.debug("Sent generated file from tool result", {
456
- filename: toolResult.filename,
457
- mimeType: toolResult.mimeType,
458
- });
459
- }
460
-
461
- // Strip fileData from result before sending/storing to avoid bloating the SSE and DB
462
- const {fileData: _fileData, ...cleanResult} = toolResult ?? {};
463
-
464
- res.write(
465
- `data: ${JSON.stringify({
466
- toolResult: {
467
- result: cleanResult,
468
- toolCallId: part.toolCallId,
469
- toolName: part.toolName,
470
- },
471
- })}\n\n`
233
+ isInitialPoll = false;
234
+ sentText = text;
235
+ sentLength = text.length;
236
+
237
+ if (currentReply?.status === "streaming") {
238
+ const sinceUpdateMs = DateTime.now()
239
+ .diff(DateTime.fromJSDate(current.updated))
240
+ .toMillis();
241
+ if (sinceUpdateMs > staleAfterMs) {
242
+ await GptHistory.updateOne(
243
+ {_id: current._id, prompts: {$elemMatch: {status: "streaming", streamId}}},
244
+ {$set: {"prompts.$.status": "error"}}
472
245
  );
473
- // Persist tool result in history (without the large file data)
474
- history.prompts.push({
475
- result: cleanResult as unknown,
476
- text: `Tool result: ${part.toolName as string}`,
477
- toolCallId: part.toolCallId as string,
478
- toolName: part.toolName as string,
479
- type: "tool-result",
480
- });
246
+ res.write(sseEvent({error: "The reply was interrupted before it finished"}));
247
+ res.write(sseEvent({done: true, historyId: id}));
248
+ break;
481
249
  }
250
+ await sleep(resumePollIntervalMs);
251
+ continue;
482
252
  }
483
253
 
484
- // Flush any remaining buffered text from the last step
485
- if (!stepHasToolCall && stepTextBuffer) {
486
- const cleaned = stepTextBuffer.replace(/\{[\s\S]*?"action"[\s\S]*?\}\s*$/g, "").trim();
487
- if (cleaned) {
488
- fullResponse += cleaned;
489
- res.write(`data: ${JSON.stringify({text: cleaned})}\n\n`);
490
- }
491
- }
492
-
493
- logger.debug("Stream completed", {fullResponseLength: fullResponse.length, partCount});
494
-
495
- // Catch generated images the stream did not report as file parts
496
- try {
497
- const files = await result.files;
498
- if (files && files.length > 0) {
499
- for (const file of files) {
500
- if (isGeneratedImageFile(file)) {
501
- sendGeneratedImage(file);
502
- }
503
- }
504
- logger.debug("Sent generated images", {count: generatedImages.length});
505
- }
506
- } catch (fileErr) {
507
- logger.debug("No files in response", {
508
- error: fileErr instanceof Error ? fileErr.message : String(fileErr),
509
- });
510
- }
511
-
512
- // Save assistant response to history
513
- if (fullResponse || generatedImages.length > 0) {
514
- const contentParts: MessageContentPart[] = generatedImages.map((img) => ({
515
- mimeType: img.mimeType,
516
- type: "image" as const,
517
- url: img.url,
518
- }));
519
- const assistantPrompt: GptHistoryPrompt = {
520
- model: aiService.modelId,
521
- text: fullResponse,
522
- type: "assistant",
523
- ...(contentParts.length > 0 ? {content: contentParts} : {}),
524
- };
525
- history.prompts.push(assistantPrompt);
526
- }
527
- await history.save();
528
-
529
- try {
530
- await AIRequest.logRequest({
531
- aiModel: modelId ?? "unknown",
532
- prompt,
533
- requestType: "general",
534
- response: fullResponse,
535
- responseTime: DateTime.now().toMillis() - startTime,
536
- userId: userId ?? undefined,
537
- });
538
- } catch (logErr) {
539
- logger.warn("Failed to log AIRequest", {
540
- error: logErr instanceof Error ? logErr.message : String(logErr),
541
- });
542
- }
543
-
544
- // Generate a title for new conversations using a cheap model call
545
- if (!history.title && fullResponse) {
546
- const perRequestApiKey = req.headers["x-ai-api-key"] as string | undefined;
547
- const title = await generateTitle(
548
- prompt,
549
- fullResponse,
550
- aiService,
551
- options,
552
- perRequestApiKey
553
- );
554
- if (title) {
555
- history.title = title;
556
- await history.save();
254
+ for (const part of currentReply?.content ?? []) {
255
+ if (part.type === "image") {
256
+ res.write(sseEvent({image: {mimeType: part.mimeType, url: part.url}}));
557
257
  }
558
258
  }
559
-
560
- logger.debug("Sending done event", {
561
- fullResponseLength: fullResponse.length,
562
- historyId: history._id.toString(),
563
- });
564
- res.write(
565
- `data: ${JSON.stringify({
566
- done: true,
567
- historyId: history._id.toString(),
568
- ...(history.title ? {title: history.title} : {}),
569
- })}\n\n`
570
- );
571
- res.end();
572
- } catch (error) {
573
- logger.error("Error in GPT stream", {
574
- error: error instanceof Error ? error.message : String(error),
575
- });
576
-
577
- try {
578
- await AIRequest.logRequest({
579
- aiModel: modelId ?? "unknown",
580
- error: error instanceof Error ? error.message : String(error),
581
- prompt,
582
- requestType: "general",
583
- responseTime: DateTime.now().toMillis() - startTime,
584
- userId: userId ?? undefined,
585
- });
586
- } catch (logErr) {
587
- logger.warn("Failed to log AIRequest error", {
588
- error: logErr instanceof Error ? logErr.message : String(logErr),
589
- });
259
+ if (currentReply?.status === "error") {
260
+ res.write(sseEvent({error: "The reply was interrupted before it finished"}));
590
261
  }
591
-
592
262
  res.write(
593
- `data: ${JSON.stringify({error: error instanceof Error ? error.message : "Unknown error"})}\n\n`
263
+ sseEvent({done: true, historyId: id, ...(current.title ? {title: current.title} : {})})
594
264
  );
595
- res.end();
596
- }
597
- } catch (outerError) {
598
- // Catch-all for errors thrown before or after SSE streaming
599
- const message = outerError instanceof Error ? outerError.message : String(outerError);
600
- const status = outerError instanceof APIError ? outerError.status : 500;
601
- const stack = outerError instanceof Error ? outerError.stack : undefined;
602
- logger.error("GPT prompt handler error", {error: message, stack, status});
603
-
604
- if (sseStarted) {
605
- // Already sent SSE headers — send error as SSE event
606
- res.write(`data: ${JSON.stringify({error: message})}\n\n`);
607
- res.end();
608
- } else {
609
- // Haven't started SSE — send a normal JSON error response
610
- res.status(status).json({
611
- detail: message,
612
- status,
613
- title: outerError instanceof APIError ? outerError.title : "Internal server error",
614
- });
265
+ break;
615
266
  }
267
+ } catch (error) {
268
+ logger.error("Error resuming GPT stream", {
269
+ error: error instanceof Error ? error.message : String(error),
270
+ });
271
+ res.write(sseEvent({error: error instanceof Error ? error.message : "Unknown error"}));
616
272
  }
617
- }
273
+ res.end();
274
+ })
618
275
  );
619
276
 
620
277
  router.patch(
@@ -637,7 +294,7 @@ export const addGptRoutes = (router: express.Router, options: GptRouteOptions):
637
294
  const {promptIndex, rating} = req.body;
638
295
  const userId = (req.user as {_id?: mongoose.Types.ObjectId} | undefined)?._id;
639
296
 
640
- if (typeof promptIndex !== "number" || promptIndex < 0) {
297
+ if (typeof promptIndex !== "number" || !Number.isInteger(promptIndex) || promptIndex < 0) {
641
298
  throw new APIError({status: 400, title: "promptIndex must be a non-negative number"});
642
299
  }
643
300
  if (rating !== null && rating !== "up" && rating !== "down") {
@@ -655,15 +312,14 @@ export const addGptRoutes = (router: express.Router, options: GptRouteOptions):
655
312
  throw new APIError({status: 400, title: "promptIndex out of range"});
656
313
  }
657
314
 
658
- if (rating === null) {
659
- history.prompts[promptIndex].rating = undefined;
660
- } else {
661
- history.prompts[promptIndex].rating = rating;
662
- }
663
- history.markModified("prompts");
664
- await history.save();
315
+ // One field update, so rows a running turn appends meanwhile are kept.
316
+ const ratingPath = `prompts.${promptIndex}.rating`;
317
+ await GptHistory.updateOne(
318
+ {_id: history._id},
319
+ rating === null ? {$unset: {[ratingPath]: ""}} : {$set: {[ratingPath]: rating}}
320
+ );
665
321
 
666
- return res.json({data: {promptIndex, rating: history.prompts[promptIndex].rating ?? null}});
322
+ return res.json({data: {promptIndex, rating}});
667
323
  })
668
324
  );
669
325
 
@@ -683,10 +339,16 @@ export const addGptRoutes = (router: express.Router, options: GptRouteOptions):
683
339
  asyncHandler(async (req: express.Request, res: express.Response) => {
684
340
  const {text} = req.body;
685
341
  const userId = (req.user as {_id?: mongoose.Types.ObjectId} | undefined)?._id;
342
+ const hasPromptReference =
343
+ typeof req.body.promptName === "string" || typeof req.body.promptLabel === "string";
344
+ const isAdmin = (req.user as {admin?: boolean} | undefined)?.admin === true;
686
345
 
687
346
  if (!text || typeof text !== "string") {
688
347
  throw new APIError({status: 400, title: "text is required"});
689
348
  }
349
+ if (hasPromptReference && !isAdmin) {
350
+ throw new APIError({status: 403, title: "Prompt registry selection requires admin access"});
351
+ }
690
352
 
691
353
  const aiService = resolveAiService(req, options);
692
354
  if (!aiService) {