palaryn 0.1.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 (607) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +716 -0
  3. package/dist/sdk/typescript/src/client.d.ts +71 -0
  4. package/dist/sdk/typescript/src/client.d.ts.map +1 -0
  5. package/dist/sdk/typescript/src/client.js +176 -0
  6. package/dist/sdk/typescript/src/client.js.map +1 -0
  7. package/dist/sdk/typescript/src/errors.d.ts +50 -0
  8. package/dist/sdk/typescript/src/errors.d.ts.map +1 -0
  9. package/dist/sdk/typescript/src/errors.js +103 -0
  10. package/dist/sdk/typescript/src/errors.js.map +1 -0
  11. package/dist/sdk/typescript/src/index.d.ts +4 -0
  12. package/dist/sdk/typescript/src/index.d.ts.map +1 -0
  13. package/dist/sdk/typescript/src/index.js +15 -0
  14. package/dist/sdk/typescript/src/index.js.map +1 -0
  15. package/dist/sdk/typescript/src/types.d.ts +101 -0
  16. package/dist/sdk/typescript/src/types.d.ts.map +1 -0
  17. package/dist/sdk/typescript/src/types.js +6 -0
  18. package/dist/sdk/typescript/src/types.js.map +1 -0
  19. package/dist/src/admin/index.d.ts +2 -0
  20. package/dist/src/admin/index.d.ts.map +1 -0
  21. package/dist/src/admin/index.js +6 -0
  22. package/dist/src/admin/index.js.map +1 -0
  23. package/dist/src/admin/routes.d.ts +5 -0
  24. package/dist/src/admin/routes.d.ts.map +1 -0
  25. package/dist/src/admin/routes.js +471 -0
  26. package/dist/src/admin/routes.js.map +1 -0
  27. package/dist/src/admin/templates.d.ts +51 -0
  28. package/dist/src/admin/templates.d.ts.map +1 -0
  29. package/dist/src/admin/templates.js +500 -0
  30. package/dist/src/admin/templates.js.map +1 -0
  31. package/dist/src/anomaly/detector.d.ts +141 -0
  32. package/dist/src/anomaly/detector.d.ts.map +1 -0
  33. package/dist/src/anomaly/detector.js +554 -0
  34. package/dist/src/anomaly/detector.js.map +1 -0
  35. package/dist/src/anomaly/index.d.ts +2 -0
  36. package/dist/src/anomaly/index.d.ts.map +1 -0
  37. package/dist/src/anomaly/index.js +7 -0
  38. package/dist/src/anomaly/index.js.map +1 -0
  39. package/dist/src/approval/manager.d.ts +147 -0
  40. package/dist/src/approval/manager.d.ts.map +1 -0
  41. package/dist/src/approval/manager.js +511 -0
  42. package/dist/src/approval/manager.js.map +1 -0
  43. package/dist/src/approval/webhook.d.ts +36 -0
  44. package/dist/src/approval/webhook.d.ts.map +1 -0
  45. package/dist/src/approval/webhook.js +135 -0
  46. package/dist/src/approval/webhook.js.map +1 -0
  47. package/dist/src/audit/logger.d.ts +70 -0
  48. package/dist/src/audit/logger.d.ts.map +1 -0
  49. package/dist/src/audit/logger.js +440 -0
  50. package/dist/src/audit/logger.js.map +1 -0
  51. package/dist/src/auth/index.d.ts +6 -0
  52. package/dist/src/auth/index.d.ts.map +1 -0
  53. package/dist/src/auth/index.js +22 -0
  54. package/dist/src/auth/index.js.map +1 -0
  55. package/dist/src/auth/password.d.ts +3 -0
  56. package/dist/src/auth/password.d.ts.map +1 -0
  57. package/dist/src/auth/password.js +25 -0
  58. package/dist/src/auth/password.js.map +1 -0
  59. package/dist/src/auth/pkce.d.ts +13 -0
  60. package/dist/src/auth/pkce.d.ts.map +1 -0
  61. package/dist/src/auth/pkce.js +58 -0
  62. package/dist/src/auth/pkce.js.map +1 -0
  63. package/dist/src/auth/providers.d.ts +28 -0
  64. package/dist/src/auth/providers.d.ts.map +1 -0
  65. package/dist/src/auth/providers.js +198 -0
  66. package/dist/src/auth/providers.js.map +1 -0
  67. package/dist/src/auth/routes.d.ts +14 -0
  68. package/dist/src/auth/routes.d.ts.map +1 -0
  69. package/dist/src/auth/routes.js +431 -0
  70. package/dist/src/auth/routes.js.map +1 -0
  71. package/dist/src/auth/session.d.ts +24 -0
  72. package/dist/src/auth/session.d.ts.map +1 -0
  73. package/dist/src/auth/session.js +105 -0
  74. package/dist/src/auth/session.js.map +1 -0
  75. package/dist/src/billing/index.d.ts +7 -0
  76. package/dist/src/billing/index.d.ts.map +1 -0
  77. package/dist/src/billing/index.js +14 -0
  78. package/dist/src/billing/index.js.map +1 -0
  79. package/dist/src/billing/plan-enforcer.d.ts +44 -0
  80. package/dist/src/billing/plan-enforcer.d.ts.map +1 -0
  81. package/dist/src/billing/plan-enforcer.js +110 -0
  82. package/dist/src/billing/plan-enforcer.js.map +1 -0
  83. package/dist/src/billing/routes.d.ts +15 -0
  84. package/dist/src/billing/routes.d.ts.map +1 -0
  85. package/dist/src/billing/routes.js +193 -0
  86. package/dist/src/billing/routes.js.map +1 -0
  87. package/dist/src/billing/stripe-client.d.ts +14 -0
  88. package/dist/src/billing/stripe-client.d.ts.map +1 -0
  89. package/dist/src/billing/stripe-client.js +51 -0
  90. package/dist/src/billing/stripe-client.js.map +1 -0
  91. package/dist/src/billing/webhook-handler.d.ts +19 -0
  92. package/dist/src/billing/webhook-handler.d.ts.map +1 -0
  93. package/dist/src/billing/webhook-handler.js +169 -0
  94. package/dist/src/billing/webhook-handler.js.map +1 -0
  95. package/dist/src/billing/webhook-routes.d.ts +5 -0
  96. package/dist/src/billing/webhook-routes.d.ts.map +1 -0
  97. package/dist/src/billing/webhook-routes.js +30 -0
  98. package/dist/src/billing/webhook-routes.js.map +1 -0
  99. package/dist/src/budget/manager.d.ts +95 -0
  100. package/dist/src/budget/manager.d.ts.map +1 -0
  101. package/dist/src/budget/manager.js +547 -0
  102. package/dist/src/budget/manager.js.map +1 -0
  103. package/dist/src/budget/usage-extractor.d.ts +38 -0
  104. package/dist/src/budget/usage-extractor.d.ts.map +1 -0
  105. package/dist/src/budget/usage-extractor.js +165 -0
  106. package/dist/src/budget/usage-extractor.js.map +1 -0
  107. package/dist/src/cli.d.ts +3 -0
  108. package/dist/src/cli.d.ts.map +1 -0
  109. package/dist/src/cli.js +115 -0
  110. package/dist/src/cli.js.map +1 -0
  111. package/dist/src/config/defaults.d.ts +3 -0
  112. package/dist/src/config/defaults.d.ts.map +1 -0
  113. package/dist/src/config/defaults.js +243 -0
  114. package/dist/src/config/defaults.js.map +1 -0
  115. package/dist/src/config/validate.d.ts +15 -0
  116. package/dist/src/config/validate.d.ts.map +1 -0
  117. package/dist/src/config/validate.js +105 -0
  118. package/dist/src/config/validate.js.map +1 -0
  119. package/dist/src/dlp/composite-scanner.d.ts +47 -0
  120. package/dist/src/dlp/composite-scanner.d.ts.map +1 -0
  121. package/dist/src/dlp/composite-scanner.js +186 -0
  122. package/dist/src/dlp/composite-scanner.js.map +1 -0
  123. package/dist/src/dlp/index.d.ts +10 -0
  124. package/dist/src/dlp/index.d.ts.map +1 -0
  125. package/dist/src/dlp/index.js +26 -0
  126. package/dist/src/dlp/index.js.map +1 -0
  127. package/dist/src/dlp/interfaces.d.ts +33 -0
  128. package/dist/src/dlp/interfaces.d.ts.map +1 -0
  129. package/dist/src/dlp/interfaces.js +3 -0
  130. package/dist/src/dlp/interfaces.js.map +1 -0
  131. package/dist/src/dlp/patterns.d.ts +9 -0
  132. package/dist/src/dlp/patterns.d.ts.map +1 -0
  133. package/dist/src/dlp/patterns.js +25 -0
  134. package/dist/src/dlp/patterns.js.map +1 -0
  135. package/dist/src/dlp/prompt-injection-backend.d.ts +68 -0
  136. package/dist/src/dlp/prompt-injection-backend.d.ts.map +1 -0
  137. package/dist/src/dlp/prompt-injection-backend.js +148 -0
  138. package/dist/src/dlp/prompt-injection-backend.js.map +1 -0
  139. package/dist/src/dlp/prompt-injection-patterns.d.ts +32 -0
  140. package/dist/src/dlp/prompt-injection-patterns.d.ts.map +1 -0
  141. package/dist/src/dlp/prompt-injection-patterns.js +290 -0
  142. package/dist/src/dlp/prompt-injection-patterns.js.map +1 -0
  143. package/dist/src/dlp/regex-backend.d.ts +32 -0
  144. package/dist/src/dlp/regex-backend.d.ts.map +1 -0
  145. package/dist/src/dlp/regex-backend.js +153 -0
  146. package/dist/src/dlp/regex-backend.js.map +1 -0
  147. package/dist/src/dlp/scanner.d.ts +122 -0
  148. package/dist/src/dlp/scanner.d.ts.map +1 -0
  149. package/dist/src/dlp/scanner.js +444 -0
  150. package/dist/src/dlp/scanner.js.map +1 -0
  151. package/dist/src/dlp/text-normalizer.d.ts +41 -0
  152. package/dist/src/dlp/text-normalizer.d.ts.map +1 -0
  153. package/dist/src/dlp/text-normalizer.js +203 -0
  154. package/dist/src/dlp/text-normalizer.js.map +1 -0
  155. package/dist/src/dlp/trufflehog-backend.d.ts +64 -0
  156. package/dist/src/dlp/trufflehog-backend.d.ts.map +1 -0
  157. package/dist/src/dlp/trufflehog-backend.js +151 -0
  158. package/dist/src/dlp/trufflehog-backend.js.map +1 -0
  159. package/dist/src/executor/http-executor.d.ts +25 -0
  160. package/dist/src/executor/http-executor.d.ts.map +1 -0
  161. package/dist/src/executor/http-executor.js +333 -0
  162. package/dist/src/executor/http-executor.js.map +1 -0
  163. package/dist/src/executor/index.d.ts +6 -0
  164. package/dist/src/executor/index.d.ts.map +1 -0
  165. package/dist/src/executor/index.js +12 -0
  166. package/dist/src/executor/index.js.map +1 -0
  167. package/dist/src/executor/interfaces.d.ts +11 -0
  168. package/dist/src/executor/interfaces.d.ts.map +1 -0
  169. package/dist/src/executor/interfaces.js +3 -0
  170. package/dist/src/executor/interfaces.js.map +1 -0
  171. package/dist/src/executor/noop-executor.d.ts +13 -0
  172. package/dist/src/executor/noop-executor.d.ts.map +1 -0
  173. package/dist/src/executor/noop-executor.js +21 -0
  174. package/dist/src/executor/noop-executor.js.map +1 -0
  175. package/dist/src/executor/registry.d.ts +30 -0
  176. package/dist/src/executor/registry.d.ts.map +1 -0
  177. package/dist/src/executor/registry.js +62 -0
  178. package/dist/src/executor/registry.js.map +1 -0
  179. package/dist/src/executor/slack-executor.d.ts +24 -0
  180. package/dist/src/executor/slack-executor.d.ts.map +1 -0
  181. package/dist/src/executor/slack-executor.js +147 -0
  182. package/dist/src/executor/slack-executor.js.map +1 -0
  183. package/dist/src/index.d.ts +25 -0
  184. package/dist/src/index.d.ts.map +1 -0
  185. package/dist/src/index.js +74 -0
  186. package/dist/src/index.js.map +1 -0
  187. package/dist/src/mcp/auth-verifier.d.ts +23 -0
  188. package/dist/src/mcp/auth-verifier.d.ts.map +1 -0
  189. package/dist/src/mcp/auth-verifier.js +162 -0
  190. package/dist/src/mcp/auth-verifier.js.map +1 -0
  191. package/dist/src/mcp/bridge.d.ts +132 -0
  192. package/dist/src/mcp/bridge.d.ts.map +1 -0
  193. package/dist/src/mcp/bridge.js +734 -0
  194. package/dist/src/mcp/bridge.js.map +1 -0
  195. package/dist/src/mcp/http-transport.d.ts +32 -0
  196. package/dist/src/mcp/http-transport.d.ts.map +1 -0
  197. package/dist/src/mcp/http-transport.js +538 -0
  198. package/dist/src/mcp/http-transport.js.map +1 -0
  199. package/dist/src/mcp/index.d.ts +10 -0
  200. package/dist/src/mcp/index.d.ts.map +1 -0
  201. package/dist/src/mcp/index.js +17 -0
  202. package/dist/src/mcp/index.js.map +1 -0
  203. package/dist/src/mcp/oauth-pages.d.ts +23 -0
  204. package/dist/src/mcp/oauth-pages.d.ts.map +1 -0
  205. package/dist/src/mcp/oauth-pages.js +121 -0
  206. package/dist/src/mcp/oauth-pages.js.map +1 -0
  207. package/dist/src/mcp/oauth-postgres-stores.d.ts +55 -0
  208. package/dist/src/mcp/oauth-postgres-stores.d.ts.map +1 -0
  209. package/dist/src/mcp/oauth-postgres-stores.js +226 -0
  210. package/dist/src/mcp/oauth-postgres-stores.js.map +1 -0
  211. package/dist/src/mcp/oauth-provider.d.ts +95 -0
  212. package/dist/src/mcp/oauth-provider.d.ts.map +1 -0
  213. package/dist/src/mcp/oauth-provider.js +360 -0
  214. package/dist/src/mcp/oauth-provider.js.map +1 -0
  215. package/dist/src/mcp/oauth-stores.d.ts +62 -0
  216. package/dist/src/mcp/oauth-stores.d.ts.map +1 -0
  217. package/dist/src/mcp/oauth-stores.js +154 -0
  218. package/dist/src/mcp/oauth-stores.js.map +1 -0
  219. package/dist/src/mcp/server.d.ts +18 -0
  220. package/dist/src/mcp/server.d.ts.map +1 -0
  221. package/dist/src/mcp/server.js +51 -0
  222. package/dist/src/mcp/server.js.map +1 -0
  223. package/dist/src/metrics/collector.d.ts +106 -0
  224. package/dist/src/metrics/collector.d.ts.map +1 -0
  225. package/dist/src/metrics/collector.js +311 -0
  226. package/dist/src/metrics/collector.js.map +1 -0
  227. package/dist/src/metrics/index.d.ts +2 -0
  228. package/dist/src/metrics/index.d.ts.map +1 -0
  229. package/dist/src/metrics/index.js +6 -0
  230. package/dist/src/metrics/index.js.map +1 -0
  231. package/dist/src/middleware/auth.d.ts +77 -0
  232. package/dist/src/middleware/auth.d.ts.map +1 -0
  233. package/dist/src/middleware/auth.js +720 -0
  234. package/dist/src/middleware/auth.js.map +1 -0
  235. package/dist/src/middleware/session.d.ts +18 -0
  236. package/dist/src/middleware/session.d.ts.map +1 -0
  237. package/dist/src/middleware/session.js +67 -0
  238. package/dist/src/middleware/session.js.map +1 -0
  239. package/dist/src/middleware/validate.d.ts +3 -0
  240. package/dist/src/middleware/validate.d.ts.map +1 -0
  241. package/dist/src/middleware/validate.js +85 -0
  242. package/dist/src/middleware/validate.js.map +1 -0
  243. package/dist/src/policy/engine.d.ts +107 -0
  244. package/dist/src/policy/engine.d.ts.map +1 -0
  245. package/dist/src/policy/engine.js +646 -0
  246. package/dist/src/policy/engine.js.map +1 -0
  247. package/dist/src/policy/index.d.ts +3 -0
  248. package/dist/src/policy/index.d.ts.map +1 -0
  249. package/dist/src/policy/index.js +8 -0
  250. package/dist/src/policy/index.js.map +1 -0
  251. package/dist/src/policy/opa-engine.d.ts +176 -0
  252. package/dist/src/policy/opa-engine.d.ts.map +1 -0
  253. package/dist/src/policy/opa-engine.js +790 -0
  254. package/dist/src/policy/opa-engine.js.map +1 -0
  255. package/dist/src/proxy/forward-proxy.d.ts +30 -0
  256. package/dist/src/proxy/forward-proxy.d.ts.map +1 -0
  257. package/dist/src/proxy/forward-proxy.js +580 -0
  258. package/dist/src/proxy/forward-proxy.js.map +1 -0
  259. package/dist/src/proxy/index.d.ts +2 -0
  260. package/dist/src/proxy/index.d.ts.map +1 -0
  261. package/dist/src/proxy/index.js +8 -0
  262. package/dist/src/proxy/index.js.map +1 -0
  263. package/dist/src/ratelimit/limiter.d.ts +45 -0
  264. package/dist/src/ratelimit/limiter.d.ts.map +1 -0
  265. package/dist/src/ratelimit/limiter.js +158 -0
  266. package/dist/src/ratelimit/limiter.js.map +1 -0
  267. package/dist/src/replay/engine.d.ts +40 -0
  268. package/dist/src/replay/engine.d.ts.map +1 -0
  269. package/dist/src/replay/engine.js +106 -0
  270. package/dist/src/replay/engine.js.map +1 -0
  271. package/dist/src/replay/index.d.ts +2 -0
  272. package/dist/src/replay/index.d.ts.map +1 -0
  273. package/dist/src/replay/index.js +6 -0
  274. package/dist/src/replay/index.js.map +1 -0
  275. package/dist/src/saas/index.d.ts +2 -0
  276. package/dist/src/saas/index.d.ts.map +1 -0
  277. package/dist/src/saas/index.js +18 -0
  278. package/dist/src/saas/index.js.map +1 -0
  279. package/dist/src/saas/routes.d.ts +18 -0
  280. package/dist/src/saas/routes.d.ts.map +1 -0
  281. package/dist/src/saas/routes.js +1566 -0
  282. package/dist/src/saas/routes.js.map +1 -0
  283. package/dist/src/server/app.d.ts +44 -0
  284. package/dist/src/server/app.d.ts.map +1 -0
  285. package/dist/src/server/app.js +854 -0
  286. package/dist/src/server/app.js.map +1 -0
  287. package/dist/src/server/errors.d.ts +32 -0
  288. package/dist/src/server/errors.d.ts.map +1 -0
  289. package/dist/src/server/errors.js +39 -0
  290. package/dist/src/server/errors.js.map +1 -0
  291. package/dist/src/server/gateway.d.ts +165 -0
  292. package/dist/src/server/gateway.d.ts.map +1 -0
  293. package/dist/src/server/gateway.js +964 -0
  294. package/dist/src/server/gateway.js.map +1 -0
  295. package/dist/src/server/index.d.ts +2 -0
  296. package/dist/src/server/index.d.ts.map +1 -0
  297. package/dist/src/server/index.js +295 -0
  298. package/dist/src/server/index.js.map +1 -0
  299. package/dist/src/server/logger.d.ts +33 -0
  300. package/dist/src/server/logger.d.ts.map +1 -0
  301. package/dist/src/server/logger.js +230 -0
  302. package/dist/src/server/logger.js.map +1 -0
  303. package/dist/src/server/stream-proxy.d.ts +32 -0
  304. package/dist/src/server/stream-proxy.d.ts.map +1 -0
  305. package/dist/src/server/stream-proxy.js +184 -0
  306. package/dist/src/server/stream-proxy.js.map +1 -0
  307. package/dist/src/storage/file-persistence.d.ts +48 -0
  308. package/dist/src/storage/file-persistence.d.ts.map +1 -0
  309. package/dist/src/storage/file-persistence.js +280 -0
  310. package/dist/src/storage/file-persistence.js.map +1 -0
  311. package/dist/src/storage/index.d.ts +5 -0
  312. package/dist/src/storage/index.d.ts.map +1 -0
  313. package/dist/src/storage/index.js +21 -0
  314. package/dist/src/storage/index.js.map +1 -0
  315. package/dist/src/storage/interfaces.d.ts +237 -0
  316. package/dist/src/storage/interfaces.d.ts.map +1 -0
  317. package/dist/src/storage/interfaces.js +3 -0
  318. package/dist/src/storage/interfaces.js.map +1 -0
  319. package/dist/src/storage/memory.d.ts +162 -0
  320. package/dist/src/storage/memory.d.ts.map +1 -0
  321. package/dist/src/storage/memory.js +603 -0
  322. package/dist/src/storage/memory.js.map +1 -0
  323. package/dist/src/storage/postgres.d.ts +267 -0
  324. package/dist/src/storage/postgres.d.ts.map +1 -0
  325. package/dist/src/storage/postgres.js +1555 -0
  326. package/dist/src/storage/postgres.js.map +1 -0
  327. package/dist/src/storage/redis.d.ts +202 -0
  328. package/dist/src/storage/redis.d.ts.map +1 -0
  329. package/dist/src/storage/redis.js +629 -0
  330. package/dist/src/storage/redis.js.map +1 -0
  331. package/dist/src/tracing/index.d.ts +2 -0
  332. package/dist/src/tracing/index.d.ts.map +1 -0
  333. package/dist/src/tracing/index.js +6 -0
  334. package/dist/src/tracing/index.js.map +1 -0
  335. package/dist/src/tracing/provider.d.ts +43 -0
  336. package/dist/src/tracing/provider.d.ts.map +1 -0
  337. package/dist/src/tracing/provider.js +74 -0
  338. package/dist/src/tracing/provider.js.map +1 -0
  339. package/dist/src/trust/calculator.d.ts +54 -0
  340. package/dist/src/trust/calculator.d.ts.map +1 -0
  341. package/dist/src/trust/calculator.js +102 -0
  342. package/dist/src/trust/calculator.js.map +1 -0
  343. package/dist/src/trust/index.d.ts +2 -0
  344. package/dist/src/trust/index.d.ts.map +1 -0
  345. package/dist/src/trust/index.js +7 -0
  346. package/dist/src/trust/index.js.map +1 -0
  347. package/dist/src/types/budget.d.ts +30 -0
  348. package/dist/src/types/budget.d.ts.map +1 -0
  349. package/dist/src/types/budget.js +3 -0
  350. package/dist/src/types/budget.js.map +1 -0
  351. package/dist/src/types/config.d.ts +176 -0
  352. package/dist/src/types/config.d.ts.map +1 -0
  353. package/dist/src/types/config.js +3 -0
  354. package/dist/src/types/config.js.map +1 -0
  355. package/dist/src/types/events.d.ts +24 -0
  356. package/dist/src/types/events.d.ts.map +1 -0
  357. package/dist/src/types/events.js +3 -0
  358. package/dist/src/types/events.js.map +1 -0
  359. package/dist/src/types/index.d.ts +8 -0
  360. package/dist/src/types/index.d.ts.map +1 -0
  361. package/dist/src/types/index.js +24 -0
  362. package/dist/src/types/index.js.map +1 -0
  363. package/dist/src/types/policy.d.ts +60 -0
  364. package/dist/src/types/policy.d.ts.map +1 -0
  365. package/dist/src/types/policy.js +3 -0
  366. package/dist/src/types/policy.js.map +1 -0
  367. package/dist/src/types/stripe-config.d.ts +12 -0
  368. package/dist/src/types/stripe-config.d.ts.map +1 -0
  369. package/dist/src/types/stripe-config.js +3 -0
  370. package/dist/src/types/stripe-config.js.map +1 -0
  371. package/dist/src/types/subscription.d.ts +24 -0
  372. package/dist/src/types/subscription.d.ts.map +1 -0
  373. package/dist/src/types/subscription.js +38 -0
  374. package/dist/src/types/subscription.js.map +1 -0
  375. package/dist/src/types/tool-call.d.ts +42 -0
  376. package/dist/src/types/tool-call.d.ts.map +1 -0
  377. package/dist/src/types/tool-call.js +3 -0
  378. package/dist/src/types/tool-call.js.map +1 -0
  379. package/dist/src/types/tool-result.d.ts +58 -0
  380. package/dist/src/types/tool-result.d.ts.map +1 -0
  381. package/dist/src/types/tool-result.js +3 -0
  382. package/dist/src/types/tool-result.js.map +1 -0
  383. package/dist/src/types/user.d.ts +101 -0
  384. package/dist/src/types/user.d.ts.map +1 -0
  385. package/dist/src/types/user.js +6 -0
  386. package/dist/src/types/user.js.map +1 -0
  387. package/dist/tests/integration/api.test.d.ts +2 -0
  388. package/dist/tests/integration/api.test.d.ts.map +1 -0
  389. package/dist/tests/integration/api.test.js +1199 -0
  390. package/dist/tests/integration/api.test.js.map +1 -0
  391. package/dist/tests/integration/proxy.test.d.ts +2 -0
  392. package/dist/tests/integration/proxy.test.d.ts.map +1 -0
  393. package/dist/tests/integration/proxy.test.js +251 -0
  394. package/dist/tests/integration/proxy.test.js.map +1 -0
  395. package/dist/tests/integration/storage.test.d.ts +16 -0
  396. package/dist/tests/integration/storage.test.d.ts.map +1 -0
  397. package/dist/tests/integration/storage.test.js +826 -0
  398. package/dist/tests/integration/storage.test.js.map +1 -0
  399. package/dist/tests/unit/admin.test.d.ts +2 -0
  400. package/dist/tests/unit/admin.test.d.ts.map +1 -0
  401. package/dist/tests/unit/admin.test.js +698 -0
  402. package/dist/tests/unit/admin.test.js.map +1 -0
  403. package/dist/tests/unit/anomaly-detector.test.d.ts +2 -0
  404. package/dist/tests/unit/anomaly-detector.test.d.ts.map +1 -0
  405. package/dist/tests/unit/anomaly-detector.test.js +903 -0
  406. package/dist/tests/unit/anomaly-detector.test.js.map +1 -0
  407. package/dist/tests/unit/approval-manager.test.d.ts +2 -0
  408. package/dist/tests/unit/approval-manager.test.d.ts.map +1 -0
  409. package/dist/tests/unit/approval-manager.test.js +528 -0
  410. package/dist/tests/unit/approval-manager.test.js.map +1 -0
  411. package/dist/tests/unit/approval-webhook.test.d.ts +2 -0
  412. package/dist/tests/unit/approval-webhook.test.d.ts.map +1 -0
  413. package/dist/tests/unit/approval-webhook.test.js +355 -0
  414. package/dist/tests/unit/approval-webhook.test.js.map +1 -0
  415. package/dist/tests/unit/audit-logger.test.d.ts +2 -0
  416. package/dist/tests/unit/audit-logger.test.d.ts.map +1 -0
  417. package/dist/tests/unit/audit-logger.test.js +635 -0
  418. package/dist/tests/unit/audit-logger.test.js.map +1 -0
  419. package/dist/tests/unit/auth-routes.test.d.ts +2 -0
  420. package/dist/tests/unit/auth-routes.test.d.ts.map +1 -0
  421. package/dist/tests/unit/auth-routes.test.js +281 -0
  422. package/dist/tests/unit/auth-routes.test.js.map +1 -0
  423. package/dist/tests/unit/auth.test.d.ts +2 -0
  424. package/dist/tests/unit/auth.test.d.ts.map +1 -0
  425. package/dist/tests/unit/auth.test.js +1382 -0
  426. package/dist/tests/unit/auth.test.js.map +1 -0
  427. package/dist/tests/unit/billing.test.d.ts +2 -0
  428. package/dist/tests/unit/billing.test.d.ts.map +1 -0
  429. package/dist/tests/unit/billing.test.js +579 -0
  430. package/dist/tests/unit/billing.test.js.map +1 -0
  431. package/dist/tests/unit/budget-manager.test.d.ts +2 -0
  432. package/dist/tests/unit/budget-manager.test.d.ts.map +1 -0
  433. package/dist/tests/unit/budget-manager.test.js +778 -0
  434. package/dist/tests/unit/budget-manager.test.js.map +1 -0
  435. package/dist/tests/unit/budget-race.test.d.ts +2 -0
  436. package/dist/tests/unit/budget-race.test.d.ts.map +1 -0
  437. package/dist/tests/unit/budget-race.test.js +58 -0
  438. package/dist/tests/unit/budget-race.test.js.map +1 -0
  439. package/dist/tests/unit/cli.test.d.ts +2 -0
  440. package/dist/tests/unit/cli.test.d.ts.map +1 -0
  441. package/dist/tests/unit/cli.test.js +93 -0
  442. package/dist/tests/unit/cli.test.js.map +1 -0
  443. package/dist/tests/unit/concurrency.test.d.ts +2 -0
  444. package/dist/tests/unit/concurrency.test.d.ts.map +1 -0
  445. package/dist/tests/unit/concurrency.test.js +1270 -0
  446. package/dist/tests/unit/concurrency.test.js.map +1 -0
  447. package/dist/tests/unit/config-validate.test.d.ts +2 -0
  448. package/dist/tests/unit/config-validate.test.d.ts.map +1 -0
  449. package/dist/tests/unit/config-validate.test.js +230 -0
  450. package/dist/tests/unit/config-validate.test.js.map +1 -0
  451. package/dist/tests/unit/defaults.test.d.ts +2 -0
  452. package/dist/tests/unit/defaults.test.d.ts.map +1 -0
  453. package/dist/tests/unit/defaults.test.js +364 -0
  454. package/dist/tests/unit/defaults.test.js.map +1 -0
  455. package/dist/tests/unit/dlp-backends.test.d.ts +2 -0
  456. package/dist/tests/unit/dlp-backends.test.d.ts.map +1 -0
  457. package/dist/tests/unit/dlp-backends.test.js +563 -0
  458. package/dist/tests/unit/dlp-backends.test.js.map +1 -0
  459. package/dist/tests/unit/dlp-scanner.test.d.ts +2 -0
  460. package/dist/tests/unit/dlp-scanner.test.d.ts.map +1 -0
  461. package/dist/tests/unit/dlp-scanner.test.js +739 -0
  462. package/dist/tests/unit/dlp-scanner.test.js.map +1 -0
  463. package/dist/tests/unit/error-responses.test.d.ts +2 -0
  464. package/dist/tests/unit/error-responses.test.d.ts.map +1 -0
  465. package/dist/tests/unit/error-responses.test.js +101 -0
  466. package/dist/tests/unit/error-responses.test.js.map +1 -0
  467. package/dist/tests/unit/executor-registry.test.d.ts +2 -0
  468. package/dist/tests/unit/executor-registry.test.d.ts.map +1 -0
  469. package/dist/tests/unit/executor-registry.test.js +390 -0
  470. package/dist/tests/unit/executor-registry.test.js.map +1 -0
  471. package/dist/tests/unit/forward-proxy.test.d.ts +2 -0
  472. package/dist/tests/unit/forward-proxy.test.d.ts.map +1 -0
  473. package/dist/tests/unit/forward-proxy.test.js +621 -0
  474. package/dist/tests/unit/forward-proxy.test.js.map +1 -0
  475. package/dist/tests/unit/gateway-features.test.d.ts +2 -0
  476. package/dist/tests/unit/gateway-features.test.d.ts.map +1 -0
  477. package/dist/tests/unit/gateway-features.test.js +753 -0
  478. package/dist/tests/unit/gateway-features.test.js.map +1 -0
  479. package/dist/tests/unit/http-executor.test.d.ts +2 -0
  480. package/dist/tests/unit/http-executor.test.d.ts.map +1 -0
  481. package/dist/tests/unit/http-executor.test.js +310 -0
  482. package/dist/tests/unit/http-executor.test.js.map +1 -0
  483. package/dist/tests/unit/mcp-bridge.test.d.ts +2 -0
  484. package/dist/tests/unit/mcp-bridge.test.d.ts.map +1 -0
  485. package/dist/tests/unit/mcp-bridge.test.js +1136 -0
  486. package/dist/tests/unit/mcp-bridge.test.js.map +1 -0
  487. package/dist/tests/unit/mcp-http-transport.test.d.ts +2 -0
  488. package/dist/tests/unit/mcp-http-transport.test.d.ts.map +1 -0
  489. package/dist/tests/unit/mcp-http-transport.test.js +899 -0
  490. package/dist/tests/unit/mcp-http-transport.test.js.map +1 -0
  491. package/dist/tests/unit/mcp-oauth.test.d.ts +2 -0
  492. package/dist/tests/unit/mcp-oauth.test.d.ts.map +1 -0
  493. package/dist/tests/unit/mcp-oauth.test.js +759 -0
  494. package/dist/tests/unit/mcp-oauth.test.js.map +1 -0
  495. package/dist/tests/unit/mcp-server.test.d.ts +15 -0
  496. package/dist/tests/unit/mcp-server.test.d.ts.map +1 -0
  497. package/dist/tests/unit/mcp-server.test.js +158 -0
  498. package/dist/tests/unit/mcp-server.test.js.map +1 -0
  499. package/dist/tests/unit/metrics.test.d.ts +2 -0
  500. package/dist/tests/unit/metrics.test.d.ts.map +1 -0
  501. package/dist/tests/unit/metrics.test.js +208 -0
  502. package/dist/tests/unit/metrics.test.js.map +1 -0
  503. package/dist/tests/unit/oauth.test.d.ts +2 -0
  504. package/dist/tests/unit/oauth.test.d.ts.map +1 -0
  505. package/dist/tests/unit/oauth.test.js +281 -0
  506. package/dist/tests/unit/oauth.test.js.map +1 -0
  507. package/dist/tests/unit/opa-circuit-breaker.test.d.ts +2 -0
  508. package/dist/tests/unit/opa-circuit-breaker.test.d.ts.map +1 -0
  509. package/dist/tests/unit/opa-circuit-breaker.test.js +297 -0
  510. package/dist/tests/unit/opa-circuit-breaker.test.js.map +1 -0
  511. package/dist/tests/unit/opa-engine.test.d.ts +2 -0
  512. package/dist/tests/unit/opa-engine.test.d.ts.map +1 -0
  513. package/dist/tests/unit/opa-engine.test.js +1813 -0
  514. package/dist/tests/unit/opa-engine.test.js.map +1 -0
  515. package/dist/tests/unit/pipeline-timing.test.d.ts +2 -0
  516. package/dist/tests/unit/pipeline-timing.test.d.ts.map +1 -0
  517. package/dist/tests/unit/pipeline-timing.test.js +528 -0
  518. package/dist/tests/unit/pipeline-timing.test.js.map +1 -0
  519. package/dist/tests/unit/policy-engine.test.d.ts +2 -0
  520. package/dist/tests/unit/policy-engine.test.d.ts.map +1 -0
  521. package/dist/tests/unit/policy-engine.test.js +1345 -0
  522. package/dist/tests/unit/policy-engine.test.js.map +1 -0
  523. package/dist/tests/unit/policy-store.test.d.ts +2 -0
  524. package/dist/tests/unit/policy-store.test.d.ts.map +1 -0
  525. package/dist/tests/unit/policy-store.test.js +60 -0
  526. package/dist/tests/unit/policy-store.test.js.map +1 -0
  527. package/dist/tests/unit/postgres-storage.test.d.ts +2 -0
  528. package/dist/tests/unit/postgres-storage.test.d.ts.map +1 -0
  529. package/dist/tests/unit/postgres-storage.test.js +614 -0
  530. package/dist/tests/unit/postgres-storage.test.js.map +1 -0
  531. package/dist/tests/unit/prompt-injection-backend.test.d.ts +2 -0
  532. package/dist/tests/unit/prompt-injection-backend.test.d.ts.map +1 -0
  533. package/dist/tests/unit/prompt-injection-backend.test.js +621 -0
  534. package/dist/tests/unit/prompt-injection-backend.test.js.map +1 -0
  535. package/dist/tests/unit/proxy-hardening.test.d.ts +2 -0
  536. package/dist/tests/unit/proxy-hardening.test.d.ts.map +1 -0
  537. package/dist/tests/unit/proxy-hardening.test.js +166 -0
  538. package/dist/tests/unit/proxy-hardening.test.js.map +1 -0
  539. package/dist/tests/unit/rate-limiter.test.d.ts +2 -0
  540. package/dist/tests/unit/rate-limiter.test.d.ts.map +1 -0
  541. package/dist/tests/unit/rate-limiter.test.js +443 -0
  542. package/dist/tests/unit/rate-limiter.test.js.map +1 -0
  543. package/dist/tests/unit/redis-storage.test.d.ts +2 -0
  544. package/dist/tests/unit/redis-storage.test.d.ts.map +1 -0
  545. package/dist/tests/unit/redis-storage.test.js +766 -0
  546. package/dist/tests/unit/redis-storage.test.js.map +1 -0
  547. package/dist/tests/unit/replay-engine.test.d.ts +2 -0
  548. package/dist/tests/unit/replay-engine.test.d.ts.map +1 -0
  549. package/dist/tests/unit/replay-engine.test.js +371 -0
  550. package/dist/tests/unit/replay-engine.test.js.map +1 -0
  551. package/dist/tests/unit/saas-routes.test.d.ts +2 -0
  552. package/dist/tests/unit/saas-routes.test.d.ts.map +1 -0
  553. package/dist/tests/unit/saas-routes.test.js +1399 -0
  554. package/dist/tests/unit/saas-routes.test.js.map +1 -0
  555. package/dist/tests/unit/session.test.d.ts +2 -0
  556. package/dist/tests/unit/session.test.d.ts.map +1 -0
  557. package/dist/tests/unit/session.test.js +532 -0
  558. package/dist/tests/unit/session.test.js.map +1 -0
  559. package/dist/tests/unit/slack-executor.test.d.ts +2 -0
  560. package/dist/tests/unit/slack-executor.test.d.ts.map +1 -0
  561. package/dist/tests/unit/slack-executor.test.js +209 -0
  562. package/dist/tests/unit/slack-executor.test.js.map +1 -0
  563. package/dist/tests/unit/storage-hardening.test.d.ts +2 -0
  564. package/dist/tests/unit/storage-hardening.test.d.ts.map +1 -0
  565. package/dist/tests/unit/storage-hardening.test.js +165 -0
  566. package/dist/tests/unit/storage-hardening.test.js.map +1 -0
  567. package/dist/tests/unit/storage.test.d.ts +2 -0
  568. package/dist/tests/unit/storage.test.d.ts.map +1 -0
  569. package/dist/tests/unit/storage.test.js +698 -0
  570. package/dist/tests/unit/storage.test.js.map +1 -0
  571. package/dist/tests/unit/text-normalizer.test.d.ts +2 -0
  572. package/dist/tests/unit/text-normalizer.test.d.ts.map +1 -0
  573. package/dist/tests/unit/text-normalizer.test.js +229 -0
  574. package/dist/tests/unit/text-normalizer.test.js.map +1 -0
  575. package/dist/tests/unit/tracing.test.d.ts +2 -0
  576. package/dist/tests/unit/tracing.test.d.ts.map +1 -0
  577. package/dist/tests/unit/tracing.test.js +611 -0
  578. package/dist/tests/unit/tracing.test.js.map +1 -0
  579. package/dist/tests/unit/trust-calculator.test.d.ts +2 -0
  580. package/dist/tests/unit/trust-calculator.test.d.ts.map +1 -0
  581. package/dist/tests/unit/trust-calculator.test.js +497 -0
  582. package/dist/tests/unit/trust-calculator.test.js.map +1 -0
  583. package/dist/tests/unit/ts-sdk.test.d.ts +2 -0
  584. package/dist/tests/unit/ts-sdk.test.d.ts.map +1 -0
  585. package/dist/tests/unit/ts-sdk.test.js +421 -0
  586. package/dist/tests/unit/ts-sdk.test.js.map +1 -0
  587. package/dist/tests/unit/usage-extractor-llm.test.d.ts +2 -0
  588. package/dist/tests/unit/usage-extractor-llm.test.d.ts.map +1 -0
  589. package/dist/tests/unit/usage-extractor-llm.test.js +139 -0
  590. package/dist/tests/unit/usage-extractor-llm.test.js.map +1 -0
  591. package/dist/tests/unit/usage-extractor.test.d.ts +2 -0
  592. package/dist/tests/unit/usage-extractor.test.d.ts.map +1 -0
  593. package/dist/tests/unit/usage-extractor.test.js +271 -0
  594. package/dist/tests/unit/usage-extractor.test.js.map +1 -0
  595. package/dist/tests/unit/user-stores.test.d.ts +2 -0
  596. package/dist/tests/unit/user-stores.test.d.ts.map +1 -0
  597. package/dist/tests/unit/user-stores.test.js +687 -0
  598. package/dist/tests/unit/user-stores.test.js.map +1 -0
  599. package/dist/tests/unit/validate.test.d.ts +2 -0
  600. package/dist/tests/unit/validate.test.d.ts.map +1 -0
  601. package/dist/tests/unit/validate.test.js +545 -0
  602. package/dist/tests/unit/validate.test.js.map +1 -0
  603. package/package.json +86 -0
  604. package/policy-packs/README.md +42 -0
  605. package/policy-packs/default.yaml +46 -0
  606. package/policy-packs/dev_fast.yaml +54 -0
  607. package/policy-packs/prod_strict.yaml +83 -0
@@ -0,0 +1,547 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BudgetManager = void 0;
4
+ /** Small epsilon for floating-point comparison tolerance in budget checks */
5
+ const BUDGET_EPSILON = 1e-10;
6
+ // Default cost table for different tool types (used when no config override provided)
7
+ const DEFAULT_COST_TABLE = {
8
+ 'http.request': 0.001,
9
+ 'http.request.write': 0.002,
10
+ 'slack.post': 0.003,
11
+ 'git.operation': 0.002,
12
+ 'db.query': 0.005,
13
+ 'browser.navigate': 0.01,
14
+ 'default': 0.001,
15
+ };
16
+ class BudgetManager {
17
+ constructor(config, store) {
18
+ this.config = {
19
+ task_budget_usd: config.task_budget_usd ?? 2.0,
20
+ user_daily_budget_usd: config.user_daily_budget_usd,
21
+ user_monthly_budget_usd: config.user_monthly_budget_usd,
22
+ workspace_daily_budget_usd: config.workspace_daily_budget_usd,
23
+ workspace_monthly_budget_usd: config.workspace_monthly_budget_usd,
24
+ max_steps_per_task: config.max_steps_per_task ?? 50,
25
+ max_retries_per_call: config.max_retries_per_call ?? 3,
26
+ max_wall_clock_ms: config.max_wall_clock_ms ?? 300000,
27
+ token_pricing: config.token_pricing,
28
+ };
29
+ // Merge config.cost_table over defaults
30
+ this.costTable = { ...DEFAULT_COST_TABLE, ...(config.cost_table || {}) };
31
+ this.externalStore = store;
32
+ this.taskStates = new Map();
33
+ this.userDailySpend = new Map();
34
+ this.userMonthlySpend = new Map();
35
+ this.workspaceDailySpend = new Map();
36
+ this.workspaceMonthlySpend = new Map();
37
+ this.callRetryCounts = new Map();
38
+ this.costRecords = new Map();
39
+ this.reservations = new Map();
40
+ this.budgetLocks = new Map();
41
+ }
42
+ // Estimate cost for a tool call
43
+ estimateCost(toolCall) {
44
+ const toolName = toolCall.tool.name;
45
+ const capability = toolCall.tool.capability;
46
+ // First try an exact match with capability suffix for write/delete operations
47
+ let cost;
48
+ if (capability === 'write' || capability === 'delete' || capability === 'admin') {
49
+ cost = this.costTable[`${toolName}.${capability}`] ?? this.costTable[`${toolName}.write`];
50
+ }
51
+ // Fall back to base tool name, then default
52
+ if (cost === undefined) {
53
+ cost = this.costTable[toolName] ?? this.costTable['default'];
54
+ }
55
+ // Apply a multiplier for destructive or elevated operations
56
+ if (capability === 'delete') {
57
+ cost *= 1.5;
58
+ }
59
+ else if (capability === 'admin') {
60
+ cost *= 2.0;
61
+ }
62
+ // Respect per-call max_cost_usd constraint as an upper bound on the estimate
63
+ if (toolCall.constraints?.max_cost_usd !== undefined && toolCall.constraints.max_cost_usd > 0) {
64
+ cost = Math.min(cost, toolCall.constraints.max_cost_usd);
65
+ }
66
+ return {
67
+ tool_name: toolName,
68
+ capability,
69
+ estimated_cost_usd: cost,
70
+ };
71
+ }
72
+ // Check if a tool call is within budget (returns allowed: false with reason if not)
73
+ // Optional overrides allow per-workspace budget configuration.
74
+ check(toolCall, overrides) {
75
+ const cfg = overrides ? { ...this.config, ...overrides } : this.config;
76
+ const state = this.getTaskState(toolCall);
77
+ const estimate = this.estimateCost(toolCall);
78
+ const estimatedCost = estimate.estimated_cost_usd;
79
+ // Check task budget
80
+ const taskBudget = cfg.task_budget_usd;
81
+ if (state.spent_usd + estimatedCost > taskBudget + BUDGET_EPSILON) {
82
+ return {
83
+ allowed: false,
84
+ reason: `Task budget exceeded: spent $${state.spent_usd.toFixed(4)} + estimated $${estimatedCost.toFixed(4)} > limit $${taskBudget.toFixed(4)}`,
85
+ report: this.getReport(toolCall, estimatedCost, cfg),
86
+ };
87
+ }
88
+ // Check step limit
89
+ const maxSteps = cfg.max_steps_per_task;
90
+ if (state.steps + 1 > maxSteps) {
91
+ return {
92
+ allowed: false,
93
+ reason: `Step limit exceeded: ${state.steps} steps taken, max is ${maxSteps}`,
94
+ report: this.getReport(toolCall, estimatedCost, cfg),
95
+ };
96
+ }
97
+ // Check wall clock time
98
+ const maxWallClock = cfg.max_wall_clock_ms;
99
+ const elapsed = Date.now() - new Date(state.started_at).getTime();
100
+ if (elapsed > maxWallClock) {
101
+ return {
102
+ allowed: false,
103
+ reason: `Wall clock time exceeded: ${elapsed}ms elapsed, max is ${maxWallClock}ms`,
104
+ report: this.getReport(toolCall, estimatedCost, cfg),
105
+ };
106
+ }
107
+ // Check user daily budget
108
+ const userDailyLimit = cfg.user_daily_budget_usd;
109
+ if (userDailyLimit !== undefined) {
110
+ const dailyKey = `${state.actor_id}:${this.getDateKey()}`;
111
+ const dailySpend = this.userDailySpend.get(dailyKey) ?? 0;
112
+ if (dailySpend + estimatedCost > userDailyLimit + BUDGET_EPSILON) {
113
+ return {
114
+ allowed: false,
115
+ reason: `User daily budget exceeded: spent $${dailySpend.toFixed(4)} + estimated $${estimatedCost.toFixed(4)} > limit $${userDailyLimit.toFixed(4)}`,
116
+ report: this.getReport(toolCall, estimatedCost, cfg),
117
+ };
118
+ }
119
+ }
120
+ // Check user monthly budget
121
+ const userMonthlyLimit = cfg.user_monthly_budget_usd;
122
+ if (userMonthlyLimit !== undefined) {
123
+ const monthlyKey = `${state.actor_id}:${this.getMonthKey()}`;
124
+ const monthlySpend = this.userMonthlySpend.get(monthlyKey) ?? 0;
125
+ if (monthlySpend + estimatedCost > userMonthlyLimit + BUDGET_EPSILON) {
126
+ return {
127
+ allowed: false,
128
+ reason: `User monthly budget exceeded: spent $${monthlySpend.toFixed(4)} + estimated $${estimatedCost.toFixed(4)} > limit $${userMonthlyLimit.toFixed(4)}`,
129
+ report: this.getReport(toolCall, estimatedCost, cfg),
130
+ };
131
+ }
132
+ }
133
+ // Check workspace daily budget
134
+ const wsDailyLimit = cfg.workspace_daily_budget_usd;
135
+ if (wsDailyLimit !== undefined) {
136
+ const dailyKey = `${state.workspace_id}:${this.getDateKey()}`;
137
+ const dailySpend = this.workspaceDailySpend.get(dailyKey) ?? 0;
138
+ if (dailySpend + estimatedCost > wsDailyLimit + BUDGET_EPSILON) {
139
+ return {
140
+ allowed: false,
141
+ reason: `Workspace daily budget exceeded: spent $${dailySpend.toFixed(4)} + estimated $${estimatedCost.toFixed(4)} > limit $${wsDailyLimit.toFixed(4)}`,
142
+ report: this.getReport(toolCall, estimatedCost, cfg),
143
+ };
144
+ }
145
+ }
146
+ // Check workspace monthly budget
147
+ const wsMonthlyLimit = cfg.workspace_monthly_budget_usd;
148
+ if (wsMonthlyLimit !== undefined) {
149
+ const monthlyKey = `${state.workspace_id}:${this.getMonthKey()}`;
150
+ const monthlySpend = this.workspaceMonthlySpend.get(monthlyKey) ?? 0;
151
+ if (monthlySpend + estimatedCost > wsMonthlyLimit + BUDGET_EPSILON) {
152
+ return {
153
+ allowed: false,
154
+ reason: `Workspace monthly budget exceeded: spent $${monthlySpend.toFixed(4)} + estimated $${estimatedCost.toFixed(4)} > limit $${wsMonthlyLimit.toFixed(4)}`,
155
+ report: this.getReport(toolCall, estimatedCost, cfg),
156
+ };
157
+ }
158
+ }
159
+ return {
160
+ allowed: true,
161
+ report: this.getReport(toolCall, estimatedCost, cfg),
162
+ };
163
+ }
164
+ /**
165
+ * Atomically check budget and reserve the estimated cost.
166
+ * The reservation temporarily reduces available budget so concurrent
167
+ * requests cannot both pass the check.
168
+ * Uses a per-task lock to serialize concurrent reservations for the same budget key.
169
+ */
170
+ async reserveAndCheck(toolCall, overrides) {
171
+ // Serialize concurrent reservations for the same task to prevent interleaving
172
+ const lockKey = toolCall.task_id;
173
+ const existingLock = this.budgetLocks.get(lockKey) ?? Promise.resolve();
174
+ let releaseLock;
175
+ const newLock = new Promise(resolve => { releaseLock = resolve; });
176
+ this.budgetLocks.set(lockKey, existingLock.then(() => newLock));
177
+ // Wait for prior reservation with timeout to prevent deadlocks
178
+ const LOCK_TIMEOUT_MS = 5000;
179
+ let timeoutHandle;
180
+ try {
181
+ await Promise.race([
182
+ existingLock,
183
+ new Promise((_, reject) => {
184
+ timeoutHandle = setTimeout(() => reject(new Error('Budget lock timeout')), LOCK_TIMEOUT_MS);
185
+ }),
186
+ ]);
187
+ }
188
+ catch {
189
+ // On timeout, proceed without lock (accept brief race risk vs permanent hang)
190
+ console.warn(`[budget] Lock timeout for task ${lockKey}, proceeding without lock`);
191
+ }
192
+ finally {
193
+ if (timeoutHandle !== undefined)
194
+ clearTimeout(timeoutHandle);
195
+ }
196
+ try {
197
+ return this._reserveAndCheckSync(toolCall, overrides);
198
+ }
199
+ finally {
200
+ releaseLock();
201
+ // Clean up lock if no one else is waiting
202
+ if (this.budgetLocks.get(lockKey) === existingLock.then(() => newLock)) {
203
+ this.budgetLocks.delete(lockKey);
204
+ }
205
+ }
206
+ }
207
+ /**
208
+ * Synchronous check-and-reserve. Must be called while holding the per-task lock.
209
+ */
210
+ _reserveAndCheckSync(toolCall, overrides) {
211
+ const result = this.check(toolCall, overrides);
212
+ if (!result.allowed) {
213
+ return result;
214
+ }
215
+ // Reserve the estimated cost
216
+ const estimate = this.estimateCost(toolCall);
217
+ const reservationKey = `reservation:${toolCall.tool_call_id}`;
218
+ // Temporarily record the estimated cost so concurrent requests see it
219
+ const state = this.getTaskState(toolCall);
220
+ state.spent_usd += estimate.estimated_cost_usd;
221
+ this.taskStates.set(toolCall.task_id, state);
222
+ // Also reserve against user/workspace counters
223
+ const dateKey = this.getDateKey();
224
+ const monthKey = this.getMonthKey();
225
+ const userDailyKey = `${state.actor_id}:${dateKey}`;
226
+ this.userDailySpend.set(userDailyKey, (this.userDailySpend.get(userDailyKey) ?? 0) + estimate.estimated_cost_usd);
227
+ const userMonthlyKey = `${state.actor_id}:${monthKey}`;
228
+ this.userMonthlySpend.set(userMonthlyKey, (this.userMonthlySpend.get(userMonthlyKey) ?? 0) + estimate.estimated_cost_usd);
229
+ const workspaceDailyKey = `${state.workspace_id}:${dateKey}`;
230
+ this.workspaceDailySpend.set(workspaceDailyKey, (this.workspaceDailySpend.get(workspaceDailyKey) ?? 0) + estimate.estimated_cost_usd);
231
+ const workspaceMonthlyKey = `${state.workspace_id}:${monthKey}`;
232
+ this.workspaceMonthlySpend.set(workspaceMonthlyKey, (this.workspaceMonthlySpend.get(workspaceMonthlyKey) ?? 0) + estimate.estimated_cost_usd);
233
+ // Track the reservation so we can commit or release later
234
+ this.reservations.set(reservationKey, {
235
+ amount: estimate.estimated_cost_usd,
236
+ taskId: toolCall.task_id,
237
+ workspaceId: state.workspace_id,
238
+ actorId: state.actor_id,
239
+ createdAt: Date.now(),
240
+ });
241
+ return { ...result, reservationKey };
242
+ }
243
+ /**
244
+ * Commit a reservation with the actual cost. Releases the difference
245
+ * between the reserved (estimated) amount and the actual cost.
246
+ */
247
+ commitReservation(reservationKey, actualCost) {
248
+ const reservation = this.reservations.get(reservationKey);
249
+ if (!reservation)
250
+ return;
251
+ const diff = reservation.amount - actualCost;
252
+ if (diff !== 0) {
253
+ // Adjust task state: remove reserved amount, add actual
254
+ const state = this.taskStates.get(reservation.taskId);
255
+ if (state) {
256
+ state.spent_usd -= diff;
257
+ this.taskStates.set(reservation.taskId, state);
258
+ }
259
+ // Adjust user/workspace counters
260
+ const dateKey = this.getDateKey();
261
+ const monthKey = this.getMonthKey();
262
+ const userDailyKey = `${reservation.actorId}:${dateKey}`;
263
+ const curUD = this.userDailySpend.get(userDailyKey);
264
+ if (curUD !== undefined)
265
+ this.userDailySpend.set(userDailyKey, curUD - diff);
266
+ const userMonthlyKey = `${reservation.actorId}:${monthKey}`;
267
+ const curUM = this.userMonthlySpend.get(userMonthlyKey);
268
+ if (curUM !== undefined)
269
+ this.userMonthlySpend.set(userMonthlyKey, curUM - diff);
270
+ const workspaceDailyKey = `${reservation.workspaceId}:${dateKey}`;
271
+ const curWD = this.workspaceDailySpend.get(workspaceDailyKey);
272
+ if (curWD !== undefined)
273
+ this.workspaceDailySpend.set(workspaceDailyKey, curWD - diff);
274
+ const workspaceMonthlyKey = `${reservation.workspaceId}:${monthKey}`;
275
+ const curWM = this.workspaceMonthlySpend.get(workspaceMonthlyKey);
276
+ if (curWM !== undefined)
277
+ this.workspaceMonthlySpend.set(workspaceMonthlyKey, curWM - diff);
278
+ }
279
+ this.reservations.delete(reservationKey);
280
+ }
281
+ /**
282
+ * Release a reservation entirely (e.g. on error before execution completes).
283
+ * Removes the reserved amount from all counters.
284
+ */
285
+ releaseReservation(reservationKey) {
286
+ const reservation = this.reservations.get(reservationKey);
287
+ if (!reservation)
288
+ return;
289
+ // Remove the reserved amount from task state
290
+ const state = this.taskStates.get(reservation.taskId);
291
+ if (state) {
292
+ state.spent_usd -= reservation.amount;
293
+ state.steps = Math.max(0, state.steps - 0); // steps not incremented during reserve
294
+ this.taskStates.set(reservation.taskId, state);
295
+ }
296
+ // Remove from user/workspace counters
297
+ const dateKey = this.getDateKey();
298
+ const monthKey = this.getMonthKey();
299
+ const userDailyKey = `${reservation.actorId}:${dateKey}`;
300
+ const curUD = this.userDailySpend.get(userDailyKey);
301
+ if (curUD !== undefined)
302
+ this.userDailySpend.set(userDailyKey, curUD - reservation.amount);
303
+ const userMonthlyKey = `${reservation.actorId}:${monthKey}`;
304
+ const curUM = this.userMonthlySpend.get(userMonthlyKey);
305
+ if (curUM !== undefined)
306
+ this.userMonthlySpend.set(userMonthlyKey, curUM - reservation.amount);
307
+ const workspaceDailyKey = `${reservation.workspaceId}:${dateKey}`;
308
+ const curWD = this.workspaceDailySpend.get(workspaceDailyKey);
309
+ if (curWD !== undefined)
310
+ this.workspaceDailySpend.set(workspaceDailyKey, curWD - reservation.amount);
311
+ const workspaceMonthlyKey = `${reservation.workspaceId}:${monthKey}`;
312
+ const curWM = this.workspaceMonthlySpend.get(workspaceMonthlyKey);
313
+ if (curWM !== undefined)
314
+ this.workspaceMonthlySpend.set(workspaceMonthlyKey, curWM - reservation.amount);
315
+ this.reservations.delete(reservationKey);
316
+ }
317
+ /**
318
+ * Record a completed tool call's cost.
319
+ * When skipCostIncrement is true (e.g. because a reservation already accounts
320
+ * for the cost), only step counting and cost record storage are performed.
321
+ */
322
+ record(toolCall, costOrRecord, skipCostIncrement = false) {
323
+ const actualCostUsd = typeof costOrRecord === 'number' ? costOrRecord : (costOrRecord.actual_cost_usd ?? costOrRecord.estimated_cost_usd);
324
+ // Store cost record for later retrieval
325
+ if (typeof costOrRecord !== 'number') {
326
+ this.costRecords.set(toolCall.tool_call_id, costOrRecord);
327
+ }
328
+ const state = this.getTaskState(toolCall);
329
+ if (!skipCostIncrement) {
330
+ // Update task state cost
331
+ state.spent_usd += actualCostUsd;
332
+ }
333
+ // Always increment steps
334
+ state.steps += 1;
335
+ this.taskStates.set(toolCall.task_id, state);
336
+ // Persist to external store if available
337
+ if (this.externalStore) {
338
+ this.externalStore.setTaskState(toolCall.task_id, state);
339
+ }
340
+ if (!skipCostIncrement) {
341
+ // Update user daily spend
342
+ const dateKey = this.getDateKey();
343
+ const monthKey = this.getMonthKey();
344
+ const userDailyKey = `${state.actor_id}:${dateKey}`;
345
+ const currentUserDaily = this.userDailySpend.get(userDailyKey) ?? 0;
346
+ this.userDailySpend.set(userDailyKey, currentUserDaily + actualCostUsd);
347
+ // Update user monthly spend
348
+ const userMonthlyKey = `${state.actor_id}:${monthKey}`;
349
+ const currentUserMonthly = this.userMonthlySpend.get(userMonthlyKey) ?? 0;
350
+ this.userMonthlySpend.set(userMonthlyKey, currentUserMonthly + actualCostUsd);
351
+ // Update workspace daily spend
352
+ const workspaceDailyKey = `${state.workspace_id}:${dateKey}`;
353
+ const currentWorkspaceDaily = this.workspaceDailySpend.get(workspaceDailyKey) ?? 0;
354
+ this.workspaceDailySpend.set(workspaceDailyKey, currentWorkspaceDaily + actualCostUsd);
355
+ // Update workspace monthly spend
356
+ const workspaceMonthlyKey = `${state.workspace_id}:${monthKey}`;
357
+ const currentWorkspaceMonthly = this.workspaceMonthlySpend.get(workspaceMonthlyKey) ?? 0;
358
+ this.workspaceMonthlySpend.set(workspaceMonthlyKey, currentWorkspaceMonthly + actualCostUsd);
359
+ // Persist counter updates to external store
360
+ if (this.externalStore) {
361
+ this.externalStore.incrementCounter(userDailyKey, actualCostUsd);
362
+ this.externalStore.incrementCounter(userMonthlyKey, actualCostUsd);
363
+ this.externalStore.incrementCounter(workspaceDailyKey, actualCostUsd);
364
+ this.externalStore.incrementCounter(workspaceMonthlyKey, actualCostUsd);
365
+ }
366
+ // Prune stale counter keys (from prior days/months) to prevent unbounded growth
367
+ this.pruneStaleCounters(dateKey, monthKey);
368
+ }
369
+ }
370
+ /** Remove counter entries whose date/month suffix doesn't match today */
371
+ pruneStaleCounters(currentDateKey, currentMonthKey) {
372
+ // Only prune periodically (when maps get large)
373
+ if (this.userDailySpend.size <= 100)
374
+ return;
375
+ for (const key of this.userDailySpend.keys()) {
376
+ if (!key.endsWith(`:${currentDateKey}`)) {
377
+ this.userDailySpend.delete(key);
378
+ }
379
+ }
380
+ for (const key of this.workspaceDailySpend.keys()) {
381
+ if (!key.endsWith(`:${currentDateKey}`)) {
382
+ this.workspaceDailySpend.delete(key);
383
+ }
384
+ }
385
+ for (const key of this.userMonthlySpend.keys()) {
386
+ if (!key.endsWith(`:${currentMonthKey}`)) {
387
+ this.userMonthlySpend.delete(key);
388
+ }
389
+ }
390
+ for (const key of this.workspaceMonthlySpend.keys()) {
391
+ if (!key.endsWith(`:${currentMonthKey}`)) {
392
+ this.workspaceMonthlySpend.delete(key);
393
+ }
394
+ }
395
+ }
396
+ // Record a retry for a tool call
397
+ recordRetry(toolCallId) {
398
+ const currentCount = this.callRetryCounts.get(toolCallId) ?? 0;
399
+ const newCount = currentCount + 1;
400
+ this.callRetryCounts.set(toolCallId, newCount);
401
+ if (this.externalStore) {
402
+ this.externalStore.incrementRetryCount(toolCallId);
403
+ }
404
+ const maxRetries = this.config.max_retries_per_call;
405
+ return {
406
+ allowed: newCount <= maxRetries,
407
+ count: newCount,
408
+ };
409
+ }
410
+ // Get budget report for a tool call
411
+ getReport(toolCall, estimatedCost, effectiveConfig) {
412
+ const cfg = effectiveConfig ?? this.config;
413
+ const state = this.getTaskState(toolCall);
414
+ return {
415
+ estimated_cost_usd: estimatedCost,
416
+ spent_cost_usd_task: state.spent_usd,
417
+ remaining_cost_usd_task: Math.max(0, (cfg.task_budget_usd ?? 2.0) - state.spent_usd),
418
+ };
419
+ }
420
+ // Get budget report with actual cost and usage data
421
+ getReportWithActual(toolCall, estimatedCost, actualCostUsd, usage) {
422
+ const base = this.getReport(toolCall, estimatedCost);
423
+ return {
424
+ ...base,
425
+ actual_cost_usd: actualCostUsd,
426
+ usage,
427
+ };
428
+ }
429
+ // Get the active cost table (for introspection)
430
+ getCostTable() {
431
+ return { ...this.costTable };
432
+ }
433
+ // Get the cost record for a specific tool call
434
+ getCostRecord(toolCallId) {
435
+ return this.costRecords.get(toolCallId);
436
+ }
437
+ // Get or create task state
438
+ getTaskState(toolCall) {
439
+ const existing = this.taskStates.get(toolCall.task_id);
440
+ if (existing) {
441
+ return existing;
442
+ }
443
+ const newState = {
444
+ task_id: toolCall.task_id,
445
+ workspace_id: toolCall.workspace_id,
446
+ actor_id: toolCall.actor.id,
447
+ spent_usd: 0,
448
+ steps: 0,
449
+ started_at: toolCall.timestamp ?? new Date().toISOString(),
450
+ };
451
+ this.taskStates.set(toolCall.task_id, newState);
452
+ return newState;
453
+ }
454
+ // Helper to get date key (YYYY-MM-DD)
455
+ getDateKey() {
456
+ return new Date().toISOString().split('T')[0];
457
+ }
458
+ // Helper to get month key (YYYY-MM)
459
+ getMonthKey() {
460
+ return new Date().toISOString().slice(0, 7);
461
+ }
462
+ // Reset task state (for testing)
463
+ async flush() {
464
+ if (this.externalStore?.flush)
465
+ await this.externalStore.flush();
466
+ }
467
+ reset() {
468
+ this.taskStates.clear();
469
+ this.userDailySpend.clear();
470
+ this.userMonthlySpend.clear();
471
+ this.workspaceDailySpend.clear();
472
+ this.workspaceMonthlySpend.clear();
473
+ this.callRetryCounts.clear();
474
+ this.costRecords.clear();
475
+ this.reservations.clear();
476
+ this.budgetLocks.clear();
477
+ this.externalStore?.reset();
478
+ }
479
+ /** Get spending data for a specific actor */
480
+ getActorSpending(actorId, days) {
481
+ const dateKey = this.getDateKey();
482
+ const monthKey = this.getMonthKey();
483
+ // Daily spend for today
484
+ const dailyKey = `${actorId}:${dateKey}`;
485
+ const dailySpend = this.userDailySpend.get(dailyKey) ?? 0;
486
+ // Monthly spend for current month
487
+ const monthlyKey = `${actorId}:${monthKey}`;
488
+ const monthlySpend = this.userMonthlySpend.get(monthlyKey) ?? 0;
489
+ // Count tasks where this actor is involved
490
+ let taskCount = 0;
491
+ let totalSpend = 0;
492
+ for (const state of this.taskStates.values()) {
493
+ if (state.actor_id === actorId) {
494
+ taskCount++;
495
+ totalSpend += state.spent_usd;
496
+ }
497
+ }
498
+ return {
499
+ daily_spend: dailySpend,
500
+ monthly_spend: monthlySpend,
501
+ task_count: taskCount,
502
+ total_spend: totalSpend,
503
+ };
504
+ }
505
+ // Get current config
506
+ getConfig() {
507
+ return { ...this.config };
508
+ }
509
+ /** Return aggregate spending across all tracked entities */
510
+ getSpendingSummary() {
511
+ let taskTotal = 0;
512
+ for (const state of this.taskStates.values()) {
513
+ taskTotal += state.spent_usd;
514
+ }
515
+ const dateKey = this.getDateKey();
516
+ const monthKey = this.getMonthKey();
517
+ let userDailyTotal = 0;
518
+ for (const [key, val] of this.userDailySpend) {
519
+ if (key.endsWith(`:${dateKey}`))
520
+ userDailyTotal += val;
521
+ }
522
+ let userMonthlyTotal = 0;
523
+ for (const [key, val] of this.userMonthlySpend) {
524
+ if (key.endsWith(`:${monthKey}`))
525
+ userMonthlyTotal += val;
526
+ }
527
+ let workspaceDailyTotal = 0;
528
+ for (const [key, val] of this.workspaceDailySpend) {
529
+ if (key.endsWith(`:${dateKey}`))
530
+ workspaceDailyTotal += val;
531
+ }
532
+ let workspaceMonthlyTotal = 0;
533
+ for (const [key, val] of this.workspaceMonthlySpend) {
534
+ if (key.endsWith(`:${monthKey}`))
535
+ workspaceMonthlyTotal += val;
536
+ }
537
+ return {
538
+ task_total: taskTotal,
539
+ user_daily_total: userDailyTotal,
540
+ user_monthly_total: userMonthlyTotal,
541
+ workspace_daily_total: workspaceDailyTotal,
542
+ workspace_monthly_total: workspaceMonthlyTotal,
543
+ };
544
+ }
545
+ }
546
+ exports.BudgetManager = BudgetManager;
547
+ //# sourceMappingURL=manager.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"manager.js","sourceRoot":"","sources":["../../../src/budget/manager.ts"],"names":[],"mappings":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B;YACpC,qBAAqB,EAAE,mBAAmB;YAC1C,uBAAuB,EAAE,qBAAqB;SAC/C,CAAC;IACJ,CAAC;CACF;AAhnBD,sCAgnBC"}
@@ -0,0 +1,38 @@
1
+ import { UsageData } from '../types/tool-result';
2
+ import { TokenPricing } from '../types/budget';
3
+ export declare class UsageExtractor {
4
+ private tokenPricing;
5
+ constructor(tokenPricing?: Record<string, TokenPricing>);
6
+ /**
7
+ * Extract usage data from HTTP response headers.
8
+ * Supports Anthropic, OpenAI-style, and generic header conventions.
9
+ */
10
+ extractFromHeaders(headers?: Record<string, string>): UsageData | undefined;
11
+ /**
12
+ * Extract usage data from response body.
13
+ * Supports OpenAI-style `usage` object and Anthropic-style `usage` object.
14
+ */
15
+ extractFromBody(body: unknown): UsageData | undefined;
16
+ /**
17
+ * Compute cost from usage data using token pricing config.
18
+ * Returns the computed cost in USD, or undefined if pricing not available.
19
+ */
20
+ computeCost(usage: UsageData, model?: string): number | undefined;
21
+ /**
22
+ * Extract model name from response body.
23
+ * Supports OpenAI format (body.model) and Anthropic format (body.model).
24
+ */
25
+ extractModelFromBody(body: unknown): string | undefined;
26
+ /**
27
+ * Detect provider from model name.
28
+ */
29
+ detectProvider(model: string): string;
30
+ /**
31
+ * Merge two UsageData objects. The second (body) takes precedence for conflicting fields,
32
+ * except provider_cost_usd which is preferred from headers.
33
+ */
34
+ merge(fromHeaders?: UsageData, fromBody?: UsageData): UsageData | undefined;
35
+ private parseIntHeader;
36
+ private parseFloatHeader;
37
+ }
38
+ //# sourceMappingURL=usage-extractor.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"usage-extractor.d.ts","sourceRoot":"","sources":["../../../src/budget/usage-extractor.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AACjD,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAE/C,qBAAa,cAAc;IACzB,OAAO,CAAC,YAAY,CAA+B;gBAEvC,YAAY,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,YAAY,CAAC;IAIvD;;;OAGG;IACH,kBAAkB,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,SAAS,GAAG,SAAS;IAkC3E;;;OAGG;IACH,eAAe,CAAC,IAAI,EAAE,OAAO,GAAG,SAAS,GAAG,SAAS;IAoCrD;;;OAGG;IACH,WAAW,CAAC,KAAK,EAAE,SAAS,EAAE,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS;IAgBjE;;;OAGG;IACH,oBAAoB,CAAC,IAAI,EAAE,OAAO,GAAG,MAAM,GAAG,SAAS;IAOvD;;OAEG;IACH,cAAc,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM;IAUrC;;;OAGG;IACH,KAAK,CAAC,WAAW,CAAC,EAAE,SAAS,EAAE,QAAQ,CAAC,EAAE,SAAS,GAAG,SAAS,GAAG,SAAS;IAmB3E,OAAO,CAAC,cAAc;IAMtB,OAAO,CAAC,gBAAgB;CAKzB"}