@terreno/ai 57.9.0 → 57.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (417) hide show
  1. package/README.md +1 -0
  2. package/bunfig.toml +1 -1
  3. package/dist/aiApp.d.ts +8 -2
  4. package/dist/aiApp.js +12 -6
  5. package/dist/harness/adminScreens.d.ts +6 -0
  6. package/dist/harness/adminScreens.js +11 -0
  7. package/dist/harness/agentLoop.d.ts +61 -0
  8. package/dist/harness/agentLoop.js +798 -0
  9. package/dist/harness/agentTaskNames.d.ts +4 -0
  10. package/dist/harness/agentTaskNames.js +7 -0
  11. package/dist/harness/approvalPolicy.d.ts +3 -0
  12. package/dist/harness/approvalPolicy.js +23 -0
  13. package/dist/harness/approvals.d.ts +101 -0
  14. package/dist/harness/approvals.js +468 -0
  15. package/dist/harness/commit.d.ts +238 -0
  16. package/dist/harness/commit.js +678 -0
  17. package/dist/harness/conversation.d.ts +75 -0
  18. package/dist/harness/conversation.js +397 -0
  19. package/dist/harness/defineAgent.d.ts +9 -0
  20. package/dist/harness/defineAgent.js +65 -0
  21. package/dist/harness/defineTask.d.ts +11 -0
  22. package/dist/harness/defineTask.js +59 -0
  23. package/dist/harness/defineTool.d.ts +7 -0
  24. package/dist/harness/defineTool.js +36 -0
  25. package/dist/harness/definitionError.d.ts +8 -0
  26. package/dist/harness/definitionError.js +15 -0
  27. package/dist/harness/errors.d.ts +133 -0
  28. package/dist/harness/errors.js +107 -0
  29. package/dist/harness/eventHub.d.ts +45 -0
  30. package/dist/harness/eventHub.js +135 -0
  31. package/dist/harness/events.d.ts +95 -0
  32. package/dist/harness/events.js +267 -0
  33. package/dist/harness/executionEnv.d.ts +31 -0
  34. package/dist/harness/executionEnv.js +2 -0
  35. package/dist/harness/extensions.d.ts +82 -0
  36. package/dist/harness/extensions.js +274 -0
  37. package/dist/harness/harness.d.ts +163 -0
  38. package/dist/harness/harness.js +522 -0
  39. package/dist/harness/harnessApp.d.ts +44 -0
  40. package/dist/harness/harnessApp.js +62 -0
  41. package/dist/harness/internalRuntime.d.ts +16 -0
  42. package/dist/harness/internalRuntime.js +18 -0
  43. package/dist/harness/leases.d.ts +35 -0
  44. package/dist/harness/leases.js +86 -0
  45. package/dist/harness/memo.d.ts +24 -0
  46. package/dist/harness/memo.js +78 -0
  47. package/dist/harness/modelCall.d.ts +45 -0
  48. package/dist/harness/modelCall.js +147 -0
  49. package/dist/harness/models/harnessApproval.d.ts +2 -0
  50. package/dist/harness/models/harnessApproval.js +100 -0
  51. package/dist/harness/models/harnessConversation.d.ts +2 -0
  52. package/dist/harness/models/harnessConversation.js +117 -0
  53. package/dist/harness/models/harnessEvent.d.ts +3 -0
  54. package/dist/harness/models/harnessEvent.js +73 -0
  55. package/dist/harness/models/harnessInboxEvent.d.ts +2 -0
  56. package/dist/harness/models/harnessInboxEvent.js +58 -0
  57. package/dist/harness/models/harnessMemo.d.ts +2 -0
  58. package/dist/harness/models/harnessMemo.js +42 -0
  59. package/dist/harness/models/harnessMessage.d.ts +2 -0
  60. package/dist/harness/models/harnessMessage.js +73 -0
  61. package/dist/harness/models/harnessOwner.d.ts +2 -0
  62. package/dist/harness/models/harnessOwner.js +36 -0
  63. package/dist/harness/models/harnessTask.d.ts +2 -0
  64. package/dist/harness/models/harnessTask.js +191 -0
  65. package/dist/harness/ownership.d.ts +89 -0
  66. package/dist/harness/ownership.js +438 -0
  67. package/dist/harness/registry.d.ts +29 -0
  68. package/dist/harness/registry.js +93 -0
  69. package/dist/harness/retryBackoff.d.ts +31 -0
  70. package/dist/harness/retryBackoff.js +50 -0
  71. package/dist/harness/routes/approvals.d.ts +13 -0
  72. package/dist/harness/routes/approvals.js +93 -0
  73. package/dist/harness/routes/conversations.d.ts +11 -0
  74. package/dist/harness/routes/conversations.js +66 -0
  75. package/dist/harness/routes/events.d.ts +19 -0
  76. package/dist/harness/routes/events.js +215 -0
  77. package/dist/harness/routes/tasks.d.ts +11 -0
  78. package/dist/harness/routes/tasks.js +108 -0
  79. package/dist/harness/runners/inProcessRunner.d.ts +64 -0
  80. package/dist/harness/runners/inProcessRunner.js +227 -0
  81. package/dist/harness/runners/jobsRunner.d.ts +62 -0
  82. package/dist/harness/runners/jobsRunner.js +212 -0
  83. package/dist/harness/runtime.d.ts +52 -0
  84. package/dist/harness/runtime.js +604 -0
  85. package/dist/harness/subagent.d.ts +53 -0
  86. package/dist/harness/subagent.js +134 -0
  87. package/dist/harness/suspend.d.ts +8 -0
  88. package/dist/harness/suspend.js +16 -0
  89. package/dist/harness/transaction.d.ts +3 -0
  90. package/dist/harness/transaction.js +22 -0
  91. package/dist/harness/waits.d.ts +100 -0
  92. package/dist/harness/waits.js +214 -0
  93. package/dist/index.d.ts +19 -1
  94. package/dist/index.js +45 -2
  95. package/dist/models/aiDataset.d.ts +2 -0
  96. package/dist/models/aiDataset.js +67 -0
  97. package/dist/models/aiRequest.js +1 -1
  98. package/dist/models/gptHistory.js +73 -0
  99. package/dist/observability/adminScreens.d.ts +5 -0
  100. package/dist/observability/adminScreens.js +32 -0
  101. package/dist/observability/compileTemplate.d.ts +1 -0
  102. package/dist/observability/compileTemplate.js +13 -0
  103. package/dist/observability/datasetImport.d.ts +11 -0
  104. package/dist/observability/datasetImport.js +185 -0
  105. package/dist/observability/evaluate.d.ts +31 -0
  106. package/dist/observability/evaluate.js +200 -0
  107. package/dist/observability/evaluatorTemplates.d.ts +17 -0
  108. package/dist/observability/evaluatorTemplates.js +111 -0
  109. package/dist/observability/jsonPath.d.ts +2 -0
  110. package/dist/observability/jsonPath.js +57 -0
  111. package/dist/observability/local/datasetStore.d.ts +87 -0
  112. package/dist/observability/local/datasetStore.js +326 -0
  113. package/dist/observability/local/evaluatorStore.d.ts +44 -0
  114. package/dist/observability/local/evaluatorStore.js +182 -0
  115. package/dist/observability/local/experimentRunner.d.ts +96 -0
  116. package/dist/observability/local/experimentRunner.js +419 -0
  117. package/dist/observability/local/localPlugin.d.ts +22 -0
  118. package/dist/observability/local/localPlugin.js +76 -0
  119. package/dist/observability/local/models/obsDataset.d.ts +2 -0
  120. package/dist/observability/local/models/obsDataset.js +37 -0
  121. package/dist/observability/local/models/obsDatasetItem.d.ts +2 -0
  122. package/dist/observability/local/models/obsDatasetItem.js +76 -0
  123. package/dist/observability/local/models/obsEvaluator.d.ts +2 -0
  124. package/dist/observability/local/models/obsEvaluator.js +98 -0
  125. package/dist/observability/local/models/obsExperiment.d.ts +2 -0
  126. package/dist/observability/local/models/obsExperiment.js +87 -0
  127. package/dist/observability/local/models/obsExperimentItem.d.ts +2 -0
  128. package/dist/observability/local/models/obsExperimentItem.js +45 -0
  129. package/dist/observability/local/models/obsPrompt.d.ts +2 -0
  130. package/dist/observability/local/models/obsPrompt.js +42 -0
  131. package/dist/observability/local/models/obsPromptLabel.d.ts +2 -0
  132. package/dist/observability/local/models/obsPromptLabel.js +39 -0
  133. package/dist/observability/local/models/obsPromptVersion.d.ts +2 -0
  134. package/dist/observability/local/models/obsPromptVersion.js +96 -0
  135. package/dist/observability/local/models/obsReviewItem.d.ts +2 -0
  136. package/dist/observability/local/models/obsReviewItem.js +75 -0
  137. package/dist/observability/local/models/obsScore.d.ts +2 -0
  138. package/dist/observability/local/models/obsScore.js +58 -0
  139. package/dist/observability/local/models/obsSpan.d.ts +2 -0
  140. package/dist/observability/local/models/obsSpan.js +64 -0
  141. package/dist/observability/local/models/obsTrace.d.ts +2 -0
  142. package/dist/observability/local/models/obsTrace.js +93 -0
  143. package/dist/observability/local/promptStore.d.ts +180 -0
  144. package/dist/observability/local/promptStore.js +444 -0
  145. package/dist/observability/local/reviewStore.d.ts +50 -0
  146. package/dist/observability/local/reviewStore.js +203 -0
  147. package/dist/observability/local/traceStore.d.ts +103 -0
  148. package/dist/observability/local/traceStore.js +325 -0
  149. package/dist/observability/observabilityApp.d.ts +24 -0
  150. package/dist/observability/observabilityApp.js +157 -0
  151. package/dist/observability/observabilityAppRegistry.d.ts +14 -0
  152. package/dist/observability/observabilityAppRegistry.js +16 -0
  153. package/dist/observability/permissions.d.ts +7 -0
  154. package/dist/observability/permissions.js +60 -0
  155. package/dist/observability/reviewPanels.d.ts +18 -0
  156. package/dist/observability/reviewPanels.js +46 -0
  157. package/dist/observability/routes/datasets.d.ts +7 -0
  158. package/dist/observability/routes/datasets.js +149 -0
  159. package/dist/observability/routes/evaluators.d.ts +7 -0
  160. package/dist/observability/routes/evaluators.js +123 -0
  161. package/dist/observability/routes/experiments.d.ts +7 -0
  162. package/dist/observability/routes/experiments.js +95 -0
  163. package/dist/observability/routes/observabilityRouteAccess.d.ts +13 -0
  164. package/dist/observability/routes/observabilityRouteAccess.js +67 -0
  165. package/dist/observability/routes/prompts.d.ts +12 -0
  166. package/dist/observability/routes/prompts.js +172 -0
  167. package/dist/observability/routes/requestAiService.d.ts +13 -0
  168. package/dist/observability/routes/requestAiService.js +21 -0
  169. package/dist/observability/routes/review.d.ts +7 -0
  170. package/dist/observability/routes/review.js +79 -0
  171. package/dist/observability/routes/status.d.ts +5 -0
  172. package/dist/observability/routes/status.js +31 -0
  173. package/dist/observability/routes/testMultiStage.d.ts +9 -0
  174. package/dist/observability/routes/testMultiStage.js +67 -0
  175. package/dist/observability/routes/traces.d.ts +7 -0
  176. package/dist/observability/routes/traces.js +129 -0
  177. package/dist/observability/schemaValidation.d.ts +5 -0
  178. package/dist/observability/schemaValidation.js +26 -0
  179. package/dist/observability/sopThresholds.d.ts +2 -0
  180. package/dist/observability/sopThresholds.js +21 -0
  181. package/dist/observability/status.d.ts +23 -0
  182. package/dist/observability/status.js +34 -0
  183. package/dist/observability/testMultiStageSchemas.d.ts +10 -0
  184. package/dist/observability/testMultiStageSchemas.js +79 -0
  185. package/dist/observability/testMultiStageWorkflow.d.ts +20 -0
  186. package/dist/observability/testMultiStageWorkflow.js +218 -0
  187. package/dist/observability/types.d.ts +159 -0
  188. package/dist/observability/types.js +67 -0
  189. package/dist/routes/files.js +18 -7
  190. package/dist/routes/gpt.js +79 -689
  191. package/dist/routes/gptActions.d.ts +9 -0
  192. package/dist/routes/gptActions.js +212 -0
  193. package/dist/routes/gptDatasets.d.ts +5 -0
  194. package/dist/routes/gptDatasets.js +94 -0
  195. package/dist/routes/gptHistories.d.ts +3 -1
  196. package/dist/routes/gptHistories.js +188 -2
  197. package/dist/service/aiDatasets.d.ts +57 -0
  198. package/dist/service/aiDatasets.js +253 -0
  199. package/dist/service/aiService.d.ts +50 -1
  200. package/dist/service/aiService.js +426 -77
  201. package/dist/service/askFiles.d.ts +49 -0
  202. package/dist/service/askFiles.js +162 -0
  203. package/dist/service/asks.d.ts +76 -0
  204. package/dist/service/asks.js +284 -0
  205. package/dist/service/chatTurn.d.ts +128 -0
  206. package/dist/service/chatTurn.js +1674 -0
  207. package/dist/service/fileStorage.d.ts +4 -0
  208. package/dist/service/fileStorage.js +7 -2
  209. package/dist/service/fillActionIds.d.ts +6 -0
  210. package/dist/service/fillActionIds.js +79 -0
  211. package/dist/service/jsonFenceModel.d.ts +7 -0
  212. package/dist/service/jsonFenceModel.js +45 -0
  213. package/dist/service/prompts.d.ts +52 -0
  214. package/dist/service/prompts.js +96 -1
  215. package/dist/service/sanitizeHtml.d.ts +11 -0
  216. package/dist/service/sanitizeHtml.js +108 -0
  217. package/dist/types/harness.d.ts +952 -0
  218. package/dist/types/harness.js +133 -0
  219. package/dist/types/index.d.ts +192 -14
  220. package/dist/types/index.js +2 -0
  221. package/dist/types/observability.d.ts +288 -0
  222. package/dist/types/observability.js +2 -0
  223. package/package.json +28 -5
  224. package/src/aiApp.test.ts +207 -1
  225. package/src/aiApp.ts +19 -6
  226. package/src/harness/adminScreens.test.ts +34 -0
  227. package/src/harness/adminScreens.ts +11 -0
  228. package/src/harness/agentLoop.test.ts +1034 -0
  229. package/src/harness/agentLoop.ts +1082 -0
  230. package/src/harness/agentRecovery.test.ts +380 -0
  231. package/src/harness/agentTaskNames.ts +4 -0
  232. package/src/harness/approvalPolicy.ts +28 -0
  233. package/src/harness/approvals.test.ts +1094 -0
  234. package/src/harness/approvals.ts +644 -0
  235. package/src/harness/commit.ts +1049 -0
  236. package/src/harness/concurrency.test.ts +155 -0
  237. package/src/harness/conversation.ts +503 -0
  238. package/src/harness/defineAgent.test.ts +92 -0
  239. package/src/harness/defineAgent.ts +82 -0
  240. package/src/harness/defineTask.test.ts +59 -0
  241. package/src/harness/defineTask.ts +76 -0
  242. package/src/harness/defineTool.ts +45 -0
  243. package/src/harness/definitionError.ts +13 -0
  244. package/src/harness/errors.test.ts +90 -0
  245. package/src/harness/errors.ts +125 -0
  246. package/src/harness/eventHub.test.ts +124 -0
  247. package/src/harness/eventHub.ts +159 -0
  248. package/src/harness/events.test.ts +769 -0
  249. package/src/harness/events.ts +384 -0
  250. package/src/harness/executionEnv.ts +34 -0
  251. package/src/harness/extensions.test.ts +1218 -0
  252. package/src/harness/extensions.ts +382 -0
  253. package/src/harness/harness.test.ts +693 -0
  254. package/src/harness/harness.ts +778 -0
  255. package/src/harness/harnessApp.ts +76 -0
  256. package/src/harness/internalRuntime.ts +44 -0
  257. package/src/harness/leases.ts +132 -0
  258. package/src/harness/memo.ts +107 -0
  259. package/src/harness/modelCall.test.ts +134 -0
  260. package/src/harness/modelCall.ts +200 -0
  261. package/src/harness/models/harnessApproval.ts +109 -0
  262. package/src/harness/models/harnessConversation.ts +142 -0
  263. package/src/harness/models/harnessEvent.ts +92 -0
  264. package/src/harness/models/harnessInboxEvent.ts +73 -0
  265. package/src/harness/models/harnessMemo.ts +43 -0
  266. package/src/harness/models/harnessMessage.ts +85 -0
  267. package/src/harness/models/harnessModels.test.ts +60 -0
  268. package/src/harness/models/harnessOwner.ts +40 -0
  269. package/src/harness/models/harnessTask.ts +206 -0
  270. package/src/harness/ownership.test.ts +1087 -0
  271. package/src/harness/ownership.ts +618 -0
  272. package/src/harness/recovery.test.ts +907 -0
  273. package/src/harness/registry.ts +124 -0
  274. package/src/harness/retries.test.ts +292 -0
  275. package/src/harness/retryBackoff.test.ts +100 -0
  276. package/src/harness/retryBackoff.ts +77 -0
  277. package/src/harness/routes/approvals.ts +123 -0
  278. package/src/harness/routes/conversations.test.ts +518 -0
  279. package/src/harness/routes/conversations.ts +102 -0
  280. package/src/harness/routes/events.ts +280 -0
  281. package/src/harness/routes/tasks.test.ts +198 -0
  282. package/src/harness/routes/tasks.ts +146 -0
  283. package/src/harness/runners/inProcessRunner.test.ts +530 -0
  284. package/src/harness/runners/inProcessRunner.ts +260 -0
  285. package/src/harness/runners/jobsRunner.test.ts +709 -0
  286. package/src/harness/runners/jobsRunner.ts +252 -0
  287. package/src/harness/runtime.ts +802 -0
  288. package/src/harness/subagent.test.ts +696 -0
  289. package/src/harness/subagent.ts +192 -0
  290. package/src/harness/suspend.ts +13 -0
  291. package/src/harness/transaction.ts +17 -0
  292. package/src/harness/versioning.test.ts +288 -0
  293. package/src/harness/waits.test.ts +693 -0
  294. package/src/harness/waits.ts +356 -0
  295. package/src/index.test.ts +13 -0
  296. package/src/index.ts +70 -0
  297. package/src/isolated/adminScreensConfig.isolated.ts +143 -0
  298. package/src/models/aiDataset.test.ts +34 -0
  299. package/src/models/aiDataset.ts +78 -0
  300. package/src/models/aiRequest.ts +2 -1
  301. package/src/models/gptHistory.test.ts +156 -0
  302. package/src/models/gptHistory.ts +89 -1
  303. package/src/observability/adminScreens.test.ts +71 -0
  304. package/src/observability/adminScreens.ts +42 -0
  305. package/src/observability/compileTemplate.test.ts +14 -0
  306. package/src/observability/compileTemplate.ts +10 -0
  307. package/src/observability/datasetImport.test.ts +95 -0
  308. package/src/observability/datasetImport.ts +220 -0
  309. package/src/observability/evaluate.test.ts +549 -0
  310. package/src/observability/evaluate.ts +254 -0
  311. package/src/observability/evaluatorTemplates.ts +124 -0
  312. package/src/observability/jsonPath.test.ts +52 -0
  313. package/src/observability/jsonPath.ts +60 -0
  314. package/src/observability/local/datasetStore.test.ts +230 -0
  315. package/src/observability/local/datasetStore.ts +452 -0
  316. package/src/observability/local/evaluatorStore.test.ts +138 -0
  317. package/src/observability/local/evaluatorStore.ts +224 -0
  318. package/src/observability/local/experimentRunner.test.ts +489 -0
  319. package/src/observability/local/experimentRunner.ts +548 -0
  320. package/src/observability/local/localPlugin.ts +88 -0
  321. package/src/observability/local/models/obsDataset.ts +38 -0
  322. package/src/observability/local/models/obsDatasetItem.ts +80 -0
  323. package/src/observability/local/models/obsEvaluator.ts +102 -0
  324. package/src/observability/local/models/obsExperiment.ts +91 -0
  325. package/src/observability/local/models/obsExperimentItem.ts +52 -0
  326. package/src/observability/local/models/obsPrompt.ts +43 -0
  327. package/src/observability/local/models/obsPromptLabel.ts +43 -0
  328. package/src/observability/local/models/obsPromptVersion.ts +107 -0
  329. package/src/observability/local/models/obsReviewItem.ts +79 -0
  330. package/src/observability/local/models/obsScore.ts +59 -0
  331. package/src/observability/local/models/obsSpan.ts +65 -0
  332. package/src/observability/local/models/obsTrace.ts +94 -0
  333. package/src/observability/local/obsModels.test.ts +119 -0
  334. package/src/observability/local/promptStore.test.ts +351 -0
  335. package/src/observability/local/promptStore.ts +665 -0
  336. package/src/observability/local/reviewStore.test.ts +164 -0
  337. package/src/observability/local/reviewStore.ts +264 -0
  338. package/src/observability/local/traceStore.test.ts +264 -0
  339. package/src/observability/local/traceStore.ts +440 -0
  340. package/src/observability/observabilityApp.test.ts +92 -0
  341. package/src/observability/observabilityApp.ts +185 -0
  342. package/src/observability/observabilityAppRegistry.test.ts +30 -0
  343. package/src/observability/observabilityAppRegistry.ts +35 -0
  344. package/src/observability/permissions.test.ts +96 -0
  345. package/src/observability/permissions.ts +78 -0
  346. package/src/observability/reviewPanels.test.ts +35 -0
  347. package/src/observability/reviewPanels.ts +64 -0
  348. package/src/observability/routes/datasets.test.ts +176 -0
  349. package/src/observability/routes/datasets.ts +252 -0
  350. package/src/observability/routes/evaluators.test.ts +119 -0
  351. package/src/observability/routes/evaluators.ts +207 -0
  352. package/src/observability/routes/experiments.test.ts +282 -0
  353. package/src/observability/routes/experiments.ts +152 -0
  354. package/src/observability/routes/observabilityRbac.test.ts +767 -0
  355. package/src/observability/routes/observabilityRouteAccess.ts +106 -0
  356. package/src/observability/routes/prompts.test.ts +320 -0
  357. package/src/observability/routes/prompts.ts +263 -0
  358. package/src/observability/routes/requestAiService.ts +36 -0
  359. package/src/observability/routes/review.test.ts +120 -0
  360. package/src/observability/routes/review.ts +125 -0
  361. package/src/observability/routes/status.test.ts +116 -0
  362. package/src/observability/routes/status.ts +47 -0
  363. package/src/observability/routes/testMultiStage.test.ts +214 -0
  364. package/src/observability/routes/testMultiStage.ts +96 -0
  365. package/src/observability/routes/traces.test.ts +167 -0
  366. package/src/observability/routes/traces.ts +172 -0
  367. package/src/observability/schemaValidation.ts +29 -0
  368. package/src/observability/sopThresholds.ts +20 -0
  369. package/src/observability/status.test.ts +38 -0
  370. package/src/observability/status.ts +56 -0
  371. package/src/observability/testMultiStageSchemas.ts +95 -0
  372. package/src/observability/testMultiStageWorkflow.test.ts +114 -0
  373. package/src/observability/testMultiStageWorkflow.ts +307 -0
  374. package/src/observability/types.ts +262 -0
  375. package/src/routes/files.test.ts +63 -9
  376. package/src/routes/files.ts +21 -7
  377. package/src/routes/gpt.test.ts +3279 -0
  378. package/src/routes/gpt.ts +91 -816
  379. package/src/routes/gptActions.test.ts +241 -0
  380. package/src/routes/gptActions.ts +255 -0
  381. package/src/routes/gptApprovals.test.ts +762 -0
  382. package/src/routes/gptDatasets.test.ts +86 -0
  383. package/src/routes/gptDatasets.ts +104 -0
  384. package/src/routes/gptFiles.test.ts +589 -0
  385. package/src/routes/gptHistories.test.ts +933 -0
  386. package/src/routes/gptHistories.ts +266 -5
  387. package/src/routes/routes.test.ts +222 -2
  388. package/src/service/aiDatasets.test.ts +123 -0
  389. package/src/service/aiDatasets.ts +316 -0
  390. package/src/service/aiService.test.ts +593 -0
  391. package/src/service/aiService.ts +568 -111
  392. package/src/service/askFiles.test.ts +96 -0
  393. package/src/service/askFiles.ts +222 -0
  394. package/src/service/asks.test.ts +946 -0
  395. package/src/service/asks.ts +423 -0
  396. package/src/service/chatTurn.ts +2287 -0
  397. package/src/service/fileStorage.test.ts +11 -0
  398. package/src/service/fileStorage.ts +10 -2
  399. package/src/service/fillActionIds.test.ts +65 -0
  400. package/src/service/fillActionIds.ts +98 -0
  401. package/src/service/jsonFenceModel.test.ts +47 -0
  402. package/src/service/jsonFenceModel.ts +53 -0
  403. package/src/service/prompts.test.ts +21 -0
  404. package/src/service/prompts.ts +147 -0
  405. package/src/service/sanitizeHtml.test.ts +61 -0
  406. package/src/service/sanitizeHtml.ts +109 -0
  407. package/src/tests/bunSetup.test.ts +7 -4
  408. package/src/tests/bunSetup.ts +7 -7
  409. package/src/tests/chatHarness.ts +478 -0
  410. package/src/tests/generateStream.ts +49 -0
  411. package/src/tests/harnessErrors.ts +22 -0
  412. package/src/tests/harnessStreaming.ts +227 -0
  413. package/src/tests/helpers.ts +9 -2
  414. package/src/types/harness.ts +1119 -0
  415. package/src/types/index.ts +209 -13
  416. package/src/types/observability.ts +344 -0
  417. package/src/types/sanitize-html.d.ts +25 -0
@@ -1,212 +1,67 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.addGptRoutes = void 0;
4
- const node_crypto_1 = require("node:crypto");
5
4
  const api_1 = require("@terreno/api");
6
- const ai_1 = require("ai");
5
+ const blocks_1 = require("@terreno/blocks");
7
6
  const luxon_1 = require("luxon");
8
- const langfuseVercelAi_1 = require("../langfuseVercelAi");
9
- const aiRequest_1 = require("../models/aiRequest");
10
7
  const gptHistory_1 = require("../models/gptHistory");
11
- const project_1 = require("../models/project");
12
- const aiService_1 = require("../service/aiService");
13
- const fileUploadsGate_1 = require("../service/fileUploadsGate");
14
- const prompts_1 = require("../service/prompts");
15
- const DEFAULT_STREAM_PERSIST_INTERVAL_MS = 1000;
8
+ const aiDatasets_1 = require("../service/aiDatasets");
9
+ const asks_1 = require("../service/asks");
10
+ const chatTurn_1 = require("../service/chatTurn");
11
+ const gptActions_1 = require("./gptActions");
12
+ const gptDatasets_1 = require("./gptDatasets");
13
+ const ASK_RESPONSE_BODY = {
14
+ description: "The user's answer to the conversation's pending ask. Send it with historyId instead of prompt.",
15
+ properties: {
16
+ action: { enum: ["accept", "decline", "cancel"], type: "string" },
17
+ content: { description: "The answer, when action is accept", type: "object" },
18
+ reason: { description: "Why the ask was cancelled, when action is cancel", type: "string" },
19
+ toolCallId: { description: "The pending ask's toolCallId", type: "string" },
20
+ },
21
+ type: "object",
22
+ };
23
+ const SURFACE_BODY = {
24
+ description: blocks_1.askSurfaceSchema.description,
25
+ enum: [...blocks_1.ASK_SURFACES],
26
+ type: "string",
27
+ };
16
28
  const DEFAULT_STREAM_RESUME_POLL_INTERVAL_MS = 500;
17
29
  const DEFAULT_STREAM_STALE_AFTER_MS = 60_000;
18
- // Bump `updated` even without new text so long tool calls do not look stale to resumers
19
- const STREAM_HEARTBEAT_MS = 10_000;
20
- const DURABLE_URL_PATTERN = /^(https?:|data:)/i;
21
- const DATA_URL_PATTERN = /^data:([^;,]*)((?:;[^;,]*)*?)(;base64)?,(.*)$/s;
22
30
  const sseEvent = (payload) => `data: ${JSON.stringify(payload)}\n\n`;
23
31
  const sleep = (ms) => new Promise((resolve) => {
24
32
  setTimeout(resolve, ms);
25
33
  });
26
- /**
27
- * Reject attachment URLs that only exist on the client (blob:, file:, content:, ph:, ...).
28
- * They cannot be read by the model provider or reloaded from history later.
29
- */
30
- const validateAttachments = (attachments) => {
31
- if (attachments === undefined || attachments === null) {
32
- return [];
33
- }
34
- if (!Array.isArray(attachments)) {
35
- throw new api_1.APIError({ status: 400, title: "attachments must be an array" });
36
- }
37
- for (const attachment of attachments) {
38
- if (attachment?.type !== "image" && attachment?.type !== "file") {
39
- continue;
40
- }
41
- if (typeof attachment.url !== "string" || !DURABLE_URL_PATTERN.test(attachment.url)) {
42
- const scheme = typeof attachment.url === "string" ? (attachment.url.split(":")[0] ?? "") : "missing";
43
- throw new api_1.APIError({
44
- detail: `Attachment "${attachment.filename ?? attachment.type}" uses a "${scheme}" URL. ` +
45
- "Upload the file first (POST /files/upload) or send it as a data: URL.",
46
- status: 400,
47
- title: "Attachment URL must be an http(s) or data: URL",
48
- });
49
- }
50
- }
51
- return attachments;
52
- };
53
- const decodeDataUrl = (url) => {
54
- const match = DATA_URL_PATTERN.exec(url);
55
- if (!match) {
56
- return undefined;
57
- }
58
- const [, mimeType, , base64Flag, payload] = match;
59
- const buffer = base64Flag
60
- ? Buffer.from(payload, "base64")
61
- : Buffer.from(decodeURIComponent(payload), "utf8");
62
- return { buffer, mimeType: mimeType || undefined };
63
- };
64
- /** Build model and history content parts, uploading data: attachments when storage is configured. */
65
- const buildAttachmentParts = async ({ attachments, fileStorageService, userId, }) => {
66
- const modelParts = [];
67
- const storedParts = [];
68
- for (const attachment of attachments) {
69
- if (attachment.type !== "image" && attachment.type !== "file") {
70
- continue;
71
- }
72
- const url = attachment.url;
73
- const modelPart = attachment.type === "image"
74
- ? { mimeType: attachment.mimeType, type: "image", url }
75
- : {
76
- filename: attachment.filename,
77
- mimeType: attachment.mimeType,
78
- type: "file",
79
- url,
80
- };
81
- modelParts.push(modelPart);
82
- const decoded = url.startsWith("data:") ? decodeDataUrl(url) : undefined;
83
- if (!fileStorageService || !userId || !decoded) {
84
- storedParts.push(modelPart);
85
- continue;
86
- }
87
- const mimeType = attachment.mimeType ?? decoded.mimeType ?? "application/octet-stream";
88
- const extension = mimeType.split("/")[1]?.split("+")[0] ?? "bin";
89
- try {
90
- const uploaded = await fileStorageService.upload({
91
- buffer: decoded.buffer,
92
- filename: attachment.filename ?? `attachment-${luxon_1.DateTime.now().toMillis()}.${extension}`,
93
- mimeType,
94
- userId,
95
- });
96
- storedParts.push({ ...modelPart, gcsKey: uploaded.gcsKey, url: uploaded.url });
97
- }
98
- catch (error) {
99
- throw new api_1.APIError({
100
- cause: error,
101
- detail: error instanceof Error ? error.message : String(error),
102
- status: 502,
103
- title: "Attachment upload failed",
104
- });
105
- }
106
- }
107
- return { modelParts, storedParts };
108
- };
109
- const toPlainPrompt = (prompt) => {
110
- const maybeSubdoc = prompt;
111
- return typeof maybeSubdoc.toObject === "function" ? maybeSubdoc.toObject() : prompt;
112
- };
113
- /** Swap stored attachment references for short-lived signed URLs the model provider can fetch. */
114
- const resolveStoredAttachmentUrls = async (prompts, fileStorageService) => {
115
- if (!fileStorageService) {
116
- return prompts;
117
- }
118
- return Promise.all(prompts.map(async (prompt) => {
119
- if (!prompt.content?.some((part) => part.type !== "text" && part.gcsKey)) {
120
- return prompt;
121
- }
122
- const plain = toPlainPrompt(prompt);
123
- const content = await Promise.all((plain.content ?? []).map(async (part) => {
124
- if (part.type === "text" || !part.gcsKey) {
125
- return part;
126
- }
127
- try {
128
- return { ...part, url: await fileStorageService.getSignedUrl(part.gcsKey) };
129
- }
130
- catch (error) {
131
- api_1.logger.warn("Could not sign stored attachment URL", {
132
- error: error instanceof Error ? error.message : String(error),
133
- gcsKey: part.gcsKey,
134
- });
135
- return part;
136
- }
137
- }));
138
- return { ...plain, content };
139
- }));
140
- };
141
- const isGeneratedImageFile = (value) => typeof value === "object" &&
142
- value !== null &&
143
- "base64" in value &&
144
- typeof value.base64 === "string" &&
145
- "mediaType" in value &&
146
- typeof value.mediaType === "string" &&
147
- value.mediaType.startsWith("image/");
148
- const DEMO_RESPONSE = "This is demo mode. To use AI features, paste your Gemini API key in Settings.";
149
- /** Send a canned SSE demo response when no AI service is available. */
150
- const sendDemoResponse = (res, historyId) => {
151
- res.setHeader("Content-Type", "text/event-stream");
152
- res.setHeader("Cache-Control", "no-cache");
153
- res.setHeader("Connection", "keep-alive");
154
- res.write(`data: ${JSON.stringify({ text: DEMO_RESPONSE })}\n\n`);
155
- res.write(`data: ${JSON.stringify({ done: true, ...(historyId ? { historyId } : {}) })}\n\n`);
156
- res.end();
157
- };
158
- /**
159
- * Resolve the AIService for a request. Priority:
160
- * 1. Per-request API key via `x-ai-api-key` header (creates a temporary AIService via createModelFn)
161
- * 2. Specific model requested + server-side model factory (creates a temporary AIService via createServerModelFn)
162
- * 3. Pre-configured aiService from route options
163
- * 4. undefined (triggers demo mode response)
164
- */
165
- const resolveAiService = (req, options, modelId) => {
166
- const perRequestKey = req.headers["x-ai-api-key"];
167
- if (perRequestKey && options.createModelFn) {
168
- return new aiService_1.AIService({ model: options.createModelFn(perRequestKey, modelId) });
169
- }
170
- if (modelId && options.createServerModelFn) {
171
- const serverModel = options.createServerModelFn(modelId);
172
- if (serverModel) {
173
- return new aiService_1.AIService({ model: serverModel });
174
- }
175
- }
176
- return options.aiService;
34
+ /** Writes turn events as server-sent events. Headers go out when the turn opens the sink. */
35
+ const createSseSink = (res) => {
36
+ let isOpen = false;
37
+ return {
38
+ emit: (event) => {
39
+ res.write(`data: ${JSON.stringify(event)}\n\n`);
40
+ },
41
+ isOpen: () => isOpen,
42
+ open: () => {
43
+ res.setHeader("Content-Type", "text/event-stream");
44
+ res.setHeader("Cache-Control", "no-cache");
45
+ res.setHeader("Connection", "keep-alive");
46
+ isOpen = true;
47
+ },
48
+ };
177
49
  };
178
- /** Generate a short title for a conversation using a cheap model call. */
179
- const generateTitle = async (prompt, response, aiService, options, perRequestApiKey) => {
180
- try {
181
- let titleService = aiService;
182
- if (options.titleModelId) {
183
- if (options.createModelFn && perRequestApiKey) {
184
- titleService = new aiService_1.AIService({
185
- model: options.createModelFn(perRequestApiKey, options.titleModelId),
186
- });
187
- }
188
- else if (options.createServerModelFn) {
189
- const titleModel = options.createServerModelFn(options.titleModelId);
190
- if (titleModel) {
191
- titleService = new aiService_1.AIService({ model: titleModel });
192
- }
193
- }
194
- }
195
- const conversationSnippet = `User: ${prompt}\nAssistant: ${response.substring(0, 500)}`;
196
- const title = await titleService.generateText({
197
- prompt: conversationSnippet,
198
- systemPrompt: prompts_1.TITLE_GENERATION_PROMPT,
199
- temperature: 0.3,
50
+ const addGptRoutes = (router, options) => {
51
+ const { mcpService, tools: routeTools, createRequestTools } = options;
52
+ const askKinds = (0, asks_1.resolveAskKinds)(options.asks);
53
+ if (options.uiBlocks) {
54
+ const uiBlocks = options.uiBlocks === true ? {} : options.uiBlocks;
55
+ (0, aiDatasets_1.configureAiDatasets)({
56
+ datasetMaxRows: uiBlocks.datasetMaxRows,
57
+ datasetTtlDays: uiBlocks.datasetTtlDays,
200
58
  });
201
- return title.trim().replace(/^["']|["']$/g, "");
59
+ (0, gptDatasets_1.addGptDatasetRoutes)(router, { openApiOptions: options.openApiOptions });
60
+ (0, gptActions_1.addGptActionRoutes)(router, options);
202
61
  }
203
- catch {
204
- return undefined;
62
+ if (askKinds.length > 0) {
63
+ (0, asks_1.assertNoReservedToolNames)(routeTools);
205
64
  }
206
- };
207
- const addGptRoutes = (router, options) => {
208
- const { mcpService, tools: routeTools, createRequestTools, toolChoice, maxSteps, fileStorageService, } = options;
209
- const persistIntervalMs = options.streamPersistIntervalMs ?? DEFAULT_STREAM_PERSIST_INTERVAL_MS;
210
65
  const resumePollIntervalMs = options.streamResumePollIntervalMs ?? DEFAULT_STREAM_RESUME_POLL_INTERVAL_MS;
211
66
  const staleAfterMs = options.streamStaleAfterMs ?? DEFAULT_STREAM_STALE_AFTER_MS;
212
67
  router.post("/gpt/prompt", [
@@ -215,6 +70,7 @@ const addGptRoutes = (router, options) => {
215
70
  .withTags(["gpt"])
216
71
  .withSummary("Stream a GPT chat response")
217
72
  .withRequestBody({
73
+ ...(askKinds.length > 0 ? { askResponse: ASK_RESPONSE_BODY } : {}),
218
74
  attachments: {
219
75
  items: {
220
76
  properties: {
@@ -231,6 +87,7 @@ const addGptRoutes = (router, options) => {
231
87
  model: { type: "string" },
232
88
  projectId: { type: "string" },
233
89
  prompt: { type: "string" },
90
+ surface: SURFACE_BODY,
234
91
  systemPrompt: { type: "string" },
235
92
  })
236
93
  .withResponse(200, { data: { type: "string" } })
@@ -239,498 +96,31 @@ const addGptRoutes = (router, options) => {
239
96
  // Use a raw handler instead of asyncHandler so we can control error responses
240
97
  // for both pre-stream (JSON) and mid-stream (SSE) errors.
241
98
  async (req, res) => {
242
- let sseStarted = false;
99
+ const sink = createSseSink(res);
243
100
  try {
244
- const { prompt, historyId, systemPrompt, attachments, model: requestModel, projectId, } = req.body;
245
- const userId = req.user?._id;
246
- if (!prompt || typeof prompt !== "string") {
247
- throw new api_1.APIError({ status: 400, title: "prompt is required" });
248
- }
249
- if (Array.isArray(attachments) && attachments.length > 0) {
250
- await (0, fileUploadsGate_1.assertFileUploadsEnabled)(req, options.fileUploadsEnabled);
251
- }
252
- const validAttachments = validateAttachments(attachments);
253
- // Resolve AI service (per-request key takes priority, then configured service)
254
- const hasPerRequestKey = !!req.headers["x-ai-api-key"];
255
- const hasCreateModelFn = !!options.createModelFn;
256
- const hasConfiguredService = !!options.aiService;
257
- api_1.logger.debug("Resolving AI service", {
258
- hasConfiguredService,
259
- hasCreateModelFn,
260
- hasPerRequestKey,
261
- });
262
- const aiService = resolveAiService(req, options, requestModel);
263
- if (!aiService) {
264
- api_1.logger.debug("No AI service available, sending demo response");
265
- return sendDemoResponse(res);
266
- }
267
- // Load or create history
268
- let history;
269
- if (historyId) {
270
- history = await gptHistory_1.GptHistory.findById(historyId);
271
- if (!history) {
272
- throw new api_1.APIError({ status: 404, title: "History not found" });
273
- }
274
- if (history.userId.toString() !== userId?.toString()) {
275
- throw new api_1.APIError({ status: 403, title: "Not authorized to access this history" });
276
- }
277
- }
278
- else {
279
- history = new gptHistory_1.GptHistory({ prompts: [], userId, ...(projectId ? { projectId } : {}) });
280
- }
281
- // If history doesn't have a projectId yet but one was provided, associate it
282
- if (projectId && !history.projectId) {
283
- history.projectId = projectId;
284
- }
285
- // Load project context if a projectId is provided (or inherited from history)
286
- const effectiveProjectId = projectId ?? history.projectId;
287
- let effectiveSystemPrompt = systemPrompt;
288
- if (effectiveProjectId) {
289
- try {
290
- const project = await project_1.Project.findById(effectiveProjectId);
291
- if (project && project.userId.toString() === userId?.toString()) {
292
- const parts = [];
293
- if (project.systemContext) {
294
- parts.push(project.systemContext);
295
- }
296
- if (project.memories.length > 0) {
297
- parts.push(`## Relevant Memories\n${project.memories.map((m) => `- ${m.text}`).join("\n")}`);
298
- }
299
- if (parts.length > 0) {
300
- const projectContext = parts.join("\n\n");
301
- effectiveSystemPrompt = effectiveSystemPrompt
302
- ? `${projectContext}\n\n${effectiveSystemPrompt}`
303
- : projectContext;
304
- }
305
- }
306
- }
307
- catch {
308
- // Project loading failure should not block the request
309
- }
310
- }
311
- // Load system prompt from Langfuse if configured
312
- if (options.langfuseSystemPromptName) {
313
- try {
314
- const langfuseResult = await (0, langfuseVercelAi_1.preparePromptForAI)({
315
- promptName: options.langfuseSystemPromptName,
316
- userId: userId?.toString(),
317
- });
318
- const langfusePrompt = typeof langfuseResult.prompt === "string" ? langfuseResult.prompt : undefined;
319
- if (langfusePrompt) {
320
- effectiveSystemPrompt = effectiveSystemPrompt
321
- ? `${langfusePrompt}\n\n${effectiveSystemPrompt}`
322
- : langfusePrompt;
323
- }
324
- }
325
- catch (err) {
326
- api_1.logger.debug(`Langfuse system prompt skipped: ${err.message}`);
327
- }
328
- }
329
- // Build content parts from attachments
330
- api_1.logger.debug("Processing attachments", {
331
- count: validAttachments.length,
332
- types: validAttachments.map((a) => ({
333
- mimeType: a.mimeType,
334
- type: a.type,
335
- urlLength: a.url?.length ?? 0,
336
- })),
337
- });
338
- const { modelParts, storedParts } = await buildAttachmentParts({
339
- attachments: validAttachments,
340
- fileStorageService,
341
- userId,
342
- });
343
- const contentParts = [{ text: prompt, type: "text" }, ...storedParts];
344
- // Add user prompt to history
345
- const hasAttachments = contentParts.length > 1;
346
- const userPrompt = {
347
- text: prompt,
348
- type: "user",
349
- ...(hasAttachments ? { content: contentParts } : {}),
350
- };
351
- history.prompts.push(userPrompt);
352
- // Build messages from history using AIService helper
353
- api_1.logger.debug("Building messages", {
354
- attachmentCount: contentParts.length - 1,
355
- historyLength: history.prompts.length,
356
- });
357
- // Earlier turns reference stored attachments; this turn sends the original URLs
358
- const earlierPrompts = await resolveStoredAttachmentUrls(history.prompts.slice(0, -1), fileStorageService);
359
- const modelUserPrompt = hasAttachments
360
- ? { ...userPrompt, content: [{ text: prompt, type: "text" }, ...modelParts] }
361
- : userPrompt;
362
- const messages = aiService.buildMessages([...earlierPrompts, modelUserPrompt]);
363
- api_1.logger.debug("Messages built", { messageCount: messages.length });
364
- // Some models (e.g. gemini-3-pro-image) don't support tool calling
365
- const modelId = aiService.modelId;
366
- const supportsTools = !modelId?.includes("image");
367
- // Merge tools from route config, per-request tools, and MCP service
368
- let allTools;
369
- const requestTools = createRequestTools ? createRequestTools(req) : undefined;
370
- if (supportsTools && (routeTools || requestTools || mcpService)) {
371
- allTools = { ...(routeTools ?? {}), ...(requestTools ?? {}) };
372
- if (mcpService) {
373
- try {
374
- const mcpTools = await mcpService.getTools();
375
- Object.assign(allTools, mcpTools);
376
- }
377
- catch {
378
- // MCP tool discovery failure should not block the request
379
- }
380
- }
381
- }
382
- // Persist the user turn and a streaming placeholder so a reload can resume the reply
383
- const streamId = (0, node_crypto_1.randomUUID)();
384
- history.prompts.push({
385
- model: aiService.modelId,
386
- status: "streaming",
387
- streamId,
388
- text: "",
389
- type: "assistant",
390
- });
391
- await history.save();
392
- const savedHistory = history;
393
- const placeholderIndex = history.prompts.length - 1;
394
- // Stream response via SSE
395
- res.setHeader("Content-Type", "text/event-stream");
396
- res.setHeader("Cache-Control", "no-cache");
397
- res.setHeader("Connection", "keep-alive");
398
- sseStarted = true;
399
- res.write(sseEvent({ historyId: history._id.toString(), started: true, streamId }));
400
- let fullResponse = "";
401
- // Buffer text per step so we can discard reasoning text when a tool call follows
402
- let stepTextBuffer = "";
403
- let stepHasToolCall = false;
404
- const currentPartialText = () => fullResponse + (stepHasToolCall ? "" : stepTextBuffer);
405
- let lastPersistedText = "";
406
- let lastPersistedAt = luxon_1.DateTime.now();
407
- let persistQueue = Promise.resolve();
408
- const persistTimer = setInterval(() => {
409
- const text = currentPartialText();
410
- const isHeartbeatDue = luxon_1.DateTime.now().diff(lastPersistedAt).toMillis() >= STREAM_HEARTBEAT_MS;
411
- if (text === lastPersistedText && !isHeartbeatDue) {
412
- return;
413
- }
414
- lastPersistedText = text;
415
- lastPersistedAt = luxon_1.DateTime.now();
416
- persistQueue = persistQueue
417
- .then(async () => {
418
- await gptHistory_1.GptHistory.updateOne({ _id: savedHistory._id, [`prompts.${placeholderIndex}.streamId`]: streamId }, {
419
- $set: {
420
- [`prompts.${placeholderIndex}.text`]: text,
421
- updated: luxon_1.DateTime.now().toJSDate(),
422
- },
423
- });
424
- })
425
- .catch((persistErr) => {
426
- api_1.logger.warn("Failed to persist partial GPT response", {
427
- error: persistErr instanceof Error ? persistErr.message : String(persistErr),
428
- });
429
- });
430
- }, persistIntervalMs);
431
- const stopPersisting = async () => {
432
- clearInterval(persistTimer);
433
- await persistQueue;
434
- };
435
- const removePlaceholder = () => {
436
- const index = savedHistory.prompts.findIndex((p) => p.streamId === streamId);
437
- if (index >= 0) {
438
- savedHistory.prompts.splice(index, 1);
439
- }
440
- };
441
- const generatedImages = [];
442
- // Stream file parts and result.files report the same images; emit each data URL once.
443
- const sendGeneratedImage = (file) => {
444
- const url = `data:${file.mediaType};base64,${file.base64}`;
445
- if (generatedImages.some((img) => img.url === url)) {
446
- return;
447
- }
448
- generatedImages.push({ mimeType: file.mediaType, url });
449
- res.write(`data: ${JSON.stringify({ image: { mimeType: file.mediaType, url } })}\n\n`);
450
- };
451
- const startTime = luxon_1.DateTime.now().toMillis();
452
- try {
453
- api_1.logger.debug("Starting streamText", { model: modelId, supportsTools });
454
- const telemetry = (0, langfuseVercelAi_1.createTelemetryConfig)({
455
- functionId: "gpt-prompt",
456
- metadata: {
457
- ...(options.langfuseSystemPromptName
458
- ? { langfusePromptName: options.langfuseSystemPromptName }
459
- : {}),
460
- },
461
- userId: userId?.toString(),
462
- });
463
- const result = (0, ai_1.streamText)({
464
- experimental_telemetry: telemetry,
465
- messages,
466
- model: aiService.model,
467
- providerOptions: !supportsTools
468
- ? {
469
- google: { responseModalities: ["TEXT", "IMAGE"] },
470
- vertex: { responseModalities: ["TEXT", "IMAGE"] },
471
- }
472
- : undefined,
473
- stopWhen: allTools ? (0, ai_1.stepCountIs)(maxSteps ?? 5) : (0, ai_1.stepCountIs)(1),
474
- system: effectiveSystemPrompt ?? undefined,
475
- temperature: aiService.defaultTemperature,
476
- toolChoice: allTools ? (toolChoice ?? "auto") : undefined,
477
- tools: allTools,
478
- });
479
- let partCount = 0;
480
- for await (const part of result.fullStream) {
481
- partCount++;
482
- if (partCount <= 5 || part.type === "error" || part.type === "file") {
483
- api_1.logger.debug("Stream part", {
484
- partCount,
485
- type: part.type,
486
- ...(part.type === "file" && isGeneratedImageFile(part.file)
487
- ? { mediaType: part.file.mediaType }
488
- : {}),
489
- ...(part.type === "error" ? { error: String(part.error ?? part) } : {}),
490
- });
491
- }
492
- // Track step boundaries to discard reasoning text from tool-call steps
493
- if (part.type === "start-step") {
494
- stepTextBuffer = "";
495
- stepHasToolCall = false;
496
- continue;
497
- }
498
- if (part.type === "finish-step") {
499
- // Only emit buffered text if no tool call happened in this step
500
- if (!stepHasToolCall && stepTextBuffer) {
501
- // Strip model reasoning that leaks as JSON action blobs
502
- const cleaned = stepTextBuffer
503
- .replace(/\{[\s\S]*?"action"[\s\S]*?\}\s*$/g, "")
504
- .trim();
505
- if (cleaned) {
506
- fullResponse += cleaned;
507
- res.write(`data: ${JSON.stringify({ text: cleaned })}\n\n`);
508
- }
509
- }
510
- stepTextBuffer = "";
511
- stepHasToolCall = false;
512
- continue;
513
- }
514
- if (part.type === "error") {
515
- const errMsg = part.error instanceof Error
516
- ? part.error.message
517
- : String(part.error ?? "Unknown stream error");
518
- api_1.logger.error("AI stream error part", { error: errMsg });
519
- res.write(`data: ${JSON.stringify({ error: errMsg })}\n\n`);
520
- continue;
521
- }
522
- if (part.type === "file") {
523
- if (isGeneratedImageFile(part.file)) {
524
- sendGeneratedImage(part.file);
525
- api_1.logger.debug("Sent inline image from stream");
526
- }
527
- continue;
528
- }
529
- if (part.type === "text-delta") {
530
- const textChunk = (part.text ?? "");
531
- if (textChunk) {
532
- stepTextBuffer += textChunk;
533
- }
534
- }
535
- else if (part.type === "tool-call") {
536
- stepHasToolCall = true;
537
- res.write(`data: ${JSON.stringify({
538
- toolCall: {
539
- args: part.input,
540
- toolCallId: part.toolCallId,
541
- toolName: part.toolName,
542
- },
543
- })}\n\n`);
544
- // Persist tool call in history
545
- history.prompts.push({
546
- args: part.input,
547
- text: `Tool call: ${part.toolName}`,
548
- toolCallId: part.toolCallId,
549
- toolName: part.toolName,
550
- type: "tool-call",
551
- });
552
- }
553
- else if (part.type === "tool-result") {
554
- const toolResult = part.output;
555
- // If the tool result contains file data, send it as a separate file SSE event
556
- if (toolResult?.fileData && typeof toolResult.fileData === "string") {
557
- res.write(`data: ${JSON.stringify({
558
- file: {
559
- filename: toolResult.filename ?? "document",
560
- mimeType: toolResult.mimeType ?? "application/octet-stream",
561
- url: toolResult.fileData,
562
- },
563
- })}\n\n`);
564
- api_1.logger.debug("Sent generated file from tool result", {
565
- filename: toolResult.filename,
566
- mimeType: toolResult.mimeType,
567
- });
568
- }
569
- // Strip fileData from result before sending/storing to avoid bloating the SSE and DB
570
- const { fileData: _fileData, ...cleanResult } = toolResult ?? {};
571
- res.write(`data: ${JSON.stringify({
572
- toolResult: {
573
- result: cleanResult,
574
- toolCallId: part.toolCallId,
575
- toolName: part.toolName,
576
- },
577
- })}\n\n`);
578
- // Persist tool result in history (without the large file data)
579
- history.prompts.push({
580
- result: cleanResult,
581
- text: `Tool result: ${part.toolName}`,
582
- toolCallId: part.toolCallId,
583
- toolName: part.toolName,
584
- type: "tool-result",
585
- });
586
- }
587
- }
588
- // Flush any remaining buffered text from the last step
589
- if (!stepHasToolCall && stepTextBuffer) {
590
- const cleaned = stepTextBuffer.replace(/\{[\s\S]*?"action"[\s\S]*?\}\s*$/g, "").trim();
591
- if (cleaned) {
592
- fullResponse += cleaned;
593
- res.write(`data: ${JSON.stringify({ text: cleaned })}\n\n`);
594
- }
595
- }
596
- api_1.logger.debug("Stream completed", { fullResponseLength: fullResponse.length, partCount });
597
- // Catch generated images the stream did not report as file parts
598
- try {
599
- const files = await result.files;
600
- if (files && files.length > 0) {
601
- for (const file of files) {
602
- if (isGeneratedImageFile(file)) {
603
- sendGeneratedImage(file);
604
- }
605
- }
606
- api_1.logger.debug("Sent generated images", { count: generatedImages.length });
607
- }
608
- }
609
- catch (fileErr) {
610
- api_1.logger.debug("No files in response", {
611
- error: fileErr instanceof Error ? fileErr.message : String(fileErr),
612
- });
613
- }
614
- // Replace the streaming placeholder with the final reply, after any tool turns
615
- await stopPersisting();
616
- removePlaceholder();
617
- if (fullResponse || generatedImages.length > 0) {
618
- const contentParts = generatedImages.map((img) => ({
619
- mimeType: img.mimeType,
620
- type: "image",
621
- url: img.url,
622
- }));
623
- const assistantPrompt = {
624
- model: aiService.modelId,
625
- status: "complete",
626
- streamId,
627
- text: fullResponse,
628
- type: "assistant",
629
- ...(contentParts.length > 0 ? { content: contentParts } : {}),
630
- };
631
- history.prompts.push(assistantPrompt);
632
- }
633
- history.markModified("prompts");
634
- await history.save();
635
- try {
636
- await aiRequest_1.AIRequest.logRequest({
637
- aiModel: modelId ?? "unknown",
638
- prompt,
639
- requestType: "general",
640
- response: fullResponse,
641
- responseTime: luxon_1.DateTime.now().toMillis() - startTime,
642
- userId: userId ?? undefined,
643
- });
644
- }
645
- catch (logErr) {
646
- api_1.logger.warn("Failed to log AIRequest", {
647
- error: logErr instanceof Error ? logErr.message : String(logErr),
648
- });
649
- }
650
- // Generate a title for new conversations using a cheap model call
651
- if (!history.title && fullResponse) {
652
- const perRequestApiKey = req.headers["x-ai-api-key"];
653
- const title = await generateTitle(prompt, fullResponse, aiService, options, perRequestApiKey);
654
- if (title) {
655
- history.title = title;
656
- await history.save();
657
- }
658
- }
659
- api_1.logger.debug("Sending done event", {
660
- fullResponseLength: fullResponse.length,
661
- historyId: history._id.toString(),
662
- });
663
- res.write(`data: ${JSON.stringify({
664
- done: true,
665
- historyId: history._id.toString(),
666
- ...(history.title ? { title: history.title } : {}),
667
- })}\n\n`);
668
- res.end();
669
- }
670
- catch (error) {
671
- api_1.logger.error("Error in GPT stream", {
672
- error: error instanceof Error ? error.message : String(error),
673
- });
674
- // Keep any partial reply so the conversation shows what streamed before the failure
675
- try {
676
- await stopPersisting();
677
- const partialText = currentPartialText();
678
- const placeholder = savedHistory.prompts.find((p) => p.streamId === streamId);
679
- if (placeholder?.status === "streaming") {
680
- if (partialText) {
681
- placeholder.text = partialText;
682
- placeholder.status = "error";
683
- }
684
- else {
685
- removePlaceholder();
686
- }
687
- savedHistory.markModified("prompts");
688
- await savedHistory.save();
689
- }
690
- }
691
- catch (saveErr) {
692
- api_1.logger.warn("Failed to save interrupted GPT response", {
693
- error: saveErr instanceof Error ? saveErr.message : String(saveErr),
694
- });
695
- }
696
- try {
697
- await aiRequest_1.AIRequest.logRequest({
698
- aiModel: modelId ?? "unknown",
699
- error: error instanceof Error ? error.message : String(error),
700
- prompt,
701
- requestType: "general",
702
- responseTime: luxon_1.DateTime.now().toMillis() - startTime,
703
- userId: userId ?? undefined,
704
- });
705
- }
706
- catch (logErr) {
707
- api_1.logger.warn("Failed to log AIRequest error", {
708
- error: logErr instanceof Error ? logErr.message : String(logErr),
709
- });
710
- }
711
- res.write(`data: ${JSON.stringify({ error: error instanceof Error ? error.message : "Unknown error" })}\n\n`);
712
- res.end();
713
- }
101
+ await (0, chatTurn_1.runChatTurn)({ body: req.body ?? {}, options, req, sink });
102
+ res.end();
714
103
  }
715
- catch (outerError) {
104
+ catch (error) {
716
105
  // Catch-all for errors thrown before or after SSE streaming
717
- const message = outerError instanceof Error ? outerError.message : String(outerError);
718
- const status = outerError instanceof api_1.APIError ? outerError.status : 500;
719
- const stack = outerError instanceof Error ? outerError.stack : undefined;
106
+ const message = error instanceof Error ? error.message : String(error);
107
+ const status = (0, api_1.isAPIError)(error) ? error.status : 500;
108
+ const stack = error instanceof Error ? error.stack : undefined;
720
109
  api_1.logger.error("GPT prompt handler error", { error: message, stack, status });
721
- if (sseStarted) {
110
+ if (sink.isOpen()) {
722
111
  // Already sent SSE headers — send error as SSE event
723
112
  res.write(`data: ${JSON.stringify({ error: message })}\n\n`);
724
113
  res.end();
114
+ return;
725
115
  }
726
- else {
727
- // Haven't started SSE — send a normal JSON error response
728
- res.status(status).json({
729
- detail: message,
730
- status,
731
- title: outerError instanceof api_1.APIError ? outerError.title : "Internal server error",
732
- });
733
- }
116
+ // Haven't started SSE — send a normal JSON error response
117
+ const fields = (0, api_1.isAPIError)(error) ? error.meta?.fields : undefined;
118
+ res.status(status).json({
119
+ detail: ((0, api_1.isAPIError)(error) ? error.detail : undefined) ?? message,
120
+ ...(fields ? { fields } : {}),
121
+ status,
122
+ title: (0, api_1.isAPIError)(error) ? error.title : "Internal server error",
123
+ });
734
124
  }
735
125
  });
736
126
  // Re-attach to a reply that /gpt/prompt is still streaming (e.g. after a reload or remount).
@@ -847,7 +237,7 @@ const addGptRoutes = (router, options) => {
847
237
  const { id } = req.params;
848
238
  const { promptIndex, rating } = req.body;
849
239
  const userId = req.user?._id;
850
- if (typeof promptIndex !== "number" || promptIndex < 0) {
240
+ if (typeof promptIndex !== "number" || !Number.isInteger(promptIndex) || promptIndex < 0) {
851
241
  throw new api_1.APIError({ status: 400, title: "promptIndex must be a non-negative number" });
852
242
  }
853
243
  if (rating !== null && rating !== "up" && rating !== "down") {
@@ -863,15 +253,10 @@ const addGptRoutes = (router, options) => {
863
253
  if (promptIndex >= history.prompts.length) {
864
254
  throw new api_1.APIError({ status: 400, title: "promptIndex out of range" });
865
255
  }
866
- if (rating === null) {
867
- history.prompts[promptIndex].rating = undefined;
868
- }
869
- else {
870
- history.prompts[promptIndex].rating = rating;
871
- }
872
- history.markModified("prompts");
873
- await history.save();
874
- return res.json({ data: { promptIndex, rating: history.prompts[promptIndex].rating ?? null } });
256
+ // One field update, so rows a running turn appends meanwhile are kept.
257
+ const ratingPath = `prompts.${promptIndex}.rating`;
258
+ await gptHistory_1.GptHistory.updateOne({ _id: history._id }, rating === null ? { $unset: { [ratingPath]: "" } } : { $set: { [ratingPath]: rating } });
259
+ return res.json({ data: { promptIndex, rating } });
875
260
  }));
876
261
  router.post("/gpt/remix", [
877
262
  (0, api_1.authenticateMiddleware)(),
@@ -886,12 +271,17 @@ const addGptRoutes = (router, options) => {
886
271
  ], (0, api_1.asyncHandler)(async (req, res) => {
887
272
  const { text } = req.body;
888
273
  const userId = req.user?._id;
274
+ const hasPromptReference = typeof req.body.promptName === "string" || typeof req.body.promptLabel === "string";
275
+ const isAdmin = req.user?.admin === true;
889
276
  if (!text || typeof text !== "string") {
890
277
  throw new api_1.APIError({ status: 400, title: "text is required" });
891
278
  }
892
- const aiService = resolveAiService(req, options);
279
+ if (hasPromptReference && !isAdmin) {
280
+ throw new api_1.APIError({ status: 403, title: "Prompt registry selection requires admin access" });
281
+ }
282
+ const aiService = (0, chatTurn_1.resolveAiService)(req, options);
893
283
  if (!aiService) {
894
- return res.json({ data: DEMO_RESPONSE });
284
+ return res.json({ data: chatTurn_1.DEMO_RESPONSE });
895
285
  }
896
286
  const result = await aiService.generateRemix({ text, userId });
897
287
  return res.json({ data: result });