@terreno/ai 57.9.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 (412) 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 +127 -0
  28. package/dist/harness/errors.js +101 -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 +520 -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 +186 -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 +57 -0
  80. package/dist/harness/runners/inProcessRunner.js +196 -0
  81. package/dist/harness/runtime.d.ts +34 -0
  82. package/dist/harness/runtime.js +551 -0
  83. package/dist/harness/subagent.d.ts +53 -0
  84. package/dist/harness/subagent.js +134 -0
  85. package/dist/harness/suspend.d.ts +8 -0
  86. package/dist/harness/suspend.js +16 -0
  87. package/dist/harness/transaction.d.ts +3 -0
  88. package/dist/harness/transaction.js +22 -0
  89. package/dist/harness/waits.d.ts +100 -0
  90. package/dist/harness/waits.js +214 -0
  91. package/dist/index.d.ts +19 -1
  92. package/dist/index.js +45 -2
  93. package/dist/models/aiDataset.d.ts +2 -0
  94. package/dist/models/aiDataset.js +67 -0
  95. package/dist/models/aiRequest.js +1 -1
  96. package/dist/models/gptHistory.js +73 -0
  97. package/dist/observability/adminScreens.d.ts +5 -0
  98. package/dist/observability/adminScreens.js +32 -0
  99. package/dist/observability/compileTemplate.d.ts +1 -0
  100. package/dist/observability/compileTemplate.js +13 -0
  101. package/dist/observability/datasetImport.d.ts +11 -0
  102. package/dist/observability/datasetImport.js +185 -0
  103. package/dist/observability/evaluate.d.ts +31 -0
  104. package/dist/observability/evaluate.js +200 -0
  105. package/dist/observability/evaluatorTemplates.d.ts +17 -0
  106. package/dist/observability/evaluatorTemplates.js +111 -0
  107. package/dist/observability/jsonPath.d.ts +2 -0
  108. package/dist/observability/jsonPath.js +57 -0
  109. package/dist/observability/local/datasetStore.d.ts +87 -0
  110. package/dist/observability/local/datasetStore.js +326 -0
  111. package/dist/observability/local/evaluatorStore.d.ts +44 -0
  112. package/dist/observability/local/evaluatorStore.js +182 -0
  113. package/dist/observability/local/experimentRunner.d.ts +96 -0
  114. package/dist/observability/local/experimentRunner.js +419 -0
  115. package/dist/observability/local/localPlugin.d.ts +22 -0
  116. package/dist/observability/local/localPlugin.js +76 -0
  117. package/dist/observability/local/models/obsDataset.d.ts +2 -0
  118. package/dist/observability/local/models/obsDataset.js +37 -0
  119. package/dist/observability/local/models/obsDatasetItem.d.ts +2 -0
  120. package/dist/observability/local/models/obsDatasetItem.js +76 -0
  121. package/dist/observability/local/models/obsEvaluator.d.ts +2 -0
  122. package/dist/observability/local/models/obsEvaluator.js +98 -0
  123. package/dist/observability/local/models/obsExperiment.d.ts +2 -0
  124. package/dist/observability/local/models/obsExperiment.js +87 -0
  125. package/dist/observability/local/models/obsExperimentItem.d.ts +2 -0
  126. package/dist/observability/local/models/obsExperimentItem.js +45 -0
  127. package/dist/observability/local/models/obsPrompt.d.ts +2 -0
  128. package/dist/observability/local/models/obsPrompt.js +42 -0
  129. package/dist/observability/local/models/obsPromptLabel.d.ts +2 -0
  130. package/dist/observability/local/models/obsPromptLabel.js +39 -0
  131. package/dist/observability/local/models/obsPromptVersion.d.ts +2 -0
  132. package/dist/observability/local/models/obsPromptVersion.js +96 -0
  133. package/dist/observability/local/models/obsReviewItem.d.ts +2 -0
  134. package/dist/observability/local/models/obsReviewItem.js +75 -0
  135. package/dist/observability/local/models/obsScore.d.ts +2 -0
  136. package/dist/observability/local/models/obsScore.js +58 -0
  137. package/dist/observability/local/models/obsSpan.d.ts +2 -0
  138. package/dist/observability/local/models/obsSpan.js +64 -0
  139. package/dist/observability/local/models/obsTrace.d.ts +2 -0
  140. package/dist/observability/local/models/obsTrace.js +93 -0
  141. package/dist/observability/local/promptStore.d.ts +180 -0
  142. package/dist/observability/local/promptStore.js +444 -0
  143. package/dist/observability/local/reviewStore.d.ts +50 -0
  144. package/dist/observability/local/reviewStore.js +203 -0
  145. package/dist/observability/local/traceStore.d.ts +103 -0
  146. package/dist/observability/local/traceStore.js +325 -0
  147. package/dist/observability/observabilityApp.d.ts +24 -0
  148. package/dist/observability/observabilityApp.js +157 -0
  149. package/dist/observability/observabilityAppRegistry.d.ts +14 -0
  150. package/dist/observability/observabilityAppRegistry.js +16 -0
  151. package/dist/observability/permissions.d.ts +7 -0
  152. package/dist/observability/permissions.js +60 -0
  153. package/dist/observability/reviewPanels.d.ts +18 -0
  154. package/dist/observability/reviewPanels.js +46 -0
  155. package/dist/observability/routes/datasets.d.ts +7 -0
  156. package/dist/observability/routes/datasets.js +149 -0
  157. package/dist/observability/routes/evaluators.d.ts +7 -0
  158. package/dist/observability/routes/evaluators.js +123 -0
  159. package/dist/observability/routes/experiments.d.ts +7 -0
  160. package/dist/observability/routes/experiments.js +95 -0
  161. package/dist/observability/routes/observabilityRouteAccess.d.ts +13 -0
  162. package/dist/observability/routes/observabilityRouteAccess.js +67 -0
  163. package/dist/observability/routes/prompts.d.ts +12 -0
  164. package/dist/observability/routes/prompts.js +172 -0
  165. package/dist/observability/routes/requestAiService.d.ts +13 -0
  166. package/dist/observability/routes/requestAiService.js +21 -0
  167. package/dist/observability/routes/review.d.ts +7 -0
  168. package/dist/observability/routes/review.js +79 -0
  169. package/dist/observability/routes/status.d.ts +5 -0
  170. package/dist/observability/routes/status.js +31 -0
  171. package/dist/observability/routes/testMultiStage.d.ts +9 -0
  172. package/dist/observability/routes/testMultiStage.js +67 -0
  173. package/dist/observability/routes/traces.d.ts +7 -0
  174. package/dist/observability/routes/traces.js +129 -0
  175. package/dist/observability/schemaValidation.d.ts +5 -0
  176. package/dist/observability/schemaValidation.js +26 -0
  177. package/dist/observability/sopThresholds.d.ts +2 -0
  178. package/dist/observability/sopThresholds.js +21 -0
  179. package/dist/observability/status.d.ts +23 -0
  180. package/dist/observability/status.js +34 -0
  181. package/dist/observability/testMultiStageSchemas.d.ts +10 -0
  182. package/dist/observability/testMultiStageSchemas.js +79 -0
  183. package/dist/observability/testMultiStageWorkflow.d.ts +20 -0
  184. package/dist/observability/testMultiStageWorkflow.js +218 -0
  185. package/dist/observability/types.d.ts +159 -0
  186. package/dist/observability/types.js +67 -0
  187. package/dist/routes/files.js +18 -7
  188. package/dist/routes/gpt.js +79 -689
  189. package/dist/routes/gptActions.d.ts +9 -0
  190. package/dist/routes/gptActions.js +212 -0
  191. package/dist/routes/gptDatasets.d.ts +5 -0
  192. package/dist/routes/gptDatasets.js +94 -0
  193. package/dist/routes/gptHistories.d.ts +3 -1
  194. package/dist/routes/gptHistories.js +188 -2
  195. package/dist/service/aiDatasets.d.ts +57 -0
  196. package/dist/service/aiDatasets.js +253 -0
  197. package/dist/service/aiService.d.ts +50 -1
  198. package/dist/service/aiService.js +426 -77
  199. package/dist/service/askFiles.d.ts +49 -0
  200. package/dist/service/askFiles.js +162 -0
  201. package/dist/service/asks.d.ts +76 -0
  202. package/dist/service/asks.js +284 -0
  203. package/dist/service/chatTurn.d.ts +128 -0
  204. package/dist/service/chatTurn.js +1674 -0
  205. package/dist/service/fileStorage.d.ts +4 -0
  206. package/dist/service/fileStorage.js +7 -2
  207. package/dist/service/fillActionIds.d.ts +6 -0
  208. package/dist/service/fillActionIds.js +79 -0
  209. package/dist/service/jsonFenceModel.d.ts +7 -0
  210. package/dist/service/jsonFenceModel.js +45 -0
  211. package/dist/service/prompts.d.ts +52 -0
  212. package/dist/service/prompts.js +96 -1
  213. package/dist/service/sanitizeHtml.d.ts +11 -0
  214. package/dist/service/sanitizeHtml.js +108 -0
  215. package/dist/types/harness.d.ts +934 -0
  216. package/dist/types/harness.js +133 -0
  217. package/dist/types/index.d.ts +192 -14
  218. package/dist/types/index.js +2 -0
  219. package/dist/types/observability.d.ts +288 -0
  220. package/dist/types/observability.js +2 -0
  221. package/package.json +17 -5
  222. package/src/aiApp.test.ts +207 -1
  223. package/src/aiApp.ts +19 -6
  224. package/src/harness/adminScreens.test.ts +34 -0
  225. package/src/harness/adminScreens.ts +11 -0
  226. package/src/harness/agentLoop.test.ts +1034 -0
  227. package/src/harness/agentLoop.ts +1082 -0
  228. package/src/harness/agentRecovery.test.ts +380 -0
  229. package/src/harness/agentTaskNames.ts +4 -0
  230. package/src/harness/approvalPolicy.ts +28 -0
  231. package/src/harness/approvals.test.ts +1094 -0
  232. package/src/harness/approvals.ts +644 -0
  233. package/src/harness/commit.ts +1049 -0
  234. package/src/harness/conversation.ts +503 -0
  235. package/src/harness/defineAgent.test.ts +92 -0
  236. package/src/harness/defineAgent.ts +82 -0
  237. package/src/harness/defineTask.test.ts +59 -0
  238. package/src/harness/defineTask.ts +76 -0
  239. package/src/harness/defineTool.ts +45 -0
  240. package/src/harness/definitionError.ts +13 -0
  241. package/src/harness/errors.test.ts +90 -0
  242. package/src/harness/errors.ts +119 -0
  243. package/src/harness/eventHub.test.ts +124 -0
  244. package/src/harness/eventHub.ts +159 -0
  245. package/src/harness/events.test.ts +757 -0
  246. package/src/harness/events.ts +384 -0
  247. package/src/harness/executionEnv.ts +34 -0
  248. package/src/harness/extensions.test.ts +1218 -0
  249. package/src/harness/extensions.ts +382 -0
  250. package/src/harness/harness.test.ts +693 -0
  251. package/src/harness/harness.ts +767 -0
  252. package/src/harness/harnessApp.ts +76 -0
  253. package/src/harness/internalRuntime.ts +44 -0
  254. package/src/harness/leases.ts +132 -0
  255. package/src/harness/memo.ts +107 -0
  256. package/src/harness/modelCall.test.ts +134 -0
  257. package/src/harness/modelCall.ts +200 -0
  258. package/src/harness/models/harnessApproval.ts +109 -0
  259. package/src/harness/models/harnessConversation.ts +142 -0
  260. package/src/harness/models/harnessEvent.ts +92 -0
  261. package/src/harness/models/harnessInboxEvent.ts +73 -0
  262. package/src/harness/models/harnessMemo.ts +43 -0
  263. package/src/harness/models/harnessMessage.ts +85 -0
  264. package/src/harness/models/harnessModels.test.ts +60 -0
  265. package/src/harness/models/harnessOwner.ts +40 -0
  266. package/src/harness/models/harnessTask.ts +201 -0
  267. package/src/harness/ownership.test.ts +1087 -0
  268. package/src/harness/ownership.ts +618 -0
  269. package/src/harness/recovery.test.ts +912 -0
  270. package/src/harness/registry.ts +124 -0
  271. package/src/harness/retries.test.ts +292 -0
  272. package/src/harness/retryBackoff.test.ts +100 -0
  273. package/src/harness/retryBackoff.ts +77 -0
  274. package/src/harness/routes/approvals.ts +123 -0
  275. package/src/harness/routes/conversations.test.ts +518 -0
  276. package/src/harness/routes/conversations.ts +102 -0
  277. package/src/harness/routes/events.ts +280 -0
  278. package/src/harness/routes/tasks.test.ts +198 -0
  279. package/src/harness/routes/tasks.ts +146 -0
  280. package/src/harness/runners/inProcessRunner.test.ts +402 -0
  281. package/src/harness/runners/inProcessRunner.ts +220 -0
  282. package/src/harness/runtime.ts +714 -0
  283. package/src/harness/subagent.test.ts +696 -0
  284. package/src/harness/subagent.ts +192 -0
  285. package/src/harness/suspend.ts +13 -0
  286. package/src/harness/transaction.ts +17 -0
  287. package/src/harness/versioning.test.ts +288 -0
  288. package/src/harness/waits.test.ts +693 -0
  289. package/src/harness/waits.ts +356 -0
  290. package/src/index.test.ts +13 -0
  291. package/src/index.ts +70 -0
  292. package/src/isolated/adminScreensConfig.isolated.ts +143 -0
  293. package/src/models/aiDataset.test.ts +34 -0
  294. package/src/models/aiDataset.ts +78 -0
  295. package/src/models/aiRequest.ts +2 -1
  296. package/src/models/gptHistory.test.ts +156 -0
  297. package/src/models/gptHistory.ts +89 -1
  298. package/src/observability/adminScreens.test.ts +71 -0
  299. package/src/observability/adminScreens.ts +42 -0
  300. package/src/observability/compileTemplate.test.ts +14 -0
  301. package/src/observability/compileTemplate.ts +10 -0
  302. package/src/observability/datasetImport.test.ts +95 -0
  303. package/src/observability/datasetImport.ts +220 -0
  304. package/src/observability/evaluate.test.ts +549 -0
  305. package/src/observability/evaluate.ts +254 -0
  306. package/src/observability/evaluatorTemplates.ts +124 -0
  307. package/src/observability/jsonPath.test.ts +52 -0
  308. package/src/observability/jsonPath.ts +60 -0
  309. package/src/observability/local/datasetStore.test.ts +230 -0
  310. package/src/observability/local/datasetStore.ts +452 -0
  311. package/src/observability/local/evaluatorStore.test.ts +138 -0
  312. package/src/observability/local/evaluatorStore.ts +224 -0
  313. package/src/observability/local/experimentRunner.test.ts +489 -0
  314. package/src/observability/local/experimentRunner.ts +548 -0
  315. package/src/observability/local/localPlugin.ts +88 -0
  316. package/src/observability/local/models/obsDataset.ts +38 -0
  317. package/src/observability/local/models/obsDatasetItem.ts +80 -0
  318. package/src/observability/local/models/obsEvaluator.ts +102 -0
  319. package/src/observability/local/models/obsExperiment.ts +91 -0
  320. package/src/observability/local/models/obsExperimentItem.ts +52 -0
  321. package/src/observability/local/models/obsPrompt.ts +43 -0
  322. package/src/observability/local/models/obsPromptLabel.ts +43 -0
  323. package/src/observability/local/models/obsPromptVersion.ts +107 -0
  324. package/src/observability/local/models/obsReviewItem.ts +79 -0
  325. package/src/observability/local/models/obsScore.ts +59 -0
  326. package/src/observability/local/models/obsSpan.ts +65 -0
  327. package/src/observability/local/models/obsTrace.ts +94 -0
  328. package/src/observability/local/obsModels.test.ts +119 -0
  329. package/src/observability/local/promptStore.test.ts +351 -0
  330. package/src/observability/local/promptStore.ts +665 -0
  331. package/src/observability/local/reviewStore.test.ts +164 -0
  332. package/src/observability/local/reviewStore.ts +264 -0
  333. package/src/observability/local/traceStore.test.ts +264 -0
  334. package/src/observability/local/traceStore.ts +440 -0
  335. package/src/observability/observabilityApp.test.ts +92 -0
  336. package/src/observability/observabilityApp.ts +185 -0
  337. package/src/observability/observabilityAppRegistry.test.ts +30 -0
  338. package/src/observability/observabilityAppRegistry.ts +35 -0
  339. package/src/observability/permissions.test.ts +96 -0
  340. package/src/observability/permissions.ts +78 -0
  341. package/src/observability/reviewPanels.test.ts +35 -0
  342. package/src/observability/reviewPanels.ts +64 -0
  343. package/src/observability/routes/datasets.test.ts +176 -0
  344. package/src/observability/routes/datasets.ts +252 -0
  345. package/src/observability/routes/evaluators.test.ts +119 -0
  346. package/src/observability/routes/evaluators.ts +207 -0
  347. package/src/observability/routes/experiments.test.ts +282 -0
  348. package/src/observability/routes/experiments.ts +152 -0
  349. package/src/observability/routes/observabilityRbac.test.ts +767 -0
  350. package/src/observability/routes/observabilityRouteAccess.ts +106 -0
  351. package/src/observability/routes/prompts.test.ts +320 -0
  352. package/src/observability/routes/prompts.ts +263 -0
  353. package/src/observability/routes/requestAiService.ts +36 -0
  354. package/src/observability/routes/review.test.ts +120 -0
  355. package/src/observability/routes/review.ts +125 -0
  356. package/src/observability/routes/status.test.ts +116 -0
  357. package/src/observability/routes/status.ts +47 -0
  358. package/src/observability/routes/testMultiStage.test.ts +214 -0
  359. package/src/observability/routes/testMultiStage.ts +96 -0
  360. package/src/observability/routes/traces.test.ts +167 -0
  361. package/src/observability/routes/traces.ts +172 -0
  362. package/src/observability/schemaValidation.ts +29 -0
  363. package/src/observability/sopThresholds.ts +20 -0
  364. package/src/observability/status.test.ts +38 -0
  365. package/src/observability/status.ts +56 -0
  366. package/src/observability/testMultiStageSchemas.ts +95 -0
  367. package/src/observability/testMultiStageWorkflow.test.ts +114 -0
  368. package/src/observability/testMultiStageWorkflow.ts +307 -0
  369. package/src/observability/types.ts +262 -0
  370. package/src/routes/files.test.ts +63 -9
  371. package/src/routes/files.ts +21 -7
  372. package/src/routes/gpt.test.ts +3279 -0
  373. package/src/routes/gpt.ts +91 -816
  374. package/src/routes/gptActions.test.ts +241 -0
  375. package/src/routes/gptActions.ts +255 -0
  376. package/src/routes/gptApprovals.test.ts +762 -0
  377. package/src/routes/gptDatasets.test.ts +86 -0
  378. package/src/routes/gptDatasets.ts +104 -0
  379. package/src/routes/gptFiles.test.ts +589 -0
  380. package/src/routes/gptHistories.test.ts +933 -0
  381. package/src/routes/gptHistories.ts +266 -5
  382. package/src/routes/routes.test.ts +222 -2
  383. package/src/service/aiDatasets.test.ts +123 -0
  384. package/src/service/aiDatasets.ts +316 -0
  385. package/src/service/aiService.test.ts +593 -0
  386. package/src/service/aiService.ts +568 -111
  387. package/src/service/askFiles.test.ts +96 -0
  388. package/src/service/askFiles.ts +222 -0
  389. package/src/service/asks.test.ts +946 -0
  390. package/src/service/asks.ts +423 -0
  391. package/src/service/chatTurn.ts +2287 -0
  392. package/src/service/fileStorage.test.ts +11 -0
  393. package/src/service/fileStorage.ts +10 -2
  394. package/src/service/fillActionIds.test.ts +65 -0
  395. package/src/service/fillActionIds.ts +98 -0
  396. package/src/service/jsonFenceModel.test.ts +47 -0
  397. package/src/service/jsonFenceModel.ts +53 -0
  398. package/src/service/prompts.test.ts +21 -0
  399. package/src/service/prompts.ts +147 -0
  400. package/src/service/sanitizeHtml.test.ts +61 -0
  401. package/src/service/sanitizeHtml.ts +109 -0
  402. package/src/tests/bunSetup.test.ts +7 -4
  403. package/src/tests/bunSetup.ts +7 -7
  404. package/src/tests/chatHarness.ts +478 -0
  405. package/src/tests/generateStream.ts +49 -0
  406. package/src/tests/harnessErrors.ts +22 -0
  407. package/src/tests/harnessStreaming.ts +227 -0
  408. package/src/tests/helpers.ts +9 -2
  409. package/src/types/harness.ts +1095 -0
  410. package/src/types/index.ts +209 -13
  411. package/src/types/observability.ts +344 -0
  412. package/src/types/sanitize-html.d.ts +25 -0
@@ -0,0 +1,798 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createAgentTasks = void 0;
4
+ const node_crypto_1 = require("node:crypto");
5
+ const ai_1 = require("ai");
6
+ const luxon_1 = require("luxon");
7
+ const jsonFenceModel_1 = require("../service/jsonFenceModel");
8
+ const parseAiJson_1 = require("../service/parseAiJson");
9
+ const harness_1 = require("../types/harness");
10
+ const agentTaskNames_1 = require("./agentTaskNames");
11
+ const defineTask_1 = require("./defineTask");
12
+ const errors_1 = require("./errors");
13
+ const events_1 = require("./events");
14
+ const extensions_1 = require("./extensions");
15
+ const internalRuntime_1 = require("./internalRuntime");
16
+ const modelCall_1 = require("./modelCall");
17
+ const suspend_1 = require("./suspend");
18
+ /** A tool's own `execute` threw; reported to the model as `Tool "<name>" failed: ...`. */
19
+ class ToolExecuteError extends Error {
20
+ constructor(toolName, cause) {
21
+ super(`Tool "${toolName}" failed: ${(0, errors_1.errorMessage)(cause)}`);
22
+ this.name = "ToolExecuteError";
23
+ }
24
+ }
25
+ /** Tool results must be JSON; `undefined` becomes `null`. */
26
+ const toJsonValue = (value) => {
27
+ if (value === undefined) {
28
+ return null;
29
+ }
30
+ return JSON.parse(JSON.stringify(value));
31
+ };
32
+ const requireAgent = (context, name) => {
33
+ const agent = context.agents.get(name);
34
+ if (!agent) {
35
+ throw (0, errors_1.harnessError)({
36
+ detail: `Agent "${name}" is not in this harness registry`,
37
+ kind: "notRegistered",
38
+ });
39
+ }
40
+ return agent;
41
+ };
42
+ const textOf = (parts) => parts
43
+ .filter((part) => part.type === "text")
44
+ .map((part) => part.text)
45
+ .join("");
46
+ const isSystemPromptRecord = (message) => message.parts.some((part) => part.type === "system-prompt");
47
+ /**
48
+ * Mark assistant messages from this turn aborted when they still contain a tool call
49
+ * with no stored result. The next request skips those messages, so a provider never
50
+ * sees a tool call without its tool result.
51
+ */
52
+ const markUnansweredToolCallsAborted = async ({ conversationId, models, turnTaskId, }) => {
53
+ const messages = await models.message.find({ conversationId, turnTaskId }).sort({ seq: 1 });
54
+ const answered = new Set();
55
+ for (const message of messages) {
56
+ if (message.role !== harness_1.HARNESS_MESSAGE_ROLES.tool) {
57
+ continue;
58
+ }
59
+ if (message.toolCallId) {
60
+ answered.add(message.toolCallId);
61
+ }
62
+ for (const part of message.parts) {
63
+ if (part.type === "tool-result") {
64
+ answered.add(part.toolCallId);
65
+ }
66
+ }
67
+ }
68
+ const orphanIds = messages
69
+ .filter((message) => message.role === harness_1.HARNESS_MESSAGE_ROLES.assistant && !message.aborted)
70
+ .filter((message) => message.parts.some((part) => part.type === "tool-call" && !answered.has(part.toolCallId)))
71
+ .map((message) => message._id);
72
+ if (orphanIds.length === 0) {
73
+ return;
74
+ }
75
+ await models.message.updateMany({ _id: { $in: orphanIds } }, { $set: { aborted: true } });
76
+ };
77
+ /** Convert the stored transcript to AI SDK messages. Aborted (partial) messages are skipped. */
78
+ const toModelMessages = (history) => {
79
+ const messages = [];
80
+ for (const message of history) {
81
+ // Aborted partials and recorded system prompts are never replayed to the model.
82
+ if (message.aborted || isSystemPromptRecord(message)) {
83
+ continue;
84
+ }
85
+ if (message.role === harness_1.HARNESS_MESSAGE_ROLES.user) {
86
+ messages.push({ content: textOf(message.parts), role: "user" });
87
+ }
88
+ else if (message.role === harness_1.HARNESS_MESSAGE_ROLES.system) {
89
+ messages.push({ content: textOf(message.parts), role: "system" });
90
+ }
91
+ else if (message.role === harness_1.HARNESS_MESSAGE_ROLES.assistant) {
92
+ const content = [];
93
+ for (const part of message.parts) {
94
+ if (part.type === "text" && part.text) {
95
+ content.push({ text: part.text, type: "text" });
96
+ }
97
+ else if (part.type === "tool-call") {
98
+ content.push({
99
+ input: part.input,
100
+ toolCallId: part.toolCallId,
101
+ toolName: part.toolName,
102
+ type: "tool-call",
103
+ });
104
+ }
105
+ }
106
+ if (content.length > 0) {
107
+ messages.push({ content, role: "assistant" });
108
+ }
109
+ }
110
+ else {
111
+ const content = message.parts
112
+ .filter((part) => part.type === "tool-result")
113
+ .map((part) => ({
114
+ output: part.isError
115
+ ? { type: "error-text", value: String(part.output) }
116
+ : { type: "json", value: (part.output ?? null) },
117
+ toolCallId: part.toolCallId,
118
+ toolName: part.toolName,
119
+ type: "tool-result",
120
+ }));
121
+ if (content.length > 0) {
122
+ messages.push({ content, role: "tool" });
123
+ }
124
+ }
125
+ }
126
+ return messages;
127
+ };
128
+ /** AI SDK tools without `execute`: the model's calls come back to the turn, which runs them. */
129
+ const buildToolSet = (available, allowed) => {
130
+ const tools = [...available.values()].filter((tool) => allowed.includes(tool.name));
131
+ if (tools.length === 0) {
132
+ return undefined;
133
+ }
134
+ return Object.fromEntries(tools.map((tool) => [
135
+ tool.name,
136
+ (0, ai_1.tool)({ description: tool.description, inputSchema: tool.parameters }),
137
+ ]));
138
+ };
139
+ const costUsd = ({ inputTokens, modelId, outputTokens, priceMap, }) => {
140
+ const price = priceMap?.[modelId];
141
+ if (!price || inputTokens === undefined || outputTokens === undefined) {
142
+ return undefined;
143
+ }
144
+ return (price.inputPerMTok * inputTokens + price.outputPerMTok * outputTokens) / 1_000_000;
145
+ };
146
+ /** Hand out `count` consecutive message seqs for a conversation inside a transaction. */
147
+ const allocateSeqs = async ({ conversationId, count, models, session, }) => {
148
+ const updated = await models.conversation.findOneAndUpdate({ _id: conversationId }, { $inc: { seq: count } }, { returnDocument: "after", session });
149
+ if (!updated) {
150
+ throw (0, errors_1.harnessError)({
151
+ detail: `Conversation ${conversationId} no longer exists`,
152
+ kind: "notFound",
153
+ });
154
+ }
155
+ return updated.seq - count + 1;
156
+ };
157
+ /**
158
+ * Decide how a request's effective system prompt is recorded. When it differs from the
159
+ * last recorded prompt (or, before any, from the conversation's instructions), `write`
160
+ * appends a `system` message with a `system-prompt` part inside the request's commit, so
161
+ * the transcript holds exactly what the model saw. `span` names it by hash and seq.
162
+ */
163
+ const promptRecord = ({ conversation, history, models, sections, system, }) => {
164
+ const hash = (0, extensions_1.hashText)(system);
165
+ const last = [...history].reverse().find(isSystemPromptRecord);
166
+ const lastPart = last?.parts.find((part) => part.type === "system-prompt");
167
+ const baseline = lastPart ? lastPart.hash : (0, extensions_1.hashText)(conversation.agent.instructions);
168
+ const span = { hash, messageSeq: lastPart ? last?.seq : undefined, sections };
169
+ if (hash === baseline) {
170
+ return { span, write: async () => { } };
171
+ }
172
+ const conversationId = String(conversation._id);
173
+ return {
174
+ span,
175
+ write: async ({ session, task }) => {
176
+ const seq = await allocateSeqs({ conversationId, count: 1, models, session });
177
+ const part = { hash, sections, text: system, type: "system-prompt" };
178
+ await (0, events_1.insertMessages)({
179
+ messages: [
180
+ {
181
+ conversationId: conversation._id,
182
+ parts: [part],
183
+ role: harness_1.HARNESS_MESSAGE_ROLES.system,
184
+ seq,
185
+ turnTaskId: task._id,
186
+ },
187
+ ],
188
+ models,
189
+ session,
190
+ });
191
+ span.messageSeq = seq;
192
+ },
193
+ };
194
+ };
195
+ /**
196
+ * The SDK parses structured output only for a final answer (`stop`, or text without tool
197
+ * calls); otherwise reading it throws. Missing output is the caller's to report.
198
+ */
199
+ const structuredOutputOf = (generated) => {
200
+ try {
201
+ return generated.output;
202
+ }
203
+ catch (error) {
204
+ if (ai_1.NoOutputGeneratedError.isInstance(error)) {
205
+ return undefined;
206
+ }
207
+ throw error;
208
+ }
209
+ };
210
+ const describeInvalidOutput = (agentName, error) => {
211
+ const cause = error.cause ? `: ${(0, errors_1.errorMessage)(error.cause)}` : "";
212
+ return `Agent "${agentName}" output does not match its schema (${error.message})${cause}`;
213
+ };
214
+ /** Parse the final answer against the agent's `output` schema. */
215
+ const parseStructuredOutput = (agent, text) => {
216
+ if (!agent.output) {
217
+ return {};
218
+ }
219
+ let parsed;
220
+ try {
221
+ parsed = JSON.parse((0, parseAiJson_1.normalizeLlmJsonTextForStructuredOutput)(text));
222
+ }
223
+ catch (error) {
224
+ return { error: `Agent "${agent.name}" output is not JSON: ${(0, errors_1.errorMessage)(error)}` };
225
+ }
226
+ const result = agent.output.safeParse(parsed);
227
+ if (!result.success) {
228
+ return {
229
+ error: `Agent "${agent.name}" output does not match its schema: ${result.error.message}`,
230
+ };
231
+ }
232
+ return { output: result.data };
233
+ };
234
+ const loadConversation = (models, conversationId) => models.conversation.findExactlyOne({ _id: conversationId });
235
+ /**
236
+ * Steering submissions waiting for this turn's next model request, and the commit write
237
+ * that stores them as user messages (before the assistant answer) and removes them from
238
+ * the queue. Entries whose message already exists are only removed.
239
+ */
240
+ const pendingSteering = async ({ conversation, models, }) => {
241
+ const steer = conversation.queued.filter((entry) => entry.whenBusy === harness_1.HARNESS_WHEN_BUSY.steer && entry.requestId);
242
+ if (steer.length === 0) {
243
+ return { entries: [], write: async () => { } };
244
+ }
245
+ const requestIds = steer.map((entry) => entry.requestId);
246
+ const delivered = new Set((await models.message
247
+ .find({ conversationId: conversation._id, requestId: { $in: requestIds } })
248
+ .select({ requestId: 1 })
249
+ .lean()).map((message) => message.requestId));
250
+ const entries = steer.filter((entry) => !delivered.has(entry.requestId));
251
+ const conversationId = String(conversation._id);
252
+ return {
253
+ entries,
254
+ write: async ({ session, task }) => {
255
+ if (entries.length > 0) {
256
+ const first = await allocateSeqs({ conversationId, count: entries.length, models, session });
257
+ await (0, events_1.insertMessages)({
258
+ messages: entries.map((entry, index) => ({
259
+ conversationId: conversation._id,
260
+ parts: [{ text: String(entry.content ?? ""), type: "text" }],
261
+ requestId: entry.requestId,
262
+ role: harness_1.HARNESS_MESSAGE_ROLES.user,
263
+ seq: first + index,
264
+ turnTaskId: task._id,
265
+ })),
266
+ models,
267
+ session,
268
+ });
269
+ }
270
+ await models.conversation.updateOne({ _id: conversation._id }, { $pull: { queued: { requestId: { $in: requestIds } } } }, { session });
271
+ },
272
+ };
273
+ };
274
+ /**
275
+ * Stream one model request, writing its text as coalesced `delta` events on the
276
+ * conversation stream while it arrives. Each attempt streams under a fresh `requestKey`,
277
+ * so a client can drop the text of an attempt that failed. Deltas are flushed before the
278
+ * response returns, so they precede the commit's `message.created`.
279
+ */
280
+ const streamModelRequest = async ({ context, conversationId, messages, model, rt, step, system, tools, }) => {
281
+ const deltas = new events_1.HarnessDeltaWriter({
282
+ models: context.models,
283
+ options: context.streaming,
284
+ source: { conversationId, requestKey: (0, node_crypto_1.randomUUID)(), step, turnTaskId: rt.taskId },
285
+ });
286
+ try {
287
+ const streamed = (0, ai_1.streamText)({
288
+ abortSignal: rt.signal,
289
+ // Retries and fallbacks are the harness's; the SDK must not retry on its own.
290
+ maxRetries: 0,
291
+ messages,
292
+ model,
293
+ // Errors are rethrown below, where the harness classifies and retries them.
294
+ onError: () => { },
295
+ system,
296
+ tools,
297
+ });
298
+ for await (const part of streamed.fullStream) {
299
+ if (part.type === "text-delta") {
300
+ deltas.push(part.text);
301
+ }
302
+ else if (part.type === "error") {
303
+ throw part.error;
304
+ }
305
+ else if (part.type === "abort") {
306
+ throw rt.signal.reason ?? new Error("Model request aborted");
307
+ }
308
+ }
309
+ const [text, toolCalls, finishReason, usage] = await Promise.all([
310
+ streamed.text,
311
+ streamed.toolCalls,
312
+ streamed.finishReason,
313
+ streamed.usage,
314
+ ]);
315
+ return { finishReason, text, toolCalls, usage };
316
+ }
317
+ finally {
318
+ await deltas.close();
319
+ }
320
+ };
321
+ /**
322
+ * Build the built-in `terreno.agent.turn@1` and `terreno.agent.tool@1` task definitions.
323
+ *
324
+ * A turn alternates two replay-safe phases. `request` calls the model (with retries and
325
+ * fallbacks) and commits the assistant message and its LLM span with the checkpoint.
326
+ * `tools` runs each tool call as a child task, waits for all of them, and commits the
327
+ * tool result messages. The loop ends when the model answers without tool calls, or once
328
+ * `maxSteps` model requests have been made.
329
+ */
330
+ const createAgentTasks = (context) => {
331
+ const { models } = context;
332
+ /**
333
+ * Free the conversation inside a turn's terminal commit, so `idle` lands with the
334
+ * outcome. Fenced on this turn; the runner's post-commit release covers thrown failures.
335
+ */
336
+ const releaseConversationIn = async ({ session, task }) => {
337
+ await models.conversation.updateOne({
338
+ _id: task.input.conversationId,
339
+ activeTurnTaskId: task._id,
340
+ status: "busy",
341
+ }, { $set: { status: "idle" }, $unset: { activeTurnTaskId: 1 } }, { session });
342
+ };
343
+ /** Extensions a conversation uses, from its snapshot. */
344
+ const conversationExtensions = (conversation) => (0, extensions_1.resolveExtensions)(context.extensions, conversation.agent.extensions ?? []);
345
+ /** Hook api for a tool call: memos are scoped to the turn task that owns the call. */
346
+ const toolHookApi = async ({ conversationId, extensions, rt, }) => {
347
+ let turnTaskId = rt.taskId;
348
+ if (extensions.length > 0) {
349
+ const self = await models.task.findExactlyOne({ _id: rt.taskId });
350
+ turnTaskId = String(self.ownership.id ?? rt.taskId);
351
+ }
352
+ return {
353
+ conversationId,
354
+ memo: (0, internalRuntime_1.internalRuntime)(rt).memoFor(turnTaskId),
355
+ signal: rt.signal,
356
+ taskId: rt.taskId,
357
+ turnTaskId,
358
+ };
359
+ };
360
+ const runTool = async (task, rt) => {
361
+ const { agentName, conversationId, enabled, input, toolCallId, toolName } = task.input;
362
+ const fail = (error) => rt.commit({ terminal: { error, status: "failed" } });
363
+ const agent = context.agents.get(agentName);
364
+ let extensions = [];
365
+ let tool;
366
+ try {
367
+ if (enabled && agent) {
368
+ extensions = conversationExtensions(await loadConversation(models, conversationId));
369
+ tool = (0, extensions_1.resolveTools)(agent, extensions).get(toolName);
370
+ }
371
+ }
372
+ catch (error) {
373
+ await fail((0, errors_1.errorMessage)(error));
374
+ return;
375
+ }
376
+ if (!tool) {
377
+ await fail(`Unknown tool "${toolName}"`);
378
+ return;
379
+ }
380
+ const parsed = tool.parameters.safeParse(input);
381
+ if (!parsed.success) {
382
+ await fail(`Invalid arguments for tool "${toolName}": ${parsed.error.message}`);
383
+ return;
384
+ }
385
+ const streamed = [];
386
+ let value;
387
+ try {
388
+ const api = await toolHookApi({ conversationId, extensions, rt });
389
+ const before = await (0, extensions_1.runBeforeTool)({
390
+ api,
391
+ approvalFor: (0, internalRuntime_1.internalRuntime)(rt).approvalFor,
392
+ call: { args: parsed.data, toolCallId, toolName },
393
+ extensions,
394
+ });
395
+ if (before.blocked !== undefined) {
396
+ await fail(before.blocked);
397
+ return;
398
+ }
399
+ const args = tool.parameters.safeParse(before.args);
400
+ if (!args.success) {
401
+ await fail(`Invalid arguments for tool "${toolName}" after beforeTool hooks: ${args.error.message}`);
402
+ return;
403
+ }
404
+ let returned;
405
+ try {
406
+ returned = await tool.execute(args.data, {
407
+ conversationId,
408
+ env: rt.env,
409
+ output: (text) => {
410
+ streamed.push(String(text));
411
+ },
412
+ signal: rt.signal,
413
+ taskId: rt.taskId,
414
+ });
415
+ }
416
+ catch (error) {
417
+ throw new ToolExecuteError(toolName, error);
418
+ }
419
+ value = toJsonValue(await (0, extensions_1.runAfterTool)({
420
+ api,
421
+ call: { args: args.data, toolCallId, toolName },
422
+ extensions,
423
+ result: toJsonValue(returned),
424
+ }));
425
+ }
426
+ catch (error) {
427
+ // An abort or an approval wait is the harness's business; any other throw is
428
+ // reported to the model.
429
+ if (rt.signal.aborted || (0, suspend_1.isHarnessSuspendSignal)(error)) {
430
+ throw error;
431
+ }
432
+ await fail(error instanceof ToolExecuteError || error instanceof extensions_1.HarnessExtensionError
433
+ ? error.message
434
+ : `Tool "${toolName}" failed: ${(0, errors_1.errorMessage)(error)}`);
435
+ return;
436
+ }
437
+ const result = { value };
438
+ if (streamed.length > 0) {
439
+ result.streamedOutput = streamed.join("");
440
+ }
441
+ await rt.commit({ terminal: { result, status: "completed" } });
442
+ };
443
+ const tool = (0, defineTask_1.defineTask)({
444
+ // The replay choice is the tool's, so it picks the phase; both phases run the same code.
445
+ initial: (input) => ({ phase: input.replay === "safe" ? "executeSafe" : "execute" }),
446
+ name: agentTaskNames_1.AGENT_TOOL_TASK_NAME,
447
+ onInterrupt: "fail",
448
+ phases: {
449
+ execute: { run: runTool },
450
+ executeSafe: { replay: "safe", run: runTool },
451
+ },
452
+ // A failing tool is reported to the model, not retried.
453
+ retry: { maxAttempts: 1 },
454
+ spanKind: "TOOL",
455
+ spanName: (input) => input.toolName,
456
+ version: 1,
457
+ });
458
+ const request = async (task, rt) => {
459
+ const { conversationId } = task.input;
460
+ const conversation = await loadConversation(models, conversationId);
461
+ const agent = requireAgent(context, conversation.agent.name);
462
+ if (!context.resolveModel) {
463
+ throw (0, errors_1.harnessError)({
464
+ detail: "Harness.open needs a models resolver to run agents",
465
+ kind: "configInvalid",
466
+ });
467
+ }
468
+ const extensions = conversationExtensions(conversation);
469
+ const history = await models.message.find({ conversationId }).sort({ seq: 1 });
470
+ const tools = buildToolSet((0, extensions_1.resolveTools)(agent, extensions), conversation.agent.tools);
471
+ const steering = await pendingSteering({ conversation, models });
472
+ // Steering messages join this request after the stored transcript; the commit stores them.
473
+ const transcript = [
474
+ ...toModelMessages(history),
475
+ ...steering.entries.map((entry) => ({ content: String(entry.content ?? ""), role: "user" })),
476
+ ];
477
+ const step = task.state.step + 1;
478
+ const internals = (0, internalRuntime_1.internalRuntime)(rt);
479
+ const api = {
480
+ conversationId,
481
+ memo: rt.memo,
482
+ signal: rt.signal,
483
+ taskId: rt.taskId,
484
+ turnTaskId: rt.taskId,
485
+ };
486
+ const built = await (0, extensions_1.buildSystemPrompt)({
487
+ api,
488
+ extensions,
489
+ input: {
490
+ agentName: agent.name,
491
+ conversationId,
492
+ messages: [...transcript],
493
+ step,
494
+ },
495
+ instructions: conversation.agent.instructions,
496
+ });
497
+ const { request: modelRequest, rewrittenBy } = await (0, extensions_1.runBeforeModelRequest)({
498
+ api,
499
+ extensions,
500
+ request: { messages: [...transcript], system: built.text },
501
+ });
502
+ const { messages, system } = modelRequest;
503
+ const prompt = promptRecord({
504
+ conversation,
505
+ history,
506
+ models,
507
+ sections: built.sections,
508
+ system,
509
+ });
510
+ const spanInput = {
511
+ // The transcript is stored permanently; the span names the slice that was sent so
512
+ // its size stays bounded however long the conversation grows.
513
+ messages: {
514
+ count: messages.length,
515
+ fromSeq: history[0]?.seq,
516
+ // A hook replaced the messages: name exactly what was sent by hash.
517
+ ...(rewrittenBy.length > 0 ? { hash: (0, extensions_1.hashText)(JSON.stringify(messages)) } : {}),
518
+ toSeq: history.at(-1)?.seq,
519
+ },
520
+ models: [conversation.agent.model, ...(conversation.agent.fallbackModels ?? [])],
521
+ ...(rewrittenBy.length > 0 ? { rewrittenBy } : {}),
522
+ system: prompt.span,
523
+ tools: tools ? Object.keys(tools) : [],
524
+ };
525
+ const commit = internals.commitWithWrites;
526
+ const startedAt = luxon_1.DateTime.now();
527
+ const resolveModel = context.resolveModel;
528
+ const { outputSchema } = conversation.agent;
529
+ const output = outputSchema
530
+ ? ai_1.Output.object({ schema: (0, ai_1.jsonSchema)(JSON.parse(outputSchema)) })
531
+ : undefined;
532
+ const requestModel = () => (0, modelCall_1.callModelWithFallback)({
533
+ call: async (model) => {
534
+ if (!output) {
535
+ return streamModelRequest({
536
+ context,
537
+ conversationId,
538
+ messages,
539
+ model,
540
+ rt,
541
+ step,
542
+ system,
543
+ tools,
544
+ });
545
+ }
546
+ try {
547
+ const generated = await (0, ai_1.generateText)({
548
+ abortSignal: rt.signal,
549
+ // Retries and fallbacks are the harness's; the SDK must not retry on its own.
550
+ maxRetries: 0,
551
+ messages,
552
+ // Same fence and preamble cleanup AIService applies before `Output` parsing.
553
+ model: output ? (0, jsonFenceModel_1.withStrippedJsonFencesModel)(model) : model,
554
+ output,
555
+ system,
556
+ tools,
557
+ });
558
+ return {
559
+ finishReason: generated.finishReason,
560
+ output: output ? structuredOutputOf(generated) : undefined,
561
+ text: generated.text,
562
+ toolCalls: generated.toolCalls,
563
+ usage: generated.usage,
564
+ };
565
+ }
566
+ catch (error) {
567
+ // The model answered, but not with the requested structure: that is the
568
+ // turn's outcome, not a model failure to retry or fall back from.
569
+ if (!ai_1.NoObjectGeneratedError.isInstance(error)) {
570
+ throw error;
571
+ }
572
+ return {
573
+ finishReason: error.finishReason ?? "stop",
574
+ invalidOutput: describeInvalidOutput(conversation.agent.name, error),
575
+ text: error.text ?? "",
576
+ toolCalls: [],
577
+ usage: error.usage ?? { inputTokens: undefined, outputTokens: undefined },
578
+ };
579
+ }
580
+ },
581
+ models: [conversation.agent.model, ...(conversation.agent.fallbackModels ?? [])],
582
+ random: context.random,
583
+ resolveModel,
584
+ retry: agent.modelRetry,
585
+ signal: rt.signal,
586
+ });
587
+ let call;
588
+ try {
589
+ call = await requestModel();
590
+ }
591
+ catch (error) {
592
+ if (!(error instanceof modelCall_1.HarnessModelCallError) || rt.signal.aborted) {
593
+ throw error;
594
+ }
595
+ // Record every failed attempt on an error LLM span with the failed outcome.
596
+ const last = error.attempts.at(-1);
597
+ await commit({ terminal: { error: error.message, status: "failed" } }, async (context) => {
598
+ await prompt.write(context);
599
+ const endedAt = luxon_1.DateTime.now();
600
+ await models.span.create([
601
+ {
602
+ durationMs: endedAt.diff(startedAt).toMillis(),
603
+ endedAt: endedAt.toJSDate(),
604
+ error: error.message,
605
+ input: spanInput,
606
+ kind: "LLM",
607
+ name: last ? `${last.provider}/${last.modelId}` : "model",
608
+ output: { attempts: error.attempts },
609
+ parentSpanId: context.task.rootSpanId,
610
+ startedAt: startedAt.toJSDate(),
611
+ startOffsetMs: startedAt.diff(context.traceStartedAt).toMillis(),
612
+ status: "error",
613
+ traceId: context.task.traceId,
614
+ },
615
+ ], { session: context.session });
616
+ await releaseConversationIn(context);
617
+ });
618
+ return;
619
+ }
620
+ const { result } = call;
621
+ const text = result.text ?? "";
622
+ const toolCalls = result.toolCalls.map((toolCall) => ({
623
+ input: toolCall.input,
624
+ toolCallId: toolCall.toolCallId,
625
+ toolName: toolCall.toolName,
626
+ }));
627
+ const parts = [
628
+ ...(text ? [{ text, type: "text" }] : []),
629
+ ...toolCalls.map((toolCall) => ({ ...toolCall, type: "tool-call" })),
630
+ ];
631
+ const writes = async (writeContext) => {
632
+ const { session, task: turn, traceStartedAt } = writeContext;
633
+ await steering.write(writeContext);
634
+ await prompt.write(writeContext);
635
+ const seq = await allocateSeqs({ conversationId, count: 1, models, session });
636
+ await (0, events_1.insertMessages)({
637
+ messages: [
638
+ {
639
+ conversationId: conversation._id,
640
+ parts,
641
+ role: harness_1.HARNESS_MESSAGE_ROLES.assistant,
642
+ seq,
643
+ turnTaskId: turn._id,
644
+ },
645
+ ],
646
+ models,
647
+ session,
648
+ });
649
+ const endedAt = luxon_1.DateTime.now();
650
+ const { inputTokens, outputTokens } = result.usage;
651
+ await models.span.create([
652
+ {
653
+ durationMs: endedAt.diff(startedAt).toMillis(),
654
+ endedAt: endedAt.toJSDate(),
655
+ input: spanInput,
656
+ kind: "LLM",
657
+ name: `${call.model.provider}/${call.model.modelId}`,
658
+ output: {
659
+ attempts: call.attempts,
660
+ finishReason: result.finishReason,
661
+ text,
662
+ toolCalls,
663
+ },
664
+ parentSpanId: turn.rootSpanId,
665
+ startedAt: startedAt.toJSDate(),
666
+ startOffsetMs: startedAt.diff(traceStartedAt).toMillis(),
667
+ status: "ok",
668
+ traceId: turn.traceId,
669
+ usage: {
670
+ costUsd: costUsd({
671
+ inputTokens,
672
+ modelId: call.model.modelId,
673
+ outputTokens,
674
+ priceMap: context.priceMap(),
675
+ }),
676
+ inputTokens,
677
+ model: call.model.modelId,
678
+ outputTokens,
679
+ },
680
+ },
681
+ ], { session });
682
+ };
683
+ if (toolCalls.length > 0) {
684
+ await commit({ phase: "tools", state: { step, text, toolCalls } }, writes);
685
+ return;
686
+ }
687
+ const finalWrites = async (writeContext) => {
688
+ await writes(writeContext);
689
+ await releaseConversationIn(writeContext);
690
+ };
691
+ const structured = outputSchema
692
+ ? { error: result.invalidOutput, output: result.output }
693
+ : parseStructuredOutput(agent, text);
694
+ if (structured.error) {
695
+ await commit({ terminal: { error: structured.error, status: "failed" } }, finalWrites);
696
+ return;
697
+ }
698
+ const done = { finishReason: "stop", steps: step, text };
699
+ if (structured.output !== undefined) {
700
+ done.output = structured.output;
701
+ }
702
+ await commit({ terminal: { result: done, status: "completed" } }, finalWrites);
703
+ };
704
+ const runTools = async (task, rt) => {
705
+ const { conversationId } = task.input;
706
+ const { step, text } = task.state;
707
+ const toolCalls = task.state.toolCalls ?? [];
708
+ const conversation = await loadConversation(models, conversationId);
709
+ const agent = requireAgent(context, conversation.agent.name);
710
+ // A wrap that throws here is reported by the tool call itself, which resolves again.
711
+ let available;
712
+ try {
713
+ available = (0, extensions_1.resolveTools)(agent, conversationExtensions(conversation));
714
+ }
715
+ catch {
716
+ available = undefined;
717
+ }
718
+ const ids = [];
719
+ for (const [index, call] of toolCalls.entries()) {
720
+ const declared = available?.get(call.toolName);
721
+ ids.push(await rt.createTask(tool, {
722
+ agentName: agent.name,
723
+ conversationId,
724
+ enabled: (available === undefined || Boolean(declared)) &&
725
+ conversation.agent.tools.includes(call.toolName),
726
+ input: call.input,
727
+ replay: declared?.replay ?? "never",
728
+ toolCallId: call.toolCallId,
729
+ toolName: call.toolName,
730
+ }, { key: `${index}:${call.toolCallId}` }));
731
+ }
732
+ const outcomes = await rt.waitForTasks(ids);
733
+ const results = toolCalls.map((call, index) => {
734
+ const outcome = outcomes[index];
735
+ const isError = outcome.status !== "completed";
736
+ return {
737
+ isError,
738
+ output: isError
739
+ ? (outcome.error ?? `Tool call ${outcome.status}`)
740
+ : (outcome.result?.value ?? null),
741
+ toolCallId: call.toolCallId,
742
+ toolName: call.toolName,
743
+ type: "tool-result",
744
+ };
745
+ });
746
+ const writes = async ({ session, task: turn }) => {
747
+ if (results.length === 0) {
748
+ return;
749
+ }
750
+ const first = await allocateSeqs({ conversationId, count: results.length, models, session });
751
+ await (0, events_1.insertMessages)({
752
+ messages: results.map((part, index) => ({
753
+ conversationId: conversation._id,
754
+ parts: [part],
755
+ role: harness_1.HARNESS_MESSAGE_ROLES.tool,
756
+ seq: first + index,
757
+ status: part.isError ? "error" : "ok",
758
+ toolCallId: part.toolCallId,
759
+ toolName: part.toolName,
760
+ turnTaskId: turn._id,
761
+ })),
762
+ models,
763
+ session,
764
+ });
765
+ };
766
+ const commit = (0, internalRuntime_1.internalRuntime)(rt).commitWithWrites;
767
+ if (step >= conversation.agent.maxSteps) {
768
+ await commit({ terminal: { result: { finishReason: "max-steps", steps: step, text }, status: "completed" } }, async (writeContext) => {
769
+ await writes(writeContext);
770
+ await releaseConversationIn(writeContext);
771
+ });
772
+ return;
773
+ }
774
+ await commit({ phase: "request", state: { step, text } }, writes);
775
+ };
776
+ const turn = (0, defineTask_1.defineTask)({
777
+ abort: async (task) => {
778
+ await markUnansweredToolCallsAborted({
779
+ conversationId: task.input.conversationId,
780
+ models,
781
+ turnTaskId: task.id,
782
+ });
783
+ },
784
+ initial: () => ({ phase: "request", state: { step: 0, text: "" } }),
785
+ name: agentTaskNames_1.AGENT_TURN_TASK_NAME,
786
+ phases: {
787
+ // A model request has no side effect, so an interrupted one is simply re-requested.
788
+ request: { replay: "safe", run: request },
789
+ // Tool children are idempotent per phase visit, so re-running the fan-out is safe.
790
+ tools: { replay: "safe", run: runTools },
791
+ },
792
+ // Model retries and fallbacks happen inside `request`; a turn that still fails, fails.
793
+ retry: { maxAttempts: 1 },
794
+ version: 1,
795
+ });
796
+ return { tool, turn };
797
+ };
798
+ exports.createAgentTasks = createAgentTasks;