@vorionsys/cognigate 1.0.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.
- package/README.md +270 -0
- package/dist/index.cjs +515 -0
- package/dist/index.d.cts +506 -0
- package/dist/index.js +477 -0
- package/package.json +54 -0
- package/src/__tests__/client.test.ts +162 -0
- package/src/client.ts +421 -0
- package/src/index.ts +76 -0
- package/src/types.ts +272 -0
- package/src/webhooks.ts +146 -0
- package/tsconfig.json +25 -0
- package/vitest.config.ts +14 -0
package/src/types.ts
ADDED
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cognigate TypeScript SDK - Type Definitions
|
|
3
|
+
*
|
|
4
|
+
* Core types for the Cognigate AI governance API
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { z } from 'zod';
|
|
8
|
+
|
|
9
|
+
// =============================================================================
|
|
10
|
+
// TRUST TIERS (BASIS Framework)
|
|
11
|
+
// =============================================================================
|
|
12
|
+
|
|
13
|
+
export enum TrustTier {
|
|
14
|
+
T0_SANDBOX = 0,
|
|
15
|
+
T1_OBSERVED = 1,
|
|
16
|
+
T2_PROVISIONAL = 2,
|
|
17
|
+
T3_VERIFIED = 3,
|
|
18
|
+
T4_OPERATIONAL = 4,
|
|
19
|
+
T5_TRUSTED = 5,
|
|
20
|
+
T6_CERTIFIED = 6,
|
|
21
|
+
T7_AUTONOMOUS = 7,
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export const TIER_THRESHOLDS: Record<TrustTier, { min: number; max: number; name: string }> = {
|
|
25
|
+
[TrustTier.T0_SANDBOX]: { min: 0, max: 199, name: 'Sandbox' },
|
|
26
|
+
[TrustTier.T1_OBSERVED]: { min: 200, max: 349, name: 'Observed' },
|
|
27
|
+
[TrustTier.T2_PROVISIONAL]: { min: 350, max: 499, name: 'Provisional' },
|
|
28
|
+
[TrustTier.T3_VERIFIED]: { min: 500, max: 649, name: 'Verified' },
|
|
29
|
+
[TrustTier.T4_OPERATIONAL]: { min: 650, max: 799, name: 'Operational' },
|
|
30
|
+
[TrustTier.T5_TRUSTED]: { min: 800, max: 875, name: 'Trusted' },
|
|
31
|
+
[TrustTier.T6_CERTIFIED]: { min: 876, max: 949, name: 'Certified' },
|
|
32
|
+
[TrustTier.T7_AUTONOMOUS]: { min: 950, max: 1000, name: 'Autonomous' },
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
// =============================================================================
|
|
36
|
+
// GOVERNANCE DECISIONS
|
|
37
|
+
// =============================================================================
|
|
38
|
+
|
|
39
|
+
export type GovernanceDecision = 'ALLOW' | 'DENY' | 'ESCALATE' | 'DEGRADE';
|
|
40
|
+
|
|
41
|
+
export interface GovernanceResult {
|
|
42
|
+
decision: GovernanceDecision;
|
|
43
|
+
trustScore: number;
|
|
44
|
+
trustTier: TrustTier;
|
|
45
|
+
grantedCapabilities: string[];
|
|
46
|
+
deniedCapabilities: string[];
|
|
47
|
+
reasoning: string;
|
|
48
|
+
constraints?: Record<string, unknown>;
|
|
49
|
+
proofId?: string;
|
|
50
|
+
timestamp: Date;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// =============================================================================
|
|
54
|
+
// INTENT PARSING
|
|
55
|
+
// =============================================================================
|
|
56
|
+
|
|
57
|
+
export interface Intent {
|
|
58
|
+
id: string;
|
|
59
|
+
entityId: string;
|
|
60
|
+
rawInput: string;
|
|
61
|
+
parsedAction: string;
|
|
62
|
+
parameters: Record<string, unknown>;
|
|
63
|
+
riskLevel: 'LOW' | 'MEDIUM' | 'HIGH' | 'CRITICAL';
|
|
64
|
+
requiredCapabilities: string[];
|
|
65
|
+
timestamp: Date;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export interface IntentParseResult {
|
|
69
|
+
intent: Intent;
|
|
70
|
+
confidence: number;
|
|
71
|
+
alternativeInterpretations?: Intent[];
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// =============================================================================
|
|
75
|
+
// TRUST STATUS
|
|
76
|
+
// =============================================================================
|
|
77
|
+
|
|
78
|
+
export interface TrustStatus {
|
|
79
|
+
entityId: string;
|
|
80
|
+
trustScore: number;
|
|
81
|
+
trustTier: TrustTier;
|
|
82
|
+
tierName: string;
|
|
83
|
+
capabilities: string[];
|
|
84
|
+
factorScores: Record<string, number>;
|
|
85
|
+
lastEvaluated: Date;
|
|
86
|
+
compliant: boolean;
|
|
87
|
+
warnings: string[];
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// =============================================================================
|
|
91
|
+
// PROOF RECORDS (Immutable Audit Trail)
|
|
92
|
+
// =============================================================================
|
|
93
|
+
|
|
94
|
+
export interface ProofRecord {
|
|
95
|
+
id: string;
|
|
96
|
+
entityId: string;
|
|
97
|
+
intentId: string;
|
|
98
|
+
decision: GovernanceDecision;
|
|
99
|
+
action: string;
|
|
100
|
+
outcome: 'SUCCESS' | 'FAILURE' | 'PARTIAL' | 'PENDING';
|
|
101
|
+
trustScoreBefore: number;
|
|
102
|
+
trustScoreAfter: number;
|
|
103
|
+
timestamp: Date;
|
|
104
|
+
hash: string;
|
|
105
|
+
previousHash: string;
|
|
106
|
+
metadata?: Record<string, unknown>;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export interface ProofChainStats {
|
|
110
|
+
totalRecords: number;
|
|
111
|
+
successRate: number;
|
|
112
|
+
averageTrustScore: number;
|
|
113
|
+
chainIntegrity: boolean;
|
|
114
|
+
lastVerified: Date;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// =============================================================================
|
|
118
|
+
// AGENTS
|
|
119
|
+
// =============================================================================
|
|
120
|
+
|
|
121
|
+
export interface Agent {
|
|
122
|
+
id: string;
|
|
123
|
+
name: string;
|
|
124
|
+
description: string;
|
|
125
|
+
ownerId: string;
|
|
126
|
+
trustScore: number;
|
|
127
|
+
trustTier: TrustTier;
|
|
128
|
+
status: 'ACTIVE' | 'PAUSED' | 'SUSPENDED' | 'TERMINATED';
|
|
129
|
+
capabilities: string[];
|
|
130
|
+
executions: number;
|
|
131
|
+
successRate: number;
|
|
132
|
+
createdAt: Date;
|
|
133
|
+
updatedAt: Date;
|
|
134
|
+
metadata?: Record<string, unknown>;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export interface CreateAgentRequest {
|
|
138
|
+
name: string;
|
|
139
|
+
description?: string;
|
|
140
|
+
template?: string;
|
|
141
|
+
initialCapabilities?: string[];
|
|
142
|
+
metadata?: Record<string, unknown>;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export interface UpdateAgentRequest {
|
|
146
|
+
name?: string;
|
|
147
|
+
description?: string;
|
|
148
|
+
status?: 'ACTIVE' | 'PAUSED';
|
|
149
|
+
metadata?: Record<string, unknown>;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// =============================================================================
|
|
153
|
+
// API RESPONSES
|
|
154
|
+
// =============================================================================
|
|
155
|
+
|
|
156
|
+
export interface ApiResponse<T> {
|
|
157
|
+
success: boolean;
|
|
158
|
+
data?: T;
|
|
159
|
+
error?: ApiError;
|
|
160
|
+
requestId: string;
|
|
161
|
+
timestamp: Date;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
export interface ApiError {
|
|
165
|
+
code: string;
|
|
166
|
+
message: string;
|
|
167
|
+
details?: Record<string, unknown>;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
export interface PaginatedResponse<T> {
|
|
171
|
+
items: T[];
|
|
172
|
+
total: number;
|
|
173
|
+
page: number;
|
|
174
|
+
pageSize: number;
|
|
175
|
+
hasMore: boolean;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// =============================================================================
|
|
179
|
+
// WEBHOOKS
|
|
180
|
+
// =============================================================================
|
|
181
|
+
|
|
182
|
+
export interface WebhookEvent {
|
|
183
|
+
id: string;
|
|
184
|
+
type: WebhookEventType;
|
|
185
|
+
entityId: string;
|
|
186
|
+
payload: Record<string, unknown>;
|
|
187
|
+
timestamp: Date;
|
|
188
|
+
signature: string;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
export type WebhookEventType =
|
|
192
|
+
| 'agent.created'
|
|
193
|
+
| 'agent.updated'
|
|
194
|
+
| 'agent.deleted'
|
|
195
|
+
| 'agent.status_changed'
|
|
196
|
+
| 'trust.score_changed'
|
|
197
|
+
| 'trust.tier_changed'
|
|
198
|
+
| 'governance.decision'
|
|
199
|
+
| 'proof.recorded'
|
|
200
|
+
| 'alert.triggered';
|
|
201
|
+
|
|
202
|
+
// =============================================================================
|
|
203
|
+
// CONFIGURATION
|
|
204
|
+
// =============================================================================
|
|
205
|
+
|
|
206
|
+
export interface CognigateConfig {
|
|
207
|
+
apiKey: string;
|
|
208
|
+
baseUrl?: string;
|
|
209
|
+
timeout?: number;
|
|
210
|
+
retries?: number;
|
|
211
|
+
debug?: boolean;
|
|
212
|
+
webhookSecret?: string;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// =============================================================================
|
|
216
|
+
// ZOD SCHEMAS (for runtime validation)
|
|
217
|
+
// =============================================================================
|
|
218
|
+
|
|
219
|
+
export const TrustStatusSchema = z.object({
|
|
220
|
+
entityId: z.string(),
|
|
221
|
+
trustScore: z.number().min(0).max(1000),
|
|
222
|
+
trustTier: z.nativeEnum(TrustTier),
|
|
223
|
+
tierName: z.string(),
|
|
224
|
+
capabilities: z.array(z.string()),
|
|
225
|
+
factorScores: z.record(z.string(), z.number()),
|
|
226
|
+
lastEvaluated: z.coerce.date(),
|
|
227
|
+
compliant: z.boolean(),
|
|
228
|
+
warnings: z.array(z.string()),
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
export const GovernanceResultSchema = z.object({
|
|
232
|
+
decision: z.enum(['ALLOW', 'DENY', 'ESCALATE', 'DEGRADE']),
|
|
233
|
+
trustScore: z.number(),
|
|
234
|
+
trustTier: z.nativeEnum(TrustTier),
|
|
235
|
+
grantedCapabilities: z.array(z.string()),
|
|
236
|
+
deniedCapabilities: z.array(z.string()),
|
|
237
|
+
reasoning: z.string(),
|
|
238
|
+
constraints: z.record(z.string(), z.unknown()).optional(),
|
|
239
|
+
proofId: z.string().optional(),
|
|
240
|
+
timestamp: z.coerce.date(),
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
export const ProofRecordSchema = z.object({
|
|
244
|
+
id: z.string(),
|
|
245
|
+
entityId: z.string(),
|
|
246
|
+
intentId: z.string(),
|
|
247
|
+
decision: z.enum(['ALLOW', 'DENY', 'ESCALATE', 'DEGRADE']),
|
|
248
|
+
action: z.string(),
|
|
249
|
+
outcome: z.enum(['SUCCESS', 'FAILURE', 'PARTIAL', 'PENDING']),
|
|
250
|
+
trustScoreBefore: z.number(),
|
|
251
|
+
trustScoreAfter: z.number(),
|
|
252
|
+
timestamp: z.coerce.date(),
|
|
253
|
+
hash: z.string(),
|
|
254
|
+
previousHash: z.string(),
|
|
255
|
+
metadata: z.record(z.string(), z.unknown()).optional(),
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
export const AgentSchema = z.object({
|
|
259
|
+
id: z.string(),
|
|
260
|
+
name: z.string(),
|
|
261
|
+
description: z.string(),
|
|
262
|
+
ownerId: z.string(),
|
|
263
|
+
trustScore: z.number(),
|
|
264
|
+
trustTier: z.nativeEnum(TrustTier),
|
|
265
|
+
status: z.enum(['ACTIVE', 'PAUSED', 'SUSPENDED', 'TERMINATED']),
|
|
266
|
+
capabilities: z.array(z.string()),
|
|
267
|
+
executions: z.number(),
|
|
268
|
+
successRate: z.number(),
|
|
269
|
+
createdAt: z.coerce.date(),
|
|
270
|
+
updatedAt: z.coerce.date(),
|
|
271
|
+
metadata: z.record(z.string(), z.unknown()).optional(),
|
|
272
|
+
});
|
package/src/webhooks.ts
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cognigate TypeScript SDK - Webhook Utilities
|
|
3
|
+
*
|
|
4
|
+
* Helpers for handling Cognigate webhooks
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { WebhookEvent, WebhookEventType } from './types.js';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Verify webhook signature
|
|
11
|
+
*/
|
|
12
|
+
export async function verifyWebhookSignature(
|
|
13
|
+
payload: string,
|
|
14
|
+
signature: string,
|
|
15
|
+
secret: string
|
|
16
|
+
): Promise<boolean> {
|
|
17
|
+
const encoder = new TextEncoder();
|
|
18
|
+
const data = encoder.encode(payload);
|
|
19
|
+
const key = await crypto.subtle.importKey(
|
|
20
|
+
'raw',
|
|
21
|
+
encoder.encode(secret),
|
|
22
|
+
{ name: 'HMAC', hash: 'SHA-256' },
|
|
23
|
+
false,
|
|
24
|
+
['sign']
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
const signatureBuffer = await crypto.subtle.sign('HMAC', key, data);
|
|
28
|
+
const expectedSignature = bufferToHex(signatureBuffer);
|
|
29
|
+
|
|
30
|
+
return timingSafeEqual(signature, expectedSignature);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Parse and validate a webhook payload
|
|
35
|
+
*/
|
|
36
|
+
export function parseWebhookPayload(
|
|
37
|
+
body: string,
|
|
38
|
+
signature: string,
|
|
39
|
+
secret: string
|
|
40
|
+
): Promise<WebhookEvent> {
|
|
41
|
+
return new Promise(async (resolve, reject) => {
|
|
42
|
+
const isValid = await verifyWebhookSignature(body, signature, secret);
|
|
43
|
+
|
|
44
|
+
if (!isValid) {
|
|
45
|
+
reject(new Error('Invalid webhook signature'));
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
try {
|
|
50
|
+
const event = JSON.parse(body) as WebhookEvent;
|
|
51
|
+
event.timestamp = new Date(event.timestamp);
|
|
52
|
+
resolve(event);
|
|
53
|
+
} catch (error) {
|
|
54
|
+
reject(new Error('Invalid webhook payload'));
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Webhook handler type
|
|
61
|
+
*/
|
|
62
|
+
export type WebhookHandler<T extends WebhookEventType = WebhookEventType> = (
|
|
63
|
+
event: WebhookEvent & { type: T }
|
|
64
|
+
) => void | Promise<void>;
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Webhook router for handling different event types
|
|
68
|
+
*/
|
|
69
|
+
export class WebhookRouter {
|
|
70
|
+
private handlers: Map<WebhookEventType | '*', WebhookHandler[]> = new Map();
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Register a handler for a specific event type
|
|
74
|
+
*/
|
|
75
|
+
on<T extends WebhookEventType>(type: T, handler: WebhookHandler<T>): this {
|
|
76
|
+
const existing = this.handlers.get(type) || [];
|
|
77
|
+
existing.push(handler as WebhookHandler);
|
|
78
|
+
this.handlers.set(type, existing);
|
|
79
|
+
return this;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Register a handler for all events
|
|
84
|
+
*/
|
|
85
|
+
onAll(handler: WebhookHandler): this {
|
|
86
|
+
const existing = this.handlers.get('*') || [];
|
|
87
|
+
existing.push(handler);
|
|
88
|
+
this.handlers.set('*', existing);
|
|
89
|
+
return this;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Handle a webhook event
|
|
94
|
+
*/
|
|
95
|
+
async handle(event: WebhookEvent): Promise<void> {
|
|
96
|
+
const typeHandlers = this.handlers.get(event.type) || [];
|
|
97
|
+
const allHandlers = this.handlers.get('*') || [];
|
|
98
|
+
|
|
99
|
+
const handlers = [...typeHandlers, ...allHandlers];
|
|
100
|
+
|
|
101
|
+
for (const handler of handlers) {
|
|
102
|
+
await handler(event);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Create an Express/Connect compatible middleware
|
|
108
|
+
*/
|
|
109
|
+
middleware(secret: string) {
|
|
110
|
+
return async (req: any, res: any, _next?: () => void) => {
|
|
111
|
+
try {
|
|
112
|
+
const signature = req.headers['x-cognigate-signature'];
|
|
113
|
+
const body = typeof req.body === 'string' ? req.body : JSON.stringify(req.body);
|
|
114
|
+
|
|
115
|
+
const event = await parseWebhookPayload(body, signature, secret);
|
|
116
|
+
await this.handle(event);
|
|
117
|
+
|
|
118
|
+
res.status(200).json({ received: true });
|
|
119
|
+
} catch (error) {
|
|
120
|
+
res.status(400).json({ error: (error as Error).message });
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// =============================================================================
|
|
127
|
+
// UTILITY FUNCTIONS
|
|
128
|
+
// =============================================================================
|
|
129
|
+
|
|
130
|
+
function bufferToHex(buffer: ArrayBuffer): string {
|
|
131
|
+
return Array.from(new Uint8Array(buffer))
|
|
132
|
+
.map(b => b.toString(16).padStart(2, '0'))
|
|
133
|
+
.join('');
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function timingSafeEqual(a: string, b: string): boolean {
|
|
137
|
+
if (a.length !== b.length) {
|
|
138
|
+
return false;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
let result = 0;
|
|
142
|
+
for (let i = 0; i < a.length; i++) {
|
|
143
|
+
result |= a.charCodeAt(i) ^ b.charCodeAt(i);
|
|
144
|
+
}
|
|
145
|
+
return result === 0;
|
|
146
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"module": "NodeNext",
|
|
5
|
+
"moduleResolution": "NodeNext",
|
|
6
|
+
"lib": ["ES2022"],
|
|
7
|
+
"strict": true,
|
|
8
|
+
"esModuleInterop": true,
|
|
9
|
+
"skipLibCheck": true,
|
|
10
|
+
"forceConsistentCasingInFileNames": true,
|
|
11
|
+
"declaration": true,
|
|
12
|
+
"declarationMap": true,
|
|
13
|
+
"sourceMap": true,
|
|
14
|
+
"outDir": "./dist",
|
|
15
|
+
"rootDir": "./src",
|
|
16
|
+
"resolveJsonModule": true,
|
|
17
|
+
"isolatedModules": true,
|
|
18
|
+
"noUnusedLocals": true,
|
|
19
|
+
"noUnusedParameters": true,
|
|
20
|
+
"noImplicitReturns": true,
|
|
21
|
+
"noFallthroughCasesInSwitch": true
|
|
22
|
+
},
|
|
23
|
+
"include": ["src/**/*"],
|
|
24
|
+
"exclude": ["node_modules", "dist", "**/*.test.ts"]
|
|
25
|
+
}
|
package/vitest.config.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { defineConfig } from 'vitest/config';
|
|
2
|
+
|
|
3
|
+
export default defineConfig({
|
|
4
|
+
test: {
|
|
5
|
+
globals: true,
|
|
6
|
+
environment: 'node',
|
|
7
|
+
include: ['src/**/*.test.ts'],
|
|
8
|
+
coverage: {
|
|
9
|
+
provider: 'v8',
|
|
10
|
+
reporter: ['text', 'json', 'html'],
|
|
11
|
+
exclude: ['**/*.test.ts', '**/index.ts'],
|
|
12
|
+
},
|
|
13
|
+
},
|
|
14
|
+
});
|