@terreno/ai 57.9.0 → 57.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (417) hide show
  1. package/README.md +1 -0
  2. package/bunfig.toml +1 -1
  3. package/dist/aiApp.d.ts +8 -2
  4. package/dist/aiApp.js +12 -6
  5. package/dist/harness/adminScreens.d.ts +6 -0
  6. package/dist/harness/adminScreens.js +11 -0
  7. package/dist/harness/agentLoop.d.ts +61 -0
  8. package/dist/harness/agentLoop.js +798 -0
  9. package/dist/harness/agentTaskNames.d.ts +4 -0
  10. package/dist/harness/agentTaskNames.js +7 -0
  11. package/dist/harness/approvalPolicy.d.ts +3 -0
  12. package/dist/harness/approvalPolicy.js +23 -0
  13. package/dist/harness/approvals.d.ts +101 -0
  14. package/dist/harness/approvals.js +468 -0
  15. package/dist/harness/commit.d.ts +238 -0
  16. package/dist/harness/commit.js +678 -0
  17. package/dist/harness/conversation.d.ts +75 -0
  18. package/dist/harness/conversation.js +397 -0
  19. package/dist/harness/defineAgent.d.ts +9 -0
  20. package/dist/harness/defineAgent.js +65 -0
  21. package/dist/harness/defineTask.d.ts +11 -0
  22. package/dist/harness/defineTask.js +59 -0
  23. package/dist/harness/defineTool.d.ts +7 -0
  24. package/dist/harness/defineTool.js +36 -0
  25. package/dist/harness/definitionError.d.ts +8 -0
  26. package/dist/harness/definitionError.js +15 -0
  27. package/dist/harness/errors.d.ts +133 -0
  28. package/dist/harness/errors.js +107 -0
  29. package/dist/harness/eventHub.d.ts +45 -0
  30. package/dist/harness/eventHub.js +135 -0
  31. package/dist/harness/events.d.ts +95 -0
  32. package/dist/harness/events.js +267 -0
  33. package/dist/harness/executionEnv.d.ts +31 -0
  34. package/dist/harness/executionEnv.js +2 -0
  35. package/dist/harness/extensions.d.ts +82 -0
  36. package/dist/harness/extensions.js +274 -0
  37. package/dist/harness/harness.d.ts +163 -0
  38. package/dist/harness/harness.js +522 -0
  39. package/dist/harness/harnessApp.d.ts +44 -0
  40. package/dist/harness/harnessApp.js +62 -0
  41. package/dist/harness/internalRuntime.d.ts +16 -0
  42. package/dist/harness/internalRuntime.js +18 -0
  43. package/dist/harness/leases.d.ts +35 -0
  44. package/dist/harness/leases.js +86 -0
  45. package/dist/harness/memo.d.ts +24 -0
  46. package/dist/harness/memo.js +78 -0
  47. package/dist/harness/modelCall.d.ts +45 -0
  48. package/dist/harness/modelCall.js +147 -0
  49. package/dist/harness/models/harnessApproval.d.ts +2 -0
  50. package/dist/harness/models/harnessApproval.js +100 -0
  51. package/dist/harness/models/harnessConversation.d.ts +2 -0
  52. package/dist/harness/models/harnessConversation.js +117 -0
  53. package/dist/harness/models/harnessEvent.d.ts +3 -0
  54. package/dist/harness/models/harnessEvent.js +73 -0
  55. package/dist/harness/models/harnessInboxEvent.d.ts +2 -0
  56. package/dist/harness/models/harnessInboxEvent.js +58 -0
  57. package/dist/harness/models/harnessMemo.d.ts +2 -0
  58. package/dist/harness/models/harnessMemo.js +42 -0
  59. package/dist/harness/models/harnessMessage.d.ts +2 -0
  60. package/dist/harness/models/harnessMessage.js +73 -0
  61. package/dist/harness/models/harnessOwner.d.ts +2 -0
  62. package/dist/harness/models/harnessOwner.js +36 -0
  63. package/dist/harness/models/harnessTask.d.ts +2 -0
  64. package/dist/harness/models/harnessTask.js +191 -0
  65. package/dist/harness/ownership.d.ts +89 -0
  66. package/dist/harness/ownership.js +438 -0
  67. package/dist/harness/registry.d.ts +29 -0
  68. package/dist/harness/registry.js +93 -0
  69. package/dist/harness/retryBackoff.d.ts +31 -0
  70. package/dist/harness/retryBackoff.js +50 -0
  71. package/dist/harness/routes/approvals.d.ts +13 -0
  72. package/dist/harness/routes/approvals.js +93 -0
  73. package/dist/harness/routes/conversations.d.ts +11 -0
  74. package/dist/harness/routes/conversations.js +66 -0
  75. package/dist/harness/routes/events.d.ts +19 -0
  76. package/dist/harness/routes/events.js +215 -0
  77. package/dist/harness/routes/tasks.d.ts +11 -0
  78. package/dist/harness/routes/tasks.js +108 -0
  79. package/dist/harness/runners/inProcessRunner.d.ts +64 -0
  80. package/dist/harness/runners/inProcessRunner.js +227 -0
  81. package/dist/harness/runners/jobsRunner.d.ts +62 -0
  82. package/dist/harness/runners/jobsRunner.js +212 -0
  83. package/dist/harness/runtime.d.ts +52 -0
  84. package/dist/harness/runtime.js +604 -0
  85. package/dist/harness/subagent.d.ts +53 -0
  86. package/dist/harness/subagent.js +134 -0
  87. package/dist/harness/suspend.d.ts +8 -0
  88. package/dist/harness/suspend.js +16 -0
  89. package/dist/harness/transaction.d.ts +3 -0
  90. package/dist/harness/transaction.js +22 -0
  91. package/dist/harness/waits.d.ts +100 -0
  92. package/dist/harness/waits.js +214 -0
  93. package/dist/index.d.ts +19 -1
  94. package/dist/index.js +45 -2
  95. package/dist/models/aiDataset.d.ts +2 -0
  96. package/dist/models/aiDataset.js +67 -0
  97. package/dist/models/aiRequest.js +1 -1
  98. package/dist/models/gptHistory.js +73 -0
  99. package/dist/observability/adminScreens.d.ts +5 -0
  100. package/dist/observability/adminScreens.js +32 -0
  101. package/dist/observability/compileTemplate.d.ts +1 -0
  102. package/dist/observability/compileTemplate.js +13 -0
  103. package/dist/observability/datasetImport.d.ts +11 -0
  104. package/dist/observability/datasetImport.js +185 -0
  105. package/dist/observability/evaluate.d.ts +31 -0
  106. package/dist/observability/evaluate.js +200 -0
  107. package/dist/observability/evaluatorTemplates.d.ts +17 -0
  108. package/dist/observability/evaluatorTemplates.js +111 -0
  109. package/dist/observability/jsonPath.d.ts +2 -0
  110. package/dist/observability/jsonPath.js +57 -0
  111. package/dist/observability/local/datasetStore.d.ts +87 -0
  112. package/dist/observability/local/datasetStore.js +326 -0
  113. package/dist/observability/local/evaluatorStore.d.ts +44 -0
  114. package/dist/observability/local/evaluatorStore.js +182 -0
  115. package/dist/observability/local/experimentRunner.d.ts +96 -0
  116. package/dist/observability/local/experimentRunner.js +419 -0
  117. package/dist/observability/local/localPlugin.d.ts +22 -0
  118. package/dist/observability/local/localPlugin.js +76 -0
  119. package/dist/observability/local/models/obsDataset.d.ts +2 -0
  120. package/dist/observability/local/models/obsDataset.js +37 -0
  121. package/dist/observability/local/models/obsDatasetItem.d.ts +2 -0
  122. package/dist/observability/local/models/obsDatasetItem.js +76 -0
  123. package/dist/observability/local/models/obsEvaluator.d.ts +2 -0
  124. package/dist/observability/local/models/obsEvaluator.js +98 -0
  125. package/dist/observability/local/models/obsExperiment.d.ts +2 -0
  126. package/dist/observability/local/models/obsExperiment.js +87 -0
  127. package/dist/observability/local/models/obsExperimentItem.d.ts +2 -0
  128. package/dist/observability/local/models/obsExperimentItem.js +45 -0
  129. package/dist/observability/local/models/obsPrompt.d.ts +2 -0
  130. package/dist/observability/local/models/obsPrompt.js +42 -0
  131. package/dist/observability/local/models/obsPromptLabel.d.ts +2 -0
  132. package/dist/observability/local/models/obsPromptLabel.js +39 -0
  133. package/dist/observability/local/models/obsPromptVersion.d.ts +2 -0
  134. package/dist/observability/local/models/obsPromptVersion.js +96 -0
  135. package/dist/observability/local/models/obsReviewItem.d.ts +2 -0
  136. package/dist/observability/local/models/obsReviewItem.js +75 -0
  137. package/dist/observability/local/models/obsScore.d.ts +2 -0
  138. package/dist/observability/local/models/obsScore.js +58 -0
  139. package/dist/observability/local/models/obsSpan.d.ts +2 -0
  140. package/dist/observability/local/models/obsSpan.js +64 -0
  141. package/dist/observability/local/models/obsTrace.d.ts +2 -0
  142. package/dist/observability/local/models/obsTrace.js +93 -0
  143. package/dist/observability/local/promptStore.d.ts +180 -0
  144. package/dist/observability/local/promptStore.js +444 -0
  145. package/dist/observability/local/reviewStore.d.ts +50 -0
  146. package/dist/observability/local/reviewStore.js +203 -0
  147. package/dist/observability/local/traceStore.d.ts +103 -0
  148. package/dist/observability/local/traceStore.js +325 -0
  149. package/dist/observability/observabilityApp.d.ts +24 -0
  150. package/dist/observability/observabilityApp.js +157 -0
  151. package/dist/observability/observabilityAppRegistry.d.ts +14 -0
  152. package/dist/observability/observabilityAppRegistry.js +16 -0
  153. package/dist/observability/permissions.d.ts +7 -0
  154. package/dist/observability/permissions.js +60 -0
  155. package/dist/observability/reviewPanels.d.ts +18 -0
  156. package/dist/observability/reviewPanels.js +46 -0
  157. package/dist/observability/routes/datasets.d.ts +7 -0
  158. package/dist/observability/routes/datasets.js +149 -0
  159. package/dist/observability/routes/evaluators.d.ts +7 -0
  160. package/dist/observability/routes/evaluators.js +123 -0
  161. package/dist/observability/routes/experiments.d.ts +7 -0
  162. package/dist/observability/routes/experiments.js +95 -0
  163. package/dist/observability/routes/observabilityRouteAccess.d.ts +13 -0
  164. package/dist/observability/routes/observabilityRouteAccess.js +67 -0
  165. package/dist/observability/routes/prompts.d.ts +12 -0
  166. package/dist/observability/routes/prompts.js +172 -0
  167. package/dist/observability/routes/requestAiService.d.ts +13 -0
  168. package/dist/observability/routes/requestAiService.js +21 -0
  169. package/dist/observability/routes/review.d.ts +7 -0
  170. package/dist/observability/routes/review.js +79 -0
  171. package/dist/observability/routes/status.d.ts +5 -0
  172. package/dist/observability/routes/status.js +31 -0
  173. package/dist/observability/routes/testMultiStage.d.ts +9 -0
  174. package/dist/observability/routes/testMultiStage.js +67 -0
  175. package/dist/observability/routes/traces.d.ts +7 -0
  176. package/dist/observability/routes/traces.js +129 -0
  177. package/dist/observability/schemaValidation.d.ts +5 -0
  178. package/dist/observability/schemaValidation.js +26 -0
  179. package/dist/observability/sopThresholds.d.ts +2 -0
  180. package/dist/observability/sopThresholds.js +21 -0
  181. package/dist/observability/status.d.ts +23 -0
  182. package/dist/observability/status.js +34 -0
  183. package/dist/observability/testMultiStageSchemas.d.ts +10 -0
  184. package/dist/observability/testMultiStageSchemas.js +79 -0
  185. package/dist/observability/testMultiStageWorkflow.d.ts +20 -0
  186. package/dist/observability/testMultiStageWorkflow.js +218 -0
  187. package/dist/observability/types.d.ts +159 -0
  188. package/dist/observability/types.js +67 -0
  189. package/dist/routes/files.js +18 -7
  190. package/dist/routes/gpt.js +79 -689
  191. package/dist/routes/gptActions.d.ts +9 -0
  192. package/dist/routes/gptActions.js +212 -0
  193. package/dist/routes/gptDatasets.d.ts +5 -0
  194. package/dist/routes/gptDatasets.js +94 -0
  195. package/dist/routes/gptHistories.d.ts +3 -1
  196. package/dist/routes/gptHistories.js +188 -2
  197. package/dist/service/aiDatasets.d.ts +57 -0
  198. package/dist/service/aiDatasets.js +253 -0
  199. package/dist/service/aiService.d.ts +50 -1
  200. package/dist/service/aiService.js +426 -77
  201. package/dist/service/askFiles.d.ts +49 -0
  202. package/dist/service/askFiles.js +162 -0
  203. package/dist/service/asks.d.ts +76 -0
  204. package/dist/service/asks.js +284 -0
  205. package/dist/service/chatTurn.d.ts +128 -0
  206. package/dist/service/chatTurn.js +1674 -0
  207. package/dist/service/fileStorage.d.ts +4 -0
  208. package/dist/service/fileStorage.js +7 -2
  209. package/dist/service/fillActionIds.d.ts +6 -0
  210. package/dist/service/fillActionIds.js +79 -0
  211. package/dist/service/jsonFenceModel.d.ts +7 -0
  212. package/dist/service/jsonFenceModel.js +45 -0
  213. package/dist/service/prompts.d.ts +52 -0
  214. package/dist/service/prompts.js +96 -1
  215. package/dist/service/sanitizeHtml.d.ts +11 -0
  216. package/dist/service/sanitizeHtml.js +108 -0
  217. package/dist/types/harness.d.ts +952 -0
  218. package/dist/types/harness.js +133 -0
  219. package/dist/types/index.d.ts +192 -14
  220. package/dist/types/index.js +2 -0
  221. package/dist/types/observability.d.ts +288 -0
  222. package/dist/types/observability.js +2 -0
  223. package/package.json +28 -5
  224. package/src/aiApp.test.ts +207 -1
  225. package/src/aiApp.ts +19 -6
  226. package/src/harness/adminScreens.test.ts +34 -0
  227. package/src/harness/adminScreens.ts +11 -0
  228. package/src/harness/agentLoop.test.ts +1034 -0
  229. package/src/harness/agentLoop.ts +1082 -0
  230. package/src/harness/agentRecovery.test.ts +380 -0
  231. package/src/harness/agentTaskNames.ts +4 -0
  232. package/src/harness/approvalPolicy.ts +28 -0
  233. package/src/harness/approvals.test.ts +1094 -0
  234. package/src/harness/approvals.ts +644 -0
  235. package/src/harness/commit.ts +1049 -0
  236. package/src/harness/concurrency.test.ts +155 -0
  237. package/src/harness/conversation.ts +503 -0
  238. package/src/harness/defineAgent.test.ts +92 -0
  239. package/src/harness/defineAgent.ts +82 -0
  240. package/src/harness/defineTask.test.ts +59 -0
  241. package/src/harness/defineTask.ts +76 -0
  242. package/src/harness/defineTool.ts +45 -0
  243. package/src/harness/definitionError.ts +13 -0
  244. package/src/harness/errors.test.ts +90 -0
  245. package/src/harness/errors.ts +125 -0
  246. package/src/harness/eventHub.test.ts +124 -0
  247. package/src/harness/eventHub.ts +159 -0
  248. package/src/harness/events.test.ts +769 -0
  249. package/src/harness/events.ts +384 -0
  250. package/src/harness/executionEnv.ts +34 -0
  251. package/src/harness/extensions.test.ts +1218 -0
  252. package/src/harness/extensions.ts +382 -0
  253. package/src/harness/harness.test.ts +693 -0
  254. package/src/harness/harness.ts +778 -0
  255. package/src/harness/harnessApp.ts +76 -0
  256. package/src/harness/internalRuntime.ts +44 -0
  257. package/src/harness/leases.ts +132 -0
  258. package/src/harness/memo.ts +107 -0
  259. package/src/harness/modelCall.test.ts +134 -0
  260. package/src/harness/modelCall.ts +200 -0
  261. package/src/harness/models/harnessApproval.ts +109 -0
  262. package/src/harness/models/harnessConversation.ts +142 -0
  263. package/src/harness/models/harnessEvent.ts +92 -0
  264. package/src/harness/models/harnessInboxEvent.ts +73 -0
  265. package/src/harness/models/harnessMemo.ts +43 -0
  266. package/src/harness/models/harnessMessage.ts +85 -0
  267. package/src/harness/models/harnessModels.test.ts +60 -0
  268. package/src/harness/models/harnessOwner.ts +40 -0
  269. package/src/harness/models/harnessTask.ts +206 -0
  270. package/src/harness/ownership.test.ts +1087 -0
  271. package/src/harness/ownership.ts +618 -0
  272. package/src/harness/recovery.test.ts +907 -0
  273. package/src/harness/registry.ts +124 -0
  274. package/src/harness/retries.test.ts +292 -0
  275. package/src/harness/retryBackoff.test.ts +100 -0
  276. package/src/harness/retryBackoff.ts +77 -0
  277. package/src/harness/routes/approvals.ts +123 -0
  278. package/src/harness/routes/conversations.test.ts +518 -0
  279. package/src/harness/routes/conversations.ts +102 -0
  280. package/src/harness/routes/events.ts +280 -0
  281. package/src/harness/routes/tasks.test.ts +198 -0
  282. package/src/harness/routes/tasks.ts +146 -0
  283. package/src/harness/runners/inProcessRunner.test.ts +530 -0
  284. package/src/harness/runners/inProcessRunner.ts +260 -0
  285. package/src/harness/runners/jobsRunner.test.ts +709 -0
  286. package/src/harness/runners/jobsRunner.ts +252 -0
  287. package/src/harness/runtime.ts +802 -0
  288. package/src/harness/subagent.test.ts +696 -0
  289. package/src/harness/subagent.ts +192 -0
  290. package/src/harness/suspend.ts +13 -0
  291. package/src/harness/transaction.ts +17 -0
  292. package/src/harness/versioning.test.ts +288 -0
  293. package/src/harness/waits.test.ts +693 -0
  294. package/src/harness/waits.ts +356 -0
  295. package/src/index.test.ts +13 -0
  296. package/src/index.ts +70 -0
  297. package/src/isolated/adminScreensConfig.isolated.ts +143 -0
  298. package/src/models/aiDataset.test.ts +34 -0
  299. package/src/models/aiDataset.ts +78 -0
  300. package/src/models/aiRequest.ts +2 -1
  301. package/src/models/gptHistory.test.ts +156 -0
  302. package/src/models/gptHistory.ts +89 -1
  303. package/src/observability/adminScreens.test.ts +71 -0
  304. package/src/observability/adminScreens.ts +42 -0
  305. package/src/observability/compileTemplate.test.ts +14 -0
  306. package/src/observability/compileTemplate.ts +10 -0
  307. package/src/observability/datasetImport.test.ts +95 -0
  308. package/src/observability/datasetImport.ts +220 -0
  309. package/src/observability/evaluate.test.ts +549 -0
  310. package/src/observability/evaluate.ts +254 -0
  311. package/src/observability/evaluatorTemplates.ts +124 -0
  312. package/src/observability/jsonPath.test.ts +52 -0
  313. package/src/observability/jsonPath.ts +60 -0
  314. package/src/observability/local/datasetStore.test.ts +230 -0
  315. package/src/observability/local/datasetStore.ts +452 -0
  316. package/src/observability/local/evaluatorStore.test.ts +138 -0
  317. package/src/observability/local/evaluatorStore.ts +224 -0
  318. package/src/observability/local/experimentRunner.test.ts +489 -0
  319. package/src/observability/local/experimentRunner.ts +548 -0
  320. package/src/observability/local/localPlugin.ts +88 -0
  321. package/src/observability/local/models/obsDataset.ts +38 -0
  322. package/src/observability/local/models/obsDatasetItem.ts +80 -0
  323. package/src/observability/local/models/obsEvaluator.ts +102 -0
  324. package/src/observability/local/models/obsExperiment.ts +91 -0
  325. package/src/observability/local/models/obsExperimentItem.ts +52 -0
  326. package/src/observability/local/models/obsPrompt.ts +43 -0
  327. package/src/observability/local/models/obsPromptLabel.ts +43 -0
  328. package/src/observability/local/models/obsPromptVersion.ts +107 -0
  329. package/src/observability/local/models/obsReviewItem.ts +79 -0
  330. package/src/observability/local/models/obsScore.ts +59 -0
  331. package/src/observability/local/models/obsSpan.ts +65 -0
  332. package/src/observability/local/models/obsTrace.ts +94 -0
  333. package/src/observability/local/obsModels.test.ts +119 -0
  334. package/src/observability/local/promptStore.test.ts +351 -0
  335. package/src/observability/local/promptStore.ts +665 -0
  336. package/src/observability/local/reviewStore.test.ts +164 -0
  337. package/src/observability/local/reviewStore.ts +264 -0
  338. package/src/observability/local/traceStore.test.ts +264 -0
  339. package/src/observability/local/traceStore.ts +440 -0
  340. package/src/observability/observabilityApp.test.ts +92 -0
  341. package/src/observability/observabilityApp.ts +185 -0
  342. package/src/observability/observabilityAppRegistry.test.ts +30 -0
  343. package/src/observability/observabilityAppRegistry.ts +35 -0
  344. package/src/observability/permissions.test.ts +96 -0
  345. package/src/observability/permissions.ts +78 -0
  346. package/src/observability/reviewPanels.test.ts +35 -0
  347. package/src/observability/reviewPanels.ts +64 -0
  348. package/src/observability/routes/datasets.test.ts +176 -0
  349. package/src/observability/routes/datasets.ts +252 -0
  350. package/src/observability/routes/evaluators.test.ts +119 -0
  351. package/src/observability/routes/evaluators.ts +207 -0
  352. package/src/observability/routes/experiments.test.ts +282 -0
  353. package/src/observability/routes/experiments.ts +152 -0
  354. package/src/observability/routes/observabilityRbac.test.ts +767 -0
  355. package/src/observability/routes/observabilityRouteAccess.ts +106 -0
  356. package/src/observability/routes/prompts.test.ts +320 -0
  357. package/src/observability/routes/prompts.ts +263 -0
  358. package/src/observability/routes/requestAiService.ts +36 -0
  359. package/src/observability/routes/review.test.ts +120 -0
  360. package/src/observability/routes/review.ts +125 -0
  361. package/src/observability/routes/status.test.ts +116 -0
  362. package/src/observability/routes/status.ts +47 -0
  363. package/src/observability/routes/testMultiStage.test.ts +214 -0
  364. package/src/observability/routes/testMultiStage.ts +96 -0
  365. package/src/observability/routes/traces.test.ts +167 -0
  366. package/src/observability/routes/traces.ts +172 -0
  367. package/src/observability/schemaValidation.ts +29 -0
  368. package/src/observability/sopThresholds.ts +20 -0
  369. package/src/observability/status.test.ts +38 -0
  370. package/src/observability/status.ts +56 -0
  371. package/src/observability/testMultiStageSchemas.ts +95 -0
  372. package/src/observability/testMultiStageWorkflow.test.ts +114 -0
  373. package/src/observability/testMultiStageWorkflow.ts +307 -0
  374. package/src/observability/types.ts +262 -0
  375. package/src/routes/files.test.ts +63 -9
  376. package/src/routes/files.ts +21 -7
  377. package/src/routes/gpt.test.ts +3279 -0
  378. package/src/routes/gpt.ts +91 -816
  379. package/src/routes/gptActions.test.ts +241 -0
  380. package/src/routes/gptActions.ts +255 -0
  381. package/src/routes/gptApprovals.test.ts +762 -0
  382. package/src/routes/gptDatasets.test.ts +86 -0
  383. package/src/routes/gptDatasets.ts +104 -0
  384. package/src/routes/gptFiles.test.ts +589 -0
  385. package/src/routes/gptHistories.test.ts +933 -0
  386. package/src/routes/gptHistories.ts +266 -5
  387. package/src/routes/routes.test.ts +222 -2
  388. package/src/service/aiDatasets.test.ts +123 -0
  389. package/src/service/aiDatasets.ts +316 -0
  390. package/src/service/aiService.test.ts +593 -0
  391. package/src/service/aiService.ts +568 -111
  392. package/src/service/askFiles.test.ts +96 -0
  393. package/src/service/askFiles.ts +222 -0
  394. package/src/service/asks.test.ts +946 -0
  395. package/src/service/asks.ts +423 -0
  396. package/src/service/chatTurn.ts +2287 -0
  397. package/src/service/fileStorage.test.ts +11 -0
  398. package/src/service/fileStorage.ts +10 -2
  399. package/src/service/fillActionIds.test.ts +65 -0
  400. package/src/service/fillActionIds.ts +98 -0
  401. package/src/service/jsonFenceModel.test.ts +47 -0
  402. package/src/service/jsonFenceModel.ts +53 -0
  403. package/src/service/prompts.test.ts +21 -0
  404. package/src/service/prompts.ts +147 -0
  405. package/src/service/sanitizeHtml.test.ts +61 -0
  406. package/src/service/sanitizeHtml.ts +109 -0
  407. package/src/tests/bunSetup.test.ts +7 -4
  408. package/src/tests/bunSetup.ts +7 -7
  409. package/src/tests/chatHarness.ts +478 -0
  410. package/src/tests/generateStream.ts +49 -0
  411. package/src/tests/harnessErrors.ts +22 -0
  412. package/src/tests/harnessStreaming.ts +227 -0
  413. package/src/tests/helpers.ts +9 -2
  414. package/src/types/harness.ts +1119 -0
  415. package/src/types/index.ts +209 -13
  416. package/src/types/observability.ts +344 -0
  417. package/src/types/sanitize-html.d.ts +25 -0
@@ -0,0 +1,356 @@
1
+ import {DateTime, Duration, type DurationLike} from "luxon";
2
+ import type mongoose from "mongoose";
3
+ import type {ClientSession} from "mongoose";
4
+
5
+ import type {
6
+ HarnessInboxEventDocument,
7
+ HarnessLeaseSettings,
8
+ HarnessTaskDocument,
9
+ HarnessTestHooks,
10
+ HarnessWaitCall,
11
+ } from "../types/harness";
12
+ import {
13
+ HARNESS_TASK_STATUSES,
14
+ HARNESS_TERMINAL_STATUSES,
15
+ HARNESS_WAIT_KINDS,
16
+ HARNESS_WAIT_RESOLUTIONS,
17
+ } from "../types/harness";
18
+ import {
19
+ commitWaitResolution,
20
+ HarnessCommitConflictError,
21
+ type HarnessCommitWrites,
22
+ type HarnessModels,
23
+ isDuplicateKeyError,
24
+ } from "./commit";
25
+ import {HarnessDefinitionError} from "./definitionError";
26
+ import {harnessError} from "./errors";
27
+ import {inTransaction} from "./transaction";
28
+
29
+ /** Most object keys a resume span lists for a delivered payload. */
30
+ const PAYLOAD_SUMMARY_KEYS = 20;
31
+
32
+ /** One `rt.waitFor` or `rt.sleep` call. */
33
+ export interface WaitRequest {
34
+ /** Sleep length, or the event wait's timeout. */
35
+ duration?: Duration;
36
+ /** Event name; absent for a sleep. */
37
+ event?: string;
38
+ kind: "event" | "sleep";
39
+ /**
40
+ * Extra writes committed with a timeout resolution. Throw `HarnessWaitRaceError` from
41
+ * them when an event that should win landed concurrently: the call then resolves again.
42
+ */
43
+ timeoutWrites?: HarnessCommitWrites;
44
+ }
45
+
46
+ /**
47
+ * Thrown from `WaitRequest.timeoutWrites` when the timeout lost a race with a matching
48
+ * event; the timeout transaction rolls back and the call is resolved again.
49
+ */
50
+ export class HarnessWaitRaceError extends Error {
51
+ constructor() {
52
+ super("A matching event raced the wait's timeout");
53
+ this.name = "HarnessWaitRaceError";
54
+ }
55
+ }
56
+
57
+ /** Times a call re-resolves after losing a timeout race before giving up. */
58
+ const MAX_WAIT_RACE_RETRIES = 3;
59
+
60
+ /** What one wait call needs: its request, plus writes and work around parking on it. */
61
+ export interface WaitCallPlan {
62
+ /** Runs after the waiting commit, before the phase stops. */
63
+ afterPark?: () => Promise<void>;
64
+ /** Extra rows committed with the waiting commit. */
65
+ onPark?: HarnessCommitWrites;
66
+ request: WaitRequest;
67
+ }
68
+
69
+ /** A wait call either has its result, or must park the task on `entry`. */
70
+ export type WaitCallResult =
71
+ | {isResolved: true; payload?: unknown}
72
+ | {entry: HarnessWaitCall; isResolved: false};
73
+
74
+ /** Names one wait call of the current phase visit: `<step>:<call index>`. */
75
+ export const waitCallKey = ({index, task}: {index: number; task: HarnessTaskDocument}): string =>
76
+ `${task.step ?? 0}:${index}`;
77
+
78
+ /** A non-negative Luxon duration, or a definition error naming `label`. */
79
+ export const parseWaitDuration = (value: DurationLike, label: string): Duration => {
80
+ let duration: Duration;
81
+ try {
82
+ duration = Duration.fromDurationLike(value);
83
+ } catch (error: unknown) {
84
+ throw new HarnessDefinitionError(
85
+ `${label} must be a Luxon duration: ${error instanceof Error ? error.message : String(error)}`
86
+ );
87
+ }
88
+ if (!duration.isValid || !(duration.toMillis() >= 0)) {
89
+ throw new HarnessDefinitionError(`${label} must be a valid, non-negative duration`);
90
+ }
91
+ return duration;
92
+ };
93
+
94
+ /** Shape of a payload for the resume span, without its values. */
95
+ const summarizePayload = (payload: unknown): Record<string, unknown> => {
96
+ if (payload === null) {
97
+ return {type: "null"};
98
+ }
99
+ if (Array.isArray(payload)) {
100
+ return {length: payload.length, type: "array"};
101
+ }
102
+ if (typeof payload === "string") {
103
+ return {length: payload.length, type: "string"};
104
+ }
105
+ if (typeof payload === "object") {
106
+ const keys = Object.keys(payload as Record<string, unknown>);
107
+ return {keyCount: keys.length, keys: keys.slice(0, PAYLOAD_SUMMARY_KEYS), type: "object"};
108
+ }
109
+ return {type: typeof payload};
110
+ };
111
+
112
+ const describeCall = (call: {event?: string; key?: string; kind: string}): string => {
113
+ const event = call.event ?? call.key;
114
+ return call.kind === HARNESS_WAIT_KINDS.event ? `rt.waitFor("${event}")` : "rt.sleep";
115
+ };
116
+
117
+ const resumeSpanName = (request: WaitRequest): string =>
118
+ request.kind === HARNESS_WAIT_KINDS.event ? `wait:${request.event}` : "sleep";
119
+
120
+ const resumeSpanInput = (entry: HarnessWaitCall): Record<string, unknown> => ({
121
+ ...(entry.key === undefined ? {} : {event: entry.key}),
122
+ kind: entry.kind,
123
+ startedAt: entry.startedAt.toISOString(),
124
+ ...(entry.timeoutAt ? {timeoutAt: entry.timeoutAt.toISOString()} : {}),
125
+ });
126
+
127
+ /**
128
+ * Resolve one wait call. A call this phase visit already resolved returns its recorded
129
+ * result. Otherwise an event wait takes the oldest undelivered event of its name; failing
130
+ * that, a passed `timeoutAt` resolves it (`undefined` for an event, done for a sleep).
131
+ * Each resolution is recorded on the task with a resume span in one fenced transaction.
132
+ * Otherwise returns the record to park the task on.
133
+ */
134
+ export const resolveWaitCall = async (
135
+ args: Parameters<typeof resolveWaitCallOnce>[0]
136
+ ): Promise<WaitCallResult> => {
137
+ for (let attempt = 0; ; attempt++) {
138
+ try {
139
+ return await resolveWaitCallOnce(args);
140
+ } catch (error: unknown) {
141
+ if (!(error instanceof HarnessWaitRaceError) || attempt >= MAX_WAIT_RACE_RETRIES) {
142
+ throw error;
143
+ }
144
+ }
145
+ }
146
+ };
147
+
148
+ const resolveWaitCallOnce = async ({
149
+ callKey,
150
+ label,
151
+ lease,
152
+ models,
153
+ request,
154
+ task,
155
+ testHooks,
156
+ }: {
157
+ callKey: string;
158
+ /** Task definition key, for error messages. */
159
+ label: string;
160
+ lease: HarnessLeaseSettings;
161
+ models: HarnessModels;
162
+ request: WaitRequest;
163
+ task: HarnessTaskDocument;
164
+ testHooks?: HarnessTestHooks;
165
+ }): Promise<WaitCallResult> => {
166
+ const recorded = task.waits?.[callKey];
167
+ if (recorded && (recorded.kind !== request.kind || recorded.key !== request.event)) {
168
+ throw new HarnessDefinitionError(
169
+ `${label}: wait call ${callKey} was ${describeCall(recorded)} on an earlier run and is ${describeCall(request)} now; run waits in the same order on every run of a phase`
170
+ );
171
+ }
172
+ if (recorded?.resolution) {
173
+ if (recorded.resolution !== HARNESS_WAIT_RESOLUTIONS.event) {
174
+ return {isResolved: true};
175
+ }
176
+ const delivered = await models.inbox.findExactlyOne({_id: recorded.eventId});
177
+ return {isResolved: true, payload: delivered.payload};
178
+ }
179
+
180
+ const now = DateTime.now();
181
+ const entry: HarnessWaitCall = recorded ?? {
182
+ kind: request.kind,
183
+ ...(request.event === undefined ? {} : {key: request.event}),
184
+ startedAt: now.toJSDate(),
185
+ ...(request.duration ? {timeoutAt: now.plus(request.duration).toJSDate()} : {}),
186
+ };
187
+ const resolve = (
188
+ resolved: Partial<HarnessWaitCall>,
189
+ output: Record<string, unknown>,
190
+ writes?: Parameters<typeof commitWaitResolution>[0]["writes"]
191
+ ): Promise<void> =>
192
+ commitWaitResolution({
193
+ callKey,
194
+ entry: {...entry, ...resolved, resolvedAt: now.toJSDate()},
195
+ lease,
196
+ models,
197
+ span: {input: resumeSpanInput(entry), name: resumeSpanName(request), output},
198
+ task,
199
+ testHooks,
200
+ writes,
201
+ });
202
+
203
+ if (request.kind === HARNESS_WAIT_KINDS.event) {
204
+ const [buffered] = await models.inbox
205
+ .find({consumedKey: {$exists: false}, name: request.event, taskId: task._id})
206
+ .sort({seq: 1})
207
+ .limit(1);
208
+ if (buffered) {
209
+ await resolve(
210
+ {eventId: buffered._id, resolution: HARNESS_WAIT_RESOLUTIONS.event},
211
+ {
212
+ event: request.event,
213
+ eventId: String(buffered._id),
214
+ payload: summarizePayload(buffered.payload),
215
+ seq: buffered.seq,
216
+ timedOut: false,
217
+ },
218
+ async ({session}) => {
219
+ const claimed = await models.inbox.updateOne(
220
+ {_id: buffered._id, consumedKey: {$exists: false}},
221
+ {$set: {consumedAt: now.toJSDate(), consumedKey: callKey}},
222
+ {session}
223
+ );
224
+ if (claimed.matchedCount === 0) {
225
+ throw new HarnessCommitConflictError(String(task._id), task.phase);
226
+ }
227
+ }
228
+ );
229
+ return {isResolved: true, payload: buffered.payload};
230
+ }
231
+ }
232
+
233
+ if (entry.timeoutAt && now >= DateTime.fromJSDate(entry.timeoutAt)) {
234
+ const isEvent = request.kind === HARNESS_WAIT_KINDS.event;
235
+ await resolve(
236
+ {resolution: isEvent ? HARNESS_WAIT_RESOLUTIONS.timeout : HARNESS_WAIT_RESOLUTIONS.elapsed},
237
+ isEvent ? {event: request.event, timedOut: true} : {elapsed: true},
238
+ request.timeoutWrites
239
+ );
240
+ return {isResolved: true};
241
+ }
242
+ return {entry, isResolved: false};
243
+ };
244
+
245
+ const assertSameEvent = (
246
+ existing: HarnessInboxEventDocument,
247
+ event: string
248
+ ): HarnessInboxEventDocument => {
249
+ if (existing.name !== event) {
250
+ throw harnessError({
251
+ detail: `requestId "${existing.requestId}" already sent event "${existing.name}" to task ${existing.taskId}, not "${event}"`,
252
+ kind: "requestIdConflict",
253
+ });
254
+ }
255
+ return existing;
256
+ };
257
+
258
+ /**
259
+ * Inside `session`'s transaction: count the event on the task (`eventSeq`), insert it into
260
+ * the task's inbox, and, when the task is waiting on that event name, return it to
261
+ * `pending`. Throws when the task is terminal. Returns whether the task was woken.
262
+ */
263
+ export const appendInboxEvent = async ({
264
+ event,
265
+ models,
266
+ payload,
267
+ requestId,
268
+ session,
269
+ taskId,
270
+ }: {
271
+ event: string;
272
+ models: HarnessModels;
273
+ payload: unknown;
274
+ requestId?: string;
275
+ session: ClientSession;
276
+ taskId: mongoose.Types.ObjectId;
277
+ }): Promise<{event: HarnessInboxEventDocument; isWoken: boolean}> => {
278
+ const counted = await models.task.findOneAndUpdate(
279
+ {_id: taskId, status: {$nin: [...HARNESS_TERMINAL_STATUSES]}},
280
+ {$inc: {eventSeq: 1}},
281
+ {returnDocument: "after", session}
282
+ );
283
+ if (!counted) {
284
+ throw harnessError({
285
+ detail: `Task ${taskId} is already terminal; it cannot receive event "${event}"`,
286
+ kind: "taskTerminal",
287
+ });
288
+ }
289
+ const [created] = await models.inbox.create(
290
+ [{name: event, payload, requestId, seq: counted.eventSeq, taskId}],
291
+ {session}
292
+ );
293
+ if (
294
+ counted.status !== HARNESS_TASK_STATUSES.waiting ||
295
+ counted.waiting?.kind !== HARNESS_WAIT_KINDS.event ||
296
+ counted.waiting.key !== event
297
+ ) {
298
+ return {event: created, isWoken: false};
299
+ }
300
+ const woken = await models.task.updateOne(
301
+ {_id: taskId, status: HARNESS_TASK_STATUSES.waiting},
302
+ {$set: {status: HARNESS_TASK_STATUSES.pending}, $unset: {waiting: 1}},
303
+ {session}
304
+ );
305
+ return {event: created, isWoken: woken.modifiedCount > 0};
306
+ };
307
+
308
+ /**
309
+ * Append an event to a task's inbox and, when the task is waiting on that event name,
310
+ * return it to `pending`, in one transaction. The task row is written every time
311
+ * (`eventSeq`), so a concurrent `rt.waitFor` commit and this send conflict and one retries:
312
+ * neither can miss the other. A repeated `requestId` returns the event sent first.
313
+ * Throws when the task is terminal.
314
+ */
315
+ export const sendEventRecords = async ({
316
+ event,
317
+ models,
318
+ payload,
319
+ requestId,
320
+ taskId,
321
+ }: {
322
+ event: string;
323
+ models: HarnessModels;
324
+ payload: unknown;
325
+ requestId?: string;
326
+ taskId: mongoose.Types.ObjectId | string;
327
+ }): Promise<{event: HarnessInboxEventDocument; isWoken: boolean}> => {
328
+ const task = await models.task.findExactlyOne({_id: taskId});
329
+ if (requestId !== undefined) {
330
+ const existing = await models.inbox.findOneOrNone({requestId, taskId: task._id});
331
+ if (existing) {
332
+ return {event: assertSameEvent(existing, event), isWoken: false};
333
+ }
334
+ }
335
+ const terminalError = (status: string): Error =>
336
+ harnessError({
337
+ detail: `Task ${task._id} is already ${status}; it cannot receive event "${event}"`,
338
+ kind: "taskTerminal",
339
+ });
340
+ if (HARNESS_TERMINAL_STATUSES.has(task.status)) {
341
+ throw terminalError(task.status);
342
+ }
343
+
344
+ try {
345
+ return await inTransaction((session) =>
346
+ appendInboxEvent({event, models, payload, requestId, session, taskId: task._id})
347
+ );
348
+ } catch (error: unknown) {
349
+ // A concurrent send with the same requestId won.
350
+ if (requestId !== undefined && isDuplicateKeyError(error)) {
351
+ const existing = await models.inbox.findExactlyOne({requestId, taskId: task._id});
352
+ return {event: assertSameEvent(existing, event), isWoken: false};
353
+ }
354
+ throw error;
355
+ }
356
+ };
package/src/index.test.ts CHANGED
@@ -19,6 +19,7 @@ describe("@terreno/ai public exports", () => {
19
19
  "addProjectRoutes",
20
20
  "assertVertexModelsEnabled",
21
21
  "compilePrompt",
22
+ "createAskTools",
22
23
  "createPrompt",
23
24
  "createTelemetryConfig",
24
25
  "createVertexProvider",
@@ -40,6 +41,10 @@ describe("@terreno/ai public exports", () => {
40
41
  "normalizeVertexModelId",
41
42
  "parseAiJson",
42
43
  "preparePromptForAI",
44
+ "getObservabilityApp",
45
+ "resetObservabilityApp",
46
+ "resolveObservabilityControl",
47
+ "validateObservabilityConfig",
43
48
  "setCached",
44
49
  "shutdownLangfuseClient",
45
50
  "shutdownTracing",
@@ -56,17 +61,23 @@ describe("@terreno/ai public exports", () => {
56
61
  "LangfuseApp",
57
62
  "LangfuseCache",
58
63
  "MCPService",
64
+ "MemoryScoreSink",
65
+ "MemoryTraceSink",
66
+ "ObservabilityApp",
59
67
  "Project",
60
68
  ] as const;
61
69
 
62
70
  const expectedConstants = [
71
+ "COMPACT_SURFACE_SYSTEM_PROMPT",
63
72
  "CONTENT_SUMMARY_PROMPT",
64
73
  "DEFAULT_GPT_MEMORY",
74
+ "DEFAULT_OBSERVABILITY_CONTROL",
65
75
  "DEFAULT_VERTEX_LOCATION",
66
76
  "GEMINI_API_BASE_URL",
67
77
  "JSON_VALUE_SYSTEM_PROMPT",
68
78
  "Output",
69
79
  "REMIX_PROMPT",
80
+ "TERRENO_ASKS_SYSTEM_PROMPT",
70
81
  "TITLE_GENERATION_PROMPT",
71
82
  "TRANSLATION_PROMPT",
72
83
  "TemperaturePresets",
@@ -98,6 +109,8 @@ describe("@terreno/ai public exports", () => {
98
109
  "remix",
99
110
  "summarization",
100
111
  "translation",
112
+ "ui_action",
113
+ "ui_blocks",
101
114
  ]);
102
115
  });
103
116
  });
package/src/index.ts CHANGED
@@ -1,3 +1,11 @@
1
+ export type {
2
+ Ask,
3
+ AskKind,
4
+ AskResponse,
5
+ AskValidationError,
6
+ SimpleCard,
7
+ SimpleCardButton,
8
+ } from "@terreno/blocks";
1
9
  export type {FlexibleSchema, JSONValue} from "ai";
2
10
  export {jsonSchema, Output} from "ai";
3
11
  export type {AIAdminAppOptions} from "./aiAdminApp";
@@ -28,17 +36,77 @@ export type {
28
36
  TraceListItem,
29
37
  } from "./langfuseTypes";
30
38
  export {createTelemetryConfig, preparePromptForAI} from "./langfuseVercelAi";
39
+ export {AIDataset} from "./models/aiDataset";
31
40
  export {AIRequest} from "./models/aiRequest";
32
41
  export {FileAttachment} from "./models/fileAttachment";
33
42
  export {GptHistory} from "./models/gptHistory";
34
43
  export {Project} from "./models/project";
44
+ export {AI_OBSERVABILITY_GROUP, observabilityAdminScreens} from "./observability/adminScreens";
45
+ export {LocalDatasetStore} from "./observability/local/datasetStore";
46
+ export {LocalEvaluatorStore} from "./observability/local/evaluatorStore";
47
+ export {LocalExperimentRunner} from "./observability/local/experimentRunner";
48
+ export {
49
+ createLocalObservabilityBundle,
50
+ createLocalObservabilityPlugin,
51
+ } from "./observability/local/localPlugin";
52
+ export {LocalPromptStore} from "./observability/local/promptStore";
53
+ export {LocalReviewStore} from "./observability/local/reviewStore";
54
+ export {
55
+ LocalScoreSink,
56
+ LocalTraceSink,
57
+ LocalTraceStore,
58
+ MemoryScoreSink,
59
+ MemoryTraceSink,
60
+ } from "./observability/local/traceStore";
61
+ export {
62
+ getObservabilityApp,
63
+ ObservabilityApp,
64
+ resetObservabilityApp,
65
+ } from "./observability/observabilityApp";
66
+ export type {
67
+ ObservabilityPluginStatus,
68
+ ObservabilityStatus,
69
+ PlaygroundAiSource,
70
+ PlaygroundAiStatus,
71
+ } from "./observability/status";
72
+ export {
73
+ buildObservabilityStatus,
74
+ buildPlaygroundAiStatus,
75
+ isLocalObservabilityPluginOn,
76
+ } from "./observability/status";
77
+ export type {
78
+ ControlPrimary,
79
+ ObservabilityAiServiceFactory,
80
+ ObservabilityAppOptions,
81
+ ObservabilityCapability,
82
+ ObservabilityControlConfig,
83
+ ObservabilityGenerateClient,
84
+ ObservabilityPlugin,
85
+ ObservabilityRequestAiServiceFactory,
86
+ PromptRegistry,
87
+ ReviewQueue,
88
+ ScoreRecord,
89
+ ScoreSink,
90
+ SpanRecord,
91
+ TraceExportResult,
92
+ TraceRecord,
93
+ TraceSink,
94
+ } from "./observability/types";
95
+ export {
96
+ DEFAULT_OBSERVABILITY_CONTROL,
97
+ resolveObservabilityControl,
98
+ validateObservabilityConfig,
99
+ } from "./observability/types";
35
100
  export {addAiRequestsExplorerRoutes} from "./routes/aiRequestsExplorer";
36
101
  export {addFileRoutes} from "./routes/files";
37
102
  export {addGptRoutes} from "./routes/gpt";
38
103
  export {addGptHistoryRoutes} from "./routes/gptHistories";
39
104
  export {addMcpRoutes} from "./routes/mcp";
40
105
  export {addProjectRoutes} from "./routes/projects";
106
+ export type {RegisteredDataset} from "./service/aiDatasets";
107
+ export {configureAiDatasets, registerAiDataset} from "./service/aiDatasets";
41
108
  export {AIService, TemperaturePresets} from "./service/aiService";
109
+ export {createAskTools} from "./service/asks";
42
110
  export {FileStorageService} from "./service/fileStorage";
43
111
  export type {ListGeminiApiModelsOptions} from "./service/gemini";
44
112
  export {
@@ -54,10 +122,12 @@ export {
54
122
  parseAiJson,
55
123
  } from "./service/parseAiJson";
56
124
  export {
125
+ COMPACT_SURFACE_SYSTEM_PROMPT,
57
126
  CONTENT_SUMMARY_PROMPT,
58
127
  DEFAULT_GPT_MEMORY,
59
128
  JSON_VALUE_SYSTEM_PROMPT,
60
129
  REMIX_PROMPT,
130
+ TERRENO_ASKS_SYSTEM_PROMPT,
61
131
  TITLE_GENERATION_PROMPT,
62
132
  TRANSLATION_PROMPT,
63
133
  } from "./service/prompts";
@@ -0,0 +1,143 @@
1
+ import {beforeEach, describe, expect, it} from "bun:test";
2
+ import {AdminApp} from "@terreno/admin-backend";
3
+ import {
4
+ addAuthRoutes,
5
+ apiErrorMiddleware,
6
+ apiUnauthorizedMiddleware,
7
+ createAccess,
8
+ setupAuth,
9
+ terrenoStatements,
10
+ type UserModel as UserModelType,
11
+ } from "@terreno/api";
12
+ import {getBaseServer, setupDb, UserModel} from "@terreno/api/testing";
13
+ import {authAsUser as loginWithCredentials} from "@terreno/test";
14
+ import mongoose from "mongoose";
15
+
16
+ import {observabilityAdminScreens} from "../observability/adminScreens";
17
+
18
+ const OBSERVABILITY_SCREEN_NAMES = [
19
+ "ai-prompts",
20
+ "ai-traces",
21
+ "ai-evaluators",
22
+ "ai-datasets",
23
+ "ai-experiments",
24
+ "ai-review",
25
+ ] as const;
26
+
27
+ const buildAdminConfigApp = (
28
+ grantsByEmail: Record<string, Record<string, string[]>>
29
+ ): ReturnType<typeof getBaseServer> => {
30
+ const app = getBaseServer();
31
+ setupAuth(app, UserModel as unknown as UserModelType);
32
+ addAuthRoutes(app, UserModel as unknown as UserModelType);
33
+
34
+ const accessControl = createAccess({
35
+ connection: mongoose.connection,
36
+ resolvePermissions: async ({user}) => {
37
+ const email = "email" in user && typeof user.email === "string" ? user.email : "";
38
+ return grantsByEmail[email] ?? {};
39
+ },
40
+ statements: terrenoStatements,
41
+ });
42
+
43
+ const admin = new AdminApp({
44
+ accessControl,
45
+ customScreens: observabilityAdminScreens({localOn: true}),
46
+ models: [],
47
+ });
48
+ admin.register(app);
49
+ app.use(apiUnauthorizedMiddleware);
50
+ app.use(apiErrorMiddleware);
51
+ return app;
52
+ };
53
+
54
+ const createPasswordUser = async ({
55
+ email,
56
+ password,
57
+ }: {
58
+ email: string;
59
+ password: string;
60
+ }): Promise<void> => {
61
+ const doc = await UserModel.create({
62
+ admin: false,
63
+ email,
64
+ name: email,
65
+ });
66
+ await (doc as unknown as {setPassword: (value: string) => Promise<void>}).setPassword(password);
67
+ await doc.save();
68
+ };
69
+
70
+ const loginAs = async (app: ReturnType<typeof getBaseServer>, email: string, password: string) => {
71
+ return loginWithCredentials(app, {email, password});
72
+ };
73
+
74
+ const screenNamesFromConfig = (body: {customScreens: {name: string}[]}): string[] => {
75
+ return body.customScreens.map((screen) => screen.name);
76
+ };
77
+
78
+ describe("observability admin config", () => {
79
+ beforeEach(async () => {
80
+ await setupDb();
81
+ await UserModel.deleteMany({});
82
+ });
83
+
84
+ it("prompts-only omits every other observability screen", async () => {
85
+ const password = "promptsOnlyPass123";
86
+ const app = buildAdminConfigApp({
87
+ "prompts-only@example.com": {
88
+ admin: ["access"],
89
+ aiPrompt: ["list", "read"],
90
+ },
91
+ });
92
+ await createPasswordUser({email: "prompts-only@example.com", password});
93
+
94
+ const agent = await loginAs(app, "prompts-only@example.com", password);
95
+ const response = await agent.get("/admin/config").expect(200);
96
+ const names = screenNamesFromConfig(response.body);
97
+
98
+ expect(names).toEqual(["ai-prompts"]);
99
+ for (const excluded of OBSERVABILITY_SCREEN_NAMES) {
100
+ if (excluded === "ai-prompts") {
101
+ continue;
102
+ }
103
+ expect(names).not.toContain(excluded);
104
+ }
105
+ });
106
+
107
+ it("traces-only omits prompts and other observability screens", async () => {
108
+ const password = "tracesOnlyPass123";
109
+ const app = buildAdminConfigApp({
110
+ "traces-only@example.com": {
111
+ admin: ["access"],
112
+ aiTrace: ["list", "read"],
113
+ },
114
+ });
115
+ await createPasswordUser({email: "traces-only@example.com", password});
116
+
117
+ const agent = await loginAs(app, "traces-only@example.com", password);
118
+ const response = await agent.get("/admin/config").expect(200);
119
+ const names = screenNamesFromConfig(response.body);
120
+
121
+ expect(names).toEqual(["ai-traces"]);
122
+ expect(names).not.toContain("ai-prompts");
123
+ expect(names).not.toContain("ai-review");
124
+ });
125
+
126
+ it("returns no observability screens when the caller has admin shell but no ai*:list grants", async () => {
127
+ const password = "shellOnlyPass123";
128
+ const app = buildAdminConfigApp({
129
+ "shell-only@example.com": {
130
+ admin: ["access"],
131
+ },
132
+ });
133
+ await createPasswordUser({email: "shell-only@example.com", password});
134
+
135
+ const agent = await loginAs(app, "shell-only@example.com", password);
136
+ const response = await agent.get("/admin/config").expect(200);
137
+ const names = screenNamesFromConfig(response.body);
138
+
139
+ for (const screenName of OBSERVABILITY_SCREEN_NAMES) {
140
+ expect(names).not.toContain(screenName);
141
+ }
142
+ });
143
+ });
@@ -0,0 +1,34 @@
1
+ import {afterEach, describe, expect, it} from "bun:test";
2
+ import mongoose from "mongoose";
3
+
4
+ import {AIDataset} from "./aiDataset";
5
+
6
+ describe("AIDataset model", () => {
7
+ afterEach(async () => {
8
+ await AIDataset.deleteMany({});
9
+ });
10
+
11
+ it("stores rows for an owner and aliases ownerId to userId", async () => {
12
+ const userId = new mongoose.Types.ObjectId();
13
+ const dataset = await AIDataset.create({
14
+ columns: [{name: "count", type: "number"}],
15
+ historyId: new mongoose.Types.ObjectId(),
16
+ rowCount: 1,
17
+ rows: [[3]],
18
+ userId,
19
+ });
20
+ expect(dataset.deleted).toBe(false);
21
+ expect(dataset.created).toBeInstanceOf(Date);
22
+ expect(dataset.expiresAt).toBeUndefined();
23
+ expect((dataset as unknown as {ownerId: mongoose.Types.ObjectId}).ownerId.toString()).toBe(
24
+ userId.toString()
25
+ );
26
+ });
27
+
28
+ it("expires documents that have expiresAt and keeps documents that do not", () => {
29
+ const indexes = AIDataset.schema.indexes();
30
+ expect(indexes).toEqual(
31
+ expect.arrayContaining([[{expiresAt: 1}, expect.objectContaining({expireAfterSeconds: 0})]])
32
+ );
33
+ });
34
+ });