trusera-sdk 0.1.0 → 0.2.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 +65 -5
- package/dist/index.cjs +1420 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +167 -0
- package/dist/index.d.ts +167 -8
- package/dist/index.js +1382 -13
- package/dist/index.js.map +1 -1
- package/package.json +31 -6
- package/dist/client.d.ts +0 -31
- package/dist/client.d.ts.map +0 -1
- package/dist/client.js +0 -135
- package/dist/client.js.map +0 -1
- package/dist/events.d.ts +0 -19
- package/dist/events.d.ts.map +0 -1
- package/dist/events.js +0 -39
- package/dist/events.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/integrations/langchain.d.ts +0 -28
- package/dist/integrations/langchain.d.ts.map +0 -1
- package/dist/integrations/langchain.js +0 -132
- package/dist/integrations/langchain.js.map +0 -1
- package/dist/interceptor.d.ts +0 -23
- package/dist/interceptor.d.ts.map +0 -1
- package/dist/interceptor.js +0 -215
- package/dist/interceptor.js.map +0 -1
package/README.md
CHANGED
|
@@ -7,9 +7,10 @@ The official TypeScript/JavaScript SDK for monitoring AI agents with [Trusera](h
|
|
|
7
7
|
|
|
8
8
|
## Key Features
|
|
9
9
|
|
|
10
|
-
- **Transparent HTTP Interception**: Zero-code instrumentation
|
|
10
|
+
- **Transparent HTTP Interception**: Zero-code instrumentation via `fetch`, `axios`, and `undici`
|
|
11
11
|
- **Policy Enforcement**: Runtime evaluation against Cedar policies with configurable enforcement modes
|
|
12
|
-
- **LangChain.js Integration**: First-class support for LangChain.js callbacks
|
|
12
|
+
- **LangChain.js Integration**: First-class support for LangChain.js callbacks with optional Cedar enforcement
|
|
13
|
+
- **ESM + CJS**: Dual-format output (built with tsup) -- works in any Node.js or Bun environment
|
|
13
14
|
- **Rich Event Tracking**: Track LLM calls, tool executions, data access, and custom events
|
|
14
15
|
- **Batched Transmission**: Automatic event batching and retry logic
|
|
15
16
|
- **Type-Safe**: Full TypeScript support with strict typing
|
|
@@ -74,6 +75,54 @@ await model.invoke("What are the top AI security risks?");
|
|
|
74
75
|
await client.close();
|
|
75
76
|
```
|
|
76
77
|
|
|
78
|
+
### Axios and Undici Interception
|
|
79
|
+
|
|
80
|
+
The interceptor automatically detects and patches `axios` and `undici` if they
|
|
81
|
+
are installed. No additional configuration is needed:
|
|
82
|
+
|
|
83
|
+
```typescript
|
|
84
|
+
import axios from "axios";
|
|
85
|
+
import { TruseraClient, TruseraInterceptor } from "trusera-sdk";
|
|
86
|
+
|
|
87
|
+
const client = new TruseraClient({ apiKey: "tsk_xxx" });
|
|
88
|
+
const interceptor = new TruseraInterceptor();
|
|
89
|
+
interceptor.install(client, { enforcement: "warn" });
|
|
90
|
+
|
|
91
|
+
// axios requests are now tracked automatically
|
|
92
|
+
await axios.get("https://api.openai.com/v1/models");
|
|
93
|
+
|
|
94
|
+
// undici requests too (if undici is installed)
|
|
95
|
+
// import { request } from "undici";
|
|
96
|
+
// await request("https://api.openai.com/v1/models");
|
|
97
|
+
|
|
98
|
+
await client.close();
|
|
99
|
+
interceptor.uninstall();
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
Libraries are detected via `require()` at install-time. If a library is not
|
|
103
|
+
installed, it is silently skipped with no errors.
|
|
104
|
+
|
|
105
|
+
### LangChain.js with Cedar Enforcement
|
|
106
|
+
|
|
107
|
+
Add Cedar policy enforcement to tool and LLM calls:
|
|
108
|
+
|
|
109
|
+
```typescript
|
|
110
|
+
import { TruseraClient, TruseraLangChainHandler, CedarEvaluator } from "trusera-sdk";
|
|
111
|
+
|
|
112
|
+
const evaluator = new CedarEvaluator();
|
|
113
|
+
await evaluator.loadPolicy(`
|
|
114
|
+
forbid (principal, action == Action::"*", resource)
|
|
115
|
+
when { resource.hostname == "langchain" };
|
|
116
|
+
`);
|
|
117
|
+
|
|
118
|
+
const handler = new TruseraLangChainHandler(client, {
|
|
119
|
+
enforcement: "block",
|
|
120
|
+
cedarEvaluator: evaluator,
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
// Denied tool/LLM calls throw in block mode
|
|
124
|
+
```
|
|
125
|
+
|
|
77
126
|
### Manual Event Tracking
|
|
78
127
|
|
|
79
128
|
For custom instrumentation:
|
|
@@ -282,17 +331,28 @@ The SDK tracks six core event types:
|
|
|
282
331
|
|
|
283
332
|
#### Methods
|
|
284
333
|
|
|
285
|
-
- `install(client: TruseraClient, options?: InterceptorOptions): void`: Install HTTP interceptor
|
|
286
|
-
- `uninstall(): void`: Restore original fetch and
|
|
334
|
+
- `install(client: TruseraClient, options?: InterceptorOptions): void`: Install HTTP interceptor for `fetch`, `axios` (if available), and `undici` (if available)
|
|
335
|
+
- `uninstall(): void`: Restore original fetch, eject axios interceptors, and restore undici functions
|
|
287
336
|
|
|
288
337
|
### `TruseraLangChainHandler`
|
|
289
338
|
|
|
290
339
|
#### Methods
|
|
291
340
|
|
|
292
|
-
- `constructor(client: TruseraClient)`: Create handler for LangChain callbacks
|
|
341
|
+
- `constructor(client: TruseraClient, options?: LangChainHandlerOptions)`: Create handler for LangChain callbacks with optional Cedar enforcement
|
|
293
342
|
- `getPendingEventCount(): number`: Get count of incomplete events
|
|
294
343
|
- `clearPendingEvents(): void`: Clear all pending events
|
|
295
344
|
|
|
345
|
+
#### LangChainHandlerOptions
|
|
346
|
+
|
|
347
|
+
```typescript
|
|
348
|
+
interface LangChainHandlerOptions {
|
|
349
|
+
enforcement?: "block" | "warn" | "log";
|
|
350
|
+
cedarEvaluator?: CedarEvaluator;
|
|
351
|
+
}
|
|
352
|
+
```
|
|
353
|
+
|
|
354
|
+
When `enforcement` is `"block"` and a Cedar policy denies a tool/LLM call, the handler throws an `Error`. In `"warn"` mode it logs via `console.warn`. In `"log"` mode (default) violations are tracked silently.
|
|
355
|
+
|
|
296
356
|
### Utility Functions
|
|
297
357
|
|
|
298
358
|
- `createEvent(type: EventType, name: string, payload?, metadata?): Event`: Create a well-formed event
|