@terreno/ai 57.9.0 → 57.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (417) hide show
  1. package/README.md +1 -0
  2. package/bunfig.toml +1 -1
  3. package/dist/aiApp.d.ts +8 -2
  4. package/dist/aiApp.js +12 -6
  5. package/dist/harness/adminScreens.d.ts +6 -0
  6. package/dist/harness/adminScreens.js +11 -0
  7. package/dist/harness/agentLoop.d.ts +61 -0
  8. package/dist/harness/agentLoop.js +798 -0
  9. package/dist/harness/agentTaskNames.d.ts +4 -0
  10. package/dist/harness/agentTaskNames.js +7 -0
  11. package/dist/harness/approvalPolicy.d.ts +3 -0
  12. package/dist/harness/approvalPolicy.js +23 -0
  13. package/dist/harness/approvals.d.ts +101 -0
  14. package/dist/harness/approvals.js +468 -0
  15. package/dist/harness/commit.d.ts +238 -0
  16. package/dist/harness/commit.js +678 -0
  17. package/dist/harness/conversation.d.ts +75 -0
  18. package/dist/harness/conversation.js +397 -0
  19. package/dist/harness/defineAgent.d.ts +9 -0
  20. package/dist/harness/defineAgent.js +65 -0
  21. package/dist/harness/defineTask.d.ts +11 -0
  22. package/dist/harness/defineTask.js +59 -0
  23. package/dist/harness/defineTool.d.ts +7 -0
  24. package/dist/harness/defineTool.js +36 -0
  25. package/dist/harness/definitionError.d.ts +8 -0
  26. package/dist/harness/definitionError.js +15 -0
  27. package/dist/harness/errors.d.ts +133 -0
  28. package/dist/harness/errors.js +107 -0
  29. package/dist/harness/eventHub.d.ts +45 -0
  30. package/dist/harness/eventHub.js +135 -0
  31. package/dist/harness/events.d.ts +95 -0
  32. package/dist/harness/events.js +267 -0
  33. package/dist/harness/executionEnv.d.ts +31 -0
  34. package/dist/harness/executionEnv.js +2 -0
  35. package/dist/harness/extensions.d.ts +82 -0
  36. package/dist/harness/extensions.js +274 -0
  37. package/dist/harness/harness.d.ts +163 -0
  38. package/dist/harness/harness.js +522 -0
  39. package/dist/harness/harnessApp.d.ts +44 -0
  40. package/dist/harness/harnessApp.js +62 -0
  41. package/dist/harness/internalRuntime.d.ts +16 -0
  42. package/dist/harness/internalRuntime.js +18 -0
  43. package/dist/harness/leases.d.ts +35 -0
  44. package/dist/harness/leases.js +86 -0
  45. package/dist/harness/memo.d.ts +24 -0
  46. package/dist/harness/memo.js +78 -0
  47. package/dist/harness/modelCall.d.ts +45 -0
  48. package/dist/harness/modelCall.js +147 -0
  49. package/dist/harness/models/harnessApproval.d.ts +2 -0
  50. package/dist/harness/models/harnessApproval.js +100 -0
  51. package/dist/harness/models/harnessConversation.d.ts +2 -0
  52. package/dist/harness/models/harnessConversation.js +117 -0
  53. package/dist/harness/models/harnessEvent.d.ts +3 -0
  54. package/dist/harness/models/harnessEvent.js +73 -0
  55. package/dist/harness/models/harnessInboxEvent.d.ts +2 -0
  56. package/dist/harness/models/harnessInboxEvent.js +58 -0
  57. package/dist/harness/models/harnessMemo.d.ts +2 -0
  58. package/dist/harness/models/harnessMemo.js +42 -0
  59. package/dist/harness/models/harnessMessage.d.ts +2 -0
  60. package/dist/harness/models/harnessMessage.js +73 -0
  61. package/dist/harness/models/harnessOwner.d.ts +2 -0
  62. package/dist/harness/models/harnessOwner.js +36 -0
  63. package/dist/harness/models/harnessTask.d.ts +2 -0
  64. package/dist/harness/models/harnessTask.js +191 -0
  65. package/dist/harness/ownership.d.ts +89 -0
  66. package/dist/harness/ownership.js +438 -0
  67. package/dist/harness/registry.d.ts +29 -0
  68. package/dist/harness/registry.js +93 -0
  69. package/dist/harness/retryBackoff.d.ts +31 -0
  70. package/dist/harness/retryBackoff.js +50 -0
  71. package/dist/harness/routes/approvals.d.ts +13 -0
  72. package/dist/harness/routes/approvals.js +93 -0
  73. package/dist/harness/routes/conversations.d.ts +11 -0
  74. package/dist/harness/routes/conversations.js +66 -0
  75. package/dist/harness/routes/events.d.ts +19 -0
  76. package/dist/harness/routes/events.js +215 -0
  77. package/dist/harness/routes/tasks.d.ts +11 -0
  78. package/dist/harness/routes/tasks.js +108 -0
  79. package/dist/harness/runners/inProcessRunner.d.ts +64 -0
  80. package/dist/harness/runners/inProcessRunner.js +227 -0
  81. package/dist/harness/runners/jobsRunner.d.ts +62 -0
  82. package/dist/harness/runners/jobsRunner.js +212 -0
  83. package/dist/harness/runtime.d.ts +52 -0
  84. package/dist/harness/runtime.js +604 -0
  85. package/dist/harness/subagent.d.ts +53 -0
  86. package/dist/harness/subagent.js +134 -0
  87. package/dist/harness/suspend.d.ts +8 -0
  88. package/dist/harness/suspend.js +16 -0
  89. package/dist/harness/transaction.d.ts +3 -0
  90. package/dist/harness/transaction.js +22 -0
  91. package/dist/harness/waits.d.ts +100 -0
  92. package/dist/harness/waits.js +214 -0
  93. package/dist/index.d.ts +19 -1
  94. package/dist/index.js +45 -2
  95. package/dist/models/aiDataset.d.ts +2 -0
  96. package/dist/models/aiDataset.js +67 -0
  97. package/dist/models/aiRequest.js +1 -1
  98. package/dist/models/gptHistory.js +73 -0
  99. package/dist/observability/adminScreens.d.ts +5 -0
  100. package/dist/observability/adminScreens.js +32 -0
  101. package/dist/observability/compileTemplate.d.ts +1 -0
  102. package/dist/observability/compileTemplate.js +13 -0
  103. package/dist/observability/datasetImport.d.ts +11 -0
  104. package/dist/observability/datasetImport.js +185 -0
  105. package/dist/observability/evaluate.d.ts +31 -0
  106. package/dist/observability/evaluate.js +200 -0
  107. package/dist/observability/evaluatorTemplates.d.ts +17 -0
  108. package/dist/observability/evaluatorTemplates.js +111 -0
  109. package/dist/observability/jsonPath.d.ts +2 -0
  110. package/dist/observability/jsonPath.js +57 -0
  111. package/dist/observability/local/datasetStore.d.ts +87 -0
  112. package/dist/observability/local/datasetStore.js +326 -0
  113. package/dist/observability/local/evaluatorStore.d.ts +44 -0
  114. package/dist/observability/local/evaluatorStore.js +182 -0
  115. package/dist/observability/local/experimentRunner.d.ts +96 -0
  116. package/dist/observability/local/experimentRunner.js +419 -0
  117. package/dist/observability/local/localPlugin.d.ts +22 -0
  118. package/dist/observability/local/localPlugin.js +76 -0
  119. package/dist/observability/local/models/obsDataset.d.ts +2 -0
  120. package/dist/observability/local/models/obsDataset.js +37 -0
  121. package/dist/observability/local/models/obsDatasetItem.d.ts +2 -0
  122. package/dist/observability/local/models/obsDatasetItem.js +76 -0
  123. package/dist/observability/local/models/obsEvaluator.d.ts +2 -0
  124. package/dist/observability/local/models/obsEvaluator.js +98 -0
  125. package/dist/observability/local/models/obsExperiment.d.ts +2 -0
  126. package/dist/observability/local/models/obsExperiment.js +87 -0
  127. package/dist/observability/local/models/obsExperimentItem.d.ts +2 -0
  128. package/dist/observability/local/models/obsExperimentItem.js +45 -0
  129. package/dist/observability/local/models/obsPrompt.d.ts +2 -0
  130. package/dist/observability/local/models/obsPrompt.js +42 -0
  131. package/dist/observability/local/models/obsPromptLabel.d.ts +2 -0
  132. package/dist/observability/local/models/obsPromptLabel.js +39 -0
  133. package/dist/observability/local/models/obsPromptVersion.d.ts +2 -0
  134. package/dist/observability/local/models/obsPromptVersion.js +96 -0
  135. package/dist/observability/local/models/obsReviewItem.d.ts +2 -0
  136. package/dist/observability/local/models/obsReviewItem.js +75 -0
  137. package/dist/observability/local/models/obsScore.d.ts +2 -0
  138. package/dist/observability/local/models/obsScore.js +58 -0
  139. package/dist/observability/local/models/obsSpan.d.ts +2 -0
  140. package/dist/observability/local/models/obsSpan.js +64 -0
  141. package/dist/observability/local/models/obsTrace.d.ts +2 -0
  142. package/dist/observability/local/models/obsTrace.js +93 -0
  143. package/dist/observability/local/promptStore.d.ts +180 -0
  144. package/dist/observability/local/promptStore.js +444 -0
  145. package/dist/observability/local/reviewStore.d.ts +50 -0
  146. package/dist/observability/local/reviewStore.js +203 -0
  147. package/dist/observability/local/traceStore.d.ts +103 -0
  148. package/dist/observability/local/traceStore.js +325 -0
  149. package/dist/observability/observabilityApp.d.ts +24 -0
  150. package/dist/observability/observabilityApp.js +157 -0
  151. package/dist/observability/observabilityAppRegistry.d.ts +14 -0
  152. package/dist/observability/observabilityAppRegistry.js +16 -0
  153. package/dist/observability/permissions.d.ts +7 -0
  154. package/dist/observability/permissions.js +60 -0
  155. package/dist/observability/reviewPanels.d.ts +18 -0
  156. package/dist/observability/reviewPanels.js +46 -0
  157. package/dist/observability/routes/datasets.d.ts +7 -0
  158. package/dist/observability/routes/datasets.js +149 -0
  159. package/dist/observability/routes/evaluators.d.ts +7 -0
  160. package/dist/observability/routes/evaluators.js +123 -0
  161. package/dist/observability/routes/experiments.d.ts +7 -0
  162. package/dist/observability/routes/experiments.js +95 -0
  163. package/dist/observability/routes/observabilityRouteAccess.d.ts +13 -0
  164. package/dist/observability/routes/observabilityRouteAccess.js +67 -0
  165. package/dist/observability/routes/prompts.d.ts +12 -0
  166. package/dist/observability/routes/prompts.js +172 -0
  167. package/dist/observability/routes/requestAiService.d.ts +13 -0
  168. package/dist/observability/routes/requestAiService.js +21 -0
  169. package/dist/observability/routes/review.d.ts +7 -0
  170. package/dist/observability/routes/review.js +79 -0
  171. package/dist/observability/routes/status.d.ts +5 -0
  172. package/dist/observability/routes/status.js +31 -0
  173. package/dist/observability/routes/testMultiStage.d.ts +9 -0
  174. package/dist/observability/routes/testMultiStage.js +67 -0
  175. package/dist/observability/routes/traces.d.ts +7 -0
  176. package/dist/observability/routes/traces.js +129 -0
  177. package/dist/observability/schemaValidation.d.ts +5 -0
  178. package/dist/observability/schemaValidation.js +26 -0
  179. package/dist/observability/sopThresholds.d.ts +2 -0
  180. package/dist/observability/sopThresholds.js +21 -0
  181. package/dist/observability/status.d.ts +23 -0
  182. package/dist/observability/status.js +34 -0
  183. package/dist/observability/testMultiStageSchemas.d.ts +10 -0
  184. package/dist/observability/testMultiStageSchemas.js +79 -0
  185. package/dist/observability/testMultiStageWorkflow.d.ts +20 -0
  186. package/dist/observability/testMultiStageWorkflow.js +218 -0
  187. package/dist/observability/types.d.ts +159 -0
  188. package/dist/observability/types.js +67 -0
  189. package/dist/routes/files.js +18 -7
  190. package/dist/routes/gpt.js +79 -689
  191. package/dist/routes/gptActions.d.ts +9 -0
  192. package/dist/routes/gptActions.js +212 -0
  193. package/dist/routes/gptDatasets.d.ts +5 -0
  194. package/dist/routes/gptDatasets.js +94 -0
  195. package/dist/routes/gptHistories.d.ts +3 -1
  196. package/dist/routes/gptHistories.js +188 -2
  197. package/dist/service/aiDatasets.d.ts +57 -0
  198. package/dist/service/aiDatasets.js +253 -0
  199. package/dist/service/aiService.d.ts +50 -1
  200. package/dist/service/aiService.js +426 -77
  201. package/dist/service/askFiles.d.ts +49 -0
  202. package/dist/service/askFiles.js +162 -0
  203. package/dist/service/asks.d.ts +76 -0
  204. package/dist/service/asks.js +284 -0
  205. package/dist/service/chatTurn.d.ts +128 -0
  206. package/dist/service/chatTurn.js +1674 -0
  207. package/dist/service/fileStorage.d.ts +4 -0
  208. package/dist/service/fileStorage.js +7 -2
  209. package/dist/service/fillActionIds.d.ts +6 -0
  210. package/dist/service/fillActionIds.js +79 -0
  211. package/dist/service/jsonFenceModel.d.ts +7 -0
  212. package/dist/service/jsonFenceModel.js +45 -0
  213. package/dist/service/prompts.d.ts +52 -0
  214. package/dist/service/prompts.js +96 -1
  215. package/dist/service/sanitizeHtml.d.ts +11 -0
  216. package/dist/service/sanitizeHtml.js +108 -0
  217. package/dist/types/harness.d.ts +952 -0
  218. package/dist/types/harness.js +133 -0
  219. package/dist/types/index.d.ts +192 -14
  220. package/dist/types/index.js +2 -0
  221. package/dist/types/observability.d.ts +288 -0
  222. package/dist/types/observability.js +2 -0
  223. package/package.json +28 -5
  224. package/src/aiApp.test.ts +207 -1
  225. package/src/aiApp.ts +19 -6
  226. package/src/harness/adminScreens.test.ts +34 -0
  227. package/src/harness/adminScreens.ts +11 -0
  228. package/src/harness/agentLoop.test.ts +1034 -0
  229. package/src/harness/agentLoop.ts +1082 -0
  230. package/src/harness/agentRecovery.test.ts +380 -0
  231. package/src/harness/agentTaskNames.ts +4 -0
  232. package/src/harness/approvalPolicy.ts +28 -0
  233. package/src/harness/approvals.test.ts +1094 -0
  234. package/src/harness/approvals.ts +644 -0
  235. package/src/harness/commit.ts +1049 -0
  236. package/src/harness/concurrency.test.ts +155 -0
  237. package/src/harness/conversation.ts +503 -0
  238. package/src/harness/defineAgent.test.ts +92 -0
  239. package/src/harness/defineAgent.ts +82 -0
  240. package/src/harness/defineTask.test.ts +59 -0
  241. package/src/harness/defineTask.ts +76 -0
  242. package/src/harness/defineTool.ts +45 -0
  243. package/src/harness/definitionError.ts +13 -0
  244. package/src/harness/errors.test.ts +90 -0
  245. package/src/harness/errors.ts +125 -0
  246. package/src/harness/eventHub.test.ts +124 -0
  247. package/src/harness/eventHub.ts +159 -0
  248. package/src/harness/events.test.ts +769 -0
  249. package/src/harness/events.ts +384 -0
  250. package/src/harness/executionEnv.ts +34 -0
  251. package/src/harness/extensions.test.ts +1218 -0
  252. package/src/harness/extensions.ts +382 -0
  253. package/src/harness/harness.test.ts +693 -0
  254. package/src/harness/harness.ts +778 -0
  255. package/src/harness/harnessApp.ts +76 -0
  256. package/src/harness/internalRuntime.ts +44 -0
  257. package/src/harness/leases.ts +132 -0
  258. package/src/harness/memo.ts +107 -0
  259. package/src/harness/modelCall.test.ts +134 -0
  260. package/src/harness/modelCall.ts +200 -0
  261. package/src/harness/models/harnessApproval.ts +109 -0
  262. package/src/harness/models/harnessConversation.ts +142 -0
  263. package/src/harness/models/harnessEvent.ts +92 -0
  264. package/src/harness/models/harnessInboxEvent.ts +73 -0
  265. package/src/harness/models/harnessMemo.ts +43 -0
  266. package/src/harness/models/harnessMessage.ts +85 -0
  267. package/src/harness/models/harnessModels.test.ts +60 -0
  268. package/src/harness/models/harnessOwner.ts +40 -0
  269. package/src/harness/models/harnessTask.ts +206 -0
  270. package/src/harness/ownership.test.ts +1087 -0
  271. package/src/harness/ownership.ts +618 -0
  272. package/src/harness/recovery.test.ts +907 -0
  273. package/src/harness/registry.ts +124 -0
  274. package/src/harness/retries.test.ts +292 -0
  275. package/src/harness/retryBackoff.test.ts +100 -0
  276. package/src/harness/retryBackoff.ts +77 -0
  277. package/src/harness/routes/approvals.ts +123 -0
  278. package/src/harness/routes/conversations.test.ts +518 -0
  279. package/src/harness/routes/conversations.ts +102 -0
  280. package/src/harness/routes/events.ts +280 -0
  281. package/src/harness/routes/tasks.test.ts +198 -0
  282. package/src/harness/routes/tasks.ts +146 -0
  283. package/src/harness/runners/inProcessRunner.test.ts +530 -0
  284. package/src/harness/runners/inProcessRunner.ts +260 -0
  285. package/src/harness/runners/jobsRunner.test.ts +709 -0
  286. package/src/harness/runners/jobsRunner.ts +252 -0
  287. package/src/harness/runtime.ts +802 -0
  288. package/src/harness/subagent.test.ts +696 -0
  289. package/src/harness/subagent.ts +192 -0
  290. package/src/harness/suspend.ts +13 -0
  291. package/src/harness/transaction.ts +17 -0
  292. package/src/harness/versioning.test.ts +288 -0
  293. package/src/harness/waits.test.ts +693 -0
  294. package/src/harness/waits.ts +356 -0
  295. package/src/index.test.ts +13 -0
  296. package/src/index.ts +70 -0
  297. package/src/isolated/adminScreensConfig.isolated.ts +143 -0
  298. package/src/models/aiDataset.test.ts +34 -0
  299. package/src/models/aiDataset.ts +78 -0
  300. package/src/models/aiRequest.ts +2 -1
  301. package/src/models/gptHistory.test.ts +156 -0
  302. package/src/models/gptHistory.ts +89 -1
  303. package/src/observability/adminScreens.test.ts +71 -0
  304. package/src/observability/adminScreens.ts +42 -0
  305. package/src/observability/compileTemplate.test.ts +14 -0
  306. package/src/observability/compileTemplate.ts +10 -0
  307. package/src/observability/datasetImport.test.ts +95 -0
  308. package/src/observability/datasetImport.ts +220 -0
  309. package/src/observability/evaluate.test.ts +549 -0
  310. package/src/observability/evaluate.ts +254 -0
  311. package/src/observability/evaluatorTemplates.ts +124 -0
  312. package/src/observability/jsonPath.test.ts +52 -0
  313. package/src/observability/jsonPath.ts +60 -0
  314. package/src/observability/local/datasetStore.test.ts +230 -0
  315. package/src/observability/local/datasetStore.ts +452 -0
  316. package/src/observability/local/evaluatorStore.test.ts +138 -0
  317. package/src/observability/local/evaluatorStore.ts +224 -0
  318. package/src/observability/local/experimentRunner.test.ts +489 -0
  319. package/src/observability/local/experimentRunner.ts +548 -0
  320. package/src/observability/local/localPlugin.ts +88 -0
  321. package/src/observability/local/models/obsDataset.ts +38 -0
  322. package/src/observability/local/models/obsDatasetItem.ts +80 -0
  323. package/src/observability/local/models/obsEvaluator.ts +102 -0
  324. package/src/observability/local/models/obsExperiment.ts +91 -0
  325. package/src/observability/local/models/obsExperimentItem.ts +52 -0
  326. package/src/observability/local/models/obsPrompt.ts +43 -0
  327. package/src/observability/local/models/obsPromptLabel.ts +43 -0
  328. package/src/observability/local/models/obsPromptVersion.ts +107 -0
  329. package/src/observability/local/models/obsReviewItem.ts +79 -0
  330. package/src/observability/local/models/obsScore.ts +59 -0
  331. package/src/observability/local/models/obsSpan.ts +65 -0
  332. package/src/observability/local/models/obsTrace.ts +94 -0
  333. package/src/observability/local/obsModels.test.ts +119 -0
  334. package/src/observability/local/promptStore.test.ts +351 -0
  335. package/src/observability/local/promptStore.ts +665 -0
  336. package/src/observability/local/reviewStore.test.ts +164 -0
  337. package/src/observability/local/reviewStore.ts +264 -0
  338. package/src/observability/local/traceStore.test.ts +264 -0
  339. package/src/observability/local/traceStore.ts +440 -0
  340. package/src/observability/observabilityApp.test.ts +92 -0
  341. package/src/observability/observabilityApp.ts +185 -0
  342. package/src/observability/observabilityAppRegistry.test.ts +30 -0
  343. package/src/observability/observabilityAppRegistry.ts +35 -0
  344. package/src/observability/permissions.test.ts +96 -0
  345. package/src/observability/permissions.ts +78 -0
  346. package/src/observability/reviewPanels.test.ts +35 -0
  347. package/src/observability/reviewPanels.ts +64 -0
  348. package/src/observability/routes/datasets.test.ts +176 -0
  349. package/src/observability/routes/datasets.ts +252 -0
  350. package/src/observability/routes/evaluators.test.ts +119 -0
  351. package/src/observability/routes/evaluators.ts +207 -0
  352. package/src/observability/routes/experiments.test.ts +282 -0
  353. package/src/observability/routes/experiments.ts +152 -0
  354. package/src/observability/routes/observabilityRbac.test.ts +767 -0
  355. package/src/observability/routes/observabilityRouteAccess.ts +106 -0
  356. package/src/observability/routes/prompts.test.ts +320 -0
  357. package/src/observability/routes/prompts.ts +263 -0
  358. package/src/observability/routes/requestAiService.ts +36 -0
  359. package/src/observability/routes/review.test.ts +120 -0
  360. package/src/observability/routes/review.ts +125 -0
  361. package/src/observability/routes/status.test.ts +116 -0
  362. package/src/observability/routes/status.ts +47 -0
  363. package/src/observability/routes/testMultiStage.test.ts +214 -0
  364. package/src/observability/routes/testMultiStage.ts +96 -0
  365. package/src/observability/routes/traces.test.ts +167 -0
  366. package/src/observability/routes/traces.ts +172 -0
  367. package/src/observability/schemaValidation.ts +29 -0
  368. package/src/observability/sopThresholds.ts +20 -0
  369. package/src/observability/status.test.ts +38 -0
  370. package/src/observability/status.ts +56 -0
  371. package/src/observability/testMultiStageSchemas.ts +95 -0
  372. package/src/observability/testMultiStageWorkflow.test.ts +114 -0
  373. package/src/observability/testMultiStageWorkflow.ts +307 -0
  374. package/src/observability/types.ts +262 -0
  375. package/src/routes/files.test.ts +63 -9
  376. package/src/routes/files.ts +21 -7
  377. package/src/routes/gpt.test.ts +3279 -0
  378. package/src/routes/gpt.ts +91 -816
  379. package/src/routes/gptActions.test.ts +241 -0
  380. package/src/routes/gptActions.ts +255 -0
  381. package/src/routes/gptApprovals.test.ts +762 -0
  382. package/src/routes/gptDatasets.test.ts +86 -0
  383. package/src/routes/gptDatasets.ts +104 -0
  384. package/src/routes/gptFiles.test.ts +589 -0
  385. package/src/routes/gptHistories.test.ts +933 -0
  386. package/src/routes/gptHistories.ts +266 -5
  387. package/src/routes/routes.test.ts +222 -2
  388. package/src/service/aiDatasets.test.ts +123 -0
  389. package/src/service/aiDatasets.ts +316 -0
  390. package/src/service/aiService.test.ts +593 -0
  391. package/src/service/aiService.ts +568 -111
  392. package/src/service/askFiles.test.ts +96 -0
  393. package/src/service/askFiles.ts +222 -0
  394. package/src/service/asks.test.ts +946 -0
  395. package/src/service/asks.ts +423 -0
  396. package/src/service/chatTurn.ts +2287 -0
  397. package/src/service/fileStorage.test.ts +11 -0
  398. package/src/service/fileStorage.ts +10 -2
  399. package/src/service/fillActionIds.test.ts +65 -0
  400. package/src/service/fillActionIds.ts +98 -0
  401. package/src/service/jsonFenceModel.test.ts +47 -0
  402. package/src/service/jsonFenceModel.ts +53 -0
  403. package/src/service/prompts.test.ts +21 -0
  404. package/src/service/prompts.ts +147 -0
  405. package/src/service/sanitizeHtml.test.ts +61 -0
  406. package/src/service/sanitizeHtml.ts +109 -0
  407. package/src/tests/bunSetup.test.ts +7 -4
  408. package/src/tests/bunSetup.ts +7 -7
  409. package/src/tests/chatHarness.ts +478 -0
  410. package/src/tests/generateStream.ts +49 -0
  411. package/src/tests/harnessErrors.ts +22 -0
  412. package/src/tests/harnessStreaming.ts +227 -0
  413. package/src/tests/helpers.ts +9 -2
  414. package/src/types/harness.ts +1119 -0
  415. package/src/types/index.ts +209 -13
  416. package/src/types/observability.ts +344 -0
  417. package/src/types/sanitize-html.d.ts +25 -0
package/src/routes/gpt.ts CHANGED
@@ -1,54 +1,49 @@
1
- import {randomUUID} from "node:crypto";
2
1
  import {
3
2
  APIError,
4
3
  asyncHandler,
5
4
  authenticateMiddleware,
6
5
  createOpenApiBuilder,
6
+ isAPIError,
7
7
  logger,
8
+ type OpenApiSchemaProperty,
8
9
  } from "@terreno/api";
9
- import type {Tool} from "ai";
10
- import {stepCountIs, streamText} from "ai";
10
+ import {ASK_SURFACES, askSurfaceSchema} from "@terreno/blocks";
11
11
  import type express from "express";
12
12
  import {DateTime} from "luxon";
13
13
  import type mongoose from "mongoose";
14
- import {createTelemetryConfig, preparePromptForAI} from "../langfuseVercelAi";
15
14
 
16
- import {AIRequest} from "../models/aiRequest";
17
15
  import {GptHistory} from "../models/gptHistory";
18
- import {Project} from "../models/project";
19
- import {AIService} from "../service/aiService";
20
- import type {FileStorageService} from "../service/fileStorage";
21
- import {assertFileUploadsEnabled} from "../service/fileUploadsGate";
22
- import {TITLE_GENERATION_PROMPT} from "../service/prompts";
23
- import type {
24
- GptHistoryDocument,
25
- GptHistoryPrompt,
26
- GptRouteOptions,
27
- MessageContentPart,
28
- } from "../types";
29
-
30
- const DEFAULT_STREAM_PERSIST_INTERVAL_MS = 1000;
31
- const DEFAULT_STREAM_RESUME_POLL_INTERVAL_MS = 500;
32
- const DEFAULT_STREAM_STALE_AFTER_MS = 60_000;
33
- // Bump `updated` even without new text so long tool calls do not look stale to resumers
34
- const STREAM_HEARTBEAT_MS = 10_000;
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",
33
+ };
35
34
 
36
- const DURABLE_URL_PATTERN = /^(https?:|data:)/i;
37
- const DATA_URL_PATTERN = /^data:([^;,]*)((?:;[^;,]*)*?)(;base64)?,(.*)$/s;
35
+ const SURFACE_BODY: OpenApiSchemaProperty = {
36
+ description: askSurfaceSchema.description,
37
+ enum: [...ASK_SURFACES],
38
+ type: "string",
39
+ };
38
40
 
39
- interface PromptAttachment {
40
- filename?: string;
41
- mimeType?: string;
42
- type?: string;
43
- url?: string;
41
+ interface SseSink extends ChatTurnSink {
42
+ isOpen: () => boolean;
44
43
  }
45
44
 
46
- interface AttachmentContentParts {
47
- /** Parts sent to the model for this request (original URLs, including data URLs). */
48
- modelParts: MessageContentPart[];
49
- /** Parts persisted to history (durable storage references when storage is configured). */
50
- storedParts: MessageContentPart[];
51
- }
45
+ const DEFAULT_STREAM_RESUME_POLL_INTERVAL_MS = 500;
46
+ const DEFAULT_STREAM_STALE_AFTER_MS = 60_000;
52
47
 
53
48
  const sseEvent = (payload: Record<string, unknown>): string =>
54
49
  `data: ${JSON.stringify(payload)}\n\n`;
@@ -58,239 +53,38 @@ const sleep = (ms: number): Promise<void> =>
58
53
  setTimeout(resolve, ms);
59
54
  });
60
55
 
61
- /**
62
- * Reject attachment URLs that only exist on the client (blob:, file:, content:, ph:, ...).
63
- * They cannot be read by the model provider or reloaded from history later.
64
- */
65
- const validateAttachments = (attachments: unknown): PromptAttachment[] => {
66
- if (attachments === undefined || attachments === null) {
67
- return [];
68
- }
69
- if (!Array.isArray(attachments)) {
70
- throw new APIError({status: 400, title: "attachments must be an array"});
71
- }
72
- for (const attachment of attachments as PromptAttachment[]) {
73
- if (attachment?.type !== "image" && attachment?.type !== "file") {
74
- continue;
75
- }
76
- if (typeof attachment.url !== "string" || !DURABLE_URL_PATTERN.test(attachment.url)) {
77
- const scheme =
78
- typeof attachment.url === "string" ? (attachment.url.split(":")[0] ?? "") : "missing";
79
- throw new APIError({
80
- detail:
81
- `Attachment "${attachment.filename ?? attachment.type}" uses a "${scheme}" URL. ` +
82
- "Upload the file first (POST /files/upload) or send it as a data: URL.",
83
- status: 400,
84
- title: "Attachment URL must be an http(s) or data: URL",
85
- });
86
- }
87
- }
88
- return attachments as PromptAttachment[];
89
- };
90
-
91
- const decodeDataUrl = (url: string): {buffer: Buffer; mimeType?: string} | undefined => {
92
- const match = DATA_URL_PATTERN.exec(url);
93
- if (!match) {
94
- return undefined;
95
- }
96
- const [, mimeType, , base64Flag, payload] = match;
97
- const buffer = base64Flag
98
- ? Buffer.from(payload, "base64")
99
- : Buffer.from(decodeURIComponent(payload), "utf8");
100
- return {buffer, mimeType: mimeType || undefined};
101
- };
102
-
103
- /** Build model and history content parts, uploading data: attachments when storage is configured. */
104
- const buildAttachmentParts = async ({
105
- attachments,
106
- fileStorageService,
107
- userId,
108
- }: {
109
- attachments: PromptAttachment[];
110
- fileStorageService?: FileStorageService;
111
- userId?: mongoose.Types.ObjectId;
112
- }): Promise<AttachmentContentParts> => {
113
- const modelParts: MessageContentPart[] = [];
114
- const storedParts: MessageContentPart[] = [];
115
- for (const attachment of attachments) {
116
- if (attachment.type !== "image" && attachment.type !== "file") {
117
- continue;
118
- }
119
- const url = attachment.url as string;
120
- const modelPart: MessageContentPart =
121
- attachment.type === "image"
122
- ? {mimeType: attachment.mimeType, type: "image", url}
123
- : {
124
- filename: attachment.filename,
125
- mimeType: attachment.mimeType as string,
126
- type: "file",
127
- url,
128
- };
129
- modelParts.push(modelPart);
130
-
131
- const decoded = url.startsWith("data:") ? decodeDataUrl(url) : undefined;
132
- if (!fileStorageService || !userId || !decoded) {
133
- storedParts.push(modelPart);
134
- continue;
135
- }
136
- const mimeType = attachment.mimeType ?? decoded.mimeType ?? "application/octet-stream";
137
- const extension = mimeType.split("/")[1]?.split("+")[0] ?? "bin";
138
- try {
139
- const uploaded = await fileStorageService.upload({
140
- buffer: decoded.buffer,
141
- filename: attachment.filename ?? `attachment-${DateTime.now().toMillis()}.${extension}`,
142
- mimeType,
143
- userId,
144
- });
145
- storedParts.push({...modelPart, gcsKey: uploaded.gcsKey, url: uploaded.url});
146
- } catch (error) {
147
- throw new APIError({
148
- cause: error,
149
- detail: error instanceof Error ? error.message : String(error),
150
- status: 502,
151
- title: "Attachment upload failed",
152
- });
153
- }
154
- }
155
- return {modelParts, storedParts};
156
- };
157
-
158
- const toPlainPrompt = (prompt: GptHistoryPrompt): GptHistoryPrompt => {
159
- const maybeSubdoc = prompt as GptHistoryPrompt & {toObject?: () => GptHistoryPrompt};
160
- return typeof maybeSubdoc.toObject === "function" ? maybeSubdoc.toObject() : prompt;
161
- };
162
-
163
- /** Swap stored attachment references for short-lived signed URLs the model provider can fetch. */
164
- const resolveStoredAttachmentUrls = async (
165
- prompts: GptHistoryPrompt[],
166
- fileStorageService?: FileStorageService
167
- ): Promise<GptHistoryPrompt[]> => {
168
- if (!fileStorageService) {
169
- return prompts;
170
- }
171
- return Promise.all(
172
- prompts.map(async (prompt) => {
173
- if (!prompt.content?.some((part) => part.type !== "text" && part.gcsKey)) {
174
- return prompt;
175
- }
176
- const plain = toPlainPrompt(prompt);
177
- const content = await Promise.all(
178
- (plain.content ?? []).map(async (part) => {
179
- if (part.type === "text" || !part.gcsKey) {
180
- return part;
181
- }
182
- try {
183
- return {...part, url: await fileStorageService.getSignedUrl(part.gcsKey)};
184
- } catch (error) {
185
- logger.warn("Could not sign stored attachment URL", {
186
- error: error instanceof Error ? error.message : String(error),
187
- gcsKey: part.gcsKey,
188
- });
189
- return part;
190
- }
191
- })
192
- );
193
- return {...plain, content};
194
- })
195
- );
196
- };
197
-
198
- interface GeneratedImageFile {
199
- base64: string;
200
- mediaType: string;
201
- }
202
-
203
- const isGeneratedImageFile = (value: unknown): value is GeneratedImageFile =>
204
- typeof value === "object" &&
205
- value !== null &&
206
- "base64" in value &&
207
- typeof value.base64 === "string" &&
208
- "mediaType" in value &&
209
- typeof value.mediaType === "string" &&
210
- value.mediaType.startsWith("image/");
211
-
212
- const DEMO_RESPONSE =
213
- "This is demo mode. To use AI features, paste your Gemini API key in Settings.";
214
-
215
- /** Send a canned SSE demo response when no AI service is available. */
216
- const sendDemoResponse = (res: express.Response, historyId?: string): void => {
217
- res.setHeader("Content-Type", "text/event-stream");
218
- res.setHeader("Cache-Control", "no-cache");
219
- res.setHeader("Connection", "keep-alive");
220
- res.write(`data: ${JSON.stringify({text: DEMO_RESPONSE})}\n\n`);
221
- res.write(`data: ${JSON.stringify({done: true, ...(historyId ? {historyId} : {})})}\n\n`);
222
- res.end();
223
- };
224
-
225
- /**
226
- * Resolve the AIService for a request. Priority:
227
- * 1. Per-request API key via `x-ai-api-key` header (creates a temporary AIService via createModelFn)
228
- * 2. Specific model requested + server-side model factory (creates a temporary AIService via createServerModelFn)
229
- * 3. Pre-configured aiService from route options
230
- * 4. undefined (triggers demo mode response)
231
- */
232
- const resolveAiService = (
233
- req: express.Request,
234
- options: GptRouteOptions,
235
- modelId?: string
236
- ): AIService | undefined => {
237
- const perRequestKey = req.headers["x-ai-api-key"] as string | undefined;
238
- if (perRequestKey && options.createModelFn) {
239
- return new AIService({model: options.createModelFn(perRequestKey, modelId)});
240
- }
241
- if (modelId && options.createServerModelFn) {
242
- const serverModel = options.createServerModelFn(modelId);
243
- if (serverModel) {
244
- return new AIService({model: serverModel});
245
- }
246
- }
247
- return options.aiService;
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
+ };
248
71
  };
249
72
 
250
- /** Generate a short title for a conversation using a cheap model call. */
251
- const generateTitle = async (
252
- prompt: string,
253
- response: string,
254
- aiService: AIService,
255
- options: GptRouteOptions,
256
- perRequestApiKey?: string
257
- ): Promise<string | undefined> => {
258
- try {
259
- let titleService = aiService;
260
- if (options.titleModelId) {
261
- if (options.createModelFn && perRequestApiKey) {
262
- titleService = new AIService({
263
- model: options.createModelFn(perRequestApiKey, options.titleModelId),
264
- });
265
- } else if (options.createServerModelFn) {
266
- const titleModel = options.createServerModelFn(options.titleModelId);
267
- if (titleModel) {
268
- titleService = new AIService({model: titleModel});
269
- }
270
- }
271
- }
272
- const conversationSnippet = `User: ${prompt}\nAssistant: ${response.substring(0, 500)}`;
273
- const title = await titleService.generateText({
274
- prompt: conversationSnippet,
275
- systemPrompt: TITLE_GENERATION_PROMPT,
276
- temperature: 0.3,
73
+ export const addGptRoutes = (router: express.Router, options: GptRouteOptions): void => {
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,
277
81
  });
278
- return title.trim().replace(/^["']|["']$/g, "");
279
- } catch {
280
- return undefined;
82
+ addGptDatasetRoutes(router, {openApiOptions: options.openApiOptions});
83
+ addGptActionRoutes(router, options);
84
+ }
85
+ if (askKinds.length > 0) {
86
+ assertNoReservedToolNames(routeTools);
281
87
  }
282
- };
283
-
284
- export const addGptRoutes = (router: express.Router, options: GptRouteOptions): void => {
285
- const {
286
- mcpService,
287
- tools: routeTools,
288
- createRequestTools,
289
- toolChoice,
290
- maxSteps,
291
- fileStorageService,
292
- } = options;
293
- const persistIntervalMs = options.streamPersistIntervalMs ?? DEFAULT_STREAM_PERSIST_INTERVAL_MS;
294
88
  const resumePollIntervalMs =
295
89
  options.streamResumePollIntervalMs ?? DEFAULT_STREAM_RESUME_POLL_INTERVAL_MS;
296
90
  const staleAfterMs = options.streamStaleAfterMs ?? DEFAULT_STREAM_STALE_AFTER_MS;
@@ -303,6 +97,7 @@ export const addGptRoutes = (router: express.Router, options: GptRouteOptions):
303
97
  .withTags(["gpt"])
304
98
  .withSummary("Stream a GPT chat response")
305
99
  .withRequestBody({
100
+ ...(askKinds.length > 0 ? {askResponse: ASK_RESPONSE_BODY} : {}),
306
101
  attachments: {
307
102
  items: {
308
103
  properties: {
@@ -319,6 +114,7 @@ export const addGptRoutes = (router: express.Router, options: GptRouteOptions):
319
114
  model: {type: "string"},
320
115
  projectId: {type: "string"},
321
116
  prompt: {type: "string"},
117
+ surface: SURFACE_BODY,
322
118
  systemPrompt: {type: "string"},
323
119
  })
324
120
  .withResponse(200, {data: {type: "string"}})
@@ -327,557 +123,31 @@ export const addGptRoutes = (router: express.Router, options: GptRouteOptions):
327
123
  // Use a raw handler instead of asyncHandler so we can control error responses
328
124
  // for both pre-stream (JSON) and mid-stream (SSE) errors.
329
125
  async (req: express.Request, res: express.Response) => {
330
- let sseStarted = false;
126
+ const sink = createSseSink(res);
331
127
  try {
332
- const {
333
- prompt,
334
- historyId,
335
- systemPrompt,
336
- attachments,
337
- model: requestModel,
338
- projectId,
339
- } = req.body;
340
- const userId = (req.user as {_id?: mongoose.Types.ObjectId} | undefined)?._id;
341
-
342
- if (!prompt || typeof prompt !== "string") {
343
- throw new APIError({status: 400, title: "prompt is required"});
344
- }
345
- if (Array.isArray(attachments) && attachments.length > 0) {
346
- await assertFileUploadsEnabled(req, options.fileUploadsEnabled);
347
- }
348
- const validAttachments = validateAttachments(attachments);
349
-
350
- // Resolve AI service (per-request key takes priority, then configured service)
351
- const hasPerRequestKey = !!req.headers["x-ai-api-key"];
352
- const hasCreateModelFn = !!options.createModelFn;
353
- const hasConfiguredService = !!options.aiService;
354
- logger.debug("Resolving AI service", {
355
- hasConfiguredService,
356
- hasCreateModelFn,
357
- hasPerRequestKey,
358
- });
359
-
360
- const aiService = resolveAiService(req, options, requestModel);
361
- if (!aiService) {
362
- logger.debug("No AI service available, sending demo response");
363
- return sendDemoResponse(res);
364
- }
365
-
366
- // Load or create history
367
- let history: GptHistoryDocument | null;
368
- if (historyId) {
369
- history = await GptHistory.findById(historyId);
370
- if (!history) {
371
- throw new APIError({status: 404, title: "History not found"});
372
- }
373
- if (history.userId.toString() !== userId?.toString()) {
374
- throw new APIError({status: 403, title: "Not authorized to access this history"});
375
- }
376
- } else {
377
- history = new GptHistory({prompts: [], userId, ...(projectId ? {projectId} : {})});
378
- }
379
-
380
- // If history doesn't have a projectId yet but one was provided, associate it
381
- if (projectId && !history.projectId) {
382
- history.projectId = projectId;
383
- }
384
-
385
- // Load project context if a projectId is provided (or inherited from history)
386
- const effectiveProjectId = projectId ?? history.projectId;
387
- let effectiveSystemPrompt = systemPrompt;
388
- if (effectiveProjectId) {
389
- try {
390
- const project = await Project.findById(effectiveProjectId);
391
- if (project && project.userId.toString() === userId?.toString()) {
392
- const parts: string[] = [];
393
- if (project.systemContext) {
394
- parts.push(project.systemContext);
395
- }
396
- if (project.memories.length > 0) {
397
- parts.push(
398
- `## Relevant Memories\n${project.memories.map((m) => `- ${m.text}`).join("\n")}`
399
- );
400
- }
401
- if (parts.length > 0) {
402
- const projectContext = parts.join("\n\n");
403
- effectiveSystemPrompt = effectiveSystemPrompt
404
- ? `${projectContext}\n\n${effectiveSystemPrompt}`
405
- : projectContext;
406
- }
407
- }
408
- } catch {
409
- // Project loading failure should not block the request
410
- }
411
- }
412
-
413
- // Load system prompt from Langfuse if configured
414
- if (options.langfuseSystemPromptName) {
415
- try {
416
- const langfuseResult = await preparePromptForAI({
417
- promptName: options.langfuseSystemPromptName,
418
- userId: userId?.toString(),
419
- });
420
- const langfusePrompt =
421
- typeof langfuseResult.prompt === "string" ? langfuseResult.prompt : undefined;
422
- if (langfusePrompt) {
423
- effectiveSystemPrompt = effectiveSystemPrompt
424
- ? `${langfusePrompt}\n\n${effectiveSystemPrompt}`
425
- : langfusePrompt;
426
- }
427
- } catch (err) {
428
- logger.debug(`Langfuse system prompt skipped: ${(err as Error).message}`);
429
- }
430
- }
431
-
432
- // Build content parts from attachments
433
- logger.debug("Processing attachments", {
434
- count: validAttachments.length,
435
- types: validAttachments.map((a) => ({
436
- mimeType: a.mimeType,
437
- type: a.type,
438
- urlLength: a.url?.length ?? 0,
439
- })),
440
- });
441
- const {modelParts, storedParts} = await buildAttachmentParts({
442
- attachments: validAttachments,
443
- fileStorageService,
444
- userId,
445
- });
446
- const contentParts: MessageContentPart[] = [{text: prompt, type: "text"}, ...storedParts];
447
-
448
- // Add user prompt to history
449
- const hasAttachments = contentParts.length > 1;
450
- const userPrompt: GptHistoryPrompt = {
451
- text: prompt,
452
- type: "user",
453
- ...(hasAttachments ? {content: contentParts} : {}),
454
- };
455
- history.prompts.push(userPrompt);
456
-
457
- // Build messages from history using AIService helper
458
- logger.debug("Building messages", {
459
- attachmentCount: contentParts.length - 1,
460
- historyLength: history.prompts.length,
461
- });
462
- // Earlier turns reference stored attachments; this turn sends the original URLs
463
- const earlierPrompts = await resolveStoredAttachmentUrls(
464
- history.prompts.slice(0, -1),
465
- fileStorageService
466
- );
467
- const modelUserPrompt: GptHistoryPrompt = hasAttachments
468
- ? {...userPrompt, content: [{text: prompt, type: "text"}, ...modelParts]}
469
- : userPrompt;
470
- const messages = aiService.buildMessages([...earlierPrompts, modelUserPrompt]);
471
- logger.debug("Messages built", {messageCount: messages.length});
472
-
473
- // Some models (e.g. gemini-3-pro-image) don't support tool calling
474
- const modelId = aiService.modelId;
475
- const supportsTools = !modelId?.includes("image");
476
-
477
- // Merge tools from route config, per-request tools, and MCP service
478
- let allTools: Record<string, Tool> | undefined;
479
- const requestTools = createRequestTools ? createRequestTools(req) : undefined;
480
- if (supportsTools && (routeTools || requestTools || mcpService)) {
481
- allTools = {...(routeTools ?? {}), ...(requestTools ?? {})};
482
- if (mcpService) {
483
- try {
484
- const mcpTools = await mcpService.getTools();
485
- Object.assign(allTools, mcpTools);
486
- } catch {
487
- // MCP tool discovery failure should not block the request
488
- }
489
- }
490
- }
491
-
492
- // Persist the user turn and a streaming placeholder so a reload can resume the reply
493
- const streamId = randomUUID();
494
- history.prompts.push({
495
- model: aiService.modelId,
496
- status: "streaming",
497
- streamId,
498
- text: "",
499
- type: "assistant",
500
- });
501
- await history.save();
502
- const savedHistory = history;
503
- const placeholderIndex = history.prompts.length - 1;
504
-
505
- // Stream response via SSE
506
- res.setHeader("Content-Type", "text/event-stream");
507
- res.setHeader("Cache-Control", "no-cache");
508
- res.setHeader("Connection", "keep-alive");
509
- sseStarted = true;
510
- res.write(sseEvent({historyId: history._id.toString(), started: true, streamId}));
511
-
512
- let fullResponse = "";
513
- // Buffer text per step so we can discard reasoning text when a tool call follows
514
- let stepTextBuffer = "";
515
- let stepHasToolCall = false;
516
- const currentPartialText = (): string =>
517
- fullResponse + (stepHasToolCall ? "" : stepTextBuffer);
518
-
519
- let lastPersistedText = "";
520
- let lastPersistedAt = DateTime.now();
521
- let persistQueue: Promise<void> = Promise.resolve();
522
- const persistTimer = setInterval(() => {
523
- const text = currentPartialText();
524
- const isHeartbeatDue =
525
- DateTime.now().diff(lastPersistedAt).toMillis() >= STREAM_HEARTBEAT_MS;
526
- if (text === lastPersistedText && !isHeartbeatDue) {
527
- return;
528
- }
529
- lastPersistedText = text;
530
- lastPersistedAt = DateTime.now();
531
- persistQueue = persistQueue
532
- .then(async () => {
533
- await GptHistory.updateOne(
534
- {_id: savedHistory._id, [`prompts.${placeholderIndex}.streamId`]: streamId},
535
- {
536
- $set: {
537
- [`prompts.${placeholderIndex}.text`]: text,
538
- updated: DateTime.now().toJSDate(),
539
- },
540
- }
541
- );
542
- })
543
- .catch((persistErr) => {
544
- logger.warn("Failed to persist partial GPT response", {
545
- error: persistErr instanceof Error ? persistErr.message : String(persistErr),
546
- });
547
- });
548
- }, persistIntervalMs);
549
- const stopPersisting = async (): Promise<void> => {
550
- clearInterval(persistTimer);
551
- await persistQueue;
552
- };
553
- const removePlaceholder = (): void => {
554
- const index = savedHistory.prompts.findIndex((p) => p.streamId === streamId);
555
- if (index >= 0) {
556
- savedHistory.prompts.splice(index, 1);
557
- }
558
- };
559
-
560
- const generatedImages: Array<{mimeType: string; url: string}> = [];
561
- // Stream file parts and result.files report the same images; emit each data URL once.
562
- const sendGeneratedImage = (file: GeneratedImageFile): void => {
563
- const url = `data:${file.mediaType};base64,${file.base64}`;
564
- if (generatedImages.some((img) => img.url === url)) {
565
- return;
566
- }
567
- generatedImages.push({mimeType: file.mediaType, url});
568
- res.write(`data: ${JSON.stringify({image: {mimeType: file.mediaType, url}})}\n\n`);
569
- };
570
- const startTime = DateTime.now().toMillis();
571
- try {
572
- logger.debug("Starting streamText", {model: modelId, supportsTools});
573
- const telemetry = createTelemetryConfig({
574
- functionId: "gpt-prompt",
575
- metadata: {
576
- ...(options.langfuseSystemPromptName
577
- ? {langfusePromptName: options.langfuseSystemPromptName}
578
- : {}),
579
- },
580
- userId: userId?.toString(),
581
- });
582
- const result = streamText({
583
- experimental_telemetry: telemetry,
584
- messages,
585
- model: aiService.model,
586
- providerOptions: !supportsTools
587
- ? {
588
- google: {responseModalities: ["TEXT", "IMAGE"]},
589
- vertex: {responseModalities: ["TEXT", "IMAGE"]},
590
- }
591
- : undefined,
592
- stopWhen: allTools ? stepCountIs(maxSteps ?? 5) : stepCountIs(1),
593
- system: effectiveSystemPrompt ?? undefined,
594
- temperature: aiService.defaultTemperature,
595
- toolChoice: allTools ? (toolChoice ?? "auto") : undefined,
596
- tools: allTools,
597
- });
598
-
599
- let partCount = 0;
600
-
601
- for await (const part of result.fullStream as AsyncIterable<{
602
- type: string;
603
- [key: string]: unknown;
604
- }>) {
605
- partCount++;
606
- if (partCount <= 5 || part.type === "error" || part.type === "file") {
607
- logger.debug("Stream part", {
608
- partCount,
609
- type: part.type,
610
- ...(part.type === "file" && isGeneratedImageFile(part.file)
611
- ? {mediaType: part.file.mediaType}
612
- : {}),
613
- ...(part.type === "error" ? {error: String(part.error ?? part)} : {}),
614
- });
615
- }
616
-
617
- // Track step boundaries to discard reasoning text from tool-call steps
618
- if (part.type === "start-step") {
619
- stepTextBuffer = "";
620
- stepHasToolCall = false;
621
- continue;
622
- }
623
- if (part.type === "finish-step") {
624
- // Only emit buffered text if no tool call happened in this step
625
- if (!stepHasToolCall && stepTextBuffer) {
626
- // Strip model reasoning that leaks as JSON action blobs
627
- const cleaned = stepTextBuffer
628
- .replace(/\{[\s\S]*?"action"[\s\S]*?\}\s*$/g, "")
629
- .trim();
630
- if (cleaned) {
631
- fullResponse += cleaned;
632
- res.write(`data: ${JSON.stringify({text: cleaned})}\n\n`);
633
- }
634
- }
635
- stepTextBuffer = "";
636
- stepHasToolCall = false;
637
- continue;
638
- }
639
-
640
- if (part.type === "error") {
641
- const errMsg =
642
- part.error instanceof Error
643
- ? part.error.message
644
- : String(part.error ?? "Unknown stream error");
645
- logger.error("AI stream error part", {error: errMsg});
646
- res.write(`data: ${JSON.stringify({error: errMsg})}\n\n`);
647
- continue;
648
- }
649
- if (part.type === "file") {
650
- if (isGeneratedImageFile(part.file)) {
651
- sendGeneratedImage(part.file);
652
- logger.debug("Sent inline image from stream");
653
- }
654
- continue;
655
- }
656
- if (part.type === "text-delta") {
657
- const textChunk = (part.text ?? "") as string;
658
- if (textChunk) {
659
- stepTextBuffer += textChunk;
660
- }
661
- } else if (part.type === "tool-call") {
662
- stepHasToolCall = true;
663
- res.write(
664
- `data: ${JSON.stringify({
665
- toolCall: {
666
- args: part.input,
667
- toolCallId: part.toolCallId,
668
- toolName: part.toolName,
669
- },
670
- })}\n\n`
671
- );
672
- // Persist tool call in history
673
- history.prompts.push({
674
- args: part.input as Record<string, unknown>,
675
- text: `Tool call: ${part.toolName as string}`,
676
- toolCallId: part.toolCallId as string,
677
- toolName: part.toolName as string,
678
- type: "tool-call",
679
- });
680
- } else if (part.type === "tool-result") {
681
- const toolResult = part.output as Record<string, unknown> | undefined;
682
-
683
- // If the tool result contains file data, send it as a separate file SSE event
684
- if (toolResult?.fileData && typeof toolResult.fileData === "string") {
685
- res.write(
686
- `data: ${JSON.stringify({
687
- file: {
688
- filename: toolResult.filename ?? "document",
689
- mimeType: toolResult.mimeType ?? "application/octet-stream",
690
- url: toolResult.fileData,
691
- },
692
- })}\n\n`
693
- );
694
- logger.debug("Sent generated file from tool result", {
695
- filename: toolResult.filename,
696
- mimeType: toolResult.mimeType,
697
- });
698
- }
699
-
700
- // Strip fileData from result before sending/storing to avoid bloating the SSE and DB
701
- const {fileData: _fileData, ...cleanResult} = toolResult ?? {};
702
-
703
- res.write(
704
- `data: ${JSON.stringify({
705
- toolResult: {
706
- result: cleanResult,
707
- toolCallId: part.toolCallId,
708
- toolName: part.toolName,
709
- },
710
- })}\n\n`
711
- );
712
- // Persist tool result in history (without the large file data)
713
- history.prompts.push({
714
- result: cleanResult as unknown,
715
- text: `Tool result: ${part.toolName as string}`,
716
- toolCallId: part.toolCallId as string,
717
- toolName: part.toolName as string,
718
- type: "tool-result",
719
- });
720
- }
721
- }
722
-
723
- // Flush any remaining buffered text from the last step
724
- if (!stepHasToolCall && stepTextBuffer) {
725
- const cleaned = stepTextBuffer.replace(/\{[\s\S]*?"action"[\s\S]*?\}\s*$/g, "").trim();
726
- if (cleaned) {
727
- fullResponse += cleaned;
728
- res.write(`data: ${JSON.stringify({text: cleaned})}\n\n`);
729
- }
730
- }
731
-
732
- logger.debug("Stream completed", {fullResponseLength: fullResponse.length, partCount});
733
-
734
- // Catch generated images the stream did not report as file parts
735
- try {
736
- const files = await result.files;
737
- if (files && files.length > 0) {
738
- for (const file of files) {
739
- if (isGeneratedImageFile(file)) {
740
- sendGeneratedImage(file);
741
- }
742
- }
743
- logger.debug("Sent generated images", {count: generatedImages.length});
744
- }
745
- } catch (fileErr) {
746
- logger.debug("No files in response", {
747
- error: fileErr instanceof Error ? fileErr.message : String(fileErr),
748
- });
749
- }
750
-
751
- // Replace the streaming placeholder with the final reply, after any tool turns
752
- await stopPersisting();
753
- removePlaceholder();
754
- if (fullResponse || generatedImages.length > 0) {
755
- const contentParts: MessageContentPart[] = generatedImages.map((img) => ({
756
- mimeType: img.mimeType,
757
- type: "image" as const,
758
- url: img.url,
759
- }));
760
- const assistantPrompt: GptHistoryPrompt = {
761
- model: aiService.modelId,
762
- status: "complete",
763
- streamId,
764
- text: fullResponse,
765
- type: "assistant",
766
- ...(contentParts.length > 0 ? {content: contentParts} : {}),
767
- };
768
- history.prompts.push(assistantPrompt);
769
- }
770
- history.markModified("prompts");
771
- await history.save();
772
-
773
- try {
774
- await AIRequest.logRequest({
775
- aiModel: modelId ?? "unknown",
776
- prompt,
777
- requestType: "general",
778
- response: fullResponse,
779
- responseTime: DateTime.now().toMillis() - startTime,
780
- userId: userId ?? undefined,
781
- });
782
- } catch (logErr) {
783
- logger.warn("Failed to log AIRequest", {
784
- error: logErr instanceof Error ? logErr.message : String(logErr),
785
- });
786
- }
787
-
788
- // Generate a title for new conversations using a cheap model call
789
- if (!history.title && fullResponse) {
790
- const perRequestApiKey = req.headers["x-ai-api-key"] as string | undefined;
791
- const title = await generateTitle(
792
- prompt,
793
- fullResponse,
794
- aiService,
795
- options,
796
- perRequestApiKey
797
- );
798
- if (title) {
799
- history.title = title;
800
- await history.save();
801
- }
802
- }
803
-
804
- logger.debug("Sending done event", {
805
- fullResponseLength: fullResponse.length,
806
- historyId: history._id.toString(),
807
- });
808
- res.write(
809
- `data: ${JSON.stringify({
810
- done: true,
811
- historyId: history._id.toString(),
812
- ...(history.title ? {title: history.title} : {}),
813
- })}\n\n`
814
- );
815
- res.end();
816
- } catch (error) {
817
- logger.error("Error in GPT stream", {
818
- error: error instanceof Error ? error.message : String(error),
819
- });
820
-
821
- // Keep any partial reply so the conversation shows what streamed before the failure
822
- try {
823
- await stopPersisting();
824
- const partialText = currentPartialText();
825
- const placeholder = savedHistory.prompts.find((p) => p.streamId === streamId);
826
- if (placeholder?.status === "streaming") {
827
- if (partialText) {
828
- placeholder.text = partialText;
829
- placeholder.status = "error";
830
- } else {
831
- removePlaceholder();
832
- }
833
- savedHistory.markModified("prompts");
834
- await savedHistory.save();
835
- }
836
- } catch (saveErr) {
837
- logger.warn("Failed to save interrupted GPT response", {
838
- error: saveErr instanceof Error ? saveErr.message : String(saveErr),
839
- });
840
- }
841
-
842
- try {
843
- await AIRequest.logRequest({
844
- aiModel: modelId ?? "unknown",
845
- error: error instanceof Error ? error.message : String(error),
846
- prompt,
847
- requestType: "general",
848
- responseTime: DateTime.now().toMillis() - startTime,
849
- userId: userId ?? undefined,
850
- });
851
- } catch (logErr) {
852
- logger.warn("Failed to log AIRequest error", {
853
- error: logErr instanceof Error ? logErr.message : String(logErr),
854
- });
855
- }
856
-
857
- res.write(
858
- `data: ${JSON.stringify({error: error instanceof Error ? error.message : "Unknown error"})}\n\n`
859
- );
860
- res.end();
861
- }
862
- } catch (outerError) {
128
+ await runChatTurn({body: req.body ?? {}, options, req, sink});
129
+ res.end();
130
+ } catch (error) {
863
131
  // Catch-all for errors thrown before or after SSE streaming
864
- const message = outerError instanceof Error ? outerError.message : String(outerError);
865
- const status = outerError instanceof APIError ? outerError.status : 500;
866
- const stack = outerError instanceof Error ? outerError.stack : undefined;
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;
867
135
  logger.error("GPT prompt handler error", {error: message, stack, status});
868
136
 
869
- if (sseStarted) {
137
+ if (sink.isOpen()) {
870
138
  // Already sent SSE headers — send error as SSE event
871
139
  res.write(`data: ${JSON.stringify({error: message})}\n\n`);
872
140
  res.end();
873
- } else {
874
- // Haven't started SSE — send a normal JSON error response
875
- res.status(status).json({
876
- detail: message,
877
- status,
878
- title: outerError instanceof APIError ? outerError.title : "Internal server error",
879
- });
141
+ return;
880
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
+ });
881
151
  }
882
152
  }
883
153
  );
@@ -1024,7 +294,7 @@ export const addGptRoutes = (router: express.Router, options: GptRouteOptions):
1024
294
  const {promptIndex, rating} = req.body;
1025
295
  const userId = (req.user as {_id?: mongoose.Types.ObjectId} | undefined)?._id;
1026
296
 
1027
- if (typeof promptIndex !== "number" || promptIndex < 0) {
297
+ if (typeof promptIndex !== "number" || !Number.isInteger(promptIndex) || promptIndex < 0) {
1028
298
  throw new APIError({status: 400, title: "promptIndex must be a non-negative number"});
1029
299
  }
1030
300
  if (rating !== null && rating !== "up" && rating !== "down") {
@@ -1042,15 +312,14 @@ export const addGptRoutes = (router: express.Router, options: GptRouteOptions):
1042
312
  throw new APIError({status: 400, title: "promptIndex out of range"});
1043
313
  }
1044
314
 
1045
- if (rating === null) {
1046
- history.prompts[promptIndex].rating = undefined;
1047
- } else {
1048
- history.prompts[promptIndex].rating = rating;
1049
- }
1050
- history.markModified("prompts");
1051
- 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
+ );
1052
321
 
1053
- return res.json({data: {promptIndex, rating: history.prompts[promptIndex].rating ?? null}});
322
+ return res.json({data: {promptIndex, rating}});
1054
323
  })
1055
324
  );
1056
325
 
@@ -1070,10 +339,16 @@ export const addGptRoutes = (router: express.Router, options: GptRouteOptions):
1070
339
  asyncHandler(async (req: express.Request, res: express.Response) => {
1071
340
  const {text} = req.body;
1072
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;
1073
345
 
1074
346
  if (!text || typeof text !== "string") {
1075
347
  throw new APIError({status: 400, title: "text is required"});
1076
348
  }
349
+ if (hasPromptReference && !isAdmin) {
350
+ throw new APIError({status: 403, title: "Prompt registry selection requires admin access"});
351
+ }
1077
352
 
1078
353
  const aiService = resolveAiService(req, options);
1079
354
  if (!aiService) {