|
| 1 | +import type { Limits } from "@trigger.dev/platform"; |
| 2 | +import { |
| 3 | + getAgentMessageUsage, |
| 4 | + incrementAgentMessageUsage, |
| 5 | + type DashboardAgentDb, |
| 6 | +} from "@internal/dashboard-agent-db"; |
| 7 | +import { getCachedLimit } from "./platform.v3.server"; |
| 8 | +import { logger } from "./logger.server"; |
| 9 | + |
| 10 | +// The repo's unlimited sentinel. Never Infinity: it serializes to null in the limit cache. |
| 11 | +export const UNLIMITED_AGENT_MESSAGES = 100_000_000; |
| 12 | + |
| 13 | +// Filled by cloud billing (TRI-12863 P0). Absent until then, and always on self-hosted, |
| 14 | +// so the fallback applies and the cap is effectively off. |
| 15 | +const AGENT_MESSAGE_LIMIT_KEY = "agentMessages" as keyof Limits; |
| 16 | + |
| 17 | +/** The billing period the counter is scoped to: a UTC calendar month, "YYYY-MM". */ |
| 18 | +export function currentAgentMessagePeriod(now: Date = new Date()): string { |
| 19 | + return `${now.getUTCFullYear()}-${String(now.getUTCMonth() + 1).padStart(2, "0")}`; |
| 20 | +} |
| 21 | + |
| 22 | +/** Pure so the send routes and, later, the MCP path share one rule. */ |
| 23 | +export function checkAgentMessageQuota({ used, limit }: { used: number; limit: number }): { |
| 24 | + reached: boolean; |
| 25 | +} { |
| 26 | + return { reached: used >= limit }; |
| 27 | +} |
| 28 | + |
| 29 | +export type AgentMessageQuota = { reached: boolean; used: number; limit: number }; |
| 30 | + |
| 31 | +/** |
| 32 | + * The period counter and the cached plan limit for one org. Fails open: an absent limit |
| 33 | + * (self-hosted, or before the cloud side ships) resolves to the unlimited sentinel, and a |
| 34 | + * counter read that throws returns `undefined` — either way there is no cap. |
| 35 | + */ |
| 36 | +export async function resolveAgentMessageQuota( |
| 37 | + db: DashboardAgentDb, |
| 38 | + params: { |
| 39 | + organizationId: string; |
| 40 | + now?: Date; |
| 41 | + readLimit?: (organizationId: string) => Promise<number>; |
| 42 | + } |
| 43 | +): Promise<AgentMessageQuota | undefined> { |
| 44 | + const readLimit = |
| 45 | + params.readLimit ?? |
| 46 | + (async (organizationId: string) => { |
| 47 | + const cached = await getCachedLimit( |
| 48 | + organizationId, |
| 49 | + AGENT_MESSAGE_LIMIT_KEY, |
| 50 | + UNLIMITED_AGENT_MESSAGES |
| 51 | + ); |
| 52 | + // A cache error leaves `val` empty; fall open to unlimited. |
| 53 | + return cached.val ?? UNLIMITED_AGENT_MESSAGES; |
| 54 | + }); |
| 55 | + try { |
| 56 | + const [limit, used] = await Promise.all([ |
| 57 | + readLimit(params.organizationId), |
| 58 | + getAgentMessageUsage(db, { |
| 59 | + organizationId: params.organizationId, |
| 60 | + period: currentAgentMessagePeriod(params.now), |
| 61 | + }), |
| 62 | + ]); |
| 63 | + return { ...checkAgentMessageQuota({ used, limit }), used, limit }; |
| 64 | + } catch (error) { |
| 65 | + logger.error("Failed to resolve dashboard agent message quota", { |
| 66 | + organizationId: params.organizationId, |
| 67 | + error, |
| 68 | + }); |
| 69 | + return undefined; |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +/** Record one sent user message. Swallows errors: the cap is a nudge, never a send blocker. */ |
| 74 | +export async function recordAgentMessageSent( |
| 75 | + db: DashboardAgentDb, |
| 76 | + params: { organizationId: string; now?: Date } |
| 77 | +): Promise<void> { |
| 78 | + try { |
| 79 | + await incrementAgentMessageUsage(db, { |
| 80 | + organizationId: params.organizationId, |
| 81 | + period: currentAgentMessagePeriod(params.now), |
| 82 | + }); |
| 83 | + } catch (error) { |
| 84 | + logger.error("Failed to record a dashboard agent message against the quota", { |
| 85 | + organizationId: params.organizationId, |
| 86 | + error, |
| 87 | + }); |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +/** |
| 92 | + * Whether an agent turn consumes quota. A wake/action turn is server-placed — the user never |
| 93 | + * spent it — so only a `message` turn that is not an action counts. The `.in` proxy already |
| 94 | + * refuses action turns; this keeps the rule explicit and testable. |
| 95 | + */ |
| 96 | +export function agentTurnCountsAgainstQuota( |
| 97 | + turn: { kind?: string; payload?: { trigger?: string } } | undefined |
| 98 | +): boolean { |
| 99 | + return turn?.kind === "message" && turn.payload?.trigger !== "action"; |
| 100 | +} |
0 commit comments