plugin-agent-orchestrator 1.1.7 → 1.2.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 (413) hide show
  1. package/README.md +207 -204
  2. package/client-v2.d.ts +2 -2
  3. package/client-v2.js +1 -1
  4. package/client.d.ts +2 -2
  5. package/client.js +1 -1
  6. package/dist/client/117.2e4cb0477da3db1f.js +10 -0
  7. package/dist/client/253.93deb9e0192e76a5.js +10 -0
  8. package/dist/client/325.8f7f509bdb35e142.js +10 -0
  9. package/dist/client/404.498d2d9516dd1dce.js +10 -0
  10. package/dist/client/418.6f01da17774a5a25.js +10 -0
  11. package/dist/client/53.774ff07505d521fa.js +10 -0
  12. package/dist/client/619.e342b0fa467d39ca.js +10 -0
  13. package/dist/client/627.fdd60bcc0f3a9cf6.js +10 -0
  14. package/dist/client/652.477e21bfff67bc1c.js +10 -0
  15. package/dist/client/981.e0546ffc34afedcd.js +10 -0
  16. package/dist/client/993.5d04b510835ab0d2.js +10 -0
  17. package/dist/client/index.js +1 -1
  18. package/dist/client-v2/117.ccf43ae8631f7d74.js +10 -0
  19. package/dist/client-v2/214.cdf76752053a8990.js +10 -0
  20. package/dist/client-v2/253.1198df22f5baf304.js +10 -0
  21. package/dist/client-v2/264.78887ff88f969467.js +10 -0
  22. package/dist/client-v2/325.fafe5a6e83ba9cb3.js +10 -0
  23. package/dist/client-v2/404.9ba964c28a151c81.js +10 -0
  24. package/dist/client-v2/{41.3deb9e57a0f64fc4.js → 41.b28cf7a675a603d1.js} +1 -1
  25. package/dist/client-v2/418.55b6b28cda8bb5f3.js +10 -0
  26. package/dist/client-v2/{53.349fff8db80b19d4.js → 53.282081f97e4308c6.js} +1 -1
  27. package/dist/client-v2/{619.0a2144aaffefe018.js → 619.8c03913873785fb6.js} +1 -1
  28. package/dist/client-v2/627.e16f4a0dd596c3e6.js +10 -0
  29. package/dist/client-v2/652.1a48364774d5383a.js +10 -0
  30. package/dist/client-v2/{70.ffff2997c10fd4b0.js → 70.9166ed70906d041f.js} +1 -1
  31. package/dist/client-v2/{981.e4a1f41cfc94a856.js → 981.a81521f0296c7db8.js} +1 -1
  32. package/dist/client-v2/993.ca5b28f1e2b4c1ef.js +10 -0
  33. package/dist/client-v2/index.js +1 -1
  34. package/dist/externalVersion.js +10 -7
  35. package/dist/locale/en-US.json +229 -1
  36. package/dist/locale/zh-CN.json +229 -1
  37. package/dist/node_modules/adm-zip/adm-zip.js +1 -1
  38. package/dist/node_modules/adm-zip/package.json +1 -1
  39. package/dist/node_modules/ajv/package.json +1 -1
  40. package/dist/node_modules/zod/index.cjs +1 -1
  41. package/dist/node_modules/zod/package.json +1 -1
  42. package/dist/server/collections/agent-execution-spans.js +145 -6
  43. package/dist/server/collections/agent-governance-audit-events.js +47 -0
  44. package/dist/server/collections/agent-governance-policies.js +47 -0
  45. package/dist/server/collections/agent-governance-policy-versions.js +48 -0
  46. package/dist/server/collections/agent-governance-prompt-bindings.js +50 -0
  47. package/dist/server/collections/agent-loop-runs.js +23 -5
  48. package/dist/server/migrations/20261110000000-add-usage-pricing-snapshots.js +117 -0
  49. package/dist/server/migrations/20261110010000-add-usage-department-attribution.js +54 -0
  50. package/dist/server/plugin.js +114 -33
  51. package/dist/server/resources/agent-governance.js +192 -0
  52. package/dist/server/resources/agent-harness-profile.js +11 -3
  53. package/dist/server/resources/agent-monitor.js +109 -339
  54. package/dist/server/resources/agent-usage.js +423 -0
  55. package/dist/server/resources/native-monitor-format.js +179 -0
  56. package/dist/server/resources/tracing.js +35 -12
  57. package/dist/server/services/AgentGovernanceService.js +500 -0
  58. package/dist/server/services/DepartmentAttribution.js +65 -0
  59. package/dist/server/services/ExecutionSpanService.js +96 -3
  60. package/dist/server/services/GovernanceAdmission.js +211 -0
  61. package/dist/server/services/HarnessCapabilities.js +118 -0
  62. package/dist/server/services/LoopBudgetService.js +29 -11
  63. package/dist/server/services/LoopRunRepository.js +15 -6
  64. package/dist/server/services/LoopRunStateMachine.js +18 -7
  65. package/dist/server/services/LoopTriggerService.js +1 -1
  66. package/dist/server/services/LoopWorkerService.js +66 -32
  67. package/dist/server/services/ManagedPromptProtection.js +99 -0
  68. package/dist/server/services/NativeAgentMonitorSyncService.js +584 -0
  69. package/dist/server/services/NativeSubAgentObserver.js +141 -508
  70. package/dist/server/services/NativeUsageAttribution.js +97 -0
  71. package/dist/server/services/TokenTracker.js +787 -104
  72. package/dist/server/services/TracingRetentionPolicy.js +66 -0
  73. package/dist/server/services/VerificationService.js +18 -8
  74. package/dist/server/skill-hub/sandbox-config.json +6 -6
  75. package/dist/server/utils/ensure-indexes.js +14 -5
  76. package/package.json +63 -53
  77. package/server.d.ts +2 -2
  78. package/server.js +1 -1
  79. package/src/client/AgentRunsTab.tsx +2 -2
  80. package/src/client/ApprovalsTab.tsx +2 -2
  81. package/src/client/HarnessProfilesTab.tsx +2 -2
  82. package/src/client/OrchestratorSettings.tsx +152 -152
  83. package/src/client/SkillPages.tsx +41 -0
  84. package/src/client/__tests__/embedded-settings.test.tsx +232 -0
  85. package/src/client/hooks/useRunEventStream.ts +76 -76
  86. package/src/client/index.tsx +2 -2
  87. package/src/client/plugin.tsx +76 -32
  88. package/src/client/skill-hub/components/ExecutionHistory.tsx +200 -200
  89. package/src/client/skill-hub/components/GitSkillImport.tsx +562 -562
  90. package/src/client/skill-hub/components/LoopSettings.tsx +331 -331
  91. package/src/client/skill-hub/components/SkillEditor.tsx +460 -460
  92. package/src/client/skill-hub/components/SkillManager.tsx +194 -194
  93. package/src/client/skill-hub/components/SkillMetrics.tsx +162 -162
  94. package/src/client/skill-hub/components/SkillTestPanel.tsx +141 -141
  95. package/src/client/skill-hub/index.tsx +6 -6
  96. package/src/client/skill-hub/locale.ts +16 -16
  97. package/src/client/skill-hub/tools/InteractionSchemasProvider.tsx +132 -132
  98. package/src/client/skill-hub/tools/SkillHubCard.tsx +104 -104
  99. package/src/client/skill-hub/tools/loopTemplates.ts +52 -52
  100. package/src/client/skill-hub/tools/registerSkillLoopCards.ts +71 -71
  101. package/src/client/skill-hub/utils/jsonFields.ts +41 -41
  102. package/src/client-v2/__tests__/embedded-settings.test.tsx +193 -0
  103. package/src/client-v2/components/AIEmployeeSelect.tsx +47 -47
  104. package/src/client-v2/components/AIEmployeesContext.tsx +97 -97
  105. package/src/client-v2/components/AgentBindingsTab.tsx +269 -269
  106. package/src/client-v2/components/AgentRunsTab.tsx +92 -83
  107. package/src/client-v2/components/ApprovalsTab.tsx +337 -337
  108. package/src/client-v2/components/EmployeeGovernanceTab.tsx +702 -0
  109. package/src/client-v2/components/HarnessProfilesTab.tsx +704 -541
  110. package/src/client-v2/components/KnowledgeAccessTab.tsx +116 -116
  111. package/src/client-v2/components/RetrievalTraceTab.tsx +162 -162
  112. package/src/client-v2/components/UsageGovernanceTab.tsx +386 -0
  113. package/src/client-v2/index.tsx +1 -1
  114. package/src/client-v2/pages/AgentBindingsPage.tsx +22 -13
  115. package/src/client-v2/pages/ApprovalsPage.tsx +19 -10
  116. package/src/client-v2/pages/EmployeeGovernancePage.tsx +22 -0
  117. package/src/client-v2/pages/ExecutionHistoryPage.tsx +19 -10
  118. package/src/client-v2/pages/HarnessProfilesPage.tsx +19 -10
  119. package/src/client-v2/pages/KnowledgeAccessPage.tsx +19 -10
  120. package/src/client-v2/pages/LoopSettingsPage.tsx +19 -10
  121. package/src/client-v2/pages/MemoryInspectorPage.tsx +22 -13
  122. package/src/client-v2/pages/RetrievalTracePage.tsx +19 -10
  123. package/src/client-v2/pages/RulesPage.tsx +22 -13
  124. package/src/client-v2/pages/SkillDefinitionsPage.tsx +19 -10
  125. package/src/client-v2/pages/SkillMetricsPage.tsx +19 -10
  126. package/src/client-v2/pages/TracingPage.tsx +22 -13
  127. package/src/client-v2/pages/UsageGovernancePage.tsx +22 -0
  128. package/src/client-v2/plugin.tsx +139 -99
  129. package/src/client-v2/skill-hub/components/ExecutionHistory.tsx +196 -196
  130. package/src/client-v2/skill-hub/components/FileLinkList.tsx +37 -37
  131. package/src/client-v2/skill-hub/components/GitSkillImport.tsx +539 -539
  132. package/src/client-v2/skill-hub/components/LoopSettings.tsx +331 -331
  133. package/src/client-v2/skill-hub/components/SkillEditor.tsx +453 -453
  134. package/src/client-v2/skill-hub/components/SkillManager.tsx +191 -174
  135. package/src/client-v2/skill-hub/components/SkillMetrics.tsx +162 -162
  136. package/src/client-v2/skill-hub/components/SkillTestPanel.tsx +135 -135
  137. package/src/client-v2/skill-hub/locale.ts +13 -13
  138. package/src/client-v2/skill-hub/tools/InteractionSchemasProvider.tsx +62 -0
  139. package/src/client-v2/skill-hub/tools/SkillHubCard.tsx +104 -0
  140. package/src/client-v2/skill-hub/tools/__tests__/interactions.test.ts +110 -0
  141. package/src/client-v2/skill-hub/tools/interactions.ts +128 -0
  142. package/src/client-v2/skill-hub/tools/loopTemplates.ts +52 -52
  143. package/src/client-v2/skill-hub/utils/jsonFields.ts +41 -41
  144. package/src/client-v2/utils/jsonFields.ts +41 -41
  145. package/src/index.ts +2 -2
  146. package/src/locale/en-US.json +229 -1
  147. package/src/locale/zh-CN.json +229 -1
  148. package/src/server/__tests__/agent-execution-context.test.ts +18 -18
  149. package/src/server/__tests__/agent-governance.integration.test.ts +392 -0
  150. package/src/server/__tests__/agent-knowledge-insights.test.ts +123 -123
  151. package/src/server/__tests__/agent-monitor-acl.integration.test.ts +274 -0
  152. package/src/server/__tests__/agent-registry-service.test.ts +147 -147
  153. package/src/server/__tests__/agent-runtime-lifecycle.test.ts +46 -46
  154. package/src/server/__tests__/agent-usage-report.integration.test.ts +342 -0
  155. package/src/server/__tests__/code-validator.test.ts +63 -63
  156. package/src/server/__tests__/external-rag-search.test.ts +107 -107
  157. package/src/server/__tests__/harness-capabilities.test.ts +95 -0
  158. package/src/server/__tests__/harness-compiler.test.ts +214 -214
  159. package/src/server/__tests__/harness-profile-service.test.ts +167 -167
  160. package/src/server/__tests__/harness-runtime-policy.test.ts +293 -293
  161. package/src/server/__tests__/harness-tool-plan.test.ts +182 -172
  162. package/src/server/__tests__/input-schema-validator.test.ts +26 -26
  163. package/src/server/__tests__/loop-approval-decision.test.ts +577 -512
  164. package/src/server/__tests__/loop-approval-expiry.test.ts +258 -258
  165. package/src/server/__tests__/loop-budget-service.test.ts +36 -0
  166. package/src/server/__tests__/loop-pattern-schema.test.ts +73 -73
  167. package/src/server/__tests__/loop-run-repository.test.ts +32 -0
  168. package/src/server/__tests__/loop-run-state-machine.test.ts +101 -101
  169. package/src/server/__tests__/loop-trigger-service.test.ts +192 -148
  170. package/src/server/__tests__/loop-worker-claim.test.ts +72 -9
  171. package/src/server/__tests__/loop-worker-execution.test.ts +382 -317
  172. package/src/server/__tests__/native-agent-monitor-sync.test.ts +350 -0
  173. package/src/server/__tests__/native-sub-agent-observer.test.ts +470 -662
  174. package/src/server/__tests__/native-usage-attribution.integration.test.ts +175 -0
  175. package/src/server/__tests__/skill-access-service.test.ts +64 -64
  176. package/src/server/__tests__/skill-execute.test.ts +70 -70
  177. package/src/server/__tests__/skill-execution-task-policy.test.ts +140 -140
  178. package/src/server/__tests__/skill-sandbox-worker-mode.test.ts +15 -15
  179. package/src/server/__tests__/skill-settings.test.ts +63 -63
  180. package/src/server/__tests__/skill-tool-name.test.ts +23 -23
  181. package/src/server/__tests__/token-tracker.test.ts +639 -0
  182. package/src/server/__tests__/tool-loop-detection.test.ts +276 -276
  183. package/src/server/__tests__/tracing-retention-policy.test.ts +37 -0
  184. package/src/server/__tests__/usage-ledger.integration.test.ts +194 -0
  185. package/src/server/collections/agent-execution-spans.ts +393 -245
  186. package/src/server/collections/agent-governance-audit-events.ts +26 -0
  187. package/src/server/collections/agent-governance-policies.ts +26 -0
  188. package/src/server/collections/agent-governance-policy-versions.ts +27 -0
  189. package/src/server/collections/agent-governance-prompt-bindings.ts +29 -0
  190. package/src/server/collections/agent-loop-action-approvals.ts +39 -39
  191. package/src/server/collections/agent-loop-runs.ts +396 -369
  192. package/src/server/collections/agent-loop-steps.ts +220 -220
  193. package/src/server/collections/agent-memory-contexts.ts +95 -95
  194. package/src/server/collections/orchestrator-config.ts +87 -87
  195. package/src/server/collections/orchestrator-logs.ts +116 -116
  196. package/src/server/collections/skill-definitions.ts +159 -159
  197. package/src/server/collections/skill-executions.ts +154 -154
  198. package/src/server/collections/skill-loop-configs.ts +65 -65
  199. package/src/server/collections/skill-metrics.ts +36 -36
  200. package/src/server/collections/skill-registry-installations.ts +41 -41
  201. package/src/server/collections/skill-worker-configs.ts +86 -86
  202. package/src/server/index.ts +1 -1
  203. package/src/server/migrations/20260423000000-add-progress-fields.ts +50 -50
  204. package/src/server/migrations/20260425000000-add-interaction-schema.ts +37 -37
  205. package/src/server/migrations/20260427000000-add-tracing-detail-fields.ts +41 -41
  206. package/src/server/migrations/20260427000001-change-other-json-to-text.ts +61 -61
  207. package/src/server/migrations/20260524001000-add-plan-approval-and-harness-profiles.ts +145 -145
  208. package/src/server/migrations/20260601000000-add-token-fields.ts +89 -89
  209. package/src/server/migrations/20260615000000-normalize-ai-employee-tool-bindings.ts +39 -39
  210. package/src/server/migrations/20260621000000-native-policy-profile-defaults.ts +190 -190
  211. package/src/server/migrations/20260727000000-remove-unused-knowledge-scopes.ts +25 -25
  212. package/src/server/migrations/20260727010000-backfill-skill-tool-names.ts +45 -45
  213. package/src/server/migrations/20261110000000-add-usage-pricing-snapshots.ts +117 -0
  214. package/src/server/migrations/20261110010000-add-usage-department-attribution.ts +35 -0
  215. package/src/server/plugin.ts +674 -573
  216. package/src/server/resources/agent-governance.ts +168 -0
  217. package/src/server/resources/agent-harness-profile.ts +110 -94
  218. package/src/server/resources/agent-knowledge-insights.ts +306 -306
  219. package/src/server/resources/agent-loop-approval.ts +84 -84
  220. package/src/server/resources/agent-loop-events-stream.ts +108 -108
  221. package/src/server/resources/agent-loop.ts +252 -252
  222. package/src/server/resources/agent-monitor.ts +215 -482
  223. package/src/server/resources/agent-usage.ts +420 -0
  224. package/src/server/resources/native-monitor-format.ts +157 -0
  225. package/src/server/resources/tracing.ts +303 -267
  226. package/src/server/services/AgentExecutionContext.ts +23 -23
  227. package/src/server/services/AgentGovernanceService.ts +548 -0
  228. package/src/server/services/AgentMemoryContextService.ts +344 -344
  229. package/src/server/services/AgentRegistryService.ts +220 -220
  230. package/src/server/services/AgentRuntimeLifecycle.ts +64 -64
  231. package/src/server/services/CodeValidator.ts +238 -238
  232. package/src/server/services/DepartmentAttribution.ts +54 -0
  233. package/src/server/services/DurableCircuitBreakerService.ts +235 -235
  234. package/src/server/services/ExecutionSpanService.ts +278 -146
  235. package/src/server/services/FileManager.ts +159 -159
  236. package/src/server/services/GovernanceAdmission.ts +239 -0
  237. package/src/server/services/HarnessCapabilities.ts +119 -0
  238. package/src/server/services/HarnessCompiler.ts +170 -170
  239. package/src/server/services/HarnessProfileService.ts +235 -235
  240. package/src/server/services/HarnessSchema.ts +194 -194
  241. package/src/server/services/InputSchemaValidator.ts +44 -44
  242. package/src/server/services/LoopBudgetService.ts +332 -271
  243. package/src/server/services/LoopControlService.ts +120 -120
  244. package/src/server/services/LoopPatternSchema.ts +231 -231
  245. package/src/server/services/LoopPatternService.ts +171 -171
  246. package/src/server/services/LoopRunRepository.ts +350 -332
  247. package/src/server/services/LoopRunStateMachine.ts +545 -525
  248. package/src/server/services/LoopSchedulerService.ts +195 -195
  249. package/src/server/services/LoopTriggerService.ts +169 -159
  250. package/src/server/services/LoopWorkerService.ts +986 -912
  251. package/src/server/services/ManagedPromptProtection.ts +88 -0
  252. package/src/server/services/NativeAgentMonitorSyncService.ts +691 -0
  253. package/src/server/services/NativeSubAgentObserver.ts +331 -757
  254. package/src/server/services/NativeUsageAttribution.ts +98 -0
  255. package/src/server/services/PathLockService.ts +317 -317
  256. package/src/server/services/PluginAiRuntimeAdapter.ts +387 -387
  257. package/src/server/services/RunEventBus.ts +58 -58
  258. package/src/server/services/SandboxRunner.ts +492 -492
  259. package/src/server/services/SkillAccessService.ts +175 -175
  260. package/src/server/services/SkillManager.ts +585 -585
  261. package/src/server/services/SkillRepositoryService.ts +236 -236
  262. package/src/server/services/TokenTracker.ts +1158 -287
  263. package/src/server/services/ToolLoopDetectionService.ts +210 -210
  264. package/src/server/services/TracingRetentionPolicy.ts +56 -0
  265. package/src/server/services/VerificationSchema.ts +31 -31
  266. package/src/server/services/VerificationService.ts +332 -313
  267. package/src/server/services/WorkerEnvManager.ts +198 -198
  268. package/src/server/services/__tests__/SkillRepositoryService.test.ts +33 -33
  269. package/src/server/services/harness-runtime-policy.ts +134 -134
  270. package/src/server/services/pythonGuardTemplate.ts +87 -87
  271. package/src/server/services/sandboxCapabilities.ts +348 -348
  272. package/src/server/skill-hub/actions/__tests__/git-import.test.ts +563 -563
  273. package/src/server/skill-hub/actions/git-import.ts +936 -936
  274. package/src/server/skill-hub/mcp/McpController.ts +107 -107
  275. package/src/server/skill-hub/middlewares/git-import-request-method-policy.ts +51 -51
  276. package/src/server/skill-hub/plugin.ts +1232 -1232
  277. package/src/server/skill-hub/sandbox-config.json +6 -6
  278. package/src/server/skill-hub/services/RegistrySkillInstallationService.ts +281 -281
  279. package/src/server/skill-hub/services/__tests__/RegistrySkillInstallationService.test.ts +295 -295
  280. package/src/server/skill-hub/tasks/SkillExecutionTask.ts +568 -568
  281. package/src/server/skill-hub/utils/json-fields.ts +57 -57
  282. package/src/server/tools/delegate-task.ts +30 -30
  283. package/src/server/tools/external-rag-search.ts +329 -329
  284. package/src/server/tools/skill-execute.ts +216 -216
  285. package/src/server/types.ts +21 -21
  286. package/src/server/utils/ai-manager.ts +24 -24
  287. package/src/server/utils/ctx-utils.ts +132 -132
  288. package/src/server/utils/ensure-indexes.ts +119 -96
  289. package/src/server/utils/ha-lock.ts +80 -80
  290. package/src/server/utils/loop-pattern-context.ts +84 -84
  291. package/src/server/utils/record-utils.ts +19 -19
  292. package/src/server/utils/skill-settings.ts +135 -135
  293. package/src/server/utils/skill-tool-name.ts +58 -58
  294. package/dist/client-v2/117.7565cff57c39318a.js +0 -10
  295. package/dist/client-v2/214.106635011859dede.js +0 -10
  296. package/dist/client-v2/253.59577b40d9875be0.js +0 -10
  297. package/dist/client-v2/264.7ed8bee71fa5935c.js +0 -10
  298. package/dist/client-v2/418.41ed200e7d6da332.js +0 -10
  299. package/dist/client-v2/794.a67a2614405ba627.js +0 -10
  300. package/dist/client-v2/993.b0688fdc5ccb000a.js +0 -10
  301. package/dist/node_modules/simple-git/dist/cjs/index.js +0 -7399
  302. package/dist/node_modules/simple-git/dist/esm/index.js +0 -4745
  303. package/dist/node_modules/simple-git/dist/esm/package.json +0 -3
  304. package/dist/node_modules/simple-git/dist/src/lib/api.d.ts +0 -13
  305. package/dist/node_modules/simple-git/dist/src/lib/args/log-format.d.ts +0 -9
  306. package/dist/node_modules/simple-git/dist/src/lib/errors/git-construct-error.d.ts +0 -15
  307. package/dist/node_modules/simple-git/dist/src/lib/errors/git-error.d.ts +0 -30
  308. package/dist/node_modules/simple-git/dist/src/lib/errors/git-plugin-error.d.ts +0 -7
  309. package/dist/node_modules/simple-git/dist/src/lib/errors/git-response-error.d.ts +0 -32
  310. package/dist/node_modules/simple-git/dist/src/lib/errors/task-configuration-error.d.ts +0 -12
  311. package/dist/node_modules/simple-git/dist/src/lib/git-factory.d.ts +0 -15
  312. package/dist/node_modules/simple-git/dist/src/lib/git-logger.d.ts +0 -21
  313. package/dist/node_modules/simple-git/dist/src/lib/parsers/parse-branch-delete.d.ts +0 -5
  314. package/dist/node_modules/simple-git/dist/src/lib/parsers/parse-branch.d.ts +0 -2
  315. package/dist/node_modules/simple-git/dist/src/lib/parsers/parse-commit.d.ts +0 -2
  316. package/dist/node_modules/simple-git/dist/src/lib/parsers/parse-diff-summary.d.ts +0 -3
  317. package/dist/node_modules/simple-git/dist/src/lib/parsers/parse-fetch.d.ts +0 -2
  318. package/dist/node_modules/simple-git/dist/src/lib/parsers/parse-list-log-summary.d.ts +0 -6
  319. package/dist/node_modules/simple-git/dist/src/lib/parsers/parse-merge.d.ts +0 -11
  320. package/dist/node_modules/simple-git/dist/src/lib/parsers/parse-move.d.ts +0 -2
  321. package/dist/node_modules/simple-git/dist/src/lib/parsers/parse-pull.d.ts +0 -6
  322. package/dist/node_modules/simple-git/dist/src/lib/parsers/parse-push.d.ts +0 -4
  323. package/dist/node_modules/simple-git/dist/src/lib/parsers/parse-remote-messages.d.ts +0 -5
  324. package/dist/node_modules/simple-git/dist/src/lib/parsers/parse-remote-objects.d.ts +0 -3
  325. package/dist/node_modules/simple-git/dist/src/lib/plugins/abort-plugin.d.ts +0 -3
  326. package/dist/node_modules/simple-git/dist/src/lib/plugins/block-unsafe-operations-plugin.d.ts +0 -3
  327. package/dist/node_modules/simple-git/dist/src/lib/plugins/command-config-prefixing-plugin.d.ts +0 -2
  328. package/dist/node_modules/simple-git/dist/src/lib/plugins/completion-detection.plugin.d.ts +0 -3
  329. package/dist/node_modules/simple-git/dist/src/lib/plugins/custom-binary.plugin.d.ts +0 -3
  330. package/dist/node_modules/simple-git/dist/src/lib/plugins/error-detection.plugin.d.ts +0 -7
  331. package/dist/node_modules/simple-git/dist/src/lib/plugins/index.d.ts +0 -11
  332. package/dist/node_modules/simple-git/dist/src/lib/plugins/plugin-store.d.ts +0 -11
  333. package/dist/node_modules/simple-git/dist/src/lib/plugins/progress-monitor-plugin.d.ts +0 -3
  334. package/dist/node_modules/simple-git/dist/src/lib/plugins/simple-git-plugin.d.ts +0 -48
  335. package/dist/node_modules/simple-git/dist/src/lib/plugins/spawn-options-plugin.d.ts +0 -3
  336. package/dist/node_modules/simple-git/dist/src/lib/plugins/suffix-paths.plugin.d.ts +0 -2
  337. package/dist/node_modules/simple-git/dist/src/lib/plugins/timout-plugin.d.ts +0 -3
  338. package/dist/node_modules/simple-git/dist/src/lib/responses/BranchDeleteSummary.d.ts +0 -12
  339. package/dist/node_modules/simple-git/dist/src/lib/responses/BranchSummary.d.ts +0 -14
  340. package/dist/node_modules/simple-git/dist/src/lib/responses/CheckIgnore.d.ts +0 -4
  341. package/dist/node_modules/simple-git/dist/src/lib/responses/CleanSummary.d.ts +0 -9
  342. package/dist/node_modules/simple-git/dist/src/lib/responses/ConfigList.d.ts +0 -13
  343. package/dist/node_modules/simple-git/dist/src/lib/responses/DiffSummary.d.ts +0 -10
  344. package/dist/node_modules/simple-git/dist/src/lib/responses/FileStatusSummary.d.ts +0 -9
  345. package/dist/node_modules/simple-git/dist/src/lib/responses/GetRemoteSummary.d.ts +0 -11
  346. package/dist/node_modules/simple-git/dist/src/lib/responses/InitSummary.d.ts +0 -9
  347. package/dist/node_modules/simple-git/dist/src/lib/responses/MergeSummary.d.ts +0 -16
  348. package/dist/node_modules/simple-git/dist/src/lib/responses/PullSummary.d.ts +0 -25
  349. package/dist/node_modules/simple-git/dist/src/lib/responses/StatusSummary.d.ts +0 -19
  350. package/dist/node_modules/simple-git/dist/src/lib/responses/TagList.d.ts +0 -7
  351. package/dist/node_modules/simple-git/dist/src/lib/runners/git-executor-chain.d.ts +0 -25
  352. package/dist/node_modules/simple-git/dist/src/lib/runners/git-executor.d.ts +0 -14
  353. package/dist/node_modules/simple-git/dist/src/lib/runners/promise-wrapped.d.ts +0 -2
  354. package/dist/node_modules/simple-git/dist/src/lib/runners/scheduler.d.ts +0 -11
  355. package/dist/node_modules/simple-git/dist/src/lib/runners/tasks-pending-queue.d.ts +0 -23
  356. package/dist/node_modules/simple-git/dist/src/lib/simple-git-api.d.ts +0 -20
  357. package/dist/node_modules/simple-git/dist/src/lib/task-callback.d.ts +0 -2
  358. package/dist/node_modules/simple-git/dist/src/lib/tasks/apply-patch.d.ts +0 -3
  359. package/dist/node_modules/simple-git/dist/src/lib/tasks/branch.d.ts +0 -7
  360. package/dist/node_modules/simple-git/dist/src/lib/tasks/change-working-directory.d.ts +0 -2
  361. package/dist/node_modules/simple-git/dist/src/lib/tasks/check-ignore.d.ts +0 -2
  362. package/dist/node_modules/simple-git/dist/src/lib/tasks/check-is-repo.d.ts +0 -9
  363. package/dist/node_modules/simple-git/dist/src/lib/tasks/checkout.d.ts +0 -2
  364. package/dist/node_modules/simple-git/dist/src/lib/tasks/clean.d.ts +0 -25
  365. package/dist/node_modules/simple-git/dist/src/lib/tasks/clone.d.ts +0 -9
  366. package/dist/node_modules/simple-git/dist/src/lib/tasks/commit.d.ts +0 -4
  367. package/dist/node_modules/simple-git/dist/src/lib/tasks/config.d.ts +0 -8
  368. package/dist/node_modules/simple-git/dist/src/lib/tasks/count-objects.d.ts +0 -12
  369. package/dist/node_modules/simple-git/dist/src/lib/tasks/diff-name-status.d.ts +0 -12
  370. package/dist/node_modules/simple-git/dist/src/lib/tasks/diff.d.ts +0 -5
  371. package/dist/node_modules/simple-git/dist/src/lib/tasks/fetch.d.ts +0 -4
  372. package/dist/node_modules/simple-git/dist/src/lib/tasks/first-commit.d.ts +0 -2
  373. package/dist/node_modules/simple-git/dist/src/lib/tasks/grep.d.ts +0 -12
  374. package/dist/node_modules/simple-git/dist/src/lib/tasks/hash-object.d.ts +0 -5
  375. package/dist/node_modules/simple-git/dist/src/lib/tasks/init.d.ts +0 -3
  376. package/dist/node_modules/simple-git/dist/src/lib/tasks/log.d.ts +0 -32
  377. package/dist/node_modules/simple-git/dist/src/lib/tasks/merge.d.ts +0 -4
  378. package/dist/node_modules/simple-git/dist/src/lib/tasks/move.d.ts +0 -3
  379. package/dist/node_modules/simple-git/dist/src/lib/tasks/pull.d.ts +0 -3
  380. package/dist/node_modules/simple-git/dist/src/lib/tasks/push.d.ts +0 -9
  381. package/dist/node_modules/simple-git/dist/src/lib/tasks/remote.d.ts +0 -8
  382. package/dist/node_modules/simple-git/dist/src/lib/tasks/reset.d.ts +0 -11
  383. package/dist/node_modules/simple-git/dist/src/lib/tasks/show.d.ts +0 -2
  384. package/dist/node_modules/simple-git/dist/src/lib/tasks/stash-list.d.ts +0 -4
  385. package/dist/node_modules/simple-git/dist/src/lib/tasks/status.d.ts +0 -3
  386. package/dist/node_modules/simple-git/dist/src/lib/tasks/sub-module.d.ts +0 -5
  387. package/dist/node_modules/simple-git/dist/src/lib/tasks/tag.d.ts +0 -18
  388. package/dist/node_modules/simple-git/dist/src/lib/tasks/task.d.ts +0 -14
  389. package/dist/node_modules/simple-git/dist/src/lib/tasks/version.d.ts +0 -9
  390. package/dist/node_modules/simple-git/dist/src/lib/types/handlers.d.ts +0 -21
  391. package/dist/node_modules/simple-git/dist/src/lib/types/index.d.ts +0 -136
  392. package/dist/node_modules/simple-git/dist/src/lib/types/tasks.d.ts +0 -19
  393. package/dist/node_modules/simple-git/dist/src/lib/utils/argument-filters.d.ts +0 -14
  394. package/dist/node_modules/simple-git/dist/src/lib/utils/exit-codes.d.ts +0 -10
  395. package/dist/node_modules/simple-git/dist/src/lib/utils/git-output-streams.d.ts +0 -7
  396. package/dist/node_modules/simple-git/dist/src/lib/utils/index.d.ts +0 -8
  397. package/dist/node_modules/simple-git/dist/src/lib/utils/line-parser.d.ts +0 -15
  398. package/dist/node_modules/simple-git/dist/src/lib/utils/simple-git-options.d.ts +0 -2
  399. package/dist/node_modules/simple-git/dist/src/lib/utils/task-options.d.ts +0 -13
  400. package/dist/node_modules/simple-git/dist/src/lib/utils/task-parser.d.ts +0 -5
  401. package/dist/node_modules/simple-git/dist/src/lib/utils/util.d.ts +0 -47
  402. package/dist/node_modules/simple-git/dist/typings/errors.d.ts +0 -5
  403. package/dist/node_modules/simple-git/dist/typings/index.d.ts +0 -14
  404. package/dist/node_modules/simple-git/dist/typings/response.d.ts +0 -556
  405. package/dist/node_modules/simple-git/dist/typings/simple-git.d.ts +0 -1033
  406. package/dist/node_modules/simple-git/dist/typings/types.d.ts +0 -22
  407. package/dist/node_modules/simple-git/node_modules/debug/package.json +0 -64
  408. package/dist/node_modules/simple-git/node_modules/debug/src/browser.js +0 -272
  409. package/dist/node_modules/simple-git/node_modules/debug/src/common.js +0 -292
  410. package/dist/node_modules/simple-git/node_modules/debug/src/index.js +0 -10
  411. package/dist/node_modules/simple-git/node_modules/debug/src/node.js +0 -263
  412. package/dist/node_modules/simple-git/package.json +0 -1
  413. package/dist/node_modules/simple-git/promise.js +0 -17
@@ -1,287 +1,1158 @@
1
- export interface TokenUsage {
2
- inputTokens: number;
3
- outputTokens: number;
4
- totalTokens: number;
5
- cost: number;
6
- }
7
-
8
- export interface BudgetConfig {
9
- budgetMaxTokens?: number;
10
- budgetMaxCost?: number;
11
- }
12
-
13
- export interface BudgetCheckResult {
14
- allowed: boolean;
15
- reason?: string;
16
- }
17
-
18
- /**
19
- * Default pricing per 1K tokens (USD).
20
- * These are conservative estimates; override via env vars:
21
- * ORCHESTRATOR_PRICE_PER_1K_INPUT
22
- * ORCHESTRATOR_PRICE_PER_1K_OUTPUT
23
- */
24
- const PRICE_PER_1K_INPUT = Number(process.env.ORCHESTRATOR_PRICE_PER_1K_INPUT || 0.003);
25
- const PRICE_PER_1K_OUTPUT = Number(process.env.ORCHESTRATOR_PRICE_PER_1K_OUTPUT || 0.015);
26
-
27
- /**
28
- * Estimate cost based on token counts.
29
- */
30
- export function estimateCost(inputTokens: number, outputTokens: number): number {
31
- return (inputTokens / 1000) * PRICE_PER_1K_INPUT + (outputTokens / 1000) * PRICE_PER_1K_OUTPUT;
32
- }
33
-
34
- /**
35
- * Extract usage_metadata from a legacy agent final state.
36
- *
37
- * Legacy agent execution returned a final state object with a `messages` array.
38
- * The last AIMessage in that array carries `usage_metadata` populated by the
39
- * LLM provider after the final generation step. This is the standard LangChain
40
- * approach — no private API access needed.
41
- *
42
- * Expected message shape:
43
- * AIMessage.usage_metadata = {
44
- * input_tokens: number,
45
- * output_tokens: number,
46
- * total_tokens: number,
47
- * }
48
- */
49
- type LegacyUsageMetadata = {
50
- input_tokens?: number;
51
- output_tokens?: number;
52
- total_tokens?: number;
53
- };
54
-
55
- type LegacyAIMessage = {
56
- usage_metadata?: LegacyUsageMetadata;
57
- [key: string]: unknown;
58
- };
59
-
60
- type LegacyFinalState =
61
- | {
62
- messages?: LegacyAIMessage[];
63
- }
64
- | null
65
- | undefined;
66
-
67
- export function extractTokenUsage(finalState: LegacyFinalState): TokenUsage | null {
68
- if (!finalState?.messages || !Array.isArray(finalState.messages)) return null;
69
-
70
- // Accumulate usage across all AI messages in the run (each LLM call adds usage)
71
- let totalInput = 0;
72
- let totalOutput = 0;
73
- let totalAll = 0;
74
-
75
- for (const msg of finalState.messages) {
76
- if (msg?.usage_metadata) {
77
- const um = msg.usage_metadata;
78
- totalInput += um.input_tokens || 0;
79
- totalOutput += um.output_tokens || 0;
80
- totalAll += um.total_tokens || 0;
81
- }
82
- }
83
-
84
- if (totalAll === 0) return null;
85
-
86
- return {
87
- inputTokens: totalInput,
88
- outputTokens: totalOutput,
89
- totalTokens: totalAll,
90
- cost: estimateCost(totalInput, totalOutput),
91
- };
92
- }
93
-
94
- /**
95
- * Service for tracking token consumption across agent runs and spans.
96
- *
97
- * Responsibilities:
98
- * 1. Parse token usage from LangGraph execution results
99
- * 2. Persist token counts to agentExecutionSpans
100
- * 3. Accumulate totals at the agentLoopRuns level
101
- * 4. Enforce budget limits (max tokens / max cost per run)
102
- */
103
- export class TokenTracker {
104
- constructor(
105
- private readonly plugin: {
106
- app: { log?: { warn?: (...args: unknown[]) => void } };
107
- db: {
108
- getRepository: (name: string) => {
109
- find?: (opts?: Record<string, unknown>) => Promise<unknown[]>;
110
- findOne?: (opts?: Record<string, unknown>) => Promise<unknown>;
111
- update?: (opts: Record<string, unknown>) => Promise<unknown>;
112
- };
113
- };
114
- },
115
- ) {}
116
-
117
- get db() {
118
- return this.plugin.db;
119
- }
120
-
121
- get app() {
122
- return this.plugin.app;
123
- }
124
-
125
- /**
126
- * Track token usage for a single execution span.
127
- * Updates the span record and accumulates into the parent run.
128
- *
129
- * @param spanId - The agentExecutionSpans.id to update
130
- * @param usage - Parsed token usage from extractTokenUsage()
131
- * @param runId - Optional agentLoopRuns.id to accumulate totals
132
- */
133
- async trackSpan(spanId: string | number | undefined, usage: TokenUsage, runId?: string | number): Promise<void> {
134
- if (!spanId) return;
135
-
136
- try {
137
- const repo = this.db.getRepository('agentExecutionSpans');
138
- if (!repo) return;
139
-
140
- await repo.update({
141
- filterByTk: spanId,
142
- values: {
143
- inputTokens: usage.inputTokens,
144
- outputTokens: usage.outputTokens,
145
- totalTokens: usage.totalTokens,
146
- cost: usage.cost,
147
- },
148
- });
149
-
150
- if (runId != null) {
151
- await this.accumulateToRun(runId);
152
- }
153
- } catch (e: unknown) {
154
- this.app.log?.warn?.('[TokenTracker] Failed to track span tokens', e);
155
- }
156
- }
157
-
158
- /**
159
- * Recalculate total token/cost for a run by summing all its spans.
160
- */
161
- async accumulateToRun(runId: string | number): Promise<void> {
162
- try {
163
- const spansRepo = this.db.getRepository('agentExecutionSpans');
164
- if (!spansRepo) return;
165
-
166
- // Project only token fields to minimize memory; cap at 10K spans per run.
167
- const spans = await spansRepo.find({
168
- filter: { agentLoopRunId: String(runId) },
169
- fields: ['inputTokens', 'outputTokens', 'cost'],
170
- limit: 10_000,
171
- });
172
-
173
- let totalInput = 0;
174
- let totalOutput = 0;
175
- let totalCost = 0;
176
-
177
- for (const span of spans) {
178
- totalInput += Number(span.get?.('inputTokens') || span.inputTokens || 0);
179
- totalOutput += Number(span.get?.('outputTokens') || span.outputTokens || 0);
180
- totalCost += Number(span.get?.('cost') || span.cost || 0);
181
- }
182
-
183
- const runsRepo = this.db.getRepository('agentLoopRuns');
184
- if (!runsRepo) return;
185
-
186
- await runsRepo.update({
187
- filterByTk: runId,
188
- values: {
189
- totalInputTokens: totalInput,
190
- totalOutputTokens: totalOutput,
191
- totalTokens: totalInput + totalOutput,
192
- totalCost,
193
- },
194
- });
195
- } catch (e: unknown) {
196
- this.app.log?.warn?.('[TokenTracker] Failed to accumulate run totals', e);
197
- }
198
- }
199
-
200
- /**
201
- * Check if a run has exceeded its budget limits.
202
- */
203
- async checkBudget(runId: string | number): Promise<BudgetCheckResult> {
204
- try {
205
- const repo = this.db.getRepository('agentLoopRuns');
206
- if (!repo) return { allowed: true };
207
-
208
- const run = await repo.findOne({ filter: { id: runId } });
209
- if (!run) return { allowed: true };
210
-
211
- const budgetMaxTokens = Number(run.get?.('budgetMaxTokens') ?? 0);
212
- const budgetMaxCost = Number(run.get?.('budgetMaxCost') ?? 0);
213
-
214
- if (budgetMaxTokens <= 0 && budgetMaxCost <= 0) return { allowed: true };
215
-
216
- const totalTokens = Number(run.get?.('totalTokens') || 0);
217
- const totalCost = Number(run.get?.('totalCost') || 0);
218
-
219
- if (budgetMaxTokens > 0 && totalTokens >= budgetMaxTokens) {
220
- return {
221
- allowed: false,
222
- reason: `Budget exceeded: ${totalTokens}/${budgetMaxTokens} tokens used. Maximum allowed tokens for this run: ${budgetMaxTokens}.`,
223
- };
224
- }
225
-
226
- if (budgetMaxCost > 0 && totalCost >= budgetMaxCost) {
227
- return {
228
- allowed: false,
229
- reason: `Budget exceeded: $${totalCost.toFixed(4)}/$${budgetMaxCost.toFixed(
230
- 4,
231
- )} spent. Maximum allowed cost for this run: $${budgetMaxCost}.`,
232
- };
233
- }
234
-
235
- return { allowed: true };
236
- } catch (e: unknown) {
237
- this.app.log?.warn?.('[TokenTracker] Budget check failed, allowing', e);
238
- return { allowed: true };
239
- }
240
- }
241
-
242
- /**
243
- * Track actual token usage from the LLM response on a span.
244
- * This is preferred over estimateAndTrack when the LLM provides real usage metadata.
245
- * Falls back to estimation if usage is null.
246
- */
247
- async trackActualUsage(
248
- spanId: string | number | undefined,
249
- tokenUsage: { inputTokens: number; outputTokens: number; totalTokens: number; cost: number } | null,
250
- input: string,
251
- output: string,
252
- runId?: string | number,
253
- ): Promise<void> {
254
- if (tokenUsage && tokenUsage.totalTokens > 0) {
255
- await this.trackSpan(spanId, tokenUsage, runId);
256
- return;
257
- }
258
- // Fall back to estimation when actual usage is not available
259
- await this.estimateAndTrack(spanId, input, output, runId);
260
- }
261
-
262
- /**
263
- * Estimate tokens from text length and track on a span.
264
- * Rough heuristic: ~4 chars per token for English text.
265
- * Use when actual usage_metadata is not available (e.g. native plugin-ai flow).
266
- */
267
- async estimateAndTrack(
268
- spanId: string | number | undefined,
269
- input: string,
270
- output: string,
271
- runId?: string | number,
272
- ): Promise<void> {
273
- if (!spanId) return;
274
- const inputTokens = Math.ceil((input || '').length / 4);
275
- const outputTokens = Math.ceil((output || '').length / 4);
276
- await this.trackSpan(
277
- spanId,
278
- {
279
- inputTokens,
280
- outputTokens,
281
- totalTokens: inputTokens + outputTokens,
282
- cost: estimateCost(inputTokens, outputTokens),
283
- },
284
- runId,
285
- );
286
- }
287
- }
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+
10
+ import { read } from '../utils/record-utils';
11
+
12
+ export interface TokenUsage {
13
+ inputTokens: number;
14
+ outputTokens: number;
15
+ totalTokens: number;
16
+ cost: number;
17
+ cachedTokens?: number;
18
+ reasoningTokens?: number;
19
+ }
20
+
21
+ export type UsageCounts = Omit<TokenUsage, 'cost'> & { cost?: number | null };
22
+ export type UsageSource = 'actual' | 'estimated' | 'missing';
23
+ export type PricingStatus = 'configured' | 'estimated' | 'native' | 'unpriced' | 'non_usd' | 'mixed_currency';
24
+
25
+ export interface BudgetConfig {
26
+ budgetMaxTokens?: number;
27
+ budgetMaxCost?: number;
28
+ }
29
+
30
+ export interface BudgetCheckResult {
31
+ allowed: boolean;
32
+ reason?: string;
33
+ }
34
+
35
+ export type PriceSnapshot = {
36
+ inputPer1k?: number | null;
37
+ outputPer1k?: number | null;
38
+ cachedInputPer1k?: number | null;
39
+ currency?: string;
40
+ version?: string;
41
+ source?: 'model_pricing_json' | 'legacy_global_env' | 'provider';
42
+ status?: PricingStatus;
43
+ cost?: number | null;
44
+ };
45
+
46
+ export type ModelCallUsageInput = {
47
+ usageIdentity: string;
48
+ nativeIdentity?: string;
49
+ sessionId: string;
50
+ messageId: string;
51
+ usage: UsageCounts | null;
52
+ model?: string;
53
+ provider?: string;
54
+ llmService?: string;
55
+ runId?: string | number | null;
56
+ spanId?: string | number;
57
+ rootRunId?: string;
58
+ parentSpanId?: string;
59
+ role?: string;
60
+ employeeUsername?: string;
61
+ userId?: string | number;
62
+ /** Trusted department snapshot captured for this exact model call. */
63
+ departmentId?: string | number;
64
+ source: 'ai_usage_event' | 'loop_runtime';
65
+ priceSnapshot?: PriceSnapshot;
66
+ nativeCost?: number | null;
67
+ occurredAt?: Date | string | number;
68
+ recomputeRunTotals?: boolean;
69
+ };
70
+
71
+ export type LoopInvocationUsageInput = {
72
+ runId: string | number;
73
+ stepId: string | number;
74
+ sessionId: string;
75
+ messageId?: string;
76
+ usage: UsageCounts | null;
77
+ input: string;
78
+ output: string;
79
+ role: string;
80
+ employeeUsername: string;
81
+ userId?: string | number;
82
+ rootRunId?: string;
83
+ };
84
+
85
+ export type NativeUsageSyncInput = {
86
+ sessionId: string;
87
+ /** An invocation was made; an empty source page is incomplete rather than a free call. */
88
+ expectEvents?: boolean;
89
+ runId?: string | number;
90
+ rootRunId?: string;
91
+ parentSpanId?: string;
92
+ role?: string;
93
+ employeeUsername?: string;
94
+ userId?: string | number;
95
+ };
96
+
97
+ type Repository = {
98
+ find?: (opts: Record<string, unknown>) => Promise<unknown[]>;
99
+ findOne?: (opts: Record<string, unknown>) => Promise<unknown>;
100
+ create?: (opts: Record<string, unknown>) => Promise<unknown>;
101
+ update?: (opts: Record<string, unknown>) => Promise<unknown>;
102
+ };
103
+
104
+ type UsageTransaction = { LOCK?: { UPDATE?: unknown } };
105
+ type UsageDatabase = {
106
+ getRepository: (name: string) => Repository | undefined;
107
+ sequelize?: {
108
+ transaction?: <T>(operation: (transaction: UsageTransaction) => Promise<T>) => Promise<T>;
109
+ };
110
+ };
111
+
112
+ type Rate = {
113
+ inputPer1k: number;
114
+ outputPer1k: number;
115
+ cachedInputPer1k?: number;
116
+ currency: string;
117
+ version: string;
118
+ };
119
+
120
+ type RecordLike = Record<string, unknown>;
121
+ type LogMetadata = { module?: string; submodule?: string; method?: string; [key: string]: unknown };
122
+
123
+ const USAGE_EVENT_TYPE = 'llm_message';
124
+ const USAGE_PAGE_SIZE = 1_000;
125
+
126
+ function asRecord(value: unknown): RecordLike {
127
+ return value && typeof value === 'object' && !Array.isArray(value) ? (value as RecordLike) : {};
128
+ }
129
+
130
+ function readNumber(value: unknown): number | undefined {
131
+ if (typeof value === 'string' && !value.trim()) return undefined;
132
+ const number = typeof value === 'number' ? value : typeof value === 'string' ? Number(value) : NaN;
133
+ return Number.isFinite(number) && number >= 0 ? number : undefined;
134
+ }
135
+
136
+ function readOptionalNumber(record: unknown, key: string): number | undefined {
137
+ const raw = read(record, key);
138
+ return readNumber(raw);
139
+ }
140
+
141
+ function identityPart(value: unknown) {
142
+ return String(value ?? '').trim();
143
+ }
144
+
145
+ function normalizedCurrency(value: unknown) {
146
+ const currency = identityPart(value).toUpperCase();
147
+ return /^[A-Z]{3}$/.test(currency) ? currency : '';
148
+ }
149
+
150
+ function usageEventIdentity(sessionId: string, messageId: string) {
151
+ return `ai-usage-event:${sessionId}:${messageId}`;
152
+ }
153
+
154
+ export function usageFromAIUsageEvent(event: unknown): UsageCounts | null {
155
+ const raw = asRecord(read(event, 'rawUsageMetadata'));
156
+ const hasRawInput = ['input_tokens', 'prompt_tokens', 'inputTokens', 'promptTokens'].some(
157
+ (key) => Object.prototype.hasOwnProperty.call(raw, key) && readNumber(raw[key]) !== undefined,
158
+ );
159
+ const hasRawOutput = ['output_tokens', 'completion_tokens', 'outputTokens', 'completionTokens'].some(
160
+ (key) => Object.prototype.hasOwnProperty.call(raw, key) && readNumber(raw[key]) !== undefined,
161
+ );
162
+ const hasRawTotal = ['total_tokens', 'totalTokens'].some(
163
+ (key) => Object.prototype.hasOwnProperty.call(raw, key) && readNumber(raw[key]) !== undefined,
164
+ );
165
+
166
+ const inputTokens = readOptionalNumber(event, 'inputTokens');
167
+ const outputTokens = readOptionalNumber(event, 'outputTokens');
168
+ const totalTokens = readOptionalNumber(event, 'totalTokens');
169
+ if (!hasRawInput && !hasRawOutput && !hasRawTotal && inputTokens === 0 && outputTokens === 0 && totalTokens === 0) {
170
+ return null;
171
+ }
172
+ if (inputTokens === undefined || outputTokens === undefined) return null;
173
+ if ((!hasRawInput && inputTokens === 0) || (!hasRawOutput && outputTokens === 0)) return null;
174
+
175
+ const cachedTokens = readOptionalNumber(event, 'cachedTokens');
176
+ const reasoningTokens = readOptionalNumber(event, 'reasoningTokens');
177
+ return {
178
+ inputTokens,
179
+ outputTokens,
180
+ totalTokens: totalTokens ?? inputTokens + outputTokens,
181
+ ...(cachedTokens === undefined ? {} : { cachedTokens }),
182
+ ...(reasoningTokens === undefined ? {} : { reasoningTokens }),
183
+ };
184
+ }
185
+
186
+ function usageIsValid(usage: UsageCounts | null): usage is UsageCounts {
187
+ return Boolean(
188
+ usage &&
189
+ readNumber(usage.inputTokens) !== undefined &&
190
+ readNumber(usage.outputTokens) !== undefined &&
191
+ readNumber(usage.totalTokens) !== undefined,
192
+ );
193
+ }
194
+
195
+ function normalizedModelKey(provider: string | undefined, model: string | undefined, llmService: string | undefined) {
196
+ const modelName = identityPart(model).toLowerCase();
197
+ if (!modelName) return '';
198
+ const providerName = identityPart(provider).toLowerCase() || identityPart(llmService).toLowerCase();
199
+ return providerName ? `${providerName}/${modelName}` : modelName;
200
+ }
201
+
202
+ function configuredRate(provider?: string, model?: string, llmService?: string): Rate | null {
203
+ const json = process.env.ORCHESTRATOR_MODEL_PRICING_JSON;
204
+ if (!json) return null;
205
+
206
+ let entries: RecordLike;
207
+ try {
208
+ entries = asRecord(JSON.parse(json));
209
+ } catch {
210
+ return null;
211
+ }
212
+
213
+ const normalizedEntries = new Map<string, RecordLike>();
214
+ for (const [key, value] of Object.entries(entries)) {
215
+ normalizedEntries.set(key.trim().toLowerCase(), asRecord(value));
216
+ }
217
+ const modelKey = normalizedModelKey(provider, model, llmService);
218
+ const entry =
219
+ normalizedEntries.get(modelKey) ??
220
+ (!provider ? normalizedEntries.get(identityPart(model).toLowerCase()) : undefined);
221
+ if (!entry) return null;
222
+
223
+ const inputPer1k = readNumber(entry.inputPer1k);
224
+ const outputPer1k = readNumber(entry.outputPer1k);
225
+ const currency = normalizedCurrency(entry.currency);
226
+ if (inputPer1k === undefined || outputPer1k === undefined || !currency) return null;
227
+
228
+ const cachedInputPer1k = readNumber(entry.cachedInputPer1k);
229
+ return {
230
+ inputPer1k,
231
+ outputPer1k,
232
+ ...(cachedInputPer1k === undefined ? {} : { cachedInputPer1k }),
233
+ currency,
234
+ version:
235
+ identityPart(entry.version) || identityPart(process.env.ORCHESTRATOR_MODEL_PRICING_VERSION) || 'unversioned',
236
+ };
237
+ }
238
+
239
+ function legacyGlobalRate(): Rate | null {
240
+ const inputPer1k = readNumber(process.env.ORCHESTRATOR_PRICE_PER_1K_INPUT);
241
+ const outputPer1k = readNumber(process.env.ORCHESTRATOR_PRICE_PER_1K_OUTPUT);
242
+ if (inputPer1k === undefined || outputPer1k === undefined) return null;
243
+ return {
244
+ inputPer1k,
245
+ outputPer1k,
246
+ currency: 'USD',
247
+ version: 'legacy-global-env',
248
+ };
249
+ }
250
+
251
+ function legacyCostEstimate(inputTokens: number, outputTokens: number): number | null {
252
+ const rate = legacyGlobalRate();
253
+ if (!rate) return null;
254
+ return (inputTokens / 1000) * rate.inputPer1k + (outputTokens / 1000) * rate.outputPer1k;
255
+ }
256
+
257
+ /**
258
+ * Compatibility estimate for older callers. It only uses rates that were explicitly configured
259
+ * through the legacy environment variables; NaN is the unpriced sentinel when no rate is set.
260
+ */
261
+ export function estimateCost(inputTokens: number, outputTokens: number): number {
262
+ return legacyCostEstimate(inputTokens, outputTokens) ?? Number.NaN;
263
+ }
264
+
265
+ function pricingFor(input: ModelCallUsageInput): {
266
+ cost: number | null;
267
+ status: PricingStatus;
268
+ priceSource: string | null;
269
+ snapshot: PriceSnapshot;
270
+ } {
271
+ const supplied = input.priceSnapshot
272
+ ? { ...input.priceSnapshot, currency: normalizedCurrency(input.priceSnapshot.currency) || undefined }
273
+ : undefined;
274
+ const nativeCost = readNumber(input.nativeCost) ?? readNumber(supplied?.cost);
275
+ if (nativeCost !== undefined) {
276
+ const currency = normalizedCurrency(supplied?.currency);
277
+ if (currency !== 'USD') {
278
+ const status = currency ? 'non_usd' : 'unpriced';
279
+ return { cost: null, status, priceSource: supplied?.source ?? 'provider', snapshot: { ...supplied, status } };
280
+ }
281
+ return {
282
+ cost: nativeCost,
283
+ status: supplied?.status ?? 'native',
284
+ priceSource: supplied?.source ?? 'provider',
285
+ snapshot: supplied ?? { status: 'native', source: 'provider' },
286
+ };
287
+ }
288
+
289
+ const suppliedInputRate = readNumber(supplied?.inputPer1k);
290
+ const suppliedOutputRate = readNumber(supplied?.outputPer1k);
291
+ const suppliedCachedInputRate = readNumber(supplied?.cachedInputPer1k);
292
+ if (supplied && identityPart(supplied.currency).toUpperCase() !== 'USD') {
293
+ const status = supplied.currency ? 'non_usd' : 'unpriced';
294
+ return { cost: null, status, priceSource: supplied.source ?? null, snapshot: { ...supplied, status } };
295
+ }
296
+ if (supplied && usageIsValid(input.usage) && suppliedInputRate !== undefined && suppliedOutputRate !== undefined) {
297
+ const cachedTokens = Math.min(input.usage.cachedTokens ?? 0, input.usage.inputTokens);
298
+ const regularInput = input.usage.inputTokens - cachedTokens;
299
+ const inputCost =
300
+ suppliedCachedInputRate === undefined
301
+ ? (input.usage.inputTokens / 1000) * suppliedInputRate
302
+ : (regularInput / 1000) * suppliedInputRate + (cachedTokens / 1000) * suppliedCachedInputRate;
303
+ return {
304
+ cost: inputCost + (input.usage.outputTokens / 1000) * suppliedOutputRate,
305
+ status: supplied.status ?? 'native',
306
+ priceSource: supplied.source ?? 'provider',
307
+ snapshot: supplied,
308
+ };
309
+ }
310
+
311
+ const rate = configuredRate(input.provider, input.model, input.llmService);
312
+ if (rate && rate.currency !== 'USD') {
313
+ return {
314
+ cost: null,
315
+ status: 'non_usd',
316
+ priceSource: 'model_pricing_json',
317
+ snapshot: { ...rate, source: 'model_pricing_json', status: 'non_usd' },
318
+ };
319
+ }
320
+ if (rate && usageIsValid(input.usage)) {
321
+ const cachedTokens = Math.min(input.usage.cachedTokens ?? 0, input.usage.inputTokens);
322
+ const regularInput = input.usage.inputTokens - cachedTokens;
323
+ const inputCost =
324
+ rate.cachedInputPer1k === undefined
325
+ ? (input.usage.inputTokens / 1000) * rate.inputPer1k
326
+ : (regularInput / 1000) * rate.inputPer1k + (cachedTokens / 1000) * rate.cachedInputPer1k;
327
+ return {
328
+ cost: inputCost + (input.usage.outputTokens / 1000) * rate.outputPer1k,
329
+ status: 'configured',
330
+ priceSource: 'model_pricing_json',
331
+ snapshot: {
332
+ inputPer1k: rate.inputPer1k,
333
+ outputPer1k: rate.outputPer1k,
334
+ ...(rate.cachedInputPer1k === undefined ? {} : { cachedInputPer1k: rate.cachedInputPer1k }),
335
+ currency: rate.currency,
336
+ version: rate.version,
337
+ source: 'model_pricing_json',
338
+ status: 'configured',
339
+ },
340
+ };
341
+ }
342
+
343
+ // The old global values remain an explicit estimate only for model-less legacy callers.
344
+ // A known provider/model never silently inherits a global rate for another model.
345
+ if (!input.model && !input.provider && !input.llmService && usageIsValid(input.usage)) {
346
+ const legacyRate = legacyGlobalRate();
347
+ const cost = legacyCostEstimate(input.usage.inputTokens, input.usage.outputTokens);
348
+ if (legacyRate && cost !== null) {
349
+ return {
350
+ cost,
351
+ status: 'estimated',
352
+ priceSource: 'legacy_global_env',
353
+ snapshot: {
354
+ inputPer1k: legacyRate.inputPer1k,
355
+ outputPer1k: legacyRate.outputPer1k,
356
+ currency: legacyRate.currency,
357
+ version: legacyRate.version,
358
+ source: 'legacy_global_env',
359
+ status: 'estimated',
360
+ },
361
+ };
362
+ }
363
+ }
364
+
365
+ return { cost: null, status: 'unpriced', priceSource: null, snapshot: { status: 'unpriced' } };
366
+ }
367
+
368
+ function storedNumber(record: unknown, key: string): number | null {
369
+ const value = read(record, key);
370
+ return value === null || value === undefined || value === '' ? null : readNumber(value) ?? null;
371
+ }
372
+
373
+ function sourceDate(value: unknown) {
374
+ if (value instanceof Date && Number.isFinite(value.getTime())) return value;
375
+ if (typeof value === 'string' || typeof value === 'number') {
376
+ const date = new Date(value);
377
+ if (Number.isFinite(date.getTime())) return date;
378
+ }
379
+ return undefined;
380
+ }
381
+
382
+ function storedPriceSnapshot(record: unknown): PriceSnapshot {
383
+ const currency = identityPart(read(record, 'priceCurrency'));
384
+ const version = identityPart(read(record, 'priceVersion'));
385
+ const source = identityPart(read(record, 'priceSource'));
386
+ const status = identityPart(read(record, 'pricingStatus'));
387
+ return {
388
+ inputPer1k: storedNumber(record, 'priceInputPer1k'),
389
+ outputPer1k: storedNumber(record, 'priceOutputPer1k'),
390
+ cachedInputPer1k: storedNumber(record, 'priceCachedInputPer1k'),
391
+ ...(currency ? { currency } : {}),
392
+ ...(version ? { version } : {}),
393
+ ...(source ? { source: source as PriceSnapshot['source'] } : {}),
394
+ ...(status ? { status: status as PricingStatus } : {}),
395
+ };
396
+ }
397
+
398
+ function sameUsage(record: unknown, usage: UsageCounts | null): boolean {
399
+ if (!usageIsValid(usage)) return false;
400
+ return (
401
+ storedNumber(record, 'inputTokens') === usage.inputTokens &&
402
+ storedNumber(record, 'outputTokens') === usage.outputTokens &&
403
+ storedNumber(record, 'totalTokens') === usage.totalTokens &&
404
+ storedNumber(record, 'cachedTokens') === (usage.cachedTokens ?? null) &&
405
+ storedNumber(record, 'reasoningTokens') === (usage.reasoningTokens ?? null)
406
+ );
407
+ }
408
+
409
+ function incompleteSessions(record: unknown): string[] {
410
+ const value = read(record, 'usageIncompleteSessions');
411
+ if (Array.isArray(value)) return value.filter((entry): entry is string => typeof entry === 'string' && !!entry);
412
+ return [];
413
+ }
414
+
415
+ function spanIdentity(record: unknown) {
416
+ const value = read(record, 'id');
417
+ return typeof value === 'string' || typeof value === 'number' ? value : undefined;
418
+ }
419
+
420
+ function isModelCall(record: unknown) {
421
+ return read(record, 'type') === 'model_call';
422
+ }
423
+
424
+ function messageIdFor(event: unknown): string {
425
+ return identityPart(read(event, 'messageId'));
426
+ }
427
+
428
+ /**
429
+ * Tracks provider usage on model-call spans. Agent/sub-agent/tool spans remain graph projections
430
+ * only and are never included in usage totals, avoiding parent/child double counting.
431
+ */
432
+ export class TokenTracker {
433
+ constructor(
434
+ private readonly plugin: {
435
+ app: {
436
+ log?: { warn?: (message: string, meta?: LogMetadata) => void };
437
+ logger?: { warn?: (message: string, meta?: LogMetadata) => void };
438
+ };
439
+ db: UsageDatabase;
440
+ },
441
+ ) {}
442
+
443
+ get db() {
444
+ return this.plugin.db;
445
+ }
446
+
447
+ get app() {
448
+ return this.plugin.app;
449
+ }
450
+
451
+ private warn(message: string, error: unknown) {
452
+ try {
453
+ this.app.log?.warn?.(message, { error });
454
+ this.app.logger?.warn?.(message, { error });
455
+ } catch {
456
+ // Telemetry logging must not affect the caller's execution.
457
+ }
458
+ }
459
+
460
+ private withTransaction<T>(operation: (transaction?: UsageTransaction) => Promise<T>) {
461
+ const sequelize = this.db.sequelize;
462
+ return sequelize?.transaction ? sequelize.transaction((active) => operation(active)) : operation();
463
+ }
464
+
465
+ private async markSessionIncomplete(runId: string | number, sessionId: string, incomplete: boolean) {
466
+ await this.withTransaction(async (transaction) => {
467
+ const repo = this.db.getRepository('agentLoopRuns');
468
+ if (!repo?.findOne || !repo.update) throw new Error('Run repository is unavailable.');
469
+ const txOptions = transaction
470
+ ? { transaction, ...(transaction.LOCK?.UPDATE === undefined ? {} : { lock: transaction.LOCK.UPDATE }) }
471
+ : {};
472
+ const run = await repo.findOne({ filterByTk: runId, ...txOptions });
473
+ if (!run) throw new Error(`Run ${runId} does not exist.`);
474
+ const sessions = new Set(incompleteSessions(run));
475
+ if (incomplete) sessions.add(sessionId);
476
+ else sessions.delete(sessionId);
477
+ const currentUsageStatus = identityPart(read(run, 'usageStatus'));
478
+ await repo.update({
479
+ filterByTk: runId,
480
+ values: {
481
+ usageIncompleteSessions: [...sessions],
482
+ ...(incomplete
483
+ ? {
484
+ usageStatus: currentUsageStatus && currentUsageStatus !== 'missing' ? 'partial' : 'missing',
485
+ pricingStatus: 'unpriced',
486
+ totalCost: null,
487
+ }
488
+ : {}),
489
+ },
490
+ ...(transaction ? { transaction } : {}),
491
+ });
492
+ });
493
+ }
494
+
495
+ /**
496
+ * Upserts a single provider model-call usage record by its durable identity. Native usage events
497
+ * use `ai-usage-event:<sessionId>:<messageId>`; retries and resume history therefore reconcile
498
+ * the same row instead of adding its tokens again.
499
+ */
500
+ async recordModelCallUsage(input: ModelCallUsageInput): Promise<string | number | undefined> {
501
+ const usageIdentity = identityPart(input.usageIdentity);
502
+ if (!usageIdentity) return undefined;
503
+
504
+ const persist = async (transaction?: UsageTransaction) => {
505
+ const repo = this.db.getRepository('agentExecutionSpans');
506
+ if (!repo?.create || !repo.update || !repo.findOne) return undefined;
507
+ const txOptions = transaction
508
+ ? { transaction, ...(transaction.LOCK?.UPDATE === undefined ? {} : { lock: transaction.LOCK.UPDATE }) }
509
+ : {};
510
+ const existingByIdentity = await repo.findOne({ filter: { usageIdentity }, ...txOptions });
511
+ const existingById =
512
+ input.spanId !== undefined ? await repo.findOne({ filterByTk: input.spanId, ...txOptions }) : null;
513
+ const existing = existingByIdentity || (existingById && isModelCall(existingById) ? existingById : null);
514
+ if (existing && !isModelCall(existing)) {
515
+ this.warn('[TokenTracker] Refused to attach model-call usage to a non-model-call span.', usageIdentity);
516
+ return undefined;
517
+ }
518
+ if (
519
+ existingById &&
520
+ !existingByIdentity &&
521
+ identityPart(read(existingById, 'usageIdentity')) &&
522
+ identityPart(read(existingById, 'usageIdentity')) !== usageIdentity
523
+ ) {
524
+ this.warn('[TokenTracker] Refused to replace a different model-call usage identity.', usageIdentity);
525
+ return undefined;
526
+ }
527
+
528
+ const origin = input.source;
529
+ const existingOrigin = identityPart(read(existing, 'usageOrigin'));
530
+ const existingUsageSource = identityPart(read(existing, 'usageSource'));
531
+ const hasIncomingUsage = usageIsValid(input.usage);
532
+ const preserveNativeData =
533
+ existingOrigin === 'ai_usage_event' &&
534
+ (origin === 'loop_runtime' || (existingUsageSource === 'actual' && !hasIncomingUsage));
535
+ const preserveKnownUsage =
536
+ (existingUsageSource === 'actual' || existingUsageSource === 'estimated') && !hasIncomingUsage;
537
+ const shouldPreserveUsage = preserveNativeData || preserveKnownUsage;
538
+ const usage = shouldPreserveUsage ? null : input.usage;
539
+ const usageSource: UsageSource = shouldPreserveUsage
540
+ ? (identityPart(read(existing, 'usageSource')) as UsageSource) || 'missing'
541
+ : usageIsValid(usage)
542
+ ? 'actual'
543
+ : 'missing';
544
+ const preservePrice =
545
+ shouldPreserveUsage || (existingUsageSource === 'actual' && sameUsage(existing, input.usage));
546
+ const historicalSnapshot = storedPriceSnapshot(existing);
547
+ const hasHistoricalRates = historicalSnapshot.inputPer1k != null && historicalSnapshot.outputPer1k != null;
548
+ let resolvedPricing: ReturnType<typeof pricingFor> | null = null;
549
+ if (!preservePrice) {
550
+ if (hasHistoricalRates && (existingUsageSource === 'actual' || existingUsageSource === 'estimated')) {
551
+ resolvedPricing = pricingFor({ ...input, nativeCost: undefined, priceSnapshot: historicalSnapshot });
552
+ } else if (existingUsageSource === 'actual') {
553
+ // Without saved unit rates a corrected quantity cannot be repriced from current prices.
554
+ // Keep the original snapshot and mark the corrected call unpriced.
555
+ const status = historicalSnapshot.status === 'non_usd' ? 'non_usd' : 'unpriced';
556
+ resolvedPricing = {
557
+ cost: null,
558
+ status,
559
+ priceSource: historicalSnapshot.source ?? null,
560
+ snapshot: { ...historicalSnapshot, status },
561
+ };
562
+ } else {
563
+ resolvedPricing = pricingFor(input);
564
+ }
565
+ }
566
+ const now = new Date();
567
+ const occurredAt = sourceDate(input.occurredAt) || sourceDate(read(existing, 'usageOccurredAt'));
568
+ // Reconciliation of a native event must never replace a previously captured department.
569
+ const existingDepartmentId = read(existing, 'departmentId');
570
+ const departmentId = existingDepartmentId ?? input.departmentId;
571
+ const values: RecordLike = {
572
+ type: 'model_call',
573
+ usageIdentity,
574
+ ...(input.nativeIdentity ? { nativeIdentity: input.nativeIdentity } : {}),
575
+ usageSessionId: input.sessionId,
576
+ usageMessageId: input.messageId,
577
+ ...(input.runId === undefined ? {} : { agentLoopRunId: input.runId }),
578
+ ...(input.rootRunId ? { rootRunId: input.rootRunId } : {}),
579
+ ...(input.parentSpanId ? { parentSpanId: input.parentSpanId } : {}),
580
+ ...(input.role ? { role: input.role } : {}),
581
+ ...(input.employeeUsername ? { employeeUsername: input.employeeUsername } : {}),
582
+ ...(input.userId == null ? {} : { userId: input.userId }),
583
+ ...(departmentId == null
584
+ ? {}
585
+ : {
586
+ departmentId,
587
+ departmentAttribution:
588
+ existingDepartmentId == null ? 'snapshot' : read(existing, 'departmentAttribution'),
589
+ }),
590
+ ...(input.model ? { model: input.model } : {}),
591
+ ...(input.provider ? { provider: input.provider } : {}),
592
+ ...(input.llmService ? { llmService: input.llmService } : {}),
593
+ source: input.source === 'ai_usage_event' ? 'native-plugin-ai' : 'loop-runtime-usage',
594
+ usageSource,
595
+ usageOrigin: shouldPreserveUsage ? existingOrigin || identityPart(read(existing, 'usageOrigin')) : origin,
596
+ inputTokens: shouldPreserveUsage ? read(existing, 'inputTokens') : usage?.inputTokens ?? null,
597
+ outputTokens: shouldPreserveUsage ? read(existing, 'outputTokens') : usage?.outputTokens ?? null,
598
+ totalTokens: shouldPreserveUsage ? read(existing, 'totalTokens') : usage?.totalTokens ?? null,
599
+ cachedTokens: shouldPreserveUsage ? read(existing, 'cachedTokens') : usage?.cachedTokens ?? null,
600
+ reasoningTokens: shouldPreserveUsage ? read(existing, 'reasoningTokens') : usage?.reasoningTokens ?? null,
601
+ cost: preservePrice ? read(existing, 'cost') : resolvedPricing?.cost ?? null,
602
+ pricingStatus: preservePrice ? read(existing, 'pricingStatus') : resolvedPricing?.status ?? 'unpriced',
603
+ priceSource: preservePrice ? read(existing, 'priceSource') : resolvedPricing?.priceSource ?? null,
604
+ priceCurrency: preservePrice ? read(existing, 'priceCurrency') : resolvedPricing?.snapshot.currency ?? null,
605
+ priceVersion: preservePrice ? read(existing, 'priceVersion') : resolvedPricing?.snapshot.version ?? null,
606
+ priceInputPer1k: preservePrice
607
+ ? read(existing, 'priceInputPer1k')
608
+ : resolvedPricing?.snapshot.inputPer1k ?? null,
609
+ priceOutputPer1k: preservePrice
610
+ ? read(existing, 'priceOutputPer1k')
611
+ : resolvedPricing?.snapshot.outputPer1k ?? null,
612
+ priceCachedInputPer1k: preservePrice
613
+ ? read(existing, 'priceCachedInputPer1k')
614
+ : resolvedPricing?.snapshot.cachedInputPer1k ?? null,
615
+ ...(occurredAt ? { usageOccurredAt: occurredAt } : {}),
616
+ ...(sourceDate(input.occurredAt) ? { startedAt: occurredAt, endedAt: occurredAt } : {}),
617
+ ...(existing
618
+ ? {}
619
+ : { status: 'success', startedAt: occurredAt || now, endedAt: occurredAt || now, createdAt: now }),
620
+ updatedAt: now,
621
+ };
622
+
623
+ const id = spanIdentity(existing);
624
+ if (id !== undefined) {
625
+ await repo.update({ filterByTk: id, values, ...txOptions });
626
+ return id;
627
+ }
628
+
629
+ const created = await repo.create({
630
+ values: {
631
+ rootRunId: input.rootRunId || `session:${input.sessionId}`,
632
+ status: 'success',
633
+ startedAt: occurredAt || now,
634
+ endedAt: occurredAt || now,
635
+ createdAt: now,
636
+ ...values,
637
+ },
638
+ ...txOptions,
639
+ });
640
+ return spanIdentity(created);
641
+ };
642
+
643
+ try {
644
+ let id: string | number | undefined;
645
+ try {
646
+ id = await this.withTransaction(persist);
647
+ } catch {
648
+ // A unique-key race aborts its transaction. Retry in a fresh transaction so the winning
649
+ // row is re-read under lock and its actual usage/price snapshot is merged safely.
650
+ id = await this.withTransaction(persist);
651
+ }
652
+ if (id === undefined) return undefined;
653
+ if (input.runId != null && input.recomputeRunTotals !== false && !(await this.accumulateToRun(input.runId))) {
654
+ return undefined;
655
+ }
656
+ return id;
657
+ } catch (error) {
658
+ this.warn('[TokenTracker] Failed to track model-call usage', error);
659
+ return undefined;
660
+ }
661
+ }
662
+
663
+ /**
664
+ * Imports authoritative core aiUsageEvents for one session. The same identity is used by the
665
+ * native monitor projection, so loop retries and observer reconciliation update one model-call.
666
+ */
667
+ async syncNativeUsageEvents(input: NativeUsageSyncInput): Promise<number> {
668
+ try {
669
+ const repo = this.db.getRepository('aiUsageEvents');
670
+ if (!repo?.find) {
671
+ if (input.expectEvents && input.runId != null)
672
+ await this.markSessionIncomplete(input.runId, input.sessionId, true);
673
+ return 0;
674
+ }
675
+ let tracked = 0;
676
+ let lastMessageId: string | undefined;
677
+ let hasMore = true;
678
+ while (hasMore) {
679
+ const events = await repo.find({
680
+ filter: {
681
+ sessionId: input.sessionId,
682
+ eventType: USAGE_EVENT_TYPE,
683
+ ...(lastMessageId ? { messageId: { $gt: lastMessageId } } : {}),
684
+ },
685
+ sort: ['messageId'],
686
+ limit: USAGE_PAGE_SIZE,
687
+ });
688
+ for (const event of events) {
689
+ const sessionId = identityPart(read(event, 'sessionId')) || input.sessionId;
690
+ const messageId = messageIdFor(event);
691
+ if (!messageId) throw new Error('Native usage event has no message identity.');
692
+ const nativeCost = readNumber(read(event, 'cost'));
693
+ const priceSnapshotValue = read(event, 'priceSnapshot');
694
+ const priceSnapshot =
695
+ priceSnapshotValue && typeof priceSnapshotValue === 'object'
696
+ ? (priceSnapshotValue as PriceSnapshot)
697
+ : undefined;
698
+ const occurredAt = sourceDate(read(event, 'occurredAt'));
699
+ const id = await this.recordModelCallUsage({
700
+ usageIdentity: usageEventIdentity(sessionId, messageId),
701
+ nativeIdentity: usageEventIdentity(sessionId, messageId),
702
+ sessionId,
703
+ messageId,
704
+ usage: usageFromAIUsageEvent(event),
705
+ model: identityPart(read(event, 'model')) || undefined,
706
+ provider: identityPart(read(event, 'provider')) || undefined,
707
+ llmService: identityPart(read(event, 'llmService')) || undefined,
708
+ runId: input.runId,
709
+ rootRunId: input.rootRunId,
710
+ parentSpanId: input.parentSpanId,
711
+ role: input.role,
712
+ employeeUsername: input.employeeUsername || identityPart(read(event, 'aiEmployeeUsername')) || undefined,
713
+ userId: input.userId ?? (read(event, 'userId') as string | number | undefined),
714
+ source: 'ai_usage_event',
715
+ recomputeRunTotals: false,
716
+ ...(occurredAt ? { occurredAt } : {}),
717
+ ...(priceSnapshot ? { priceSnapshot } : {}),
718
+ ...(nativeCost === undefined ? {} : { nativeCost }),
719
+ });
720
+ if (id === undefined) throw new Error(`Could not persist usage event ${messageId}.`);
721
+ tracked += 1;
722
+ }
723
+ if (events.length < USAGE_PAGE_SIZE) {
724
+ hasMore = false;
725
+ continue;
726
+ }
727
+ const nextMessageId = messageIdFor(events[events.length - 1]);
728
+ if (!nextMessageId || nextMessageId === lastMessageId)
729
+ throw new Error('Native usage pagination did not advance.');
730
+ lastMessageId = nextMessageId;
731
+ }
732
+ if (input.runId != null && (tracked > 0 || input.expectEvents)) {
733
+ // An empty query cannot prove an invocation consumed no tokens. Retain a previous
734
+ // incomplete marker until at least one source event can be fully reconciled.
735
+ await this.markSessionIncomplete(input.runId, input.sessionId, tracked === 0);
736
+ if (!(await this.accumulateToRun(input.runId))) throw new Error('Could not recalculate usage totals.');
737
+ }
738
+ return tracked;
739
+ } catch (error) {
740
+ this.warn('[TokenTracker] Failed to synchronize native usage events', error);
741
+ if (input.runId != null) {
742
+ try {
743
+ await this.markSessionIncomplete(input.runId, input.sessionId, true);
744
+ } catch (markError) {
745
+ this.warn('[TokenTracker] Failed to mark incomplete run usage', markError);
746
+ }
747
+ }
748
+ return -1;
749
+ }
750
+ }
751
+
752
+ /**
753
+ * Persists the loop adapter's transcript aggregate only as a non-additive summary when the
754
+ * authoritative aiUsageEvents rows are unavailable. Summary rows stay out of run totals so a
755
+ * later native reconciliation cannot double-count resume history or model-call children.
756
+ */
757
+ async trackLoopInvocationUsage(input: LoopInvocationUsageInput): Promise<void> {
758
+ const nativeRows = await this.syncNativeUsageEvents({
759
+ sessionId: input.sessionId,
760
+ expectEvents: true,
761
+ runId: input.runId,
762
+ rootRunId: input.rootRunId,
763
+ role: input.role,
764
+ employeeUsername: input.employeeUsername,
765
+ userId: input.userId,
766
+ });
767
+ if (nativeRows !== 0) return;
768
+
769
+ try {
770
+ const repo = this.db.getRepository('agentExecutionSpans');
771
+ if (!repo?.findOne || !repo?.create || !repo.update) return;
772
+ const usageIdentity = `loop-invocation-summary:${input.runId}:${input.stepId}`;
773
+ const existing = await repo.findOne({ filter: { usageIdentity } });
774
+ const now = new Date();
775
+ let usage: UsageCounts | null = usageIsValid(input.usage) ? input.usage : null;
776
+ let source: UsageSource = usage ? 'actual' : 'missing';
777
+ if (!usage) {
778
+ const inputTokens = Math.ceil((input.input || '').length / 4);
779
+ const outputTokens = Math.ceil((input.output || '').length / 4);
780
+ usage = { inputTokens, outputTokens, totalTokens: inputTokens + outputTokens };
781
+ source = 'estimated';
782
+ }
783
+ const priced = pricingFor({
784
+ usageIdentity,
785
+ sessionId: input.sessionId,
786
+ messageId: input.messageId || '',
787
+ usage,
788
+ source: 'loop_runtime',
789
+ });
790
+ const values: RecordLike = {
791
+ type: 'invocation_summary',
792
+ usageIdentity,
793
+ usageSessionId: input.sessionId,
794
+ usageMessageId: input.messageId || null,
795
+ agentLoopRunId: input.runId,
796
+ agentLoopStepId: input.stepId,
797
+ rootRunId: input.rootRunId || `loop:${input.runId}`,
798
+ role: input.role,
799
+ employeeUsername: input.employeeUsername,
800
+ ...(input.userId == null ? {} : { userId: input.userId }),
801
+ source: 'loop-runtime-summary',
802
+ usageSource: source,
803
+ usageOrigin: 'loop_invocation_summary',
804
+ inputTokens: usage.inputTokens,
805
+ outputTokens: usage.outputTokens,
806
+ totalTokens: usage.totalTokens,
807
+ cachedTokens: usage.cachedTokens ?? null,
808
+ reasoningTokens: usage.reasoningTokens ?? null,
809
+ cost: priced.cost,
810
+ pricingStatus: priced.cost === null ? 'unpriced' : source === 'estimated' ? 'estimated' : priced.status,
811
+ priceSource: priced.priceSource,
812
+ priceCurrency: priced.snapshot.currency ?? null,
813
+ priceVersion: priced.snapshot.version ?? null,
814
+ priceInputPer1k: priced.snapshot.inputPer1k ?? null,
815
+ priceOutputPer1k: priced.snapshot.outputPer1k ?? null,
816
+ priceCachedInputPer1k: priced.snapshot.cachedInputPer1k ?? null,
817
+ status: 'success',
818
+ startedAt: now,
819
+ endedAt: now,
820
+ updatedAt: now,
821
+ };
822
+ const existingId = spanIdentity(existing);
823
+ if (existingId !== undefined) {
824
+ await repo.update({ filterByTk: existingId, values });
825
+ } else {
826
+ await repo.create({ values: { createdAt: now, ...values } });
827
+ }
828
+ } catch (error) {
829
+ this.warn('[TokenTracker] Failed to persist loop invocation usage summary', error);
830
+ }
831
+ }
832
+
833
+ /**
834
+ * Compatibility API for callers that already hold an agentExecutionSpans primary key. This
835
+ * method deliberately keeps span identity separate from agentLoopSteps IDs.
836
+ */
837
+ async trackSpan(spanId: string | number | undefined, usage: TokenUsage, runId?: string | number): Promise<void> {
838
+ if (spanId === undefined || spanId === null || spanId === '') return;
839
+
840
+ try {
841
+ const repo = this.db.getRepository('agentExecutionSpans');
842
+ if (!repo?.findOne || !repo.update) return;
843
+ const span = await repo.findOne({ filterByTk: spanId });
844
+ if (!span) return;
845
+ const inputTokens = readNumber(usage.inputTokens);
846
+ const outputTokens = readNumber(usage.outputTokens);
847
+ const totalTokens = readNumber(usage.totalTokens);
848
+ if (inputTokens === undefined || outputTokens === undefined || totalTokens === undefined) return;
849
+
850
+ const model = identityPart(read(span, 'model')) || undefined;
851
+ const provider = identityPart(read(span, 'provider')) || undefined;
852
+ const llmService = identityPart(read(span, 'llmService')) || undefined;
853
+ const resolved = pricingFor({
854
+ usageIdentity: identityPart(read(span, 'usageIdentity')) || `legacy-span:${spanId}`,
855
+ nativeIdentity: identityPart(read(span, 'nativeIdentity')) || undefined,
856
+ sessionId: identityPart(read(span, 'sessionId')),
857
+ messageId: identityPart(read(span, 'messageId')),
858
+ usage: { inputTokens, outputTokens, totalTokens, cost: usage.cost, cachedTokens: usage.cachedTokens },
859
+ model,
860
+ provider,
861
+ llmService,
862
+ source: 'loop_runtime',
863
+ });
864
+ await repo.update({
865
+ filterByTk: spanId,
866
+ values: {
867
+ inputTokens,
868
+ outputTokens,
869
+ totalTokens,
870
+ cachedTokens: usage.cachedTokens ?? null,
871
+ reasoningTokens: usage.reasoningTokens ?? null,
872
+ usageSource: 'actual',
873
+ usageOrigin: 'legacy_tracker',
874
+ cost: resolved.cost,
875
+ pricingStatus: resolved.status,
876
+ priceSource: resolved.priceSource,
877
+ priceCurrency: resolved.snapshot.currency ?? null,
878
+ priceVersion: resolved.snapshot.version ?? null,
879
+ priceInputPer1k: resolved.snapshot.inputPer1k ?? null,
880
+ priceOutputPer1k: resolved.snapshot.outputPer1k ?? null,
881
+ priceCachedInputPer1k: resolved.snapshot.cachedInputPer1k ?? null,
882
+ updatedAt: new Date(),
883
+ },
884
+ });
885
+
886
+ if (runId != null) await this.accumulateToRun(runId);
887
+ } catch (error) {
888
+ this.warn('[TokenTracker] Failed to track span tokens', error);
889
+ }
890
+ }
891
+
892
+ /** Recalculates totals from model-call spans only. Parent and child span tokens are not added. */
893
+ async accumulateToRun(runId: string | number): Promise<boolean> {
894
+ try {
895
+ return await this.withTransaction(async (transaction) => {
896
+ const spansRepo = this.db.getRepository('agentExecutionSpans');
897
+ const runsRepo = this.db.getRepository('agentLoopRuns');
898
+ if (!spansRepo?.find || !runsRepo?.findOne || !runsRepo.update) return false;
899
+ const txOptions = transaction
900
+ ? { transaction, ...(transaction.LOCK?.UPDATE === undefined ? {} : { lock: transaction.LOCK.UPDATE }) }
901
+ : {};
902
+ // All writers recomputing a run acquire the same row lock before reading its spans.
903
+ // A later writer therefore includes every model-call committed by earlier writers.
904
+ const run = await runsRepo.findOne({ filterByTk: runId, ...txOptions });
905
+ if (!run) return false;
906
+
907
+ let totalInput = 0;
908
+ let totalOutput = 0;
909
+ let totalTokens = 0;
910
+ let totalCost = 0;
911
+ let hasKnownUsage = false;
912
+ let hasMissingUsage = incompleteSessions(run).length > 0;
913
+ let hasEstimatedUsage = false;
914
+ let hasActualUsage = false;
915
+ let hasUnpriced = incompleteSessions(run).length > 0;
916
+ let hasNonUsd = false;
917
+ let hasEstimatedPrice = false;
918
+ let rowCount = 0;
919
+ let lastId: string | number | undefined;
920
+
921
+ let hasMore = true;
922
+ while (hasMore) {
923
+ const spans = await spansRepo.find({
924
+ filter: {
925
+ agentLoopRunId: runId,
926
+ type: 'model_call',
927
+ ...(lastId === undefined ? {} : { id: { $gt: lastId } }),
928
+ },
929
+ fields: [
930
+ 'id',
931
+ 'inputTokens',
932
+ 'outputTokens',
933
+ 'totalTokens',
934
+ 'cost',
935
+ 'usageSource',
936
+ 'pricingStatus',
937
+ 'priceCurrency',
938
+ ],
939
+ sort: ['id'],
940
+ limit: USAGE_PAGE_SIZE,
941
+ ...(transaction ? { transaction } : {}),
942
+ });
943
+ rowCount += spans.length;
944
+ for (const span of spans) {
945
+ const source = identityPart(read(span, 'usageSource'));
946
+ const inputTokens = storedNumber(span, 'inputTokens');
947
+ const outputTokens = storedNumber(span, 'outputTokens');
948
+ const storedTotalTokens = storedNumber(span, 'totalTokens');
949
+ if (source === 'missing' || inputTokens === null || outputTokens === null) {
950
+ hasMissingUsage = true;
951
+ } else {
952
+ hasKnownUsage = true;
953
+ totalInput += inputTokens;
954
+ totalOutput += outputTokens;
955
+ totalTokens += storedTotalTokens ?? inputTokens + outputTokens;
956
+ if (source === 'estimated' || !source) hasEstimatedUsage = true;
957
+ if (source === 'actual') hasActualUsage = true;
958
+ }
959
+
960
+ const pricingStatus = identityPart(read(span, 'pricingStatus'));
961
+ const currency = identityPart(read(span, 'priceCurrency')).toUpperCase();
962
+ const cost = storedNumber(span, 'cost');
963
+ if (pricingStatus === 'non_usd' || (currency && currency !== 'USD')) hasNonUsd = true;
964
+ if (pricingStatus === 'unpriced' || cost === null || currency !== 'USD') {
965
+ hasUnpriced = true;
966
+ } else {
967
+ totalCost += cost;
968
+ if (pricingStatus === 'estimated') hasEstimatedPrice = true;
969
+ }
970
+ }
971
+ if (spans.length < USAGE_PAGE_SIZE) {
972
+ hasMore = false;
973
+ continue;
974
+ }
975
+ const nextId = spanIdentity(spans[spans.length - 1]);
976
+ if (nextId === undefined || nextId === lastId) throw new Error('Usage rollup pagination did not advance.');
977
+ lastId = nextId;
978
+ }
979
+
980
+ const usageStatus = !hasKnownUsage
981
+ ? 'missing'
982
+ : hasMissingUsage
983
+ ? 'partial'
984
+ : hasEstimatedUsage
985
+ ? 'estimated'
986
+ : hasActualUsage
987
+ ? 'actual'
988
+ : 'partial';
989
+ const pricingStatus = hasNonUsd
990
+ ? 'non_usd'
991
+ : hasUnpriced || rowCount === 0
992
+ ? 'unpriced'
993
+ : hasEstimatedPrice
994
+ ? 'estimated'
995
+ : 'configured';
996
+ await runsRepo.update({
997
+ filterByTk: runId,
998
+ values: {
999
+ totalInputTokens: hasKnownUsage ? totalInput : null,
1000
+ totalOutputTokens: hasKnownUsage ? totalOutput : null,
1001
+ totalTokens: hasKnownUsage ? totalTokens : null,
1002
+ totalCost: hasUnpriced || rowCount === 0 ? null : totalCost,
1003
+ usageStatus,
1004
+ pricingStatus,
1005
+ },
1006
+ ...(transaction ? { transaction } : {}),
1007
+ });
1008
+ return true;
1009
+ });
1010
+ } catch (error) {
1011
+ this.warn('[TokenTracker] Failed to accumulate run totals', error);
1012
+ return false;
1013
+ }
1014
+ }
1015
+
1016
+ /** Checks configured budgets. Unknown cost is never treated as a free run. */
1017
+ async checkBudget(runId: string | number): Promise<BudgetCheckResult> {
1018
+ try {
1019
+ const repo = this.db.getRepository('agentLoopRuns');
1020
+ if (!repo?.findOne) return { allowed: true };
1021
+
1022
+ const run = await repo.findOne({ filter: { id: runId } });
1023
+ if (!run) return { allowed: true };
1024
+
1025
+ const budgetMaxTokens = Number(read(run, 'budgetMaxTokens') ?? 0);
1026
+ const budgetMaxCost = Number(read(run, 'budgetMaxCost') ?? 0);
1027
+ if (budgetMaxTokens <= 0 && budgetMaxCost <= 0) return { allowed: true };
1028
+
1029
+ const usageStatus = identityPart(read(run, 'usageStatus'));
1030
+ const pricingStatus = identityPart(read(run, 'pricingStatus'));
1031
+ const hasIncompleteSessions = incompleteSessions(run).length > 0;
1032
+ const totalTokens = readNumber(read(run, 'totalTokens'));
1033
+ const totalCost = readNumber(read(run, 'totalCost'));
1034
+ const hasInvocations = (readNumber(read(run, 'invocationCount')) ?? 0) > 0;
1035
+
1036
+ if (
1037
+ budgetMaxTokens > 0 &&
1038
+ (hasIncompleteSessions ||
1039
+ usageStatus === 'partial' ||
1040
+ usageStatus === 'missing' ||
1041
+ (totalTokens === undefined && (usageStatus !== '' || hasInvocations)))
1042
+ ) {
1043
+ return {
1044
+ allowed: false,
1045
+ reason: 'Token usage is unavailable, so this run cannot be checked against its token budget.',
1046
+ };
1047
+ }
1048
+ if (
1049
+ budgetMaxCost > 0 &&
1050
+ (hasIncompleteSessions ||
1051
+ pricingStatus === 'unpriced' ||
1052
+ pricingStatus === 'non_usd' ||
1053
+ (totalCost === undefined && (pricingStatus !== '' || hasInvocations)))
1054
+ ) {
1055
+ return {
1056
+ allowed: false,
1057
+ reason: 'Model pricing is unavailable, so this run cannot be checked against its cost budget.',
1058
+ };
1059
+ }
1060
+ if (budgetMaxTokens > 0 && totalTokens !== undefined && totalTokens >= budgetMaxTokens) {
1061
+ return {
1062
+ allowed: false,
1063
+ reason: `Budget exceeded: ${totalTokens}/${budgetMaxTokens} tokens used. Maximum allowed tokens for this run: ${budgetMaxTokens}.`,
1064
+ };
1065
+ }
1066
+ if (budgetMaxCost > 0 && totalCost !== undefined && totalCost >= budgetMaxCost) {
1067
+ return {
1068
+ allowed: false,
1069
+ reason: `Budget exceeded: $${totalCost.toFixed(4)}/$${budgetMaxCost.toFixed(
1070
+ 4,
1071
+ )} spent. Maximum allowed cost for this run: $${budgetMaxCost}.`,
1072
+ };
1073
+ }
1074
+
1075
+ return { allowed: true };
1076
+ } catch (error) {
1077
+ this.warn('[TokenTracker] Budget check failed, allowing', error);
1078
+ return { allowed: true };
1079
+ }
1080
+ }
1081
+
1082
+ /**
1083
+ * Compatibility API for callers that update an existing span. Missing usage falls back to an
1084
+ * explicit text estimate; zero-token actual usage remains actual and is never re-estimated.
1085
+ */
1086
+ async trackActualUsage(
1087
+ spanId: string | number | undefined,
1088
+ tokenUsage: UsageCounts | null,
1089
+ input: string,
1090
+ output: string,
1091
+ runId?: string | number,
1092
+ ): Promise<void> {
1093
+ if (usageIsValid(tokenUsage)) {
1094
+ await this.trackSpan(spanId, { ...tokenUsage, cost: tokenUsage.cost ?? Number.NaN }, runId);
1095
+ return;
1096
+ }
1097
+ await this.estimateAndTrack(spanId, input, output, runId);
1098
+ }
1099
+
1100
+ /** Stores the legacy text-length estimate explicitly, scoped to an existing span. */
1101
+ async estimateAndTrack(
1102
+ spanId: string | number | undefined,
1103
+ input: string,
1104
+ output: string,
1105
+ runId?: string | number,
1106
+ ): Promise<void> {
1107
+ if (spanId === undefined || spanId === null || spanId === '') return;
1108
+ try {
1109
+ const repo = this.db.getRepository('agentExecutionSpans');
1110
+ if (!repo?.findOne || !repo.update) return;
1111
+ const span = await repo.findOne({ filterByTk: spanId });
1112
+ if (!span) return;
1113
+ const inputTokens = Math.ceil((input || '').length / 4);
1114
+ const outputTokens = Math.ceil((output || '').length / 4);
1115
+ const usage: UsageCounts = {
1116
+ inputTokens,
1117
+ outputTokens,
1118
+ totalTokens: inputTokens + outputTokens,
1119
+ };
1120
+ const resolved = pricingFor({
1121
+ usageIdentity: identityPart(read(span, 'usageIdentity')) || `legacy-span:${spanId}`,
1122
+ sessionId: identityPart(read(span, 'sessionId')),
1123
+ messageId: identityPart(read(span, 'messageId')),
1124
+ usage,
1125
+ model: identityPart(read(span, 'model')) || undefined,
1126
+ provider: identityPart(read(span, 'provider')) || undefined,
1127
+ llmService: identityPart(read(span, 'llmService')) || undefined,
1128
+ source: 'loop_runtime',
1129
+ });
1130
+ await repo.update({
1131
+ filterByTk: spanId,
1132
+ values: {
1133
+ inputTokens,
1134
+ outputTokens,
1135
+ totalTokens: inputTokens + outputTokens,
1136
+ cachedTokens: null,
1137
+ reasoningTokens: null,
1138
+ usageSource: 'estimated',
1139
+ usageOrigin: 'text_length_estimate',
1140
+ cost: resolved.cost,
1141
+ pricingStatus: resolved.cost === null ? 'unpriced' : 'estimated',
1142
+ priceSource: resolved.priceSource,
1143
+ priceCurrency: resolved.snapshot.currency ?? null,
1144
+ priceVersion: resolved.snapshot.version ?? null,
1145
+ priceInputPer1k: resolved.snapshot.inputPer1k ?? null,
1146
+ priceOutputPer1k: resolved.snapshot.outputPer1k ?? null,
1147
+ priceCachedInputPer1k: resolved.snapshot.cachedInputPer1k ?? null,
1148
+ updatedAt: new Date(),
1149
+ },
1150
+ });
1151
+ if (runId != null) await this.accumulateToRun(runId);
1152
+ } catch (error) {
1153
+ this.warn('[TokenTracker] Failed to estimate span tokens', error);
1154
+ }
1155
+ }
1156
+ }
1157
+
1158
+ export { usageEventIdentity };