@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
@@ -0,0 +1,933 @@
1
+ import {afterEach, beforeAll, describe, expect, it, mock} from "bun:test";
2
+ import {
3
+ configureOpenApiValidator,
4
+ type ModelRouterOptions,
5
+ Permissions,
6
+ resetOpenApiValidatorConfig,
7
+ TerrenoApp,
8
+ z,
9
+ } from "@terreno/api";
10
+ import {askJsonSchemas, pendingAskListSchema, turnResultSchema} from "@terreno/blocks";
11
+ import type {LanguageModel} from "ai";
12
+ import {DateTime} from "luxon";
13
+
14
+ import {AIRequest} from "../models/aiRequest";
15
+ import {GptHistory} from "../models/gptHistory";
16
+ import {AIService} from "../service/aiService";
17
+ import {COMPACT_SURFACE_SYSTEM_PROMPT} from "../service/prompts";
18
+ import {
19
+ type Agent,
20
+ buildApp,
21
+ conversationOf,
22
+ createScriptedModel,
23
+ deferred,
24
+ failingTextStep,
25
+ loadHistory,
26
+ modelCall,
27
+ PLAN_ASK_CALL,
28
+ PLAN_ASK_ROW,
29
+ PLAN_SIMPLE_CARD,
30
+ REGION_ASK_CALL,
31
+ REGION_SIMPLE_CARD,
32
+ rowsOf,
33
+ serveApp,
34
+ systemPromptOf,
35
+ TEAM_ANSWER,
36
+ textStep,
37
+ toolCallStep,
38
+ toolNamesOf,
39
+ USER_PROMPT,
40
+ } from "../tests/chatHarness";
41
+ import {authAsUser, authAsUserWithCredentials, ensureTestUsers, UserModel} from "../tests/helpers";
42
+ import type {GptHistoryDocument} from "../types";
43
+ import {addGptHistoryRoutes} from "./gptHistories";
44
+
45
+ const OTHER_USER = {admin: false, email: "other@example.com", name: "Other", password: "password"};
46
+
47
+ const TEAM_ANSWER_ROW = {
48
+ result: TEAM_ANSWER,
49
+ text: "Tool result: ask_choice",
50
+ toolCallId: "call_plan",
51
+ toolName: "ask_choice",
52
+ type: "tool-result",
53
+ };
54
+
55
+ const TEAM_REPLY = "Setting up the Team plan.";
56
+
57
+ const ANSWERED_PLAN_ROWS = [
58
+ {text: USER_PROMPT, type: "user"},
59
+ {...PLAN_ASK_ROW, ask: {kind: "choice", status: "answered"}},
60
+ TEAM_ANSWER_ROW,
61
+ {model: "scripted-model", text: TEAM_REPLY, type: "assistant"},
62
+ ];
63
+
64
+ const TEAM_BUTTON = {buttonId: "option:team", toolCallId: "call_plan"};
65
+
66
+ const createHistory = async (agent: Agent): Promise<string> => {
67
+ const res = await agent.post("/gpt/histories").send({});
68
+ expect(res.status).toBe(201);
69
+ return res.body.data._id;
70
+ };
71
+
72
+ const postTurn = (agent: Agent, historyId: string, body: Record<string, unknown>) =>
73
+ agent.post(`/gpt/histories/${historyId}/turn`).send(body);
74
+
75
+ /** Creates a history and runs a headless turn that pauses on the plan ask. */
76
+ const pauseOnPlanAsk = async (agent: Agent): Promise<string> => {
77
+ const historyId = await createHistory(agent);
78
+ const res = await postTurn(agent, historyId, {prompt: USER_PROMPT});
79
+ expect(res.body.data.pendingAsk?.toolCallId).toBe("call_plan");
80
+ return historyId;
81
+ };
82
+
83
+ /** Reloads the history until the title is saved, the last write of a turn that replied. */
84
+ const waitForTitle = async (historyId: string): Promise<GptHistoryDocument> => {
85
+ const deadline = DateTime.now().plus({seconds: 5});
86
+ while (DateTime.now() < deadline) {
87
+ const history = await loadHistory(historyId);
88
+ if (history.title) {
89
+ return history;
90
+ }
91
+ await new Promise((resolve) => setTimeout(resolve, 10));
92
+ }
93
+ throw new Error(`History ${historyId} never saved a title`);
94
+ };
95
+
96
+ describe("/gpt/histories headless turns", () => {
97
+ beforeAll(async () => {
98
+ await ensureTestUsers();
99
+ await ensureTestUsers([OTHER_USER]);
100
+ await GptHistory.deleteMany({});
101
+ });
102
+
103
+ afterEach(async () => {
104
+ await AIRequest.deleteMany({});
105
+ await GptHistory.deleteMany({});
106
+ });
107
+
108
+ describe("POST /gpt/histories/:id/turn", () => {
109
+ it("pauses on an ask with its simple card, then resumes with the pressed button's answer", async () => {
110
+ const model = createScriptedModel({
111
+ steps: [toolCallStep(PLAN_ASK_CALL), textStep(TEAM_REPLY)],
112
+ });
113
+ const agent = await authAsUser(buildApp({asks: true, model}), "notAdmin");
114
+ const historyId = await createHistory(agent);
115
+
116
+ const asked = await postTurn(agent, historyId, {prompt: USER_PROMPT, surface: "compact"});
117
+ const answered = await postTurn(agent, historyId, {...TEAM_BUTTON, surface: "compact"});
118
+
119
+ expect(asked.status).toBe(200);
120
+ expect(asked.body.data).toEqual({
121
+ historyId,
122
+ pendingAsk: {kind: "choice", simple: PLAN_SIMPLE_CARD, toolCallId: "call_plan"},
123
+ text: "",
124
+ });
125
+ expect(answered.status).toBe(200);
126
+ expect(answered.body.data).toEqual({historyId, text: TEAM_REPLY, title: "Workspace setup"});
127
+ for (const res of [asked, answered]) {
128
+ expect(turnResultSchema.parse(res.body.data)).toEqual(res.body.data);
129
+ }
130
+ expect(conversationOf(modelCall(model, 1)).at(-1)).toEqual({
131
+ content: [
132
+ {
133
+ output: {type: "json", value: TEAM_ANSWER},
134
+ toolCallId: "call_plan",
135
+ toolName: "ask_choice",
136
+ type: "tool-result",
137
+ },
138
+ ],
139
+ role: "tool",
140
+ });
141
+ for (const index of [0, 1]) {
142
+ const call = modelCall(model, index);
143
+ expect(toolNamesOf(call)).toEqual(["ask_choice", "ask_confirm"]);
144
+ expect(call.tools?.[0]?.inputSchema.properties).toMatchObject({options: {maxItems: 3}});
145
+ expect(systemPromptOf(call)).toEndWith(COMPACT_SURFACE_SYSTEM_PROMPT);
146
+ }
147
+ const history = await loadHistory(historyId);
148
+ expect(history.pendingAsk).toBeUndefined();
149
+ expect(rowsOf(history)).toEqual(ANSWERED_PLAN_ROWS);
150
+ });
151
+
152
+ it.each([
153
+ {buttonId: "approve", confirmed: true, reply: "Sent the weekly report."},
154
+ {buttonId: "deny", confirmed: false, reply: "I did not send it."},
155
+ ])(
156
+ "pauses a compact turn on a confirm card without handoff and resumes with the $buttonId button",
157
+ async ({buttonId, confirmed, reply}) => {
158
+ const reportInput = {
159
+ confirmLabel: "Send report",
160
+ denyLabel: "Not now",
161
+ prompt: "Send the weekly report to the team now?",
162
+ };
163
+ const model = createScriptedModel({
164
+ steps: [
165
+ toolCallStep({input: reportInput, toolCallId: "call_report", toolName: "ask_confirm"}),
166
+ textStep(reply),
167
+ ],
168
+ });
169
+ const agent = await authAsUser(buildApp({asks: true, model}), "notAdmin");
170
+ const historyId = await createHistory(agent);
171
+
172
+ const asked = await postTurn(agent, historyId, {
173
+ prompt: "Send the weekly report",
174
+ surface: "compact",
175
+ });
176
+ const answered = await postTurn(agent, historyId, {
177
+ buttonId,
178
+ surface: "compact",
179
+ toolCallId: "call_report",
180
+ });
181
+
182
+ expect(asked.body.data).toEqual({
183
+ historyId,
184
+ pendingAsk: {
185
+ kind: "confirm",
186
+ simple: {
187
+ buttons: [
188
+ {
189
+ id: "approve",
190
+ label: "Send report",
191
+ response: {action: "accept", content: {confirmed: true}},
192
+ style: "primary",
193
+ },
194
+ {
195
+ id: "deny",
196
+ label: "Not now",
197
+ response: {action: "accept", content: {confirmed: false}},
198
+ style: "cancel",
199
+ },
200
+ ],
201
+ handoff: false,
202
+ kind: "confirm",
203
+ text: "Send the weekly report to the team now?",
204
+ toolCallId: "call_report",
205
+ },
206
+ toolCallId: "call_report",
207
+ },
208
+ text: "",
209
+ });
210
+ expect(turnResultSchema.parse(asked.body.data)).toEqual(asked.body.data);
211
+ expect(answered.body.data).toEqual({historyId, text: reply, title: "Workspace setup"});
212
+ expect(conversationOf(modelCall(model, 1)).at(-1)).toEqual({
213
+ content: [
214
+ {
215
+ output: {type: "json", value: {action: "accept", content: {confirmed}}},
216
+ toolCallId: "call_report",
217
+ toolName: "ask_confirm",
218
+ type: "tool-result",
219
+ },
220
+ ],
221
+ role: "tool",
222
+ });
223
+ }
224
+ );
225
+
226
+ it.each([
227
+ {
228
+ answer: {action: "accept", content: {changed: false, markdown: "Ship it on Friday."}},
229
+ buttonId: "approve",
230
+ reply: "Posted the draft as is.",
231
+ },
232
+ {answer: {action: "decline"}, buttonId: "cancel", reply: "I left the draft alone."},
233
+ ])(
234
+ "pauses on a markdown card with handoff and resumes with the $buttonId button",
235
+ async ({answer, buttonId, reply}) => {
236
+ const draftInput = {initial: "Ship it on Friday.", prompt: "Edit the release note."};
237
+ const model = createScriptedModel({
238
+ steps: [
239
+ toolCallStep({input: draftInput, toolCallId: "call_note", toolName: "ask_markdown"}),
240
+ textStep(reply),
241
+ ],
242
+ });
243
+ const agent = await authAsUser(buildApp({asks: true, model}), "notAdmin");
244
+ const historyId = await createHistory(agent);
245
+
246
+ const asked = await postTurn(agent, historyId, {prompt: "Draft a release note"});
247
+ const answered = await postTurn(agent, historyId, {buttonId, toolCallId: "call_note"});
248
+
249
+ expect(asked.body.data).toEqual({
250
+ historyId,
251
+ pendingAsk: {
252
+ kind: "markdown",
253
+ simple: {
254
+ buttons: [
255
+ {
256
+ id: "approve",
257
+ label: "Approve draft",
258
+ response: {
259
+ action: "accept",
260
+ content: {changed: false, markdown: "Ship it on Friday."},
261
+ },
262
+ style: "primary",
263
+ },
264
+ {id: "cancel", label: "Cancel", response: {action: "decline"}, style: "cancel"},
265
+ ],
266
+ handoff: true,
267
+ kind: "markdown",
268
+ text: "Edit the release note.",
269
+ toolCallId: "call_note",
270
+ },
271
+ toolCallId: "call_note",
272
+ },
273
+ text: "",
274
+ });
275
+ expect(turnResultSchema.parse(asked.body.data)).toEqual(asked.body.data);
276
+ expect(answered.body.data).toEqual({historyId, text: reply, title: "Workspace setup"});
277
+ expect(conversationOf(modelCall(model, 1)).at(-1)).toEqual({
278
+ content: [
279
+ {
280
+ output: {type: "json", value: answer},
281
+ toolCallId: "call_note",
282
+ toolName: "ask_markdown",
283
+ type: "tool-result",
284
+ },
285
+ ],
286
+ role: "tool",
287
+ });
288
+ }
289
+ );
290
+
291
+ it.each([
292
+ {
293
+ answer: {action: "accept", content: {values: {email: "billing@acme.test", seats: 5}}},
294
+ buttonId: "submit-defaults",
295
+ reply: "Sent the invoice to billing@acme.test.",
296
+ },
297
+ {answer: {action: "decline"}, buttonId: "cancel", reply: "I left the invoice alone."},
298
+ ])(
299
+ "pauses on a form card with handoff and resumes with the $buttonId button",
300
+ async ({answer, buttonId, reply}) => {
301
+ const formInput = {
302
+ fields: [
303
+ {
304
+ default: "billing@acme.test",
305
+ id: "email",
306
+ label: "Email",
307
+ required: true,
308
+ type: "email",
309
+ },
310
+ {default: 5, id: "seats", integer: true, label: "Seats", min: 1, type: "number"},
311
+ {id: "po", label: "PO number", type: "text"},
312
+ ],
313
+ prompt: "Where should I send the invoice?",
314
+ };
315
+ const model = createScriptedModel({
316
+ steps: [
317
+ toolCallStep({input: formInput, toolCallId: "call_invoice", toolName: "ask_form"}),
318
+ textStep(reply),
319
+ ],
320
+ });
321
+ const agent = await authAsUser(buildApp({asks: true, model}), "notAdmin");
322
+ const historyId = await createHistory(agent);
323
+
324
+ const asked = await postTurn(agent, historyId, {prompt: "Invoice details"});
325
+ const answered = await postTurn(agent, historyId, {buttonId, toolCallId: "call_invoice"});
326
+
327
+ expect(asked.body.data).toEqual({
328
+ historyId,
329
+ pendingAsk: {
330
+ kind: "form",
331
+ simple: {
332
+ buttons: [
333
+ {
334
+ id: "submit-defaults",
335
+ label: "Submit defaults",
336
+ response: {
337
+ action: "accept",
338
+ content: {values: {email: "billing@acme.test", seats: 5}},
339
+ },
340
+ style: "primary",
341
+ },
342
+ {id: "cancel", label: "Cancel", response: {action: "decline"}, style: "cancel"},
343
+ ],
344
+ handoff: true,
345
+ kind: "form",
346
+ text: "Where should I send the invoice?",
347
+ toolCallId: "call_invoice",
348
+ },
349
+ toolCallId: "call_invoice",
350
+ },
351
+ text: "",
352
+ });
353
+ expect(turnResultSchema.parse(asked.body.data)).toEqual(asked.body.data);
354
+ expect(answered.body.data).toEqual({historyId, text: reply, title: "Workspace setup"});
355
+ expect(conversationOf(modelCall(model, 1)).at(-1)).toEqual({
356
+ content: [
357
+ {
358
+ output: {type: "json", value: answer},
359
+ toolCallId: "call_invoice",
360
+ toolName: "ask_form",
361
+ type: "tool-result",
362
+ },
363
+ ],
364
+ role: "tool",
365
+ });
366
+ }
367
+ );
368
+
369
+ it("resumes with a full askResponse, as a client that renders the ask sends it", async () => {
370
+ const model = createScriptedModel({
371
+ steps: [toolCallStep(PLAN_ASK_CALL), textStep(TEAM_REPLY)],
372
+ });
373
+ const agent = await authAsUser(buildApp({asks: true, model}), "notAdmin");
374
+ const historyId = await pauseOnPlanAsk(agent);
375
+
376
+ const res = await postTurn(agent, historyId, {
377
+ askResponse: {toolCallId: "call_plan", ...TEAM_ANSWER},
378
+ });
379
+
380
+ expect(res.status).toBe(200);
381
+ expect(res.body.data).toEqual({historyId, text: TEAM_REPLY, title: "Workspace setup"});
382
+ expect(rowsOf(await loadHistory(historyId))).toEqual(ANSWERED_PLAN_ROWS);
383
+ expect(systemPromptOf(modelCall(model, 1))).not.toContain(COMPACT_SURFACE_SYSTEM_PROMPT);
384
+ });
385
+
386
+ it("cancels the pending ask when a prompt arrives, and returns the reply", async () => {
387
+ const model = createScriptedModel({
388
+ steps: [toolCallStep(PLAN_ASK_CALL), textStep("Team is $20 per seat.")],
389
+ });
390
+ const agent = await authAsUser(buildApp({asks: true, model}), "notAdmin");
391
+ const historyId = await pauseOnPlanAsk(agent);
392
+
393
+ const res = await postTurn(agent, historyId, {prompt: "What does Team cost?"});
394
+
395
+ expect(res.body.data).toEqual({
396
+ historyId,
397
+ text: "Team is $20 per seat.",
398
+ title: "Workspace setup",
399
+ });
400
+ const history = await loadHistory(historyId);
401
+ expect(history.pendingAsk).toBeUndefined();
402
+ expect(rowsOf(history).slice(1, 3)).toEqual([
403
+ {...PLAN_ASK_ROW, ask: {kind: "choice", status: "cancelled"}},
404
+ {...TEAM_ANSWER_ROW, result: {action: "cancel", reason: "user_sent_message"}},
405
+ ]);
406
+ });
407
+
408
+ it("returns the error and the ask again when the model fails before streaming, so the button can be pressed again", async () => {
409
+ const model = createScriptedModel({steps: [toolCallStep(PLAN_ASK_CALL)]});
410
+ const agent = await authAsUser(buildApp({asks: true, model}), "notAdmin");
411
+ const historyId = await pauseOnPlanAsk(agent);
412
+ model.doStream.mockImplementationOnce(async () => {
413
+ throw new Error("The model is overloaded");
414
+ });
415
+
416
+ const res = await postTurn(agent, historyId, TEAM_BUTTON);
417
+
418
+ expect(res.status).toBe(200);
419
+ expect(res.body.data).toEqual({
420
+ error: "The model is overloaded",
421
+ historyId,
422
+ pendingAsk: {kind: "choice", simple: PLAN_SIMPLE_CARD, toolCallId: "call_plan"},
423
+ text: "",
424
+ });
425
+ const history = await loadHistory(historyId);
426
+ expect(history.pendingAsk?.toolCallId).toBe("call_plan");
427
+ expect(rowsOf(history).at(-1)).toEqual({
428
+ ...PLAN_ASK_ROW,
429
+ ask: {kind: "choice", status: "pending"},
430
+ });
431
+ });
432
+
433
+ it("returns the error and keeps the message when the model stream fails mid-reply", async () => {
434
+ const model = createScriptedModel({
435
+ steps: [failingTextStep("Setting up", "Connection reset")],
436
+ });
437
+ const agent = await authAsUser(buildApp({asks: true, model}), "notAdmin");
438
+ const historyId = await createHistory(agent);
439
+
440
+ const res = await postTurn(agent, historyId, {prompt: USER_PROMPT});
441
+
442
+ expect(res.status).toBe(200);
443
+ expect(res.body.data).toEqual({error: "Connection reset", historyId, text: ""});
444
+ expect(rowsOf(await loadHistory(historyId))).toEqual([{text: USER_PROMPT, type: "user"}]);
445
+ });
446
+
447
+ it("finishes and saves the turn when the client disconnects mid-turn", async () => {
448
+ const arrived = deferred();
449
+ const release = deferred();
450
+ const model = createScriptedModel({
451
+ holds: {1: {arrived, release: release.promise}},
452
+ steps: [toolCallStep(PLAN_ASK_CALL), textStep(TEAM_REPLY)],
453
+ });
454
+ const served = await serveApp(buildApp({asks: true, model}));
455
+ try {
456
+ const agent = await authAsUser(served.url, "notAdmin");
457
+ const historyId = await pauseOnPlanAsk(agent);
458
+ const request = postTurn(agent, historyId, {...TEAM_BUTTON, surface: "compact"});
459
+ const outcome = request.then(
460
+ () => "responded",
461
+ (error: Error) => error.message
462
+ );
463
+ await arrived.promise;
464
+
465
+ request.abort();
466
+
467
+ expect(await outcome).toBe("Aborted");
468
+ expect((await loadHistory(historyId)).title).toBeUndefined();
469
+ release.resolve();
470
+ const history = await waitForTitle(historyId);
471
+ expect(history.pendingAsk).toBeUndefined();
472
+ expect(rowsOf(history)).toEqual(ANSWERED_PLAN_ROWS);
473
+ } finally {
474
+ release.resolve();
475
+ await served.close();
476
+ }
477
+ });
478
+ });
479
+
480
+ describe("rejected turns", () => {
481
+ it("returns 400 UNKNOWN_BUTTON for a button that is not on the card, and keeps the ask pending", async () => {
482
+ const model = createScriptedModel({steps: [toolCallStep(PLAN_ASK_CALL)]});
483
+ const agent = await authAsUser(buildApp({asks: true, model}), "notAdmin");
484
+ const historyId = await pauseOnPlanAsk(agent);
485
+
486
+ const res = await postTurn(agent, historyId, {
487
+ buttonId: "option:gold",
488
+ toolCallId: "call_plan",
489
+ });
490
+
491
+ expect(res.status).toBe(400);
492
+ expect(res.body).toEqual({
493
+ code: "UNKNOWN_BUTTON",
494
+ detail: "The button is not on the pending ask's simple card. See fields.",
495
+ meta: {
496
+ fields: [
497
+ {
498
+ code: "UNKNOWN_BUTTON",
499
+ fix: 'Send the id of one of the card\'s buttons: "option:team", "option:starter", "option:enterprise".',
500
+ message: 'Button "option:gold" is not on the pending ask\'s simple card.',
501
+ path: "buttonId",
502
+ },
503
+ ],
504
+ },
505
+ requestId: expect.any(String),
506
+ status: 400,
507
+ title: "Unknown buttonId",
508
+ });
509
+ expect(model.doStream).toHaveBeenCalledTimes(1);
510
+ expect((await loadHistory(historyId)).pendingAsk?.toolCallId).toBe("call_plan");
511
+ });
512
+
513
+ it("returns 409 for a toolCallId that is not the pending ask, and for an ask already answered", async () => {
514
+ const model = createScriptedModel({
515
+ steps: [toolCallStep(PLAN_ASK_CALL), textStep(TEAM_REPLY)],
516
+ });
517
+ const agent = await authAsUser(buildApp({asks: true, model}), "notAdmin");
518
+ const historyId = await pauseOnPlanAsk(agent);
519
+
520
+ const wrongAsk = await postTurn(agent, historyId, {...TEAM_BUTTON, toolCallId: "call_old"});
521
+ await postTurn(agent, historyId, TEAM_BUTTON);
522
+ const answeredTwice = await postTurn(agent, historyId, TEAM_BUTTON);
523
+ const answerAfterAnswer = await postTurn(agent, historyId, {
524
+ askResponse: {toolCallId: "call_plan", ...TEAM_ANSWER},
525
+ });
526
+
527
+ expect(wrongAsk.status).toBe(409);
528
+ expect(wrongAsk.body).toEqual({
529
+ detail: "Tool call call_old is not the ask this conversation is waiting on.",
530
+ requestId: expect.any(String),
531
+ status: 409,
532
+ title: "This ask is no longer pending",
533
+ });
534
+ for (const res of [answeredTwice, answerAfterAnswer]) {
535
+ expect(res.status).toBe(409);
536
+ expect(res.body.title).toBe("This ask is no longer pending");
537
+ }
538
+ expect(model.doStream).toHaveBeenCalledTimes(2);
539
+ });
540
+
541
+ it("returns 403 to another user and to an admin, without running the turn", async () => {
542
+ const model = createScriptedModel({steps: [toolCallStep(PLAN_ASK_CALL)]});
543
+ const app = buildApp({asks: true, model});
544
+ const owner = await authAsUser(app, "notAdmin");
545
+ const historyId = await pauseOnPlanAsk(owner);
546
+ const otherUser = await authAsUserWithCredentials(app, OTHER_USER);
547
+ const admin = await authAsUser(app, "admin");
548
+
549
+ const fromOtherUser = await postTurn(otherUser, historyId, TEAM_BUTTON);
550
+ const fromAdmin = await postTurn(admin, historyId, TEAM_BUTTON);
551
+
552
+ expect(fromOtherUser.status).toBe(403);
553
+ expect(fromOtherUser.body).toMatchObject({
554
+ code: "action-access-denied",
555
+ title: "Access denied",
556
+ });
557
+ expect(fromAdmin.status).toBe(403);
558
+ expect(fromAdmin.body.title).toBe("Not authorized to access this history");
559
+ expect(model.doStream).toHaveBeenCalledTimes(1);
560
+ expect((await loadHistory(historyId)).pendingAsk?.toolCallId).toBe("call_plan");
561
+ });
562
+
563
+ it("returns 404 for an unknown history", async () => {
564
+ const model = createScriptedModel({steps: []});
565
+ const agent = await authAsUser(buildApp({asks: true, model}), "notAdmin");
566
+
567
+ const res = await postTurn(agent, "6710c2a1f1e2d3c4b5a69788", {prompt: "Hi"});
568
+
569
+ expect(res.status).toBe(404);
570
+ expect(model.doStream).not.toHaveBeenCalled();
571
+ });
572
+
573
+ it("returns 400 with fields for an askResponse that does not fit the ask", async () => {
574
+ const model = createScriptedModel({steps: [toolCallStep(PLAN_ASK_CALL)]});
575
+ const agent = await authAsUser(buildApp({asks: true, model}), "notAdmin");
576
+ const historyId = await pauseOnPlanAsk(agent);
577
+
578
+ const res = await postTurn(agent, historyId, {
579
+ askResponse: {action: "accept", content: {selected: ["gold"]}, toolCallId: "call_plan"},
580
+ });
581
+
582
+ expect(res.status).toBe(400);
583
+ expect(res.body).toEqual({
584
+ detail: "The answer does not match the ask. See fields.",
585
+ meta: {
586
+ fields: [
587
+ {
588
+ code: "OPTION_NOT_OFFERED",
589
+ fix: "Use the id of one of the ask's options.",
590
+ message: '"gold" is not one of the offered options.',
591
+ path: "content.selected[0]",
592
+ },
593
+ ],
594
+ },
595
+ requestId: expect.any(String),
596
+ status: 400,
597
+ title: "Invalid askResponse",
598
+ });
599
+ expect(model.doStream).toHaveBeenCalledTimes(1);
600
+ });
601
+
602
+ const oneShape = "Send only one of prompt, askResponse, or toolCallId with buttonId.";
603
+
604
+ it.each([
605
+ {
606
+ body: {},
607
+ fields: {prompt: "Send one of prompt, askResponse, or toolCallId with buttonId."},
608
+ name: "empty",
609
+ },
610
+ {
611
+ body: {buttonId: "option:team", prompt: "Team"},
612
+ fields: {buttonId: oneShape, prompt: oneShape},
613
+ name: "two turn shapes",
614
+ },
615
+ {
616
+ body: {buttonId: "option:team"},
617
+ fields: {toolCallId: "Send toolCallId with buttonId."},
618
+ name: "a buttonId without its toolCallId",
619
+ },
620
+ {
621
+ body: {prompt: "Hi", surface: "watch"},
622
+ fields: {surface: 'Invalid option: expected one of "full"|"compact"'},
623
+ name: "an unknown surface",
624
+ },
625
+ ])("returns 400 with fields for a body that is $name", async ({body, fields}) => {
626
+ const model = createScriptedModel({steps: []});
627
+ const agent = await authAsUser(buildApp({asks: true, model}), "notAdmin");
628
+ const historyId = await createHistory(agent);
629
+
630
+ const res = await postTurn(agent, historyId, body);
631
+
632
+ expect(res.status).toBe(400);
633
+ expect(res.body).toEqual({
634
+ code: "action-body-validation-failed",
635
+ detail: "The request body did not match the action's schema",
636
+ meta: {fields},
637
+ requestId: expect.any(String),
638
+ status: 400,
639
+ title: "Validation failed",
640
+ });
641
+ expect(model.doStream).not.toHaveBeenCalled();
642
+ });
643
+
644
+ it("rejects a body with a key the turn does not define", async () => {
645
+ const model = createScriptedModel({steps: []});
646
+ const agent = await authAsUser(buildApp({asks: true, model}), "notAdmin");
647
+ const historyId = await createHistory(agent);
648
+
649
+ const res = await postTurn(agent, historyId, {buttonID: "option:team", prompt: "Hi"});
650
+
651
+ expect(res.status).toBe(400);
652
+ expect(res.body.title).toBe("Validation failed");
653
+ expect(model.doStream).not.toHaveBeenCalled();
654
+ });
655
+ });
656
+
657
+ describe("GET /gpt/histories/pendingAsks", () => {
658
+ it("lists the caller's pending asks, newest first, without other users' or deleted conversations", async () => {
659
+ const model = createScriptedModel({
660
+ steps: [
661
+ textStep("Hello."),
662
+ toolCallStep(PLAN_ASK_CALL),
663
+ toolCallStep(REGION_ASK_CALL),
664
+ toolCallStep(PLAN_ASK_CALL),
665
+ toolCallStep(PLAN_ASK_CALL),
666
+ textStep("Hello."),
667
+ ],
668
+ });
669
+ const app = buildApp({asks: true, model});
670
+ const owner = await authAsUser(app, "notAdmin");
671
+ const otherUser = await authAsUserWithCredentials(app, OTHER_USER);
672
+ const empty = await owner.get("/gpt/histories/pendingAsks");
673
+ const titled = await createHistory(owner);
674
+ await postTurn(owner, titled, {prompt: "Hi"});
675
+ await postTurn(owner, titled, {prompt: USER_PROMPT});
676
+ const newest = await createHistory(owner);
677
+ await postTurn(owner, newest, {prompt: "Where should my data live?"});
678
+ await pauseOnPlanAsk(otherUser);
679
+ const deleted = await pauseOnPlanAsk(owner);
680
+ await GptHistory.updateOne({_id: deleted}, {deleted: true});
681
+ const answered = await createHistory(owner);
682
+ await postTurn(owner, answered, {prompt: "Hi"});
683
+
684
+ const res = await owner.get("/gpt/histories/pendingAsks");
685
+
686
+ expect(empty.status).toBe(200);
687
+ expect(empty.body.data).toEqual([]);
688
+ expect(res.status).toBe(200);
689
+ expect(res.body.data).toEqual([
690
+ {
691
+ created: expect.any(String),
692
+ historyId: newest,
693
+ kind: "choice",
694
+ simple: REGION_SIMPLE_CARD,
695
+ toolCallId: "call_region",
696
+ },
697
+ {
698
+ created: expect.any(String),
699
+ historyId: titled,
700
+ kind: "choice",
701
+ simple: PLAN_SIMPLE_CARD,
702
+ title: "Workspace setup",
703
+ toolCallId: "call_plan",
704
+ },
705
+ ]);
706
+ expect(pendingAskListSchema.parse(res.body.data)).toEqual(res.body.data);
707
+ const [newer, older] = res.body.data.map((item: {created: string}) =>
708
+ DateTime.fromISO(item.created)
709
+ );
710
+ expect(newer > older).toBe(true);
711
+ expect(newer.diffNow().as("minutes")).toBeGreaterThan(-1);
712
+ });
713
+ });
714
+
715
+ describe("OpenAPI", () => {
716
+ it("documents pendingAsks and turn with their request and response schemas", async () => {
717
+ const agent = await authAsUser(
718
+ buildApp({asks: true, model: createScriptedModel({steps: []})}),
719
+ "notAdmin"
720
+ );
721
+
722
+ const res = await agent.get("/openapi.json");
723
+
724
+ const pendingAsks = res.body.paths["/gpt/histories/pendingAsks"]?.get;
725
+ const turn = res.body.paths["/gpt/histories/{id}/turn"]?.post;
726
+ expect(pendingAsks).toMatchObject({
727
+ operationId: "gpthistories_pendingAsks",
728
+ summary: "List the caller's pending asks",
729
+ });
730
+ expect(turn).toMatchObject({
731
+ operationId: "gpthistories_turn",
732
+ summary: "Run a chat turn and return the reply as JSON",
733
+ });
734
+ const turnBody = turn.requestBody.content["application/json"].schema;
735
+ expect(Object.keys(turnBody.properties).sort()).toEqual([
736
+ "askResponse",
737
+ "buttonId",
738
+ "prompt",
739
+ "surface",
740
+ "toolCallId",
741
+ ]);
742
+ expect(turnBody.properties.surface).toMatchObject({enum: ["full", "compact"]});
743
+ const turnData = turn.responses["200"].content["application/json"].schema.properties.data;
744
+ expect(Object.keys(turnData.properties).sort()).toEqual([
745
+ "error",
746
+ "historyId",
747
+ "pendingAsk",
748
+ "text",
749
+ "title",
750
+ ]);
751
+ const pendingAsksData =
752
+ pendingAsks.responses["200"].content["application/json"].schema.properties.data;
753
+ expect(pendingAsksData.type).toBe("array");
754
+ expect(Object.keys(pendingAsksData.items.properties).sort()).toEqual([
755
+ "created",
756
+ "historyId",
757
+ "kind",
758
+ "simple",
759
+ "title",
760
+ "toolCallId",
761
+ ]);
762
+ });
763
+ });
764
+
765
+ describe("published JSON Schemas", () => {
766
+ /** Rebuilds a published document as a validator, standing in for a native client's decoder. */
767
+ const validatorFor = (fileName: string) => {
768
+ const document = askJsonSchemas()[fileName];
769
+ if (!document) {
770
+ throw new Error(`@terreno/blocks publishes no ${fileName}.`);
771
+ }
772
+ return z.fromJSONSchema(document as Parameters<typeof z.fromJSONSchema>[0]);
773
+ };
774
+
775
+ it("describe the whole response bodies of turn and pendingAsks, requestId included", async () => {
776
+ const model = createScriptedModel({
777
+ steps: [toolCallStep(PLAN_ASK_CALL), textStep(TEAM_REPLY)],
778
+ });
779
+ const agent = await authAsUser(buildApp({asks: true, model}), "notAdmin");
780
+ const historyId = await createHistory(agent);
781
+
782
+ const asked = await postTurn(agent, historyId, {prompt: USER_PROMPT, surface: "compact"});
783
+ const listed = await agent.get("/gpt/histories/pendingAsks");
784
+ const answered = await postTurn(agent, historyId, {...TEAM_BUTTON, surface: "compact"});
785
+
786
+ const turnResponse = validatorFor("turnResponse.schema.json");
787
+ const pendingAsksResponse = validatorFor("pendingAsksResponse.schema.json");
788
+ expect(listed.body.data).toHaveLength(1);
789
+ for (const res of [asked, listed, answered]) {
790
+ expect(res.body.requestId).toEqual(expect.any(String));
791
+ }
792
+ expect(turnResponse.safeParse(asked.body).error).toBeUndefined();
793
+ expect(turnResponse.safeParse(answered.body).error).toBeUndefined();
794
+ expect(pendingAsksResponse.safeParse(listed.body).error).toBeUndefined();
795
+ });
796
+ });
797
+
798
+ describe("route options", () => {
799
+ const historyApp = (validation?: ModelRouterOptions<GptHistoryDocument>["validation"]) =>
800
+ new TerrenoApp({
801
+ configureApp: (router, options) => {
802
+ addGptHistoryRoutes(router, {...options, validation});
803
+ },
804
+ skipListen: true,
805
+ userModel: UserModel,
806
+ }).build();
807
+
808
+ afterEach(() => {
809
+ resetOpenApiValidatorConfig();
810
+ });
811
+
812
+ it("creates a history from an empty body when the host validates request bodies", async () => {
813
+ configureOpenApiValidator();
814
+ const agent = await authAsUser(historyApp(), "notAdmin");
815
+
816
+ const created = await agent.post("/gpt/histories").send({});
817
+
818
+ expect(created.status).toBe(201);
819
+ const read = await agent.get(`/gpt/histories/${created.body.data._id}`);
820
+ expect(read.status).toBe(200);
821
+ });
822
+
823
+ it("keeps the host's validation options", async () => {
824
+ configureOpenApiValidator();
825
+ const onAdditionalPropertiesRemoved = mock((_properties: string[]) => {});
826
+ const agent = await authAsUser(historyApp({onAdditionalPropertiesRemoved}), "notAdmin");
827
+
828
+ const created = await agent.post("/gpt/histories").send({extra: true});
829
+
830
+ expect(created.status).toBe(201);
831
+ expect(onAdditionalPropertiesRemoved).toHaveBeenCalledTimes(1);
832
+ expect(onAdditionalPropertiesRemoved.mock.calls[0]?.[0]).toEqual(["extra"]);
833
+ });
834
+
835
+ it("keeps request validation off when the host turns it off", async () => {
836
+ configureOpenApiValidator();
837
+ const agent = await authAsUser(historyApp(false), "notAdmin");
838
+
839
+ const created = await agent.post("/gpt/histories").send({extra: true});
840
+
841
+ expect(created.status).toBe(400);
842
+ expect(created.body.title).not.toBe("Request validation failed");
843
+ });
844
+
845
+ it("adds no headless actions without chat options", async () => {
846
+ const app = new TerrenoApp({
847
+ configureApp: (router, options) => {
848
+ addGptHistoryRoutes(router, options);
849
+ },
850
+ skipListen: true,
851
+ userModel: UserModel,
852
+ }).build();
853
+ const agent = await authAsUser(app, "notAdmin");
854
+
855
+ const res = await agent.get("/openapi.json");
856
+
857
+ const historyPaths = Object.keys(res.body.paths).filter((path) =>
858
+ path.startsWith("/gpt/histories")
859
+ );
860
+ expect(historyPaths.sort()).toEqual(["/gpt/histories/", "/gpt/histories/{id}"]);
861
+ });
862
+
863
+ it("adds no headless actions when the chat options leave asks off", async () => {
864
+ const model = createScriptedModel({steps: [textStep("Hello.")]});
865
+ const app = new TerrenoApp({
866
+ configureApp: (router, options) => {
867
+ addGptHistoryRoutes(router, {
868
+ ...options,
869
+ chat: {aiService: new AIService({model: model as unknown as LanguageModel})},
870
+ });
871
+ },
872
+ skipListen: true,
873
+ userModel: UserModel,
874
+ }).build();
875
+ const agent = await authAsUser(app, "notAdmin");
876
+ const historyId = await createHistory(agent);
877
+
878
+ const res = await agent.get("/openapi.json");
879
+ const pendingAsks = await agent.get("/gpt/histories/pendingAsks");
880
+ await postTurn(agent, historyId, {prompt: "Hi"});
881
+
882
+ const historyPaths = Object.keys(res.body.paths).filter((path) =>
883
+ path.startsWith("/gpt/histories")
884
+ );
885
+ expect(historyPaths.sort()).toEqual(["/gpt/histories/", "/gpt/histories/{id}"]);
886
+ expect(pendingAsks.status).toBe(404);
887
+ expect(model.doStream).not.toHaveBeenCalled();
888
+ });
889
+
890
+ it("keeps the host's collection and instance actions next to pendingAsks and turn", async () => {
891
+ const model = createScriptedModel({steps: [textStep("Hello.")]});
892
+ const app = new TerrenoApp({
893
+ configureApp: (router, options) => {
894
+ addGptHistoryRoutes(router, {
895
+ ...options,
896
+ chat: {
897
+ aiService: new AIService({model: model as unknown as LanguageModel}),
898
+ asks: true,
899
+ },
900
+ collectionActions: {
901
+ count: {
902
+ handler: async () => ({count: 7}),
903
+ method: "GET",
904
+ permissions: [Permissions.IsAuthenticated],
905
+ },
906
+ },
907
+ instanceActions: {
908
+ archive: {
909
+ handler: async ({doc}) => ({archived: doc._id.toString()}),
910
+ method: "POST",
911
+ permissions: [Permissions.IsOwner],
912
+ },
913
+ },
914
+ });
915
+ },
916
+ skipListen: true,
917
+ userModel: UserModel,
918
+ }).build();
919
+ const agent = await authAsUser(app, "notAdmin");
920
+ const historyId = await createHistory(agent);
921
+
922
+ const count = await agent.get("/gpt/histories/count");
923
+ const archive = await agent.post(`/gpt/histories/${historyId}/archive`);
924
+ const pendingAsks = await agent.get("/gpt/histories/pendingAsks");
925
+ const turn = await postTurn(agent, historyId, {prompt: "Hi"});
926
+
927
+ expect(count.body.data).toEqual({count: 7});
928
+ expect(archive.body.data).toEqual({archived: historyId});
929
+ expect(pendingAsks.body.data).toEqual([]);
930
+ expect(turn.body.data).toEqual({historyId, text: "Hello.", title: "Workspace setup"});
931
+ });
932
+ });
933
+ });