ai-shield-core 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 (78) hide show
  1. package/dist/audit/logger.d.ts +40 -0
  2. package/dist/audit/logger.d.ts.map +1 -0
  3. package/dist/audit/logger.js +100 -0
  4. package/dist/audit/logger.js.map +1 -0
  5. package/dist/audit/types.d.ts +12 -0
  6. package/dist/audit/types.d.ts.map +1 -0
  7. package/dist/audit/types.js +3 -0
  8. package/dist/audit/types.js.map +1 -0
  9. package/dist/cache/lru.d.ts +27 -0
  10. package/dist/cache/lru.d.ts.map +1 -0
  11. package/dist/cache/lru.js +74 -0
  12. package/dist/cache/lru.js.map +1 -0
  13. package/dist/cost/anomaly.d.ts +10 -0
  14. package/dist/cost/anomaly.d.ts.map +1 -0
  15. package/dist/cost/anomaly.js +42 -0
  16. package/dist/cost/anomaly.js.map +1 -0
  17. package/dist/cost/pricing.d.ts +7 -0
  18. package/dist/cost/pricing.d.ts.map +1 -0
  19. package/dist/cost/pricing.js +51 -0
  20. package/dist/cost/pricing.js.map +1 -0
  21. package/dist/cost/tracker.d.ts +24 -0
  22. package/dist/cost/tracker.d.ts.map +1 -0
  23. package/dist/cost/tracker.js +136 -0
  24. package/dist/cost/tracker.js.map +1 -0
  25. package/dist/index.d.ts +18 -0
  26. package/dist/index.d.ts.map +1 -0
  27. package/dist/index.js +59 -0
  28. package/dist/index.js.map +1 -0
  29. package/dist/policy/engine.d.ts +36 -0
  30. package/dist/policy/engine.d.ts.map +1 -0
  31. package/dist/policy/engine.js +127 -0
  32. package/dist/policy/engine.js.map +1 -0
  33. package/dist/policy/tools.d.ts +25 -0
  34. package/dist/policy/tools.d.ts.map +1 -0
  35. package/dist/policy/tools.js +158 -0
  36. package/dist/policy/tools.js.map +1 -0
  37. package/dist/scanner/canary.d.ts +9 -0
  38. package/dist/scanner/canary.d.ts.map +1 -0
  39. package/dist/scanner/canary.js +19 -0
  40. package/dist/scanner/canary.js.map +1 -0
  41. package/dist/scanner/chain.d.ts +17 -0
  42. package/dist/scanner/chain.d.ts.map +1 -0
  43. package/dist/scanner/chain.js +69 -0
  44. package/dist/scanner/chain.js.map +1 -0
  45. package/dist/scanner/heuristic.d.ts +28 -0
  46. package/dist/scanner/heuristic.d.ts.map +1 -0
  47. package/dist/scanner/heuristic.js +375 -0
  48. package/dist/scanner/heuristic.js.map +1 -0
  49. package/dist/scanner/pii.d.ts +17 -0
  50. package/dist/scanner/pii.d.ts.map +1 -0
  51. package/dist/scanner/pii.js +255 -0
  52. package/dist/scanner/pii.js.map +1 -0
  53. package/dist/shield.d.ts +31 -0
  54. package/dist/shield.d.ts.map +1 -0
  55. package/dist/shield.js +184 -0
  56. package/dist/shield.js.map +1 -0
  57. package/dist/types.d.ts +182 -0
  58. package/dist/types.d.ts.map +1 -0
  59. package/dist/types.js +6 -0
  60. package/dist/types.js.map +1 -0
  61. package/package.json +27 -0
  62. package/src/audit/logger.ts +135 -0
  63. package/src/audit/schema.sql +51 -0
  64. package/src/audit/types.ts +16 -0
  65. package/src/cache/lru.ts +93 -0
  66. package/src/cost/anomaly.ts +57 -0
  67. package/src/cost/pricing.ts +58 -0
  68. package/src/cost/tracker.ts +182 -0
  69. package/src/index.ts +91 -0
  70. package/src/policy/engine.ts +163 -0
  71. package/src/policy/tools.ts +189 -0
  72. package/src/scanner/canary.ts +30 -0
  73. package/src/scanner/chain.ts +88 -0
  74. package/src/scanner/heuristic.ts +427 -0
  75. package/src/scanner/pii.ts +313 -0
  76. package/src/shield.ts +228 -0
  77. package/src/types.ts +242 -0
  78. package/tsconfig.json +8 -0
package/src/types.ts ADDED
@@ -0,0 +1,242 @@
1
+ // ============================================================
2
+ // AI Shield Core Types
3
+ // ============================================================
4
+
5
+ // --- Scanner Types ---
6
+
7
+ export type ScanDecision = "allow" | "warn" | "block";
8
+
9
+ export type ViolationType =
10
+ | "prompt_injection"
11
+ | "pii_detected"
12
+ | "tool_denied"
13
+ | "tool_rate_limit"
14
+ | "budget_exceeded"
15
+ | "content_policy"
16
+ | "manifest_drift";
17
+
18
+ export interface Violation {
19
+ type: ViolationType;
20
+ scanner: string;
21
+ score: number;
22
+ threshold: number;
23
+ message: string;
24
+ detail?: string;
25
+ }
26
+
27
+ export interface ScanResult {
28
+ safe: boolean;
29
+ decision: ScanDecision;
30
+ sanitized: string;
31
+ violations: Violation[];
32
+ meta: {
33
+ scanDurationMs: number;
34
+ scannersRun: string[];
35
+ cached: boolean;
36
+ };
37
+ }
38
+
39
+ export interface ScannerResult {
40
+ decision: ScanDecision;
41
+ violations: Violation[];
42
+ sanitized?: string;
43
+ durationMs: number;
44
+ }
45
+
46
+ export interface Scanner {
47
+ name: string;
48
+ scan(input: string, context: ScanContext): Promise<ScannerResult>;
49
+ }
50
+
51
+ // --- Context ---
52
+
53
+ export interface ScanContext {
54
+ agentId?: string;
55
+ sessionId?: string;
56
+ userId?: string;
57
+ userType?: "lead" | "agency" | "customer" | "internal";
58
+ locale?: string;
59
+ preset?: PresetName;
60
+ tools?: ToolCall[];
61
+ }
62
+
63
+ export type PresetName = "public_website" | "internal_support" | "ops_agent";
64
+
65
+ // --- PII Types ---
66
+
67
+ export type PIIType =
68
+ | "email"
69
+ | "phone"
70
+ | "iban"
71
+ | "credit_card"
72
+ | "german_tax_id"
73
+ | "german_personal_id"
74
+ | "german_social_security"
75
+ | "ip_address"
76
+ | "url_with_credentials";
77
+
78
+ export type PIIAction = "block" | "mask" | "tokenize" | "allow";
79
+
80
+ export interface PIIEntity {
81
+ type: PIIType;
82
+ value: string;
83
+ start: number;
84
+ end: number;
85
+ confidence: number;
86
+ }
87
+
88
+ // --- Tool Policy Types ---
89
+
90
+ export interface ToolCall {
91
+ name: string;
92
+ arguments?: Record<string, unknown>;
93
+ serverId?: string;
94
+ }
95
+
96
+ export interface ToolPermissions {
97
+ allowed: string[];
98
+ denied?: string[];
99
+ maxCallsPerMinute?: number;
100
+ maxCallsPerSession?: number;
101
+ requireApproval?: string[];
102
+ }
103
+
104
+ export interface ToolPolicy {
105
+ permissions: Record<string, ToolPermissions>;
106
+ global?: {
107
+ dangerousPatterns?: string[];
108
+ readOnlyMode?: boolean;
109
+ maxToolChainDepth?: number;
110
+ };
111
+ }
112
+
113
+ export interface ToolManifestPin {
114
+ serverId: string;
115
+ toolsHash: string;
116
+ toolCount: number;
117
+ knownTools: string[];
118
+ pinnedAt: Date;
119
+ }
120
+
121
+ // --- Cost Types ---
122
+
123
+ export type BudgetPeriod = "hourly" | "daily" | "monthly";
124
+
125
+ export interface BudgetConfig {
126
+ softLimit: number;
127
+ hardLimit: number;
128
+ period: BudgetPeriod;
129
+ }
130
+
131
+ export interface CostEstimate {
132
+ inputTokens: number;
133
+ outputTokens: number;
134
+ estimatedCost: number;
135
+ model: string;
136
+ }
137
+
138
+ export interface CostRecord {
139
+ entityId: string;
140
+ model: string;
141
+ inputTokens: number;
142
+ outputTokens: number;
143
+ cost: number;
144
+ timestamp: Date;
145
+ }
146
+
147
+ export interface BudgetCheckResult {
148
+ allowed: boolean;
149
+ currentSpend: number;
150
+ remainingBudget: number;
151
+ warning?: string;
152
+ }
153
+
154
+ // --- Audit Types ---
155
+
156
+ export interface AuditRecord {
157
+ id: string;
158
+ timestamp: Date;
159
+ sessionId?: string;
160
+ agentId?: string;
161
+ userIdHash?: string;
162
+ requestType: "chat" | "tool_call" | "agent_to_agent";
163
+ inputHash: string;
164
+ inputTokenCount?: number;
165
+ model?: string;
166
+ securityDecision: ScanDecision;
167
+ securityReason?: string;
168
+ violations: Violation[];
169
+ scanDurationMs: number;
170
+ outputTokenCount?: number;
171
+ toolsCalled?: string[];
172
+ costUsd?: number;
173
+ }
174
+
175
+ // --- Config Types ---
176
+
177
+ export interface InjectionConfig {
178
+ enabled?: boolean;
179
+ strictness?: "low" | "medium" | "high";
180
+ action?: "block" | "warn" | "flag";
181
+ threshold?: number;
182
+ customPatterns?: RegExp[];
183
+ }
184
+
185
+ export interface PIIConfig {
186
+ enabled?: boolean;
187
+ action?: PIIAction;
188
+ locale?: string;
189
+ types?: Partial<Record<PIIType, PIIAction>>;
190
+ allowedTypes?: PIIType[];
191
+ }
192
+
193
+ export interface CostConfig {
194
+ enabled?: boolean;
195
+ budgets?: Record<string, BudgetConfig>;
196
+ pricing?: Record<string, { inputPer1M: number; outputPer1M: number }>;
197
+ redisUrl?: string;
198
+ }
199
+
200
+ export interface AuditConfig {
201
+ enabled?: boolean;
202
+ store?: "postgresql" | "memory" | "console";
203
+ connectionString?: string;
204
+ batchSize?: number;
205
+ flushIntervalMs?: number;
206
+ retentionDays?: number;
207
+ }
208
+
209
+ export interface ToolConfig {
210
+ enabled?: boolean;
211
+ policies?: Record<string, ToolPermissions>;
212
+ globalDangerousPatterns?: string[];
213
+ maxToolChainDepth?: number;
214
+ manifestPins?: ToolManifestPin[];
215
+ }
216
+
217
+ export interface CacheConfig {
218
+ /** Disable caching (default: enabled when cache config is provided) */
219
+ enabled?: boolean;
220
+ /** Maximum cached entries (default: 1000) */
221
+ maxSize?: number;
222
+ /** TTL in milliseconds (default: 300_000 = 5 minutes) */
223
+ ttlMs?: number;
224
+ }
225
+
226
+ export interface ShieldConfig {
227
+ injection?: InjectionConfig;
228
+ pii?: PIIConfig;
229
+ cost?: CostConfig;
230
+ audit?: AuditConfig;
231
+ tools?: ToolConfig;
232
+ cache?: CacheConfig;
233
+ preset?: PresetName;
234
+ }
235
+
236
+ // --- Model Pricing ---
237
+
238
+ export interface ModelPricing {
239
+ inputPer1M: number;
240
+ outputPer1M: number;
241
+ cachedInputPer1M?: number;
242
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,8 @@
1
+ {
2
+ "extends": "../../tsconfig.json",
3
+ "compilerOptions": {
4
+ "outDir": "dist",
5
+ "rootDir": "src"
6
+ },
7
+ "include": ["src"]
8
+ }