@retinue/agentkit 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 (405) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +310 -0
  3. package/dist/adapters/bullmq/consumer.d.ts +33 -0
  4. package/dist/adapters/bullmq/consumer.js +41 -0
  5. package/dist/adapters/bullmq/dispatcher.d.ts +74 -0
  6. package/dist/adapters/bullmq/dispatcher.js +160 -0
  7. package/dist/adapters/bullmq/export.d.ts +31 -0
  8. package/dist/adapters/bullmq/export.js +53 -0
  9. package/dist/adapters/bullmq/extraction.d.ts +42 -0
  10. package/dist/adapters/bullmq/extraction.js +63 -0
  11. package/dist/adapters/bullmq/index.d.ts +13 -0
  12. package/dist/adapters/bullmq/index.js +13 -0
  13. package/dist/adapters/bullmq/lock.d.ts +77 -0
  14. package/dist/adapters/bullmq/lock.js +126 -0
  15. package/dist/adapters/bullmq/queue.d.ts +50 -0
  16. package/dist/adapters/bullmq/queue.js +81 -0
  17. package/dist/adapters/memory/artifact-exports.d.ts +11 -0
  18. package/dist/adapters/memory/artifact-exports.js +102 -0
  19. package/dist/adapters/memory/artifacts.d.ts +15 -0
  20. package/dist/adapters/memory/artifacts.js +134 -0
  21. package/dist/adapters/memory/blobs.d.ts +7 -0
  22. package/dist/adapters/memory/blobs.js +27 -0
  23. package/dist/adapters/memory/evaluation.d.ts +18 -0
  24. package/dist/adapters/memory/evaluation.js +148 -0
  25. package/dist/adapters/memory/files.d.ts +27 -0
  26. package/dist/adapters/memory/files.js +0 -0
  27. package/dist/adapters/memory/flows.d.ts +16 -0
  28. package/dist/adapters/memory/flows.js +117 -0
  29. package/dist/adapters/memory/hitl.d.ts +9 -0
  30. package/dist/adapters/memory/hitl.js +130 -0
  31. package/dist/adapters/memory/idempotency.d.ts +13 -0
  32. package/dist/adapters/memory/idempotency.js +32 -0
  33. package/dist/adapters/memory/index.d.ts +39 -0
  34. package/dist/adapters/memory/index.js +107 -0
  35. package/dist/adapters/memory/knowledge.d.ts +43 -0
  36. package/dist/adapters/memory/knowledge.js +248 -0
  37. package/dist/adapters/memory/mcp.d.ts +9 -0
  38. package/dist/adapters/memory/mcp.js +37 -0
  39. package/dist/adapters/memory/message-store.d.ts +17 -0
  40. package/dist/adapters/memory/message-store.js +70 -0
  41. package/dist/adapters/memory/principal-memory.d.ts +7 -0
  42. package/dist/adapters/memory/principal-memory.js +83 -0
  43. package/dist/adapters/memory/runtime.d.ts +29 -0
  44. package/dist/adapters/memory/runtime.js +0 -0
  45. package/dist/adapters/memory/sessions.d.ts +29 -0
  46. package/dist/adapters/memory/sessions.js +0 -0
  47. package/dist/adapters/memory/skills.d.ts +10 -0
  48. package/dist/adapters/memory/skills.js +41 -0
  49. package/dist/adapters/memory/thread-summary.d.ts +7 -0
  50. package/dist/adapters/memory/thread-summary.js +29 -0
  51. package/dist/adapters/memory/usage-limits.d.ts +13 -0
  52. package/dist/adapters/memory/usage-limits.js +72 -0
  53. package/dist/adapters/memory/usage.d.ts +16 -0
  54. package/dist/adapters/memory/usage.js +279 -0
  55. package/dist/adapters/otel/index.d.ts +111 -0
  56. package/dist/adapters/otel/index.js +133 -0
  57. package/dist/adapters/postgres/artifact-exports.d.ts +12 -0
  58. package/dist/adapters/postgres/artifact-exports.js +117 -0
  59. package/dist/adapters/postgres/artifacts.d.ts +16 -0
  60. package/dist/adapters/postgres/artifacts.js +172 -0
  61. package/dist/adapters/postgres/checkpoint-store.d.ts +16 -0
  62. package/dist/adapters/postgres/checkpoint-store.js +34 -0
  63. package/dist/adapters/postgres/config.d.ts +15 -0
  64. package/dist/adapters/postgres/config.js +187 -0
  65. package/dist/adapters/postgres/conversation-store.d.ts +4 -0
  66. package/dist/adapters/postgres/conversation-store.js +82 -0
  67. package/dist/adapters/postgres/evaluation.d.ts +17 -0
  68. package/dist/adapters/postgres/evaluation.js +193 -0
  69. package/dist/adapters/postgres/file-content.d.ts +30 -0
  70. package/dist/adapters/postgres/file-content.js +111 -0
  71. package/dist/adapters/postgres/files.d.ts +19 -0
  72. package/dist/adapters/postgres/files.js +209 -0
  73. package/dist/adapters/postgres/flows.d.ts +20 -0
  74. package/dist/adapters/postgres/flows.js +206 -0
  75. package/dist/adapters/postgres/hitl.d.ts +5 -0
  76. package/dist/adapters/postgres/hitl.js +247 -0
  77. package/dist/adapters/postgres/index.d.ts +35 -0
  78. package/dist/adapters/postgres/index.js +35 -0
  79. package/dist/adapters/postgres/knowledge.d.ts +48 -0
  80. package/dist/adapters/postgres/knowledge.js +255 -0
  81. package/dist/adapters/postgres/memory.d.ts +14 -0
  82. package/dist/adapters/postgres/memory.js +194 -0
  83. package/dist/adapters/postgres/message-store.d.ts +11 -0
  84. package/dist/adapters/postgres/message-store.js +145 -0
  85. package/dist/adapters/postgres/migrations.d.ts +69 -0
  86. package/dist/adapters/postgres/migrations.js +1594 -0
  87. package/dist/adapters/postgres/pg-executor.d.ts +19 -0
  88. package/dist/adapters/postgres/pg-executor.js +32 -0
  89. package/dist/adapters/postgres/retention.d.ts +26 -0
  90. package/dist/adapters/postgres/retention.js +59 -0
  91. package/dist/adapters/postgres/rollups.d.ts +17 -0
  92. package/dist/adapters/postgres/rollups.js +267 -0
  93. package/dist/adapters/postgres/run-coordinator.d.ts +5 -0
  94. package/dist/adapters/postgres/run-coordinator.js +98 -0
  95. package/dist/adapters/postgres/run-event-log.d.ts +26 -0
  96. package/dist/adapters/postgres/run-event-log.js +30 -0
  97. package/dist/adapters/postgres/run-store.d.ts +4 -0
  98. package/dist/adapters/postgres/run-store.js +199 -0
  99. package/dist/adapters/postgres/schema.d.ts +39 -0
  100. package/dist/adapters/postgres/schema.js +70 -0
  101. package/dist/adapters/postgres/session-state.d.ts +7 -0
  102. package/dist/adapters/postgres/session-state.js +99 -0
  103. package/dist/adapters/postgres/sql.d.ts +8 -0
  104. package/dist/adapters/postgres/sql.js +2 -0
  105. package/dist/adapters/postgres/transaction.d.ts +37 -0
  106. package/dist/adapters/postgres/transaction.js +93 -0
  107. package/dist/adapters/postgres/unit-of-work.d.ts +18 -0
  108. package/dist/adapters/postgres/unit-of-work.js +8 -0
  109. package/dist/adapters/postgres/usage-limits.d.ts +15 -0
  110. package/dist/adapters/postgres/usage-limits.js +136 -0
  111. package/dist/adapters/postgres/usage.d.ts +15 -0
  112. package/dist/adapters/postgres/usage.js +226 -0
  113. package/dist/adapters/redis/index.d.ts +9 -0
  114. package/dist/adapters/redis/index.js +9 -0
  115. package/dist/adapters/redis/realtime.d.ts +74 -0
  116. package/dist/adapters/redis/realtime.js +112 -0
  117. package/dist/adapters/supabase/index.d.ts +88 -0
  118. package/dist/adapters/supabase/index.js +84 -0
  119. package/dist/adapters/supabase/realtime.d.ts +30 -0
  120. package/dist/adapters/supabase/realtime.js +53 -0
  121. package/dist/adapters/supabase/rls.d.ts +99 -0
  122. package/dist/adapters/supabase/rls.js +216 -0
  123. package/dist/adapters/supabase/storage.d.ts +50 -0
  124. package/dist/adapters/supabase/storage.js +207 -0
  125. package/dist/agents/agent.d.ts +66 -0
  126. package/dist/agents/agent.js +209 -0
  127. package/dist/agents/define.d.ts +21 -0
  128. package/dist/agents/define.js +63 -0
  129. package/dist/agents/engine.d.ts +98 -0
  130. package/dist/agents/engine.js +462 -0
  131. package/dist/agents/index.d.ts +50 -0
  132. package/dist/agents/index.js +17 -0
  133. package/dist/artifacts/index.d.ts +114 -0
  134. package/dist/artifacts/index.js +219 -0
  135. package/dist/authorization/index.d.ts +76 -0
  136. package/dist/authorization/index.js +76 -0
  137. package/dist/capabilities/index.d.ts +120 -0
  138. package/dist/capabilities/index.js +167 -0
  139. package/dist/capabilities/runtime.d.ts +89 -0
  140. package/dist/capabilities/runtime.js +84 -0
  141. package/dist/citations/index.d.ts +161 -0
  142. package/dist/citations/index.js +182 -0
  143. package/dist/context/assembler.d.ts +82 -0
  144. package/dist/context/assembler.js +129 -0
  145. package/dist/context/compaction.d.ts +45 -0
  146. package/dist/context/compaction.js +55 -0
  147. package/dist/context/index.d.ts +75 -0
  148. package/dist/context/index.js +17 -0
  149. package/dist/core/content-parts.d.ts +194 -0
  150. package/dist/core/content-parts.js +23 -0
  151. package/dist/core/context.d.ts +51 -0
  152. package/dist/core/context.js +9 -0
  153. package/dist/core/env.d.ts +25 -0
  154. package/dist/core/env.js +41 -0
  155. package/dist/core/errors.d.ts +30 -0
  156. package/dist/core/errors.js +65 -0
  157. package/dist/core/events.d.ts +139 -0
  158. package/dist/core/events.js +99 -0
  159. package/dist/core/ids.d.ts +52 -0
  160. package/dist/core/ids.js +13 -0
  161. package/dist/core/index.d.ts +9 -0
  162. package/dist/core/index.js +9 -0
  163. package/dist/core/tokens.d.ts +22 -0
  164. package/dist/core/tokens.js +22 -0
  165. package/dist/core/validation.d.ts +34 -0
  166. package/dist/core/validation.js +176 -0
  167. package/dist/documents/extraction.d.ts +121 -0
  168. package/dist/documents/extraction.js +293 -0
  169. package/dist/documents/index.d.ts +199 -0
  170. package/dist/documents/index.js +65 -0
  171. package/dist/documents/parsers/pdf.d.ts +47 -0
  172. package/dist/documents/parsers/pdf.js +508 -0
  173. package/dist/documents/parsers/text.d.ts +59 -0
  174. package/dist/documents/parsers/text.js +325 -0
  175. package/dist/documents/read-tool.d.ts +52 -0
  176. package/dist/documents/read-tool.js +109 -0
  177. package/dist/documents/render.d.ts +29 -0
  178. package/dist/documents/render.js +59 -0
  179. package/dist/documents/vision.d.ts +159 -0
  180. package/dist/documents/vision.js +214 -0
  181. package/dist/entries/adapters-bullmq.d.ts +8 -0
  182. package/dist/entries/adapters-bullmq.js +8 -0
  183. package/dist/entries/adapters-otel.d.ts +13 -0
  184. package/dist/entries/adapters-otel.js +13 -0
  185. package/dist/entries/adapters-postgres.d.ts +10 -0
  186. package/dist/entries/adapters-postgres.js +10 -0
  187. package/dist/entries/adapters-redis.d.ts +3 -0
  188. package/dist/entries/adapters-redis.js +3 -0
  189. package/dist/entries/context.d.ts +20 -0
  190. package/dist/entries/context.js +20 -0
  191. package/dist/entries/flows.d.ts +15 -0
  192. package/dist/entries/flows.js +15 -0
  193. package/dist/entries/hitl.d.ts +10 -0
  194. package/dist/entries/hitl.js +10 -0
  195. package/dist/entries/knowledge.d.ts +18 -0
  196. package/dist/entries/knowledge.js +19 -0
  197. package/dist/entries/mcp.d.ts +10 -0
  198. package/dist/entries/mcp.js +10 -0
  199. package/dist/entries/observability.d.ts +14 -0
  200. package/dist/entries/observability.js +16 -0
  201. package/dist/entries/persistence.d.ts +11 -0
  202. package/dist/entries/persistence.js +11 -0
  203. package/dist/entries/providers.d.ts +14 -0
  204. package/dist/entries/providers.js +14 -0
  205. package/dist/entries/runtime.d.ts +13 -0
  206. package/dist/entries/runtime.js +13 -0
  207. package/dist/entries/server.d.ts +24 -0
  208. package/dist/entries/server.js +24 -0
  209. package/dist/entries/tools.d.ts +21 -0
  210. package/dist/entries/tools.js +21 -0
  211. package/dist/entries/usage.d.ts +10 -0
  212. package/dist/entries/usage.js +10 -0
  213. package/dist/evaluation/gate.d.ts +168 -0
  214. package/dist/evaluation/gate.js +180 -0
  215. package/dist/evaluation/graders.d.ts +125 -0
  216. package/dist/evaluation/graders.js +203 -0
  217. package/dist/evaluation/index.d.ts +120 -0
  218. package/dist/evaluation/index.js +183 -0
  219. package/dist/evaluation/judge.d.ts +75 -0
  220. package/dist/evaluation/judge.js +111 -0
  221. package/dist/export/index.d.ts +162 -0
  222. package/dist/export/index.js +363 -0
  223. package/dist/export/markdown.d.ts +19 -0
  224. package/dist/export/markdown.js +29 -0
  225. package/dist/export/pdf.d.ts +73 -0
  226. package/dist/export/pdf.js +407 -0
  227. package/dist/files/context.d.ts +97 -0
  228. package/dist/files/context.js +185 -0
  229. package/dist/files/index.d.ts +210 -0
  230. package/dist/files/index.js +338 -0
  231. package/dist/files/read-tool.d.ts +81 -0
  232. package/dist/files/read-tool.js +163 -0
  233. package/dist/files/turn-parts.d.ts +96 -0
  234. package/dist/files/turn-parts.js +171 -0
  235. package/dist/flows/index.d.ts +270 -0
  236. package/dist/flows/index.js +62 -0
  237. package/dist/flows/interpreter.d.ts +146 -0
  238. package/dist/flows/interpreter.js +426 -0
  239. package/dist/flows/runner.d.ts +145 -0
  240. package/dist/flows/runner.js +270 -0
  241. package/dist/graphql/index.d.ts +8 -0
  242. package/dist/graphql/index.js +8 -0
  243. package/dist/graphql/resolvers.d.ts +237 -0
  244. package/dist/graphql/resolvers.js +253 -0
  245. package/dist/graphql/schema.d.ts +11 -0
  246. package/dist/graphql/schema.js +258 -0
  247. package/dist/graphql/sse.d.ts +77 -0
  248. package/dist/graphql/sse.js +100 -0
  249. package/dist/hitl/approved-execution.d.ts +127 -0
  250. package/dist/hitl/approved-execution.js +177 -0
  251. package/dist/hitl/index.d.ts +79 -0
  252. package/dist/hitl/index.js +12 -0
  253. package/dist/hitl/service.d.ts +221 -0
  254. package/dist/hitl/service.js +268 -0
  255. package/dist/idempotency/index.d.ts +70 -0
  256. package/dist/idempotency/index.js +59 -0
  257. package/dist/index.d.ts +103 -0
  258. package/dist/index.js +60 -0
  259. package/dist/knowledge/chunking.d.ts +57 -0
  260. package/dist/knowledge/chunking.js +158 -0
  261. package/dist/knowledge/index.d.ts +119 -0
  262. package/dist/knowledge/index.js +166 -0
  263. package/dist/knowledge/retrieval.d.ts +146 -0
  264. package/dist/knowledge/retrieval.js +170 -0
  265. package/dist/loadtest/harness.d.ts +168 -0
  266. package/dist/loadtest/harness.js +507 -0
  267. package/dist/loadtest/index.d.ts +13 -0
  268. package/dist/loadtest/index.js +13 -0
  269. package/dist/loadtest/injection.d.ts +89 -0
  270. package/dist/loadtest/injection.js +147 -0
  271. package/dist/loadtest/metrics.d.ts +197 -0
  272. package/dist/loadtest/metrics.js +160 -0
  273. package/dist/loadtest/runbooks.d.ts +28 -0
  274. package/dist/loadtest/runbooks.js +159 -0
  275. package/dist/loadtest/scenario.d.ts +104 -0
  276. package/dist/loadtest/scenario.js +208 -0
  277. package/dist/mcp/egress.d.ts +53 -0
  278. package/dist/mcp/egress.js +115 -0
  279. package/dist/mcp/index.d.ts +93 -0
  280. package/dist/mcp/index.js +33 -0
  281. package/dist/mcp/provider.d.ts +62 -0
  282. package/dist/mcp/provider.js +0 -0
  283. package/dist/models/index.d.ts +98 -0
  284. package/dist/models/index.js +74 -0
  285. package/dist/models/pricing.d.ts +24 -0
  286. package/dist/models/pricing.js +37 -0
  287. package/dist/models/provider-factory.d.ts +31 -0
  288. package/dist/models/provider-factory.js +67 -0
  289. package/dist/models/streaming.d.ts +145 -0
  290. package/dist/models/streaming.js +272 -0
  291. package/dist/models/vision.d.ts +38 -0
  292. package/dist/models/vision.js +62 -0
  293. package/dist/persistence/index.d.ts +1654 -0
  294. package/dist/persistence/index.js +226 -0
  295. package/dist/principal-memory/index.d.ts +106 -0
  296. package/dist/principal-memory/index.js +89 -0
  297. package/dist/retention/index.d.ts +89 -0
  298. package/dist/retention/index.js +70 -0
  299. package/dist/runtime/checkpoint.d.ts +37 -0
  300. package/dist/runtime/checkpoint.js +22 -0
  301. package/dist/runtime/index.d.ts +118 -0
  302. package/dist/runtime/index.js +69 -0
  303. package/dist/runtime/retry.d.ts +95 -0
  304. package/dist/runtime/retry.js +126 -0
  305. package/dist/runtime/serialization.d.ts +85 -0
  306. package/dist/runtime/serialization.js +95 -0
  307. package/dist/runtime/streaming.d.ts +54 -0
  308. package/dist/runtime/streaming.js +115 -0
  309. package/dist/runtime/worker.d.ts +130 -0
  310. package/dist/runtime/worker.js +405 -0
  311. package/dist/security/checklist.d.ts +53 -0
  312. package/dist/security/checklist.js +204 -0
  313. package/dist/security/findings.d.ts +56 -0
  314. package/dist/security/findings.js +168 -0
  315. package/dist/security/index.d.ts +14 -0
  316. package/dist/security/index.js +14 -0
  317. package/dist/security/prompt-safety.d.ts +100 -0
  318. package/dist/security/prompt-safety.js +133 -0
  319. package/dist/server/boot.d.ts +32 -0
  320. package/dist/server/boot.js +36 -0
  321. package/dist/server/cli-worker.d.ts +37 -0
  322. package/dist/server/cli-worker.js +151 -0
  323. package/dist/server/cli.d.ts +27 -0
  324. package/dist/server/cli.js +74 -0
  325. package/dist/server/config.d.ts +42 -0
  326. package/dist/server/config.js +127 -0
  327. package/dist/server/health.d.ts +59 -0
  328. package/dist/server/health.js +90 -0
  329. package/dist/server/host.d.ts +39 -0
  330. package/dist/server/host.js +124 -0
  331. package/dist/server/index.d.ts +15 -0
  332. package/dist/server/index.js +15 -0
  333. package/dist/server/main.d.ts +16 -0
  334. package/dist/server/main.js +31 -0
  335. package/dist/server/sse-route.d.ts +21 -0
  336. package/dist/server/sse-route.js +282 -0
  337. package/dist/skills/index.d.ts +67 -0
  338. package/dist/skills/index.js +31 -0
  339. package/dist/skills/resolver.d.ts +54 -0
  340. package/dist/skills/resolver.js +121 -0
  341. package/dist/teams/index.d.ts +93 -0
  342. package/dist/teams/index.js +207 -0
  343. package/dist/telemetry/index.d.ts +157 -0
  344. package/dist/telemetry/index.js +71 -0
  345. package/dist/telemetry/instrument.d.ts +108 -0
  346. package/dist/telemetry/instrument.js +232 -0
  347. package/dist/telemetry/log-events.d.ts +17 -0
  348. package/dist/telemetry/log-events.js +58 -0
  349. package/dist/telemetry/metrics.d.ts +123 -0
  350. package/dist/telemetry/metrics.js +135 -0
  351. package/dist/telemetry/noop.d.ts +39 -0
  352. package/dist/telemetry/noop.js +143 -0
  353. package/dist/telemetry/redaction.d.ts +64 -0
  354. package/dist/telemetry/redaction.js +153 -0
  355. package/dist/telemetry/spans.d.ts +56 -0
  356. package/dist/telemetry/spans.js +78 -0
  357. package/dist/telemetry/trace-context.d.ts +55 -0
  358. package/dist/telemetry/trace-context.js +60 -0
  359. package/dist/toolkit/compute.d.ts +53 -0
  360. package/dist/toolkit/compute.js +152 -0
  361. package/dist/toolkit/data.d.ts +98 -0
  362. package/dist/toolkit/data.js +235 -0
  363. package/dist/toolkit/http.d.ts +113 -0
  364. package/dist/toolkit/http.js +205 -0
  365. package/dist/toolkit/index.d.ts +21 -0
  366. package/dist/toolkit/index.js +17 -0
  367. package/dist/toolkit/web.d.ts +107 -0
  368. package/dist/toolkit/web.js +147 -0
  369. package/dist/tools/define.d.ts +25 -0
  370. package/dist/tools/define.js +45 -0
  371. package/dist/tools/delegating.d.ts +132 -0
  372. package/dist/tools/delegating.js +211 -0
  373. package/dist/tools/index.d.ts +129 -0
  374. package/dist/tools/index.js +33 -0
  375. package/dist/tools/library/compute.d.ts +11 -0
  376. package/dist/tools/library/compute.js +46 -0
  377. package/dist/tools/library/data.d.ts +16 -0
  378. package/dist/tools/library/data.js +92 -0
  379. package/dist/tools/library/http.d.ts +28 -0
  380. package/dist/tools/library/http.js +71 -0
  381. package/dist/tools/library/index.d.ts +97 -0
  382. package/dist/tools/library/index.js +134 -0
  383. package/dist/tools/library/knowledge.d.ts +39 -0
  384. package/dist/tools/library/knowledge.js +58 -0
  385. package/dist/tools/library/web.d.ts +19 -0
  386. package/dist/tools/library/web.js +65 -0
  387. package/dist/tools/meta-tools.d.ts +19 -0
  388. package/dist/tools/meta-tools.js +36 -0
  389. package/dist/tools/registry.d.ts +146 -0
  390. package/dist/tools/registry.js +291 -0
  391. package/dist/usage/index.d.ts +105 -0
  392. package/dist/usage/index.js +20 -0
  393. package/dist/usage/quota.d.ts +258 -0
  394. package/dist/usage/quota.js +510 -0
  395. package/dist/usage/recorder.d.ts +29 -0
  396. package/dist/usage/recorder.js +96 -0
  397. package/dist/usage/rollups.d.ts +121 -0
  398. package/dist/usage/rollups.js +157 -0
  399. package/dist/worker/export.d.ts +57 -0
  400. package/dist/worker/export.js +81 -0
  401. package/dist/worker/extraction.d.ts +57 -0
  402. package/dist/worker/extraction.js +84 -0
  403. package/dist/worker/main.d.ts +103 -0
  404. package/dist/worker/main.js +159 -0
  405. package/package.json +187 -0
@@ -0,0 +1,258 @@
1
+ /**
2
+ * Rollup buckets and quota enforcement (#139).
3
+ *
4
+ * Phase 5 gave usage a recording hook and #100 made it durable. Nothing aggregated it and nothing enforced a
5
+ * limit, so one customer's consumption was unbounded.
6
+ *
7
+ * Four decisions carry this module.
8
+ *
9
+ * **Buckets are identified by their start, truncated to the period.** Two writers asking "which bucket does T
10
+ * belong to" must agree, and they do because truncation is a pure function of T rather than a range someone
11
+ * chooses.
12
+ *
13
+ * **The quota check happens at admission, before any provider call.** AC-2's wording is "before work starts",
14
+ * and the reason is what the alternative costs: a limit enforced mid-run leaves a half-written answer, a
15
+ * partial charge, and a user who has to guess whether to retry. Refusing admission is a complete outcome.
16
+ *
17
+ * **The warning fires below the limit, not at it.** A customer told at 100% is told when work is already
18
+ * failing. The threshold is a fraction so it scales with the limit rather than being a constant that is
19
+ * meaningless at one plan size and useless at another.
20
+ *
21
+ * **Enforcement reads a rollup, not the ledger.** Admission is on the hot path of every message; a check that
22
+ * scanned raw events would make the platform slower in exact proportion to how much it had been used.
23
+ */
24
+ import type { ExecutionContext } from "../core/context.js";
25
+ import type { QuotaWindow, RollupPeriod, UsageLimitStore, UsageRollupStore, UsageStore, UsageTotals } from "../persistence/index.js";
26
+ import type { PrincipalId } from "../core/ids.js";
27
+ /** Zero, as a total. Named because "no usage" appears in several places and an object literal invites drift. */
28
+ export declare const NO_USAGE: UsageTotals;
29
+ /**
30
+ * The instant a period's bucket opens, for a given moment.
31
+ *
32
+ * Truncation in UTC, deliberately. A tenant-local day would make a bucket's identity depend on a timezone
33
+ * setting that can change, and a rollup already written under the old offset would silently belong to a
34
+ * different day than one written after — so "yesterday" would double-count an hour or lose one. Presenting
35
+ * totals in local time is a display concern; *storing* them in one is a correctness bug.
36
+ */
37
+ export declare const bucketStartFor: (period: RollupPeriod, at: string) => string;
38
+ /** The bucket after this one. For tiling a range without arithmetic at the call site. */
39
+ export declare const nextBucket: (period: RollupPeriod, bucketStart: string) => string;
40
+ /** Every bucket start covering `[from, to)`, in order. */
41
+ export declare const bucketsBetween: (period: RollupPeriod, from: string, to: string) => readonly string[];
42
+ /**
43
+ * What a tenant may consume in a period.
44
+ *
45
+ * Every field optional, and an omitted field is *unbounded* rather than zero. That direction is deliberate: a
46
+ * misconfigured quota that blocks everything is an outage, and a misconfigured quota that blocks nothing is a
47
+ * bill — and the bill is visible in the rollups this module also provides, whereas the outage is only visible
48
+ * to the customer it is happening to.
49
+ */
50
+ /** How the window reads in a sentence: "your 5,000 spend limit for **the day** / **any 5 hours**". */
51
+ export declare const describeWindow: (window: QuotaWindow) => string;
52
+ export type QuotaLimits = {
53
+ /**
54
+ * The span this allowance covers.
55
+ *
56
+ * Was `period: RollupPeriod` until #181. Widened rather than supplemented, so there is exactly one place a
57
+ * window is described and no combination of fields that means two things at once.
58
+ */
59
+ readonly window: QuotaWindow;
60
+ /**
61
+ * Whose allowance this is — absent means the whole tenant's (#175).
62
+ *
63
+ * Load-bearing, not informational: it decides **which rollup the usage is read from**. A per-person limit
64
+ * checked against the tenant's total is not a per-person limit — the first busy colleague exhausts everyone's
65
+ * allowance, and the person refused has spent nothing. That was the shape of the bug before this existed: the
66
+ * guard already accepted per-principal *limits* through `resolveLimits`, and always compared them against
67
+ * tenant-wide usage.
68
+ */
69
+ readonly principalId?: PrincipalId;
70
+ /**
71
+ * The model this allowance covers, or absent for any model — #182.
72
+ *
73
+ * Load-bearing in the same way `principalId` is: it decides **which records the usage is read from**. A limit
74
+ * on an expensive model checked against all traffic is not a per-model limit — a busy hour on a cheap model
75
+ * exhausts it, and the person refused has not touched the model they are being refused for.
76
+ */
77
+ readonly modelId?: string;
78
+ readonly costMinorUnits?: number;
79
+ readonly inputTokens?: number;
80
+ readonly outputTokens?: number;
81
+ /**
82
+ * Fraction of a limit at which a warning fires. Defaults to 0.8.
83
+ *
84
+ * A fraction rather than an absolute, so it scales with the limit instead of being meaningless on a large
85
+ * plan and constantly tripping on a small one.
86
+ */
87
+ readonly warnAt?: number;
88
+ };
89
+ export declare const DEFAULT_WARN_AT = 0.8;
90
+ /** Which limit was hit. Separate values because the sentence a user reads differs. */
91
+ export declare const QUOTA_DIMENSIONS: readonly ["cost", "input-tokens", "output-tokens"];
92
+ export type QuotaDimension = (typeof QUOTA_DIMENSIONS)[number];
93
+ /**
94
+ * The admission answer.
95
+ *
96
+ * A union, so "refused" has no `allowed` shape to hide in: a caller cannot read a refusal as a permissive
97
+ * default, which for a spend limit is the failure that costs money.
98
+ */
99
+ export type QuotaDecision = {
100
+ readonly admitted: true;
101
+ readonly usage: UsageTotals;
102
+ readonly warnings: readonly QuotaWarning[];
103
+ } | {
104
+ readonly admitted: false;
105
+ readonly dimension: QuotaDimension;
106
+ readonly limit: number;
107
+ readonly used: number;
108
+ /** Present when the limit that refused is scoped to one model — #182. */
109
+ readonly modelId?: string;
110
+ readonly message: string;
111
+ readonly retryAfter: string;
112
+ };
113
+ /**
114
+ * One limit, with what it allows, what has been used, and when that changes — #183.
115
+ *
116
+ * Shaped for rendering: the window as words rather than a union to switch on, the scope as a word rather than an
117
+ * optional id to test for presence, and the fraction computed once here rather than in every client.
118
+ */
119
+ export type QuotaExplanation = {
120
+ readonly window: string;
121
+ readonly modelId?: string;
122
+ readonly scope: "workspace" | "personal";
123
+ readonly resetsAt: string;
124
+ /** The sentence a refusal would use — "It resets at T", or the sliding-window wording. Empty when neither. */
125
+ readonly resetNote: string;
126
+ readonly dimensions: readonly {
127
+ readonly dimension: QuotaDimension;
128
+ readonly limit: number;
129
+ readonly used: number;
130
+ readonly fraction: number;
131
+ }[];
132
+ };
133
+ export type QuotaWarning = {
134
+ readonly dimension: QuotaDimension;
135
+ readonly limit: number;
136
+ readonly used: number;
137
+ readonly fraction: number;
138
+ /** Present when the limit is scoped to one model — #182. */
139
+ readonly modelId?: string;
140
+ readonly message: string;
141
+ };
142
+ /**
143
+ * Told when a tenant crosses a warning threshold.
144
+ *
145
+ * A sink of its own rather than a `RunEvent`, because a `RunEvent` carries a `runId` and a quota warning fires
146
+ * *before* a run exists — which is the whole point of warning at admission. Squeezing it into the run stream
147
+ * would mean inventing a run id for an event about not starting one.
148
+ */
149
+ export interface QuotaObserver {
150
+ onWarning(context: ExecutionContext, warning: QuotaWarning): Promise<void> | void;
151
+ onRefusal?(context: ExecutionContext, refusal: Extract<QuotaDecision, {
152
+ admitted: false;
153
+ }>): Promise<void> | void;
154
+ }
155
+ /**
156
+ * What is being admitted, beyond who is asking — #182.
157
+ *
158
+ * The model belongs here rather than on `ExecutionContext`: the context is who and where, and a per-model limit
159
+ * is about *what this run will use*. Two runs by the same person in the same workspace can be subject to
160
+ * different limits, which is not something an identity can express.
161
+ *
162
+ * Absent `modelId` means model-scoped limits do not apply. That is the safe direction for a *check* — it cannot
163
+ * refuse the wrong work — and the caller that knows the model is the one that must say so.
164
+ */
165
+ export type QuotaSubject = {
166
+ readonly at?: string;
167
+ readonly modelId?: string;
168
+ };
169
+ export type QuotaGuardDeps = {
170
+ readonly rollups: UsageRollupStore;
171
+ /**
172
+ * The limits for this tenant, or undefined for unlimited.
173
+ *
174
+ * A function rather than a value: limits are per tenant and change without a redeploy, and a value captured
175
+ * at construction would be the limits of whoever booted the process.
176
+ */
177
+ /**
178
+ * **Every** limit that applies, shortest span first — widened from a single limit by #182.
179
+ *
180
+ * A list rather than one, because a person is subject to several at once and all of them bind: a five-hour
181
+ * cap, a monthly cap, a per-model cap. Returning the most specific one meant the others were configured,
182
+ * visible and unenforced. An empty list is unbounded.
183
+ */
184
+ readonly resolveLimits: (context: ExecutionContext, about: QuotaSubject) => Promise<readonly QuotaLimits[]> | readonly QuotaLimits[];
185
+ /**
186
+ * The ledger, needed **only** for a rolling window (#181) — rollups are calendar buckets and cannot answer an
187
+ * arbitrary interval.
188
+ *
189
+ * Optional, so a deployment with only calendar limits wires nothing new. A rolling limit configured without it
190
+ * throws at admission with a message naming the missing piece, rather than admitting the run: a spend guard
191
+ * that cannot read spend must not be the thing that says yes.
192
+ */
193
+ readonly usage?: Pick<UsageStore, "totalsBetween">;
194
+ readonly observer?: QuotaObserver;
195
+ readonly clock?: () => string;
196
+ readonly log?: (message: string, detail?: Readonly<Record<string, unknown>>) => void;
197
+ };
198
+ export declare const createQuotaGuard: (deps: QuotaGuardDeps) => {
199
+ /**
200
+ * Decide whether a run may start — AC-2.
201
+ *
202
+ * Reads the current period's rollup, not the ledger: admission is on the hot path of every message, and a
203
+ * check that scanned raw events would make the platform slower in proportion to how much it had been used.
204
+ */
205
+ admit(context: ExecutionContext, about?: QuotaSubject): Promise<QuotaDecision>;
206
+ /**
207
+ * The limits in force for this context — an empty list for unlimited.
208
+ *
209
+ * Exposed so a UI can render "you have used X of Y" without a second source for Y — a panel that took its
210
+ * limit from configuration while enforcement took it from here would eventually disagree, and the version a
211
+ * user sees would be the wrong one.
212
+ */
213
+ limits(context: ExecutionContext, about?: QuotaSubject): Promise<readonly QuotaLimits[]>;
214
+ /**
215
+ * Every limit with its usage and its reset — #183.
216
+ *
217
+ * A limit nobody can see is a limit that surprises people, and once several apply at once "how much have I
218
+ * got left" stops being answerable by reading one number. This is the same `read` the refusal path uses, so
219
+ * a panel cannot disagree with enforcement about either the figure or the reset time — the failure that a
220
+ * second implementation of "how full is it" always eventually produces.
221
+ *
222
+ * Ordered as the resolver ordered them, shortest span first, which puts the limit most likely to stop you at
223
+ * the top without the caller having to sort by anything.
224
+ */
225
+ explain(context: ExecutionContext, about?: QuotaSubject): Promise<readonly QuotaExplanation[]>;
226
+ /** Throws the refusal, for a caller that would rather not branch. Same decision, different ergonomics. */
227
+ assertAdmitted(context: ExecutionContext, about?: QuotaSubject): Promise<QuotaDecision>;
228
+ };
229
+ export type QuotaGuard = ReturnType<typeof createQuotaGuard>;
230
+ /**
231
+ * `resolveLimits` backed by the admin-configured store — #175.
232
+ *
233
+ * The guard already took `resolveLimits` as a function so limits could change without a redeploy. What was
234
+ * missing was anything to resolve them *from*: every deployment had to hardcode them, which is not a
235
+ * configuration.
236
+ *
237
+ * **Per-person first, tenant default second, unbounded last.** The store decides which row applies; this decides
238
+ * what to do when a person has no override — and it deliberately does *not* fall back to checking the tenant
239
+ * default against the person's own usage. That would compare a tenant-sized allowance to one person's spend, so
240
+ * nobody would ever hit it and the limit would silently do nothing.
241
+ *
242
+ * So the resolved limit carries the grain it was configured at, and the guard reads the matching rollup. A limit
243
+ * and the usage it is compared against have to be the same shape, and this is the one place that can guarantee
244
+ * it.
245
+ */
246
+ export declare const createStoredLimitResolver: (deps: {
247
+ readonly limits: UsageLimitStore;
248
+ }) => (context: ExecutionContext, about?: QuotaSubject) => Promise<readonly QuotaLimits[]>;
249
+ /**
250
+ * The order periods are considered in, shortest first.
251
+ *
252
+ * Shortest first because a shorter window is the tighter constraint in practice: someone with a monthly
253
+ * allowance who has burned it in a day is stopped by the daily limit a day earlier, and being stopped early is
254
+ * recoverable where a surprise at month end is not. It is a default, and a deployment that disagrees passes its
255
+ * own order.
256
+ */
257
+ export declare const PERIOD_PRECEDENCE: readonly RollupPeriod[];
258
+ //# sourceMappingURL=quota.d.ts.map