@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
@@ -1,11 +1,14 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.AIService = exports.TemperaturePresets = void 0;
4
+ const node_crypto_1 = require("node:crypto");
4
5
  const api_1 = require("@terreno/api");
6
+ const blocks_1 = require("@terreno/blocks");
5
7
  const ai_1 = require("ai");
6
8
  const luxon_1 = require("luxon");
7
9
  const aiRequest_1 = require("../models/aiRequest");
8
- const parseAiJson_1 = require("./parseAiJson");
10
+ const observabilityAppRegistry_1 = require("../observability/observabilityAppRegistry");
11
+ const jsonFenceModel_1 = require("./jsonFenceModel");
9
12
  const prompts_1 = require("./prompts");
10
13
  exports.TemperaturePresets = {
11
14
  BALANCED: 0.7,
@@ -16,44 +19,20 @@ exports.TemperaturePresets = {
16
19
  MAXIMUM: 2.0,
17
20
  };
18
21
  /**
19
- * Wraps a language model so non-streaming `doGenerate` text parts are normalized via
20
- * {@link normalizeLlmJsonTextForStructuredOutput} (fences, preamble, balanced slice, light repairs)
21
- * before Vercel `Output.*` parsing.
22
+ * True for the call row of an ask the model made. Approval asks are display-only: the model did
23
+ * not call a tool by that name with those arguments, so they are not replayed.
22
24
  */
23
- const withStrippedJsonFencesModel = (model) => {
24
- if (typeof model === "string") {
25
- return model;
26
- }
27
- return new Proxy(model, {
28
- get(target, prop, receiver) {
29
- if (prop === "doGenerate") {
30
- const original = Reflect.get(target, prop, receiver);
31
- if (typeof original !== "function") {
32
- return original;
33
- }
34
- const boundGenerate = original;
35
- return async (options) => {
36
- const result = await Promise.resolve(boundGenerate.call(target, options));
37
- if (!result?.content || !Array.isArray(result.content)) {
38
- return result;
39
- }
40
- return {
41
- ...result,
42
- content: result.content.map((part) => {
43
- if (part.type !== "text" || typeof part.text !== "string") {
44
- return part;
45
- }
46
- return {
47
- ...part,
48
- text: (0, parseAiJson_1.normalizeLlmJsonTextForStructuredOutput)(part.text),
49
- };
50
- }),
51
- };
52
- };
53
- }
54
- return Reflect.get(target, prop, receiver);
55
- },
56
- });
25
+ const isModelAskCall = (prompt) => prompt.type === "tool-call" && prompt.ask !== undefined && prompt.ask.origin !== "approval";
26
+ /** Result rows of the model's ask tool calls, by tool call id. */
27
+ const collectAskResults = (prompts) => {
28
+ const askCallIds = new Set(prompts
29
+ .filter((prompt) => isModelAskCall(prompt) && prompt.toolCallId)
30
+ .map((prompt) => prompt.toolCallId));
31
+ return new Map(prompts
32
+ .filter((prompt) => prompt.type === "tool-result" &&
33
+ prompt.toolCallId !== undefined &&
34
+ askCallIds.has(prompt.toolCallId))
35
+ .map((prompt) => [prompt.toolCallId, prompt]));
57
36
  };
58
37
  const getModelId = (model) => {
59
38
  if (typeof model === "string") {
@@ -61,6 +40,27 @@ const getModelId = (model) => {
61
40
  }
62
41
  return model.modelId ?? "unknown";
63
42
  };
43
+ const toIsoUtc = (millis) => {
44
+ return luxon_1.DateTime.fromMillis(millis, { zone: "utc" }).toISO() ?? "";
45
+ };
46
+ const readTokenUsage = (usage) => {
47
+ return {
48
+ inputTokens: usage?.inputTokens ?? usage?.promptTokens,
49
+ outputTokens: usage?.outputTokens ?? usage?.completionTokens,
50
+ totalTokens: usage?.totalTokens,
51
+ };
52
+ };
53
+ const computeCostUsd = (params) => {
54
+ if (!params.priceMap) {
55
+ return undefined;
56
+ }
57
+ const price = params.priceMap[params.modelId];
58
+ if (!price || params.inputTokens === undefined || params.outputTokens === undefined) {
59
+ return undefined;
60
+ }
61
+ return ((price.inputPerMTok * params.inputTokens + price.outputPerMTok * params.outputTokens) /
62
+ 1_000_000);
63
+ };
64
64
  class AIService {
65
65
  model;
66
66
  defaultTemperature;
@@ -74,7 +74,7 @@ class AIService {
74
74
  }
75
75
  getModelForStructuredJson() {
76
76
  if (!this.structuredJsonModel) {
77
- this.structuredJsonModel = withStrippedJsonFencesModel(this.model);
77
+ this.structuredJsonModel = (0, jsonFenceModel_1.withStrippedJsonFencesModel)(this.model);
78
78
  }
79
79
  return this.structuredJsonModel;
80
80
  }
@@ -127,8 +127,7 @@ class AIService {
127
127
  response: rawText ?? "",
128
128
  system: params.system,
129
129
  });
130
- await this.logRequest({
131
- aiModel: getModelId(this.model),
130
+ await this.logRequestAndTrace({
132
131
  error: errorDescription,
133
132
  metadata: {
134
133
  errorStack,
@@ -136,10 +135,12 @@ class AIService {
136
135
  rawModelTextCaptured: Boolean(rawText && rawText.length > 0),
137
136
  system: params.system,
138
137
  },
138
+ observability: params.observability,
139
139
  prompt: params.prompt,
140
140
  requestType: params.requestType,
141
141
  response: responseForLog,
142
142
  responseTime: params.responseTime,
143
+ startTime: params.startTime,
143
144
  userId: params.userId,
144
145
  });
145
146
  }
@@ -151,8 +152,184 @@ class AIService {
151
152
  // Logging failures should not break the main flow
152
153
  }
153
154
  }
155
+ async resolveObservability(options) {
156
+ let promptRef;
157
+ let systemPrompt = options.systemPrompt;
158
+ let sensitive = options.sensitive ?? false;
159
+ if (options.promptName) {
160
+ const registry = (0, observabilityAppRegistry_1.getObservabilityApp)()?.promptRegistry;
161
+ if (!registry) {
162
+ throw new api_1.APIError({ status: 400, title: "Prompt registry is not configured" });
163
+ }
164
+ const label = options.promptLabel ?? "production";
165
+ const version = await registry.get({ label, name: options.promptName });
166
+ if (!version) {
167
+ throw new api_1.APIError({
168
+ status: 400,
169
+ title: `Unknown prompt "${options.promptName}" with label "${label}"`,
170
+ });
171
+ }
172
+ promptRef = version;
173
+ systemPrompt = version.body;
174
+ if (options.sensitive === undefined) {
175
+ sensitive = Boolean(version.sensitive);
176
+ }
177
+ }
178
+ return {
179
+ priceMap: options.priceMap,
180
+ promptRef,
181
+ sensitive,
182
+ sessionId: options.sessionId,
183
+ skipTrace: options.skipTrace === true,
184
+ systemPrompt,
185
+ };
186
+ }
187
+ observabilityFields(options) {
188
+ return {
189
+ priceMap: options.priceMap,
190
+ promptLabel: options.promptLabel,
191
+ promptName: options.promptName,
192
+ sensitive: options.sensitive,
193
+ sessionId: options.sessionId,
194
+ skipTrace: options.skipTrace,
195
+ };
196
+ }
197
+ buildTraceRecord(params) {
198
+ const modelId = getModelId(this.model);
199
+ const priceMap = params.observability.priceMap;
200
+ const app = (0, observabilityAppRegistry_1.getObservabilityApp)();
201
+ const effectivePriceMap = priceMap ?? app?.priceMap;
202
+ const costUsd = computeCostUsd({
203
+ inputTokens: params.inputTokens,
204
+ modelId,
205
+ outputTokens: params.outputTokens,
206
+ priceMap: effectivePriceMap,
207
+ });
208
+ const usage = {
209
+ inputTokens: params.inputTokens,
210
+ model: modelId,
211
+ outputTokens: params.outputTokens,
212
+ ...(costUsd === undefined ? {} : { costUsd }),
213
+ };
214
+ const status = params.error ? "error" : "ok";
215
+ const startedAt = toIsoUtc(params.startTime);
216
+ const endedAt = toIsoUtc(params.startTime + params.responseTime);
217
+ const spanName = params.observability.promptRef?.name ?? params.requestType;
218
+ const childSpans = params.childSpans ?? [];
219
+ let spans;
220
+ if (childSpans.length > 0) {
221
+ const rootId = (0, node_crypto_1.randomUUID)();
222
+ const rootSpan = {
223
+ durationMs: params.responseTime,
224
+ endedAt,
225
+ id: rootId,
226
+ input: params.prompt,
227
+ kind: "CHAIN",
228
+ name: spanName,
229
+ output: params.response,
230
+ startedAt,
231
+ status,
232
+ usage,
233
+ ...(params.error ? { error: params.error } : {}),
234
+ };
235
+ spans = [
236
+ rootSpan,
237
+ ...childSpans.map((child) => {
238
+ return {
239
+ ...child,
240
+ parentSpanId: child.parentSpanId ?? rootId,
241
+ };
242
+ }),
243
+ ];
244
+ }
245
+ else {
246
+ spans = [
247
+ {
248
+ durationMs: params.responseTime,
249
+ endedAt,
250
+ id: (0, node_crypto_1.randomUUID)(),
251
+ input: params.prompt,
252
+ kind: "LLM",
253
+ name: spanName,
254
+ output: params.response,
255
+ startedAt,
256
+ status,
257
+ usage,
258
+ ...(params.error ? { error: params.error } : {}),
259
+ },
260
+ ];
261
+ }
262
+ const promptRef = params.observability.promptRef;
263
+ return {
264
+ endedAt,
265
+ id: (0, node_crypto_1.randomUUID)(),
266
+ input: params.prompt,
267
+ name: spanName,
268
+ output: params.response,
269
+ prompts: promptRef
270
+ ? [{ label: promptRef.label, name: promptRef.name, version: promptRef.version }]
271
+ : [],
272
+ sensitive: params.observability.sensitive,
273
+ sessionId: params.observability.sessionId,
274
+ spans,
275
+ startedAt,
276
+ status,
277
+ usage,
278
+ userId: params.userId?.toString(),
279
+ ...(params.error ? { errorSummary: params.error } : {}),
280
+ };
281
+ }
282
+ async logRequestAndTrace(params) {
283
+ await this.logRequest({
284
+ aiModel: getModelId(this.model),
285
+ error: params.error,
286
+ metadata: {
287
+ ...params.metadata,
288
+ ...(params.inputTokens === undefined ? {} : { inputTokens: params.inputTokens }),
289
+ ...(params.outputTokens === undefined ? {} : { outputTokens: params.outputTokens }),
290
+ },
291
+ prompt: params.prompt,
292
+ requestType: params.requestType,
293
+ response: params.response,
294
+ responseTime: params.responseTime,
295
+ tokensUsed: params.tokensUsed,
296
+ userId: params.userId,
297
+ });
298
+ const app = (0, observabilityAppRegistry_1.getObservabilityApp)();
299
+ if (!app || params.observability.skipTrace) {
300
+ return;
301
+ }
302
+ const trace = this.buildTraceRecord({
303
+ childSpans: params.childSpans,
304
+ error: params.error,
305
+ inputTokens: params.inputTokens,
306
+ observability: params.observability,
307
+ outputTokens: params.outputTokens,
308
+ prompt: params.prompt,
309
+ requestType: params.requestType,
310
+ response: params.response,
311
+ responseTime: params.responseTime,
312
+ startTime: params.startTime,
313
+ userId: params.userId,
314
+ });
315
+ const results = await Promise.allSettled(app.traceSinks.map((sink) => {
316
+ return sink.export(trace);
317
+ }));
318
+ for (const result of results) {
319
+ if (result.status === "rejected") {
320
+ api_1.logger.error("Observability TraceSink.export failed", { error: result.reason });
321
+ }
322
+ }
323
+ }
324
+ async resolveGenerateObservability(options) {
325
+ return this.resolveObservability(options);
326
+ }
327
+ async recordGenerate(params) {
328
+ await this.logRequestAndTrace(params);
329
+ }
154
330
  async generateText(options) {
155
- const { prompt, systemPrompt, temperature, maxOutputTokens, userId } = options;
331
+ const observability = await this.resolveObservability(options);
332
+ const { prompt, temperature, maxOutputTokens, userId } = options;
156
333
  const startTime = luxon_1.DateTime.now().toMillis();
157
334
  try {
158
335
  const result = await (0, ai_1.generateText)({
@@ -160,39 +337,136 @@ class AIService {
160
337
  maxOutputTokens,
161
338
  model: this.model,
162
339
  prompt,
163
- system: systemPrompt,
340
+ system: observability.systemPrompt,
164
341
  temperature: temperature ?? this.defaultTemperature,
165
342
  });
166
343
  const responseTime = luxon_1.DateTime.now().toMillis() - startTime;
167
- await this.logRequest({
168
- aiModel: getModelId(this.model),
344
+ const tokens = readTokenUsage(result.usage);
345
+ await this.logRequestAndTrace({
346
+ inputTokens: tokens.inputTokens,
347
+ observability,
348
+ outputTokens: tokens.outputTokens,
169
349
  prompt,
170
350
  requestType: "general",
171
351
  response: result.text,
172
352
  responseTime,
173
- tokensUsed: result.usage?.totalTokens,
353
+ startTime,
354
+ tokensUsed: tokens.totalTokens,
174
355
  userId,
175
356
  });
176
357
  return result.text;
177
358
  }
178
359
  catch (error) {
179
360
  const responseTime = luxon_1.DateTime.now().toMillis() - startTime;
180
- await this.logRequest({
181
- aiModel: getModelId(this.model),
361
+ await this.logRequestAndTrace({
182
362
  error: error instanceof Error ? error.message : String(error),
363
+ observability,
183
364
  prompt,
184
365
  requestType: "general",
185
366
  responseTime,
367
+ startTime,
186
368
  userId,
187
369
  });
188
370
  throw error;
189
371
  }
190
372
  }
373
+ /**
374
+ * One block document. Uses deterministic temperature. A failed check is repaired once unless
375
+ * `repair` is false. A second failure throws 422 and logs `metadata.errorCodes`.
376
+ */
377
+ async generateBlocks(options) {
378
+ const { prompt, repair = true, systemPrompt, userId } = options;
379
+ const system = systemPrompt ?? prompts_1.TERRENO_UI_BLOCKS_SYSTEM_PROMPT;
380
+ const startTime = luxon_1.DateTime.now().toMillis();
381
+ const requestDocument = async (userPrompt) => {
382
+ try {
383
+ const result = await (0, ai_1.generateText)({
384
+ experimental_telemetry: { functionId: "generate-blocks", isEnabled: true },
385
+ model: this.getModelForStructuredJson(),
386
+ output: ai_1.Output.object({
387
+ schema: (0, ai_1.jsonSchema)(blocks_1.blocksJsonSchema),
388
+ }),
389
+ prompt: userPrompt,
390
+ system,
391
+ temperature: exports.TemperaturePresets.DETERMINISTIC,
392
+ });
393
+ return { document: result.output };
394
+ }
395
+ catch (error) {
396
+ return { error: error instanceof Error ? error.message : String(error) };
397
+ }
398
+ };
399
+ const finish = async ({ error, errorCodes, response, }) => {
400
+ await this.logRequest({
401
+ aiModel: getModelId(this.model),
402
+ error,
403
+ metadata: errorCodes ? { errorCodes } : undefined,
404
+ prompt,
405
+ requestType: "ui_blocks",
406
+ response,
407
+ responseTime: luxon_1.DateTime.now().toMillis() - startTime,
408
+ userId,
409
+ });
410
+ };
411
+ const first = await requestDocument(prompt);
412
+ if (!first.document) {
413
+ await finish({ error: first.error ?? "Block generation failed" });
414
+ throw new api_1.APIError({ status: 502, title: "Block generation failed" });
415
+ }
416
+ const firstCheck = (0, blocks_1.validateBlocks)(first.document);
417
+ if (first.document && firstCheck?.ok) {
418
+ await finish({ response: JSON.stringify(first.document) });
419
+ return first.document;
420
+ }
421
+ const firstCodes = firstCheck && !firstCheck.ok ? firstCheck.errors.map((error) => error.code) : [];
422
+ const firstDetail = firstCheck && !firstCheck.ok
423
+ ? firstCheck.errors
424
+ .map((error) => `${error.path} ${error.code} ${error.message}`)
425
+ .join("\n")
426
+ : first.error;
427
+ if (!repair) {
428
+ await finish({
429
+ error: firstDetail ?? "Block document failed validation",
430
+ errorCodes: firstCodes,
431
+ });
432
+ throw new api_1.APIError({
433
+ meta: { fields: Object.fromEntries(firstCodes.map((code) => [code, code])) },
434
+ status: 422,
435
+ title: "Block document failed validation",
436
+ });
437
+ }
438
+ const second = await requestDocument(`${prompt}\n\nErrors:\n${firstDetail ?? "invalid document"}`);
439
+ if (!second.document) {
440
+ await finish({ error: second.error ?? "Block generation failed" });
441
+ throw new api_1.APIError({ status: 502, title: "Block generation failed" });
442
+ }
443
+ const secondCheck = (0, blocks_1.validateBlocks)(second.document);
444
+ if (second.document && secondCheck?.ok) {
445
+ await finish({ response: JSON.stringify(second.document) });
446
+ return second.document;
447
+ }
448
+ const errorCodes = secondCheck && !secondCheck.ok ? secondCheck.errors.map((error) => error.code) : firstCodes;
449
+ await finish({
450
+ error: "Block document failed validation",
451
+ errorCodes,
452
+ });
453
+ throw new api_1.APIError({
454
+ meta: {
455
+ fields: Object.fromEntries((errorCodes.length > 0 ? errorCodes : ["invalid"]).map((code) => [code, code])),
456
+ },
457
+ status: 422,
458
+ title: "Block document failed validation",
459
+ });
460
+ }
191
461
  /** Any JSON value (object, array, primitive, or null) via the AI SDK `Output.json()` parser. */
192
462
  async generateJsonValue(options) {
193
- const { maxOutputTokens, outputDescription, outputName, prompt, systemPrompt, temperature, userId, } = options;
463
+ const observability = await this.resolveObservability({
464
+ ...options,
465
+ systemPrompt: options.systemPrompt ?? prompts_1.JSON_VALUE_SYSTEM_PROMPT,
466
+ });
467
+ const { maxOutputTokens, outputDescription, outputName, prompt, temperature, userId } = options;
194
468
  const startTime = luxon_1.DateTime.now().toMillis();
195
- const system = systemPrompt ?? prompts_1.JSON_VALUE_SYSTEM_PROMPT;
469
+ const system = observability.systemPrompt ?? prompts_1.JSON_VALUE_SYSTEM_PROMPT;
196
470
  try {
197
471
  const result = await (0, ai_1.generateText)({
198
472
  experimental_telemetry: { functionId: "generate-json-value", isEnabled: true },
@@ -204,13 +478,17 @@ class AIService {
204
478
  temperature: temperature ?? exports.TemperaturePresets.DETERMINISTIC,
205
479
  });
206
480
  const responseTime = luxon_1.DateTime.now().toMillis() - startTime;
207
- await this.logRequest({
208
- aiModel: getModelId(this.model),
481
+ const tokens = readTokenUsage(result.usage);
482
+ await this.logRequestAndTrace({
483
+ inputTokens: tokens.inputTokens,
484
+ observability,
485
+ outputTokens: tokens.outputTokens,
209
486
  prompt,
210
487
  requestType: "json_value",
211
488
  response: JSON.stringify(result.output),
212
489
  responseTime,
213
- tokensUsed: result.usage?.totalTokens,
490
+ startTime,
491
+ tokensUsed: tokens.totalTokens,
214
492
  userId,
215
493
  });
216
494
  return result.output;
@@ -219,9 +497,11 @@ class AIService {
219
497
  const responseTime = luxon_1.DateTime.now().toMillis() - startTime;
220
498
  await this.logStructuredJsonFailure({
221
499
  error,
500
+ observability,
222
501
  prompt,
223
502
  requestType: "json_value",
224
503
  responseTime,
504
+ startTime,
225
505
  system,
226
506
  userId,
227
507
  });
@@ -230,9 +510,13 @@ class AIService {
230
510
  }
231
511
  /** Typed object from a Zod schema, `jsonSchema(...)`, or other `FlexibleSchema` (`Output.object()`). */
232
512
  async generateJsonObject(options) {
233
- const { maxOutputTokens, prompt, schema, schemaDescription, schemaName, systemPrompt, temperature, userId, } = options;
513
+ const observability = await this.resolveObservability({
514
+ ...options,
515
+ systemPrompt: options.systemPrompt ?? prompts_1.JSON_VALUE_SYSTEM_PROMPT,
516
+ });
517
+ const { maxOutputTokens, prompt, schema, schemaDescription, schemaName, temperature, userId } = options;
234
518
  const startTime = luxon_1.DateTime.now().toMillis();
235
- const system = systemPrompt ?? prompts_1.JSON_VALUE_SYSTEM_PROMPT;
519
+ const system = observability.systemPrompt ?? prompts_1.JSON_VALUE_SYSTEM_PROMPT;
236
520
  try {
237
521
  const result = await (0, ai_1.generateText)({
238
522
  experimental_telemetry: { functionId: "generate-json-object", isEnabled: true },
@@ -248,13 +532,17 @@ class AIService {
248
532
  temperature: temperature ?? exports.TemperaturePresets.DETERMINISTIC,
249
533
  });
250
534
  const responseTime = luxon_1.DateTime.now().toMillis() - startTime;
251
- await this.logRequest({
252
- aiModel: getModelId(this.model),
535
+ const tokens = readTokenUsage(result.usage);
536
+ await this.logRequestAndTrace({
537
+ inputTokens: tokens.inputTokens,
538
+ observability,
539
+ outputTokens: tokens.outputTokens,
253
540
  prompt,
254
541
  requestType: "json_object",
255
542
  response: JSON.stringify(result.output),
256
543
  responseTime,
257
- tokensUsed: result.usage?.totalTokens,
544
+ startTime,
545
+ tokensUsed: tokens.totalTokens,
258
546
  userId,
259
547
  });
260
548
  return result.output;
@@ -263,9 +551,11 @@ class AIService {
263
551
  const responseTime = luxon_1.DateTime.now().toMillis() - startTime;
264
552
  await this.logStructuredJsonFailure({
265
553
  error,
554
+ observability,
266
555
  prompt,
267
556
  requestType: "json_object",
268
557
  responseTime,
558
+ startTime,
269
559
  system,
270
560
  userId,
271
561
  });
@@ -277,9 +567,13 @@ class AIService {
277
567
  * and this method returns the plain array (`Output.array()`).
278
568
  */
279
569
  async generateJsonArray(options) {
280
- const { element, maxOutputTokens, outputDescription, outputName, prompt, systemPrompt, temperature, userId, } = options;
570
+ const observability = await this.resolveObservability({
571
+ ...options,
572
+ systemPrompt: options.systemPrompt ?? prompts_1.JSON_VALUE_SYSTEM_PROMPT,
573
+ });
574
+ const { element, maxOutputTokens, outputDescription, outputName, prompt, temperature, userId } = options;
281
575
  const startTime = luxon_1.DateTime.now().toMillis();
282
- const system = systemPrompt ?? prompts_1.JSON_VALUE_SYSTEM_PROMPT;
576
+ const system = observability.systemPrompt ?? prompts_1.JSON_VALUE_SYSTEM_PROMPT;
283
577
  try {
284
578
  const result = await (0, ai_1.generateText)({
285
579
  experimental_telemetry: { functionId: "generate-json-array", isEnabled: true },
@@ -295,13 +589,17 @@ class AIService {
295
589
  temperature: temperature ?? exports.TemperaturePresets.DETERMINISTIC,
296
590
  });
297
591
  const responseTime = luxon_1.DateTime.now().toMillis() - startTime;
298
- await this.logRequest({
299
- aiModel: getModelId(this.model),
592
+ const tokens = readTokenUsage(result.usage);
593
+ await this.logRequestAndTrace({
594
+ inputTokens: tokens.inputTokens,
595
+ observability,
596
+ outputTokens: tokens.outputTokens,
300
597
  prompt,
301
598
  requestType: "json_array",
302
599
  response: JSON.stringify(result.output),
303
600
  responseTime,
304
- tokensUsed: result.usage?.totalTokens,
601
+ startTime,
602
+ tokensUsed: tokens.totalTokens,
305
603
  userId,
306
604
  });
307
605
  return result.output;
@@ -310,9 +608,11 @@ class AIService {
310
608
  const responseTime = luxon_1.DateTime.now().toMillis() - startTime;
311
609
  await this.logStructuredJsonFailure({
312
610
  error,
611
+ observability,
313
612
  prompt,
314
613
  requestType: "json_array",
315
614
  responseTime,
615
+ startTime,
316
616
  system,
317
617
  userId,
318
618
  });
@@ -320,7 +620,8 @@ class AIService {
320
620
  }
321
621
  }
322
622
  async *generateTextStream(options) {
323
- const { prompt, systemPrompt, temperature, maxOutputTokens, userId } = options;
623
+ const observability = await this.resolveObservability(options);
624
+ const { prompt, temperature, maxOutputTokens, userId } = options;
324
625
  const startTime = luxon_1.DateTime.now().toMillis();
325
626
  let fullResponse = "";
326
627
  try {
@@ -329,7 +630,7 @@ class AIService {
329
630
  maxOutputTokens,
330
631
  model: this.model,
331
632
  prompt,
332
- system: systemPrompt,
633
+ system: observability.systemPrompt,
333
634
  temperature: temperature ?? this.defaultTemperature,
334
635
  });
335
636
  for await (const chunk of result.textStream) {
@@ -338,24 +639,29 @@ class AIService {
338
639
  }
339
640
  const responseTime = luxon_1.DateTime.now().toMillis() - startTime;
340
641
  const usage = await result.usage;
341
- await this.logRequest({
342
- aiModel: getModelId(this.model),
642
+ const tokens = readTokenUsage(usage);
643
+ await this.logRequestAndTrace({
644
+ inputTokens: tokens.inputTokens,
645
+ observability,
646
+ outputTokens: tokens.outputTokens,
343
647
  prompt,
344
648
  requestType: "general",
345
649
  response: fullResponse,
346
650
  responseTime,
347
- tokensUsed: usage?.totalTokens,
651
+ startTime,
652
+ tokensUsed: tokens.totalTokens,
348
653
  userId,
349
654
  });
350
655
  }
351
656
  catch (error) {
352
657
  const responseTime = luxon_1.DateTime.now().toMillis() - startTime;
353
- await this.logRequest({
354
- aiModel: getModelId(this.model),
658
+ await this.logRequestAndTrace({
355
659
  error: error instanceof Error ? error.message : String(error),
660
+ observability,
356
661
  prompt,
357
662
  requestType: "general",
358
663
  responseTime,
664
+ startTime,
359
665
  userId,
360
666
  });
361
667
  throw error;
@@ -363,6 +669,7 @@ class AIService {
363
669
  }
364
670
  async generateRemix(options) {
365
671
  return this.generateText({
672
+ ...this.observabilityFields(options),
366
673
  prompt: options.text,
367
674
  systemPrompt: prompts_1.REMIX_PROMPT,
368
675
  temperature: exports.TemperaturePresets.BALANCED,
@@ -371,6 +678,7 @@ class AIService {
371
678
  }
372
679
  async generateSummary(options) {
373
680
  return this.generateText({
681
+ ...this.observabilityFields(options),
374
682
  prompt: options.text,
375
683
  systemPrompt: prompts_1.CONTENT_SUMMARY_PROMPT,
376
684
  temperature: exports.TemperaturePresets.LOW,
@@ -381,18 +689,55 @@ class AIService {
381
689
  const { text, targetLanguage, sourceLanguage = "auto-detect", userId } = options;
382
690
  const systemPrompt = prompts_1.TRANSLATION_PROMPT.replace("{sourceLanguage}", sourceLanguage).replace("{targetLanguage}", targetLanguage);
383
691
  return this.generateText({
692
+ ...this.observabilityFields(options),
384
693
  prompt: text,
385
694
  systemPrompt,
386
695
  temperature: exports.TemperaturePresets.LOW,
387
696
  userId,
388
697
  });
389
698
  }
699
+ /**
700
+ * Converts history rows to model messages. Ask call/result pairs are kept so the model sees what
701
+ * it asked and how the user answered; other tool rows, approval asks, and asks still waiting for
702
+ * an answer are skipped. Consecutive ask calls came from one step and share one assistant message.
703
+ */
390
704
  buildMessages(prompts) {
391
705
  const messages = [];
706
+ const askResults = collectAskResults(prompts);
707
+ let askStep;
392
708
  for (const prompt of prompts) {
709
+ if (isModelAskCall(prompt)) {
710
+ const askResult = prompt.toolCallId ? askResults.get(prompt.toolCallId) : undefined;
711
+ if (!askResult || !prompt.toolCallId || !prompt.toolName) {
712
+ continue;
713
+ }
714
+ if (!askStep) {
715
+ askStep = { calls: [], results: [] };
716
+ messages.push({ content: askStep.calls, role: "assistant" }, { content: askStep.results, role: "tool" });
717
+ }
718
+ askStep.calls.push({
719
+ input: prompt.args ?? {},
720
+ toolCallId: prompt.toolCallId,
721
+ toolName: prompt.toolName,
722
+ type: "tool-call",
723
+ });
724
+ askStep.results.push({
725
+ output: { type: "json", value: (askResult.result ?? null) },
726
+ toolCallId: prompt.toolCallId,
727
+ toolName: prompt.toolName,
728
+ type: "tool-result",
729
+ });
730
+ continue;
731
+ }
732
+ askStep = undefined;
393
733
  if (prompt.type === "tool-call" || prompt.type === "tool-result") {
394
734
  continue;
395
735
  }
736
+ // A reply still streaming (or one that failed before any output) is not a real turn yet
737
+ if (prompt.status === "streaming" ||
738
+ (prompt.status === "error" && !prompt.text && !prompt.content?.length)) {
739
+ continue;
740
+ }
396
741
  const role = prompt.type;
397
742
  if (prompt.content && prompt.content.length > 0 && role === "user") {
398
743
  const parts = [];
@@ -434,7 +779,11 @@ class AIService {
434
779
  return messages;
435
780
  }
436
781
  async *generateChatStream(options) {
437
- const { messages, systemPrompt, tools, toolChoice, stopWhen, userId } = options;
782
+ const observability = await this.resolveObservability({
783
+ ...options,
784
+ systemPrompt: options.systemPrompt ?? prompts_1.DEFAULT_GPT_MEMORY,
785
+ });
786
+ const { messages, tools, toolChoice, stopWhen, userId } = options;
438
787
  const startTime = luxon_1.DateTime.now().toMillis();
439
788
  let fullResponse = "";
440
789
  const promptText = messages.map((m) => `${m.role}: ${m.content}`).join("\n");
@@ -444,7 +793,7 @@ class AIService {
444
793
  messages: messages.map((m) => ({ content: m.content, role: m.role })),
445
794
  model: this.model,
446
795
  stopWhen: stopWhen ?? (0, ai_1.stepCountIs)(1),
447
- system: systemPrompt ?? prompts_1.DEFAULT_GPT_MEMORY,
796
+ system: observability.systemPrompt ?? prompts_1.DEFAULT_GPT_MEMORY,
448
797
  temperature: this.defaultTemperature,
449
798
  toolChoice,
450
799
  tools,
@@ -455,24 +804,29 @@ class AIService {
455
804
  }
456
805
  const responseTime = luxon_1.DateTime.now().toMillis() - startTime;
457
806
  const usage = await result.usage;
458
- await this.logRequest({
459
- aiModel: getModelId(this.model),
807
+ const tokens = readTokenUsage(usage);
808
+ await this.logRequestAndTrace({
809
+ inputTokens: tokens.inputTokens,
810
+ observability,
811
+ outputTokens: tokens.outputTokens,
460
812
  prompt: promptText,
461
813
  requestType: "general",
462
814
  response: fullResponse,
463
815
  responseTime,
464
- tokensUsed: usage?.totalTokens,
816
+ startTime,
817
+ tokensUsed: tokens.totalTokens,
465
818
  userId,
466
819
  });
467
820
  }
468
821
  catch (error) {
469
822
  const responseTime = luxon_1.DateTime.now().toMillis() - startTime;
470
- await this.logRequest({
471
- aiModel: getModelId(this.model),
823
+ await this.logRequestAndTrace({
472
824
  error: error instanceof Error ? error.message : String(error),
825
+ observability,
473
826
  prompt: promptText,
474
827
  requestType: "general",
475
828
  responseTime,
829
+ startTime,
476
830
  userId,
477
831
  });
478
832
  throw error;