@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
@@ -3,13 +3,18 @@ import {TerrenoApp} from "@terreno/api";
3
3
  import {jsonSchema, type LanguageModel, type Tool, tool} from "ai";
4
4
  import {assert} from "chai";
5
5
  import type express from "express";
6
+ import {DateTime} from "luxon";
6
7
  import mongoose from "mongoose";
7
8
  import type supertest from "supertest";
8
9
 
9
10
  import {AIRequest} from "../models/aiRequest";
10
11
  import {GptHistory} from "../models/gptHistory";
11
12
  import {Project} from "../models/project";
13
+ import {MemoryTraceSink} from "../observability/local/traceStore";
14
+ import {ObservabilityApp, resetObservabilityApp} from "../observability/observabilityApp";
15
+ import type {ObservabilityPlugin} from "../observability/types";
12
16
  import {AIService} from "../service/aiService";
17
+ import type {FileStorageService} from "../service/fileStorage";
13
18
  import type {MCPService} from "../service/mcpService";
14
19
  import {authAsUser, ensureTestUsers, UserModel} from "../tests/helpers";
15
20
  import {addAiRequestsExplorerRoutes} from "./aiRequestsExplorer";
@@ -95,7 +100,7 @@ const createImageModel = () => ({
95
100
  },
96
101
  }),
97
102
  })),
98
- modelId: "gemini-2.5-flash-image",
103
+ modelId: "gemini-3-pro-image",
99
104
  provider: "mock-provider",
100
105
  specificationVersion: "v2" as const,
101
106
  supportedUrls: {},
@@ -198,6 +203,7 @@ describe("AI Routes", () => {
198
203
 
199
204
  afterEach(async () => {
200
205
  streamTextOverride = undefined;
206
+ resetObservabilityApp();
201
207
  await AIRequest.deleteMany({});
202
208
  await GptHistory.deleteMany({});
203
209
  });
@@ -239,6 +245,15 @@ describe("AI Routes", () => {
239
245
  expect(res.status).toBe(400);
240
246
  });
241
247
 
248
+ it("forbids non-admin prompt registry selection", async () => {
249
+ const agent = await authAsUser(app, "notAdmin");
250
+ const res = await agent
251
+ .post("/gpt/remix")
252
+ .send({promptLabel: "latest", promptName: "private-prompt", text: "Hello"});
253
+
254
+ assert.equal(res.status, 403);
255
+ });
256
+
242
257
  it("returns demo response when no aiService configured", async () => {
243
258
  const demoApp = new TerrenoApp({
244
259
  configureApp: (router, options) => {
@@ -290,6 +305,90 @@ describe("AI Routes", () => {
290
305
  expect(histories.length).toBeGreaterThanOrEqual(1);
291
306
  });
292
307
 
308
+ it("forbids non-admin prompt registry selection", async () => {
309
+ const agent = await authAsUser(app, "notAdmin");
310
+ const res = await agent
311
+ .post("/gpt/prompt")
312
+ .send({prompt: "Hi", promptLabel: "latest", promptName: "private-prompt"});
313
+
314
+ assert.equal(res.status, 403);
315
+ });
316
+
317
+ it("does not let client sensitive false downgrade a sensitive prompt", async () => {
318
+ const sink = new MemoryTraceSink();
319
+ const plugin: ObservabilityPlugin = {
320
+ capabilities: new Set([
321
+ "datasets",
322
+ "experiments",
323
+ "prompts",
324
+ "reviewQueue",
325
+ "scores",
326
+ "traces",
327
+ ]),
328
+ datasetStore: {},
329
+ experimentRunner: {},
330
+ id: "local",
331
+ promptRegistry: {
332
+ get: async () => ({
333
+ body: "Sensitive system prompt",
334
+ label: "production",
335
+ name: "sensitive-prompt",
336
+ sensitive: true,
337
+ version: 1,
338
+ }),
339
+ },
340
+ reviewQueue: {},
341
+ traceSink: sink,
342
+ };
343
+ new ObservabilityApp({plugins: [plugin]});
344
+
345
+ const agent = await authAsUser(app, "admin");
346
+ const res = await agent
347
+ .post("/gpt/prompt")
348
+ .send({prompt: "Hi", promptName: "sensitive-prompt", sensitive: false})
349
+ .buffer(true)
350
+ .parse(sseCollect);
351
+
352
+ assert.equal(res.status, 200);
353
+ assert.equal(sink.traces.length, 1);
354
+ assert.isTrue(sink.traces[0]?.sensitive);
355
+ });
356
+
357
+ it("emits a memory-sink trace with userId and sessionId", async () => {
358
+ const sink = new MemoryTraceSink();
359
+ const plugin: ObservabilityPlugin = {
360
+ capabilities: new Set([
361
+ "datasets",
362
+ "experiments",
363
+ "prompts",
364
+ "reviewQueue",
365
+ "scores",
366
+ "traces",
367
+ ]),
368
+ datasetStore: {},
369
+ experimentRunner: {},
370
+ id: "local",
371
+ promptRegistry: {get: async () => undefined},
372
+ reviewQueue: {},
373
+ traceSink: sink,
374
+ };
375
+ new ObservabilityApp({plugins: [plugin]});
376
+
377
+ const agent = await authAsUser(app, "notAdmin");
378
+ const user = await UserModel.findOne({email: "notAdmin@example.com"});
379
+ const res = await agent
380
+ .post("/gpt/prompt")
381
+ .set("x-ai-session-id", "sess-gpt-1")
382
+ .send({prompt: "Hi"})
383
+ .buffer(true)
384
+ .parse(sseCollect);
385
+
386
+ expect(res.status).toBe(200);
387
+ expect(sink.traces.length).toBe(1);
388
+ expect(sink.traces[0].sessionId).toBe("sess-gpt-1");
389
+ expect(sink.traces[0].userId).toBe(user?._id.toString());
390
+ });
391
+
293
392
  it("sends demo response when no ai service configured", async () => {
294
393
  const demoApp = new TerrenoApp({
295
394
  configureApp: (router, options) => {
@@ -345,11 +444,11 @@ describe("AI Routes", () => {
345
444
  const agent = await authAsUser(customApp, "notAdmin");
346
445
  const res = await agent
347
446
  .post("/gpt/prompt")
348
- .send({model: "gemini-2.5-pro", prompt: "Hi"})
447
+ .send({model: "gemini-3.1-pro-preview", prompt: "Hi"})
349
448
  .buffer(true)
350
449
  .parse(sseCollect);
351
450
  expect(res.status).toBe(200);
352
- expect(createServerModelFn).toHaveBeenCalledWith("gemini-2.5-pro");
451
+ expect(createServerModelFn).toHaveBeenCalledWith("gemini-3.1-pro-preview");
353
452
  });
354
453
 
355
454
  it("falls back to default aiService when createServerModelFn returns null", async () => {
@@ -717,6 +816,25 @@ describe("AI Routes", () => {
717
816
  });
718
817
 
719
818
  it("forwards model tool-call and tool-result stream events via SSE", async () => {
819
+ const sink = new MemoryTraceSink();
820
+ const plugin: ObservabilityPlugin = {
821
+ capabilities: new Set([
822
+ "datasets",
823
+ "experiments",
824
+ "prompts",
825
+ "reviewQueue",
826
+ "scores",
827
+ "traces",
828
+ ]),
829
+ datasetStore: {},
830
+ experimentRunner: {},
831
+ id: "local",
832
+ promptRegistry: {get: async () => undefined},
833
+ reviewQueue: {},
834
+ traceSink: sink,
835
+ };
836
+ new ObservabilityApp({plugins: [plugin]});
837
+
720
838
  const toolModel = {
721
839
  doGenerate: mock(async () => ({
722
840
  content: [{text: "ok", type: "text" as const}],
@@ -741,17 +859,31 @@ describe("AI Routes", () => {
741
859
  type: "tool-call" as const,
742
860
  });
743
861
  controller.enqueue({
744
- output: {
862
+ providerExecuted: true,
863
+ result: {
745
864
  fileData: "data:application/pdf;base64,AAAA",
746
865
  filename: "result.pdf",
747
866
  mimeType: "application/pdf",
748
867
  results: ["item1"],
749
868
  },
750
- providerExecuted: true,
751
869
  toolCallId: "tc1",
752
870
  toolName: "search",
753
871
  type: "tool-result" as const,
754
872
  });
873
+ controller.enqueue({
874
+ input: {id: "second"},
875
+ providerExecuted: true,
876
+ toolCallId: "tc2",
877
+ toolName: "lookup",
878
+ type: "tool-call" as const,
879
+ });
880
+ controller.enqueue({
881
+ providerExecuted: true,
882
+ result: {value: "second-result"},
883
+ toolCallId: "tc2",
884
+ toolName: "lookup",
885
+ type: "tool-result" as const,
886
+ });
755
887
  controller.enqueue({
756
888
  finishReason: "stop" as const,
757
889
  type: "finish" as const,
@@ -772,6 +904,7 @@ describe("AI Routes", () => {
772
904
  aiService: new AIService({model: toolModel as unknown as LanguageModel}),
773
905
  openApiOptions: options,
774
906
  tools: {
907
+ lookup: {description: "Lookup item"} as unknown as Tool,
775
908
  search: {description: "Web search"} as unknown as Tool,
776
909
  },
777
910
  });
@@ -789,6 +922,29 @@ describe("AI Routes", () => {
789
922
  const body = (res as SseResponse).body;
790
923
  expect(body).toContain("toolCall");
791
924
  expect(body).toContain("toolResult");
925
+ assert.equal(sink.traces.length, 1);
926
+ const trace = sink.traces[0];
927
+ assert.equal(trace.spans[0]?.kind, "CHAIN");
928
+ assert.equal(trace.spans.length, 3);
929
+ const toolSpans = trace.spans.filter((span) => span.kind === "TOOL");
930
+ assert.sameMembers(
931
+ toolSpans.map((span) => span.name),
932
+ ["search", "lookup"]
933
+ );
934
+ const toolSpan = toolSpans.find((span) => span.name === "search");
935
+ assert.isDefined(toolSpan);
936
+ assert.equal(toolSpan?.name, "search");
937
+ assert.deepEqual(toolSpan?.input, {q: "hello"});
938
+ assert.deepEqual(toolSpan?.output, {
939
+ filename: "result.pdf",
940
+ mimeType: "application/pdf",
941
+ results: ["item1"],
942
+ });
943
+ assert.equal(toolSpan?.parentSpanId, trace.spans[0]?.id);
944
+ assert.equal(
945
+ toolSpans.find((span) => span.name === "lookup")?.parentSpanId,
946
+ trace.spans[0]?.id
947
+ );
792
948
  });
793
949
 
794
950
  it("emits a file SSE event when a tool execution returns fileData", async () => {
@@ -1242,7 +1398,522 @@ describe("AI Routes", () => {
1242
1398
  });
1243
1399
  });
1244
1400
 
1401
+ describe("GPT Prompt durable attachments and resumable streams", () => {
1402
+ const getNotAdminId = async (): Promise<mongoose.Types.ObjectId> => {
1403
+ const notAdmin = (await UserModel.findOne({
1404
+ email: "notAdmin@example.com",
1405
+ })) as mongoose.Document & {_id: mongoose.Types.ObjectId};
1406
+ return notAdmin._id;
1407
+ };
1408
+
1409
+ const createFakeStorage = () => {
1410
+ const upload = mock(
1411
+ async ({filename, mimeType}: {buffer: Buffer; filename: string; mimeType: string}) => ({
1412
+ filename,
1413
+ gcsKey: `uploads/test/${filename}`,
1414
+ mimeType,
1415
+ size: 5,
1416
+ url: `https://storage.googleapis.com/bucket/uploads/test/${filename}`,
1417
+ })
1418
+ );
1419
+ const getSignedUrl = mock(async (gcsKey: string) => `https://signed.example.com/${gcsKey}`);
1420
+ return {
1421
+ getSignedUrl,
1422
+ service: {getSignedUrl, upload} as unknown as FileStorageService,
1423
+ upload,
1424
+ };
1425
+ };
1426
+
1427
+ const textOnlyStream = (capture?: (options: StreamTextOptions) => void) => {
1428
+ streamTextOverride = (streamOptions) => {
1429
+ capture?.(streamOptions);
1430
+ return {
1431
+ files: Promise.resolve([]),
1432
+ fullStream: (async function* () {
1433
+ yield {type: "start-step"};
1434
+ yield {text: "Reply", type: "text-delta"};
1435
+ yield {type: "finish-step"};
1436
+ })(),
1437
+ } as unknown as StreamTextResult;
1438
+ };
1439
+ };
1440
+
1441
+ const buildApp = (extra: Partial<Parameters<typeof addGptRoutes>[1]> = {}) =>
1442
+ new TerrenoApp({
1443
+ configureApp: (router, options) => {
1444
+ addGptRoutes(router, {aiService, openApiOptions: options, ...extra});
1445
+ },
1446
+ skipListen: true,
1447
+ userModel: UserModel,
1448
+ }).build();
1449
+
1450
+ it("rejects attachments when file uploads are disabled", async () => {
1451
+ const gated = buildApp({fileUploadsEnabled: async () => false});
1452
+ const agent = await authAsUser(gated, "notAdmin");
1453
+ const res = await agent.post("/gpt/prompt").send({
1454
+ attachments: [{mimeType: "image/png", type: "image", url: "data:image/png;base64,aGk="}],
1455
+ prompt: "What is this?",
1456
+ });
1457
+ expect(res.status).toBe(403);
1458
+ expect(res.body.title).toBe("File uploads are disabled");
1459
+ expect(await GptHistory.countDocuments({})).toBe(0);
1460
+ });
1461
+
1462
+ it("rejects client-only attachment URLs before streaming", async () => {
1463
+ const agent = await authAsUser(app, "notAdmin");
1464
+ const res = await agent.post("/gpt/prompt").send({
1465
+ attachments: [{mimeType: "image/png", type: "image", url: "blob:http://localhost/abc"}],
1466
+ prompt: "What is this?",
1467
+ });
1468
+ expect(res.status).toBeGreaterThanOrEqual(400);
1469
+ expect(res.body.title).toContain("Attachment URL must be an http(s) or data: URL");
1470
+ expect(await GptHistory.countDocuments({})).toBe(0);
1471
+ });
1472
+
1473
+ it("rejects file: attachment URLs", async () => {
1474
+ const agent = await authAsUser(app, "notAdmin");
1475
+ const res = await agent.post("/gpt/prompt").send({
1476
+ attachments: [
1477
+ {filename: "a.pdf", mimeType: "application/pdf", type: "file", url: "file:///a.pdf"},
1478
+ ],
1479
+ prompt: "Read this",
1480
+ });
1481
+ expect(res.status).toBeGreaterThanOrEqual(400);
1482
+ expect(res.body.title).toContain("Attachment URL");
1483
+ });
1484
+
1485
+ it("uploads data: attachments and stores a durable reference", async () => {
1486
+ const storage = createFakeStorage();
1487
+ let modelMessages: unknown;
1488
+ textOnlyStream((streamOptions) => {
1489
+ modelMessages = streamOptions.messages;
1490
+ });
1491
+ const agent = await authAsUser(buildApp({fileStorageService: storage.service}), "notAdmin");
1492
+ const dataUrl = "data:image/png;base64,aGVsbG8=";
1493
+ const res = await agent
1494
+ .post("/gpt/prompt")
1495
+ .send({
1496
+ attachments: [{filename: "pic.png", mimeType: "image/png", type: "image", url: dataUrl}],
1497
+ prompt: "Describe",
1498
+ })
1499
+ .buffer(true)
1500
+ .parse(sseCollect);
1501
+ expect(res.status).toBe(200);
1502
+
1503
+ expect(storage.upload).toHaveBeenCalledTimes(1);
1504
+ const uploadArgs = storage.upload.mock.calls[0][0];
1505
+ expect(uploadArgs.buffer.toString()).toBe("hello");
1506
+ expect(uploadArgs.mimeType).toBe("image/png");
1507
+
1508
+ const history = await GptHistory.findExactlyOne({"prompts.text": "Describe"});
1509
+ const imagePart = history.prompts[0].content?.find((p) => p.type === "image");
1510
+ expect(imagePart?.gcsKey).toBe("uploads/test/pic.png");
1511
+ expect(imagePart?.url).toBe("https://storage.googleapis.com/bucket/uploads/test/pic.png");
1512
+ // The model still receives the original data for this turn
1513
+ expect(JSON.stringify(modelMessages)).toContain("aGVsbG8=");
1514
+ });
1515
+
1516
+ it("decodes non-base64 data: attachments and names unnamed uploads", async () => {
1517
+ const storage = createFakeStorage();
1518
+ textOnlyStream();
1519
+ const agent = await authAsUser(buildApp({fileStorageService: storage.service}), "notAdmin");
1520
+ await agent
1521
+ .post("/gpt/prompt")
1522
+ .send({
1523
+ attachments: [{mimeType: "text/plain", type: "file", url: "data:text/plain,hi%20there"}],
1524
+ prompt: "Read",
1525
+ })
1526
+ .buffer(true)
1527
+ .parse(sseCollect);
1528
+ const uploadArgs = storage.upload.mock.calls[0][0];
1529
+ expect(uploadArgs.buffer.toString()).toBe("hi there");
1530
+ expect(uploadArgs.filename).toMatch(/^attachment-\d+\.plain$/);
1531
+ });
1532
+
1533
+ it("keeps data: attachments inline when no storage is configured", async () => {
1534
+ textOnlyStream();
1535
+ const agent = await authAsUser(app, "notAdmin");
1536
+ await agent
1537
+ .post("/gpt/prompt")
1538
+ .send({
1539
+ attachments: [
1540
+ {mimeType: "image/png", type: "image", url: "data:image/png;base64,aGVsbG8="},
1541
+ ],
1542
+ prompt: "Inline",
1543
+ })
1544
+ .buffer(true)
1545
+ .parse(sseCollect);
1546
+ const history = await GptHistory.findExactlyOne({"prompts.text": "Inline"});
1547
+ const imagePart = history.prompts[0].content?.find((p) => p.type === "image");
1548
+ expect(imagePart?.url).toBe("data:image/png;base64,aGVsbG8=");
1549
+ expect(imagePart?.gcsKey).toBeUndefined();
1550
+ });
1551
+
1552
+ it("returns 502 when the attachment upload fails", async () => {
1553
+ const failingStorage = {
1554
+ getSignedUrl: mock(async () => ""),
1555
+ upload: mock(async () => {
1556
+ throw new Error("bucket unavailable");
1557
+ }),
1558
+ } as unknown as FileStorageService;
1559
+ const agent = await authAsUser(buildApp({fileStorageService: failingStorage}), "notAdmin");
1560
+ const res = await agent.post("/gpt/prompt").send({
1561
+ attachments: [{mimeType: "image/png", type: "image", url: "data:image/png;base64,aGk="}],
1562
+ prompt: "Fail",
1563
+ });
1564
+ expect(res.status).toBeGreaterThanOrEqual(400);
1565
+ expect(res.body.title).toBe("Attachment upload failed");
1566
+ });
1567
+
1568
+ it("signs stored attachment URLs for earlier turns", async () => {
1569
+ const storage = createFakeStorage();
1570
+ let modelMessages: unknown;
1571
+ textOnlyStream((streamOptions) => {
1572
+ modelMessages = streamOptions.messages;
1573
+ });
1574
+ const history = await GptHistory.create({
1575
+ prompts: [
1576
+ {
1577
+ content: [
1578
+ {text: "Look", type: "text"},
1579
+ {
1580
+ gcsKey: "uploads/test/old.png",
1581
+ mimeType: "image/png",
1582
+ type: "image",
1583
+ url: "https://storage.googleapis.com/bucket/uploads/test/old.png",
1584
+ },
1585
+ ],
1586
+ text: "Look",
1587
+ type: "user",
1588
+ },
1589
+ {status: "complete", text: "Nice", type: "assistant"},
1590
+ ],
1591
+ userId: await getNotAdminId(),
1592
+ });
1593
+ const agent = await authAsUser(buildApp({fileStorageService: storage.service}), "notAdmin");
1594
+ await agent
1595
+ .post("/gpt/prompt")
1596
+ .send({historyId: history._id.toString(), prompt: "And now?"})
1597
+ .buffer(true)
1598
+ .parse(sseCollect);
1599
+ expect(storage.getSignedUrl).toHaveBeenCalledWith("uploads/test/old.png");
1600
+ expect(JSON.stringify(modelMessages)).toContain("signed.example.com/uploads/test/old.png");
1601
+ });
1602
+
1603
+ it("emits a started event and saves the completed reply with its streamId", async () => {
1604
+ textOnlyStream();
1605
+ const agent = await authAsUser(app, "notAdmin");
1606
+ const res = await agent
1607
+ .post("/gpt/prompt")
1608
+ .send({prompt: "Hello"})
1609
+ .buffer(true)
1610
+ .parse(sseCollect);
1611
+ const body = (res as SseResponse).body;
1612
+ const firstEvent = JSON.parse(body.split("\n\n")[0].slice(6));
1613
+ expect(firstEvent.started).toBe(true);
1614
+ expect(typeof firstEvent.historyId).toBe("string");
1615
+ expect(typeof firstEvent.streamId).toBe("string");
1616
+
1617
+ const history = await GptHistory.findExactlyOne({_id: firstEvent.historyId});
1618
+ const assistant = history.prompts.filter((p) => p.type === "assistant");
1619
+ expect(assistant).toHaveLength(1);
1620
+ expect(assistant[0].status).toBe("complete");
1621
+ expect(assistant[0].streamId).toBe(firstEvent.streamId);
1622
+ expect(assistant[0].text).toBe("Reply");
1623
+ });
1624
+
1625
+ it("persists partial assistant text while the reply streams", async () => {
1626
+ let persistedDuringStream: {status?: string; text?: string} | undefined;
1627
+ streamTextOverride = () =>
1628
+ ({
1629
+ files: Promise.resolve([]),
1630
+ fullStream: (async function* () {
1631
+ yield {type: "start-step"};
1632
+ yield {text: "partial ", type: "text-delta"};
1633
+ const deadline = DateTime.now().plus({seconds: 3});
1634
+ while (DateTime.now() < deadline) {
1635
+ const doc = await GptHistory.findOne({"prompts.text": "Stream me"});
1636
+ const reply = doc?.prompts.find((p) => p.type === "assistant");
1637
+ if (reply?.text) {
1638
+ persistedDuringStream = {status: reply.status, text: reply.text};
1639
+ break;
1640
+ }
1641
+ await new Promise((resolve) => setTimeout(resolve, 10));
1642
+ }
1643
+ yield {text: "done", type: "text-delta"};
1644
+ yield {type: "finish-step"};
1645
+ })(),
1646
+ }) as unknown as StreamTextResult;
1647
+ const agent = await authAsUser(buildApp({streamPersistIntervalMs: 10}), "notAdmin");
1648
+ await agent.post("/gpt/prompt").send({prompt: "Stream me"}).buffer(true).parse(sseCollect);
1649
+ expect(persistedDuringStream).toEqual({status: "streaming", text: "partial "});
1650
+ const history = await GptHistory.findExactlyOne({"prompts.text": "Stream me"});
1651
+ expect(history.prompts.find((p) => p.type === "assistant")?.text).toBe("partial done");
1652
+ });
1653
+
1654
+ it("keeps partial text marked as error when the stream fails", async () => {
1655
+ streamTextOverride = () =>
1656
+ ({
1657
+ files: Promise.resolve([]),
1658
+ fullStream: (async function* () {
1659
+ yield {text: "half an ans", type: "text-delta"};
1660
+ throw new Error("provider dropped");
1661
+ })(),
1662
+ }) as unknown as StreamTextResult;
1663
+ const agent = await authAsUser(app, "notAdmin");
1664
+ await agent.post("/gpt/prompt").send({prompt: "Break"}).buffer(true).parse(sseCollect);
1665
+ const history = await GptHistory.findExactlyOne({"prompts.text": "Break"});
1666
+ const assistant = history.prompts.find((p) => p.type === "assistant");
1667
+ expect(assistant?.status).toBe("error");
1668
+ expect(assistant?.text).toBe("half an ans");
1669
+ });
1670
+
1671
+ it("drops the placeholder when the stream fails before any output", async () => {
1672
+ streamTextOverride = () =>
1673
+ ({
1674
+ files: Promise.resolve([]),
1675
+ fullStream: (async function* () {
1676
+ yield {type: "start-step"};
1677
+ throw new Error("provider dropped");
1678
+ })(),
1679
+ }) as unknown as StreamTextResult;
1680
+ const agent = await authAsUser(app, "notAdmin");
1681
+ await agent.post("/gpt/prompt").send({prompt: "Nothing"}).buffer(true).parse(sseCollect);
1682
+ const history = await GptHistory.findExactlyOne({"prompts.text": "Nothing"});
1683
+ expect(history.prompts.map((p) => p.type)).toEqual(["user"]);
1684
+ });
1685
+
1686
+ it("resumes an in-flight reply and finishes when it completes", async () => {
1687
+ const history = await GptHistory.create({
1688
+ prompts: [
1689
+ {text: "Q", type: "user"},
1690
+ {status: "streaming", streamId: "s1", text: "Hello", type: "assistant"},
1691
+ ],
1692
+ userId: await getNotAdminId(),
1693
+ });
1694
+ const agent = await authAsUser(buildApp({streamResumePollIntervalMs: 10}), "notAdmin");
1695
+ setTimeout(() => {
1696
+ void GptHistory.updateOne(
1697
+ {_id: history._id},
1698
+ {
1699
+ $set: {
1700
+ "prompts.1.content": [{mimeType: "image/png", type: "image", url: "https://x/i.png"}],
1701
+ "prompts.1.status": "complete",
1702
+ "prompts.1.text": "Hello world",
1703
+ title: "Greeting",
1704
+ },
1705
+ }
1706
+ ).exec();
1707
+ }, 60);
1708
+ const res = await agent
1709
+ .get(`/gpt/histories/${history._id}/stream`)
1710
+ .buffer(true)
1711
+ .parse(sseCollect);
1712
+ expect(res.status).toBe(200);
1713
+ const events = (res as SseResponse).body
1714
+ .split("\n\n")
1715
+ .filter(Boolean)
1716
+ .map((e) => JSON.parse(e.slice(6)));
1717
+ expect(events[0]).toEqual({historyId: history._id.toString(), resumed: true, streamId: "s1"});
1718
+ expect(events.filter((e) => e.text).map((e) => e.text)).toEqual(["Hello", " world"]);
1719
+ expect(events).toContainEqual({image: {mimeType: "image/png", url: "https://x/i.png"}});
1720
+ expect(events[events.length - 1]).toEqual({
1721
+ done: true,
1722
+ historyId: history._id.toString(),
1723
+ title: "Greeting",
1724
+ });
1725
+ });
1726
+
1727
+ it("skips text the client already shows via offset", async () => {
1728
+ const history = await GptHistory.create({
1729
+ prompts: [
1730
+ {text: "Q", type: "user"},
1731
+ {status: "complete", streamId: "s2", text: "Hello world", type: "assistant"},
1732
+ ],
1733
+ userId: await getNotAdminId(),
1734
+ });
1735
+ const agent = await authAsUser(app, "notAdmin");
1736
+ const res = await agent
1737
+ .get(`/gpt/histories/${history._id}/stream?streamId=s2&offset=5`)
1738
+ .buffer(true)
1739
+ .parse(sseCollect);
1740
+ const body = (res as SseResponse).body;
1741
+ assert.include(body, '"replace":true,"text":"Hello world"');
1742
+ });
1743
+
1744
+ it("replaces a stale client placeholder on the first resume poll", async () => {
1745
+ const history = await GptHistory.create({
1746
+ prompts: [
1747
+ {text: "Q", type: "user"},
1748
+ {status: "complete", streamId: "s2", text: "Answer", type: "assistant"},
1749
+ ],
1750
+ userId: await getNotAdminId(),
1751
+ });
1752
+ const agent = await authAsUser(app, "notAdmin");
1753
+ const res = await agent
1754
+ .get(`/gpt/histories/${history._id}/stream?streamId=s2&offset=12`)
1755
+ .buffer(true)
1756
+ .parse(sseCollect);
1757
+
1758
+ assert.include((res as SseResponse).body, '"replace":true,"text":"Answer"');
1759
+ });
1760
+
1761
+ it("sends a replace event when persisted text is rewritten", async () => {
1762
+ const history = await GptHistory.create({
1763
+ prompts: [
1764
+ {text: "Q", type: "user"},
1765
+ {status: "streaming", streamId: "s3", text: "thinking...", type: "assistant"},
1766
+ ],
1767
+ userId: await getNotAdminId(),
1768
+ });
1769
+ const agent = await authAsUser(buildApp({streamResumePollIntervalMs: 10}), "notAdmin");
1770
+ setTimeout(() => {
1771
+ void GptHistory.updateOne(
1772
+ {_id: history._id},
1773
+ {$set: {"prompts.1.status": "complete", "prompts.1.text": "Answer"}}
1774
+ ).exec();
1775
+ }, 60);
1776
+ const res = await agent
1777
+ .get(`/gpt/histories/${history._id}/stream`)
1778
+ .buffer(true)
1779
+ .parse(sseCollect);
1780
+ expect((res as SseResponse).body).toContain('"replace":true,"text":"Answer"');
1781
+ });
1782
+
1783
+ it("finishes immediately when nothing is streaming", async () => {
1784
+ const history = await GptHistory.create({
1785
+ prompts: [{text: "Q", type: "user"}],
1786
+ userId: await getNotAdminId(),
1787
+ });
1788
+ const agent = await authAsUser(app, "notAdmin");
1789
+ const res = await agent
1790
+ .get(`/gpt/histories/${history._id}/stream`)
1791
+ .buffer(true)
1792
+ .parse(sseCollect);
1793
+ const body = (res as SseResponse).body;
1794
+ expect(body).toContain('"resumed":true');
1795
+ expect(body).toContain('"done":true');
1796
+ });
1797
+
1798
+ it("marks a stale streaming reply as interrupted", async () => {
1799
+ const history = await GptHistory.create({
1800
+ prompts: [
1801
+ {text: "Q", type: "user"},
1802
+ {status: "streaming", streamId: "s4", text: "Par", type: "assistant"},
1803
+ ],
1804
+ userId: await getNotAdminId(),
1805
+ });
1806
+ await new Promise((resolve) => setTimeout(resolve, 20));
1807
+ const agent = await authAsUser(buildApp({streamStaleAfterMs: 5}), "notAdmin");
1808
+ const res = await agent
1809
+ .get(`/gpt/histories/${history._id}/stream`)
1810
+ .buffer(true)
1811
+ .parse(sseCollect);
1812
+ const body = (res as SseResponse).body;
1813
+ expect(body).toContain("interrupted");
1814
+ expect(body).toContain('"done":true');
1815
+ const updated = await GptHistory.findExactlyOne({_id: history._id});
1816
+ expect(updated.prompts[1].status).toBe("error");
1817
+ });
1818
+
1819
+ it("reports an interrupted reply that already ended in error", async () => {
1820
+ const history = await GptHistory.create({
1821
+ prompts: [
1822
+ {text: "Q", type: "user"},
1823
+ {status: "error", streamId: "s5", text: "Par", type: "assistant"},
1824
+ ],
1825
+ userId: await getNotAdminId(),
1826
+ });
1827
+ const agent = await authAsUser(app, "notAdmin");
1828
+ const res = await agent
1829
+ .get(`/gpt/histories/${history._id}/stream?streamId=s5`)
1830
+ .buffer(true)
1831
+ .parse(sseCollect);
1832
+ expect((res as SseResponse).body).toContain("interrupted");
1833
+ });
1834
+
1835
+ it("rejects resume for other users and missing histories", async () => {
1836
+ const history = await GptHistory.create({
1837
+ prompts: [{text: "Q", type: "user"}],
1838
+ userId: await getNotAdminId(),
1839
+ });
1840
+ const admin = await authAsUser(app, "admin");
1841
+ const forbidden = await admin.get(`/gpt/histories/${history._id}/stream`);
1842
+ expect(forbidden.status).toBe(403);
1843
+ const missing = await admin.get(
1844
+ `/gpt/histories/${new mongoose.Types.ObjectId().toString()}/stream`
1845
+ );
1846
+ expect(missing.status).toBe(404);
1847
+ });
1848
+ });
1849
+
1245
1850
  describe("GPT Prompt error handling", () => {
1851
+ it("preserves completed tool spans when the stream later fails", async () => {
1852
+ const sink = new MemoryTraceSink();
1853
+ const plugin: ObservabilityPlugin = {
1854
+ capabilities: new Set([
1855
+ "datasets",
1856
+ "experiments",
1857
+ "prompts",
1858
+ "reviewQueue",
1859
+ "scores",
1860
+ "traces",
1861
+ ]),
1862
+ datasetStore: {},
1863
+ experimentRunner: {},
1864
+ id: "local",
1865
+ promptRegistry: {get: async () => undefined},
1866
+ reviewQueue: {},
1867
+ traceSink: sink,
1868
+ };
1869
+ new ObservabilityApp({plugins: [plugin]});
1870
+ streamTextOverride = () =>
1871
+ ({
1872
+ files: Promise.resolve([]),
1873
+ fullStream: (async function* () {
1874
+ yield {type: "start-step"};
1875
+ yield {
1876
+ input: {query: "before failure"},
1877
+ toolCallId: "tc-error",
1878
+ toolName: "search",
1879
+ type: "tool-call",
1880
+ };
1881
+ yield {
1882
+ output: {items: ["one"]},
1883
+ toolCallId: "tc-error",
1884
+ toolName: "search",
1885
+ type: "tool-result",
1886
+ };
1887
+ throw new Error("stream failed after tool");
1888
+ })(),
1889
+ }) as unknown as StreamTextResult;
1890
+ const errApp = new TerrenoApp({
1891
+ configureApp: (router, options) => {
1892
+ addGptRoutes(router, {
1893
+ aiService,
1894
+ openApiOptions: options,
1895
+ tools: {search: {description: "Search"} as unknown as Tool},
1896
+ });
1897
+ },
1898
+ skipListen: true,
1899
+ userModel: UserModel,
1900
+ }).build();
1901
+ const agent = await authAsUser(errApp, "notAdmin");
1902
+
1903
+ const res = await agent
1904
+ .post("/gpt/prompt")
1905
+ .send({prompt: "Search, then fail"})
1906
+ .buffer(true)
1907
+ .parse(sseCollect);
1908
+
1909
+ assert.equal(res.status, 200);
1910
+ assert.equal(sink.traces.length, 1);
1911
+ assert.equal(sink.traces[0]?.status, "error");
1912
+ const toolSpan = sink.traces[0]?.spans.find((span) => span.kind === "TOOL");
1913
+ assert.deepEqual(toolSpan?.input, {query: "before failure"});
1914
+ assert.deepEqual(toolSpan?.output, {items: ["one"]});
1915
+ });
1916
+
1246
1917
  it("sends an outer SSE error when error handling itself fails after streaming starts", async () => {
1247
1918
  streamTextOverride = () =>
1248
1919
  ({