floe-guard 0.13.0 → 0.13.1

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.
@@ -1,4 +1,4 @@
1
- import { B as BudgetGuard, a as BudgetExceeded } from '../guard-BXtXoark.cjs';
1
+ import { B as BudgetGuard, a as BudgetExceeded } from '../guard-Ct38zQZS.cjs';
2
2
 
3
3
  /**
4
4
  * LiveKit Agents adapter (optional peer: `npm i @livekit/agents`).
@@ -1,4 +1,4 @@
1
- import { B as BudgetGuard, a as BudgetExceeded } from '../guard-BXtXoark.js';
1
+ import { B as BudgetGuard, a as BudgetExceeded } from '../guard-Ct38zQZS.js';
2
2
 
3
3
  /**
4
4
  * LiveKit Agents adapter (optional peer: `npm i @livekit/agents`).
@@ -1,4 +1,4 @@
1
- import { B as BudgetGuard, a as BudgetExceeded } from '../guard-BXtXoark.cjs';
1
+ import { B as BudgetGuard, a as BudgetExceeded } from '../guard-Ct38zQZS.cjs';
2
2
 
3
3
  /**
4
4
  * Retell custom-LLM WebSocket adapter (optional peer: the `ws` server your custom
@@ -1,4 +1,4 @@
1
- import { B as BudgetGuard, a as BudgetExceeded } from '../guard-BXtXoark.js';
1
+ import { B as BudgetGuard, a as BudgetExceeded } from '../guard-Ct38zQZS.js';
2
2
 
3
3
  /**
4
4
  * Retell custom-LLM WebSocket adapter (optional peer: the `ws` server your custom
@@ -1,5 +1,5 @@
1
- import { B as BudgetGuard, F as FloeGuardError } from '../guard-BXtXoark.cjs';
2
- export { a as BudgetExceeded } from '../guard-BXtXoark.cjs';
1
+ import { B as BudgetGuard, F as FloeGuardError } from '../guard-Ct38zQZS.cjs';
2
+ export { a as BudgetExceeded } from '../guard-Ct38zQZS.cjs';
3
3
 
4
4
  /**
5
5
  * Vapi custom-LLM adapter (no SDK dependency — typed structurally).
@@ -1,5 +1,5 @@
1
- import { B as BudgetGuard, F as FloeGuardError } from '../guard-BXtXoark.js';
2
- export { a as BudgetExceeded } from '../guard-BXtXoark.js';
1
+ import { B as BudgetGuard, F as FloeGuardError } from '../guard-Ct38zQZS.js';
2
+ export { a as BudgetExceeded } from '../guard-Ct38zQZS.js';
3
3
 
4
4
  /**
5
5
  * Vapi custom-LLM adapter (no SDK dependency — typed structurally).
@@ -566,12 +566,14 @@ declare class BudgetGuard {
566
566
  private accrueStep;
567
567
  /**
568
568
  * The ONE choke point, across two dimensions and two scopes. Returns the first
569
- * ceiling that blocks as `[dimension, scope]` — dimension `"usd" | "tokens"`,
570
- * scope `"aggregate" | "step"` — or `null` if the call fits everywhere.
571
- * Aggregate is checked before step (the guard-wide ceiling is the hard limit).
569
+ * ceiling that blocks as `[dimension, scope, spent, limit]` — dimension
570
+ * `"usd" | "tokens"`, scope `"aggregate" | "step"` — or `null` if the call
571
+ * fits everywhere. Aggregate is checked before step (the guard-wide ceiling
572
+ * is the hard limit). `spent` / `limit` are captured here so `raiseBlock`
573
+ * (called outside the lock) reads no shared state and stays race-free.
572
574
  */
573
575
  private blockingCross;
574
- /** Notify + throw the right error for a [dimension, scope] block. */
576
+ /** Notify + throw the right error for a [dimension, scope, spent, limit] block. */
575
577
  private raiseBlock;
576
578
  /**
577
579
  * Scope a per-step USD and/or token cap for a **sequential agent loop**.
@@ -566,12 +566,14 @@ declare class BudgetGuard {
566
566
  private accrueStep;
567
567
  /**
568
568
  * The ONE choke point, across two dimensions and two scopes. Returns the first
569
- * ceiling that blocks as `[dimension, scope]` — dimension `"usd" | "tokens"`,
570
- * scope `"aggregate" | "step"` — or `null` if the call fits everywhere.
571
- * Aggregate is checked before step (the guard-wide ceiling is the hard limit).
569
+ * ceiling that blocks as `[dimension, scope, spent, limit]` — dimension
570
+ * `"usd" | "tokens"`, scope `"aggregate" | "step"` — or `null` if the call
571
+ * fits everywhere. Aggregate is checked before step (the guard-wide ceiling
572
+ * is the hard limit). `spent` / `limit` are captured here so `raiseBlock`
573
+ * (called outside the lock) reads no shared state and stays race-free.
572
574
  */
573
575
  private blockingCross;
574
- /** Notify + throw the right error for a [dimension, scope] block. */
576
+ /** Notify + throw the right error for a [dimension, scope, spent, limit] block. */
575
577
  private raiseBlock;
576
578
  /**
577
579
  * Scope a per-step USD and/or token cap for a **sequential agent loop**.
package/dist/index.cjs CHANGED
@@ -1939,19 +1939,21 @@ var BudgetGuard = class {
1939
1939
  }
1940
1940
  /**
1941
1941
  * The ONE choke point, across two dimensions and two scopes. Returns the first
1942
- * ceiling that blocks as `[dimension, scope]` — dimension `"usd" | "tokens"`,
1943
- * scope `"aggregate" | "step"` — or `null` if the call fits everywhere.
1944
- * Aggregate is checked before step (the guard-wide ceiling is the hard limit).
1942
+ * ceiling that blocks as `[dimension, scope, spent, limit]` — dimension
1943
+ * `"usd" | "tokens"`, scope `"aggregate" | "step"` — or `null` if the call
1944
+ * fits everywhere. Aggregate is checked before step (the guard-wide ceiling
1945
+ * is the hard limit). `spent` / `limit` are captured here so `raiseBlock`
1946
+ * (called outside the lock) reads no shared state and stays race-free.
1945
1947
  */
1946
1948
  blockingCross(estimateUsd, estimateTokens) {
1947
1949
  const committed = this.spentUsd + this.reserved;
1948
1950
  if (committed > this.limitUsd - EPS || committed + estimateUsd > this.limitUsd + EPS) {
1949
- return ["usd", "aggregate"];
1951
+ return ["usd", "aggregate", this.spentUsd, this.limitUsd];
1950
1952
  }
1951
1953
  if (this.tokenLimit !== null) {
1952
1954
  const committedT = this.spentTokens + this.reservedTokens;
1953
1955
  if (committedT >= this.tokenLimit || committedT + estimateTokens > this.tokenLimit) {
1954
- return ["tokens", "aggregate"];
1956
+ return ["tokens", "aggregate", committedT, this.tokenLimit];
1955
1957
  }
1956
1958
  }
1957
1959
  const step = this.steps.length ? this.steps[this.steps.length - 1] : null;
@@ -1959,36 +1961,26 @@ var BudgetGuard = class {
1959
1961
  if (step.maxUsd !== null) {
1960
1962
  const sCommitted = step.spentUsd + step.reservedUsd;
1961
1963
  if (sCommitted > step.maxUsd - EPS || sCommitted + estimateUsd > step.maxUsd + EPS) {
1962
- return ["usd", "step"];
1964
+ return ["usd", "step", sCommitted, step.maxUsd];
1963
1965
  }
1964
1966
  }
1965
1967
  if (step.maxTokens !== null) {
1966
1968
  const sCommittedT = step.spentTokens + step.reservedTokens;
1967
1969
  if (sCommittedT >= step.maxTokens || sCommittedT + estimateTokens > step.maxTokens) {
1968
- return ["tokens", "step"];
1970
+ return ["tokens", "step", sCommittedT, step.maxTokens];
1969
1971
  }
1970
1972
  }
1971
1973
  }
1972
1974
  return null;
1973
1975
  }
1974
- /** Notify + throw the right error for a [dimension, scope] block. */
1976
+ /** Notify + throw the right error for a [dimension, scope, spent, limit] block. */
1975
1977
  raiseBlock(blocked) {
1976
- const [dimension, scope] = blocked;
1978
+ const [dimension, scope, spent, limit] = blocked;
1977
1979
  if (dimension === "usd") {
1978
- this.onBlock(this.spentUsd, this.limitUsd);
1979
- throw new BudgetExceeded(this.spentUsd, this.limitUsd);
1980
+ this.onBlock(spent, limit);
1981
+ throw new BudgetExceeded(spent, limit);
1980
1982
  }
1981
- let spentT;
1982
- let limitT;
1983
- if (scope === "step") {
1984
- const step = this.steps[this.steps.length - 1];
1985
- spentT = step.spentTokens + step.reservedTokens;
1986
- limitT = step.maxTokens ?? 0;
1987
- } else {
1988
- spentT = this.spentTokens + this.reservedTokens;
1989
- limitT = this.tokenLimit ?? 0;
1990
- }
1991
- throw new TokenBudgetExceeded(spentT, limitT, scope);
1983
+ throw new TokenBudgetExceeded(Math.trunc(spent), Math.trunc(limit), scope);
1992
1984
  }
1993
1985
  /**
1994
1986
  * Scope a per-step USD and/or token cap for a **sequential agent loop**.
@@ -2276,7 +2268,7 @@ function budgetGuardMiddleware(guard) {
2276
2268
 
2277
2269
  // src/retry.ts
2278
2270
  function defaultRetryIf(error) {
2279
- return !(error instanceof BudgetExceeded);
2271
+ return !(error instanceof FloeGuardError);
2280
2272
  }
2281
2273
  async function withBudgetRetry(guard, call, options = {}) {
2282
2274
  const maxAttempts = options.maxAttempts ?? 2;