forma-sdk 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/src/index.ts ADDED
@@ -0,0 +1,122 @@
1
+ /**
2
+ * FORMA SDK for Node.js — The AI Agent Firewall
3
+ *
4
+ * Blocks unauthorized actions, PII leaks, and risky decisions before
5
+ * they execute. One line of code. Works with OpenAI, Anthropic and any
6
+ * Node.js application.
7
+ *
8
+ * @example
9
+ * ```typescript
10
+ * import * as forma from "forma-sdk";
11
+ *
12
+ * // Zero-config (after `forma setup`):
13
+ * forma.init();
14
+ *
15
+ * // Or explicit:
16
+ * forma.init({ apiKey: "tl_live_...", preset: "india_fintech" });
17
+ *
18
+ * // Gate any action:
19
+ * await forma.gate("loan-agent", { prompt: userInput });
20
+ *
21
+ * // Wrap OpenAI — every call auto-gated:
22
+ * const openai = forma.wrapOpenAI(new OpenAI({ apiKey: "..." }));
23
+ *
24
+ * // Preview (dry-run, never throws):
25
+ * const r = forma.preview("Aadhaar 2341 1234 1236");
26
+ * // r.decision === "block"
27
+ *
28
+ * // Verify enforcement is live:
29
+ * const v = forma.verify();
30
+ * // v.verified === true, v.probes_passed === 4
31
+ * ```
32
+ *
33
+ * @module forma-sdk
34
+ */
35
+
36
+ export {
37
+ FormaClient,
38
+ FormaGateBlock,
39
+ FormaAPIError,
40
+ } from "./client";
41
+
42
+ export type {
43
+ FormaConfig,
44
+ GateOpts,
45
+ GateResult,
46
+ PreviewResult,
47
+ VerifyResult,
48
+ StatusResult,
49
+ StepRecord,
50
+ } from "./client";
51
+
52
+ import { FormaClient, FormaConfig, GateOpts, GateResult, PreviewResult, VerifyResult, StatusResult } from "./client";
53
+
54
+ // ── Module-level client (tl.init() / tl.preview() style) ─────────────────────
55
+
56
+ let _client: FormaClient | null = null;
57
+
58
+ /** Initialize FORMA — call once at the top of your app. */
59
+ export function init(config: FormaConfig = {}): FormaClient {
60
+ _client = new FormaClient(config);
61
+ return _client;
62
+ }
63
+
64
+ /** Returns the active client (throws if init() not called). */
65
+ export function getClient(): FormaClient {
66
+ if (!_client) {
67
+ // Auto-init from env/config file with no enforce — still useful for API calls
68
+ _client = new FormaClient({});
69
+ }
70
+ return _client;
71
+ }
72
+
73
+ /**
74
+ * Gate an action — checks locally (instant) then server.
75
+ * Throws FormaGateBlock if blocked (raiseOnBlock=true by default).
76
+ */
77
+ export async function gate(agentId: string, opts: GateOpts = {}): Promise<GateResult> {
78
+ return getClient().gate(agentId, opts);
79
+ }
80
+
81
+ /**
82
+ * Dry-run gate check — never throws, never blocks, returns the decision.
83
+ * Use this to test FORMA without affecting your application flow.
84
+ */
85
+ export function preview(prompt: string, actionType: "llm_call" | "tool_call" = "llm_call"): PreviewResult {
86
+ return getClient().preview(prompt, actionType);
87
+ }
88
+
89
+ /**
90
+ * Run adversarial probes against the local gate.
91
+ * Returns { verified: true } when all probes pass — Aadhaar/PAN block + clean allow.
92
+ */
93
+ export function verify(): VerifyResult {
94
+ return getClient().verify();
95
+ }
96
+
97
+ /** Real-time enforcement status — active packs, pii_check, frameworks etc. */
98
+ export function status(): StatusResult {
99
+ return getClient().status();
100
+ }
101
+
102
+ /**
103
+ * Wrap an OpenAI client so every chat.completions.create() is auto-gated.
104
+ * @example
105
+ * const openai = forma.wrapOpenAI(new OpenAI({ apiKey: "sk-..." }), "loan-agent");
106
+ */
107
+ export function wrapOpenAI<T extends { chat: { completions: { create: (...args: unknown[]) => unknown } } }>(
108
+ client: T, agentId = "openai-agent"
109
+ ): T {
110
+ return getClient().wrapOpenAI(client, agentId);
111
+ }
112
+
113
+ /**
114
+ * Wrap an Anthropic client so every messages.create() is auto-gated.
115
+ * @example
116
+ * const anthropic = forma.wrapAnthropic(new Anthropic({ apiKey: "sk-ant-..." }), "kyc-agent");
117
+ */
118
+ export function wrapAnthropic<T extends { messages: { create: (...args: unknown[]) => unknown } }>(
119
+ client: T, agentId = "anthropic-agent"
120
+ ): T {
121
+ return getClient().wrapAnthropic(client, agentId);
122
+ }