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