@salimassili/ai-costguard 1.2.0 → 2.0.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.
- package/CHANGELOG.md +62 -0
- package/LICENSE +21 -0
- package/README.md +415 -177
- package/benchmarks/run.mjs +229 -0
- package/benchmarks/token-accuracy.mjs +86 -0
- package/dist/cli.d.ts +50 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +178 -0
- package/dist/cli.js.map +1 -0
- package/dist/core/CostGuard.d.ts +4 -5
- package/dist/core/CostGuard.d.ts.map +1 -1
- package/dist/core/CostGuard.js +2 -3
- package/dist/core/CostGuard.js.map +1 -1
- package/dist/core/GuardCore.d.ts +93 -13
- package/dist/core/GuardCore.d.ts.map +1 -1
- package/dist/core/GuardCore.js +372 -158
- package/dist/core/GuardCore.js.map +1 -1
- package/dist/core/GuardFree.d.ts +42 -18
- package/dist/core/GuardFree.d.ts.map +1 -1
- package/dist/core/GuardFree.js +95 -140
- package/dist/core/GuardFree.js.map +1 -1
- package/dist/core/GuardPro.d.ts +76 -8
- package/dist/core/GuardPro.d.ts.map +1 -1
- package/dist/core/GuardPro.js +213 -130
- package/dist/core/GuardPro.js.map +1 -1
- package/dist/core/event-log.d.ts +37 -0
- package/dist/core/event-log.d.ts.map +1 -0
- package/dist/core/event-log.js +49 -0
- package/dist/core/event-log.js.map +1 -0
- package/dist/core/events.d.ts +20 -0
- package/dist/core/events.d.ts.map +1 -0
- package/dist/core/events.js +46 -0
- package/dist/core/events.js.map +1 -0
- package/dist/core/similarity.d.ts +13 -0
- package/dist/core/similarity.d.ts.map +1 -0
- package/dist/core/similarity.js +51 -0
- package/dist/core/similarity.js.map +1 -0
- package/dist/core/tokenizer.d.ts +18 -0
- package/dist/core/tokenizer.d.ts.map +1 -0
- package/dist/core/tokenizer.js +137 -0
- package/dist/core/tokenizer.js.map +1 -0
- package/dist/core/types.d.ts +151 -5
- package/dist/core/types.d.ts.map +1 -1
- package/dist/core/types.js +0 -3
- package/dist/core/types.js.map +1 -1
- package/dist/core/webhooks.d.ts +15 -0
- package/dist/core/webhooks.d.ts.map +1 -0
- package/dist/core/webhooks.js +58 -0
- package/dist/core/webhooks.js.map +1 -0
- package/dist/dashboard.d.ts +73 -0
- package/dist/dashboard.d.ts.map +1 -0
- package/dist/dashboard.js +201 -0
- package/dist/dashboard.js.map +1 -0
- package/dist/index.d.ts +4 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -3
- package/dist/index.js.map +1 -1
- package/dist/pricing/index.d.ts +26 -2
- package/dist/pricing/index.d.ts.map +1 -1
- package/dist/pricing/index.js +100 -13
- package/dist/pricing/index.js.map +1 -1
- package/dist/pro.d.ts +3 -0
- package/dist/pro.d.ts.map +1 -0
- package/dist/pro.js +2 -0
- package/dist/pro.js.map +1 -0
- package/docs/BENCHMARKS.md +70 -0
- package/docs/DASHBOARD.md +61 -0
- package/docs/INTEGRATIONS.md +153 -0
- package/examples/integrations/anthropic-workflow-budget.mjs +36 -0
- package/examples/integrations/ci-budget-check.mjs +32 -0
- package/examples/integrations/crewai-budget-gate.mjs +31 -0
- package/examples/integrations/langchain-retry-storm.mjs +32 -0
- package/examples/integrations/mastra-agent.mjs +41 -0
- package/examples/integrations/openai-agent-loop.mjs +44 -0
- package/examples/integrations/vercel-ai-chatbot.mjs +29 -0
- package/package.json +76 -46
package/dist/core/GuardCore.d.ts
CHANGED
|
@@ -1,22 +1,102 @@
|
|
|
1
|
+
import type { GuardConfig, GuardErrorCode, GuardEventHandler, GuardEventName, GuardState, RequestContext } from './types.js';
|
|
1
2
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
* Maximum simplicity. Maximum virality. Zero complexity.
|
|
5
|
-
* Instant safety layer every AI developer installs by default.
|
|
3
|
+
* Result returned by the guard evaluator.
|
|
6
4
|
*/
|
|
7
|
-
|
|
5
|
+
export interface GuardCheckResult {
|
|
6
|
+
/** Final guard decision. */
|
|
7
|
+
decision: 'allow' | 'block';
|
|
8
|
+
/** Request context that was evaluated. */
|
|
9
|
+
context: RequestContext;
|
|
10
|
+
/** Human-readable block reason, present when decision is "block". */
|
|
11
|
+
reason?: string;
|
|
12
|
+
/** Highest prompt similarity seen during loop detection. */
|
|
13
|
+
similarity?: number;
|
|
14
|
+
}
|
|
8
15
|
/**
|
|
9
|
-
*
|
|
10
|
-
* FREE CORE: Local protection + viral logs + real-time risk warnings
|
|
16
|
+
* Extra structured metadata attached to GuardError.
|
|
11
17
|
*/
|
|
12
|
-
export
|
|
18
|
+
export interface GuardErrorMetadata {
|
|
19
|
+
/** Stable machine-readable block reason. */
|
|
20
|
+
code: GuardErrorCode;
|
|
21
|
+
/** Human-readable block reason. */
|
|
22
|
+
reason: string;
|
|
23
|
+
/** Evaluated request context. */
|
|
24
|
+
context: RequestContext;
|
|
25
|
+
/** Current scope key for budget/history isolation. */
|
|
26
|
+
scopeKey: string;
|
|
27
|
+
/** Highest prompt similarity involved in a loop/retry decision. */
|
|
28
|
+
similarity?: number;
|
|
29
|
+
}
|
|
13
30
|
/**
|
|
14
|
-
*
|
|
31
|
+
* Error thrown when a guarded request is blocked before reaching the AI provider.
|
|
15
32
|
*/
|
|
16
|
-
export declare function middleware(config: GuardConfig): (req: any, res: any, next: any) => void;
|
|
17
33
|
export declare class GuardError extends Error {
|
|
18
|
-
|
|
19
|
-
|
|
34
|
+
/** Stable machine-readable block reason. */
|
|
35
|
+
readonly code: GuardErrorCode;
|
|
36
|
+
/** Request context that caused the block. */
|
|
37
|
+
readonly context: RequestContext;
|
|
38
|
+
/** Structured error metadata for API responses and logging. */
|
|
39
|
+
readonly metadata: GuardErrorMetadata;
|
|
40
|
+
/**
|
|
41
|
+
* Creates a GuardError for a blocked request.
|
|
42
|
+
*/
|
|
43
|
+
constructor(message: string, context?: RequestContext, code?: GuardErrorCode, metadata?: Partial<GuardErrorMetadata>);
|
|
44
|
+
toJSON(): GuardErrorMetadata;
|
|
20
45
|
}
|
|
21
|
-
|
|
46
|
+
/**
|
|
47
|
+
* Shared synchronous evaluator used by the free proxy guard and middleware.
|
|
48
|
+
*/
|
|
49
|
+
export declare class GuardCore {
|
|
50
|
+
private readonly config;
|
|
51
|
+
private readonly state;
|
|
52
|
+
private readonly emitter;
|
|
53
|
+
/**
|
|
54
|
+
* Creates a process-local guard evaluator.
|
|
55
|
+
*/
|
|
56
|
+
constructor(config?: GuardConfig, sharedState?: GuardState);
|
|
57
|
+
/**
|
|
58
|
+
* Subscribes to guard events.
|
|
59
|
+
*/
|
|
60
|
+
on(eventName: GuardEventName, handler: GuardEventHandler): () => void;
|
|
61
|
+
/**
|
|
62
|
+
* Removes a guard event handler.
|
|
63
|
+
*/
|
|
64
|
+
off(eventName: GuardEventName, handler: GuardEventHandler): void;
|
|
65
|
+
/**
|
|
66
|
+
* Returns the mutable process-local state used by this evaluator.
|
|
67
|
+
*/
|
|
68
|
+
getState(): GuardState;
|
|
69
|
+
/**
|
|
70
|
+
* Returns true when a proxied method path should be evaluated.
|
|
71
|
+
*/
|
|
72
|
+
shouldGuardMethod(methodPath: string): boolean;
|
|
73
|
+
/**
|
|
74
|
+
* Extracts a normalized request context from OpenAI-like method arguments.
|
|
75
|
+
*/
|
|
76
|
+
extractContext(args: readonly unknown[], method?: string): RequestContext;
|
|
77
|
+
/**
|
|
78
|
+
* Checks a request context, records allowed calls, emits events, and throws GuardError on block.
|
|
79
|
+
*/
|
|
80
|
+
check(context: RequestContext): GuardCheckResult;
|
|
81
|
+
/**
|
|
82
|
+
* Reconciles actual provider usage from OpenAI/Anthropic-like response objects when available.
|
|
83
|
+
*/
|
|
84
|
+
recordActualUsage(context: RequestContext, response: unknown): void;
|
|
85
|
+
private checkBudget;
|
|
86
|
+
private checkMaxSteps;
|
|
87
|
+
private findLoopSimilarity;
|
|
88
|
+
private checkRetryStorm;
|
|
89
|
+
private recordAttempt;
|
|
90
|
+
private recordAllowed;
|
|
91
|
+
private pushHistory;
|
|
92
|
+
private block;
|
|
93
|
+
private emit;
|
|
94
|
+
private getScopeState;
|
|
95
|
+
private pruneScope;
|
|
96
|
+
private extractScope;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Creates an empty process-local guard state.
|
|
100
|
+
*/
|
|
101
|
+
export declare function createGuardState(): GuardState;
|
|
22
102
|
//# sourceMappingURL=GuardCore.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"GuardCore.d.ts","sourceRoot":"","sources":["../../src/core/GuardCore.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"GuardCore.d.ts","sourceRoot":"","sources":["../../src/core/GuardCore.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EACV,WAAW,EACX,cAAc,EACd,iBAAiB,EACjB,cAAc,EAGd,UAAU,EACV,cAAc,EACf,MAAM,YAAY,CAAC;AAmBpB;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B,4BAA4B;IAC5B,QAAQ,EAAE,OAAO,GAAG,OAAO,CAAC;IAC5B,0CAA0C;IAC1C,OAAO,EAAE,cAAc,CAAC;IACxB,qEAAqE;IACrE,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,4DAA4D;IAC5D,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAED;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC,4CAA4C;IAC5C,IAAI,EAAE,cAAc,CAAC;IACrB,mCAAmC;IACnC,MAAM,EAAE,MAAM,CAAC;IACf,iCAAiC;IACjC,OAAO,EAAE,cAAc,CAAC;IACxB,sDAAsD;IACtD,QAAQ,EAAE,MAAM,CAAC;IACjB,mEAAmE;IACnE,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAED;;GAEG;AACH,qBAAa,UAAW,SAAQ,KAAK;IACnC,4CAA4C;IAC5C,QAAQ,CAAC,IAAI,EAAE,cAAc,CAAC;IAC9B,6CAA6C;IAC7C,QAAQ,CAAC,OAAO,EAAE,cAAc,CAAC;IACjC,+DAA+D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