@lelu-auth/lelu 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.
package/dist/index.mjs ADDED
@@ -0,0 +1,296 @@
1
+ import { z } from 'zod';
2
+
3
+ // src/vercel/index.ts
4
+ function secureTool(opts) {
5
+ const { client, actor, action, actingFor } = opts;
6
+ const wrapped = {
7
+ parameters: opts.tool.parameters,
8
+ async execute(args, options) {
9
+ const confidence = typeof opts.confidence === "function" ? opts.confidence(args) : opts.confidence ?? 1;
10
+ let decision;
11
+ try {
12
+ decision = await client.agentAuthorize({
13
+ actor,
14
+ action,
15
+ context: { confidence, actingFor }
16
+ });
17
+ } catch (err) {
18
+ return {
19
+ allowed: false,
20
+ reason: `Lelu authorization check failed: ${String(err)}`,
21
+ requiresHumanReview: false
22
+ };
23
+ }
24
+ if (decision.requiresHumanReview) {
25
+ return {
26
+ allowed: false,
27
+ reason: `Action '${action}' for agent '${actor}' is queued for human review. Reason: ${decision.reason}. Confidence: ${(confidence * 100).toFixed(0)}%.`,
28
+ requiresHumanReview: true
29
+ };
30
+ }
31
+ if (!decision.allowed) {
32
+ const denied = {
33
+ allowed: false,
34
+ reason: `Action '${action}' was denied for agent '${actor}'. Reason: ${decision.reason}.`,
35
+ requiresHumanReview: false
36
+ };
37
+ if (decision.downgradedScope !== void 0) {
38
+ denied.downgradedScope = decision.downgradedScope;
39
+ }
40
+ return {
41
+ ...denied
42
+ };
43
+ }
44
+ if (!opts.tool.execute) {
45
+ throw new Error(
46
+ `[Lelu] secureTool: the wrapped tool '${action}' has no execute function.`
47
+ );
48
+ }
49
+ return opts.tool.execute(args, options);
50
+ }
51
+ };
52
+ if (opts.tool.description !== void 0) {
53
+ wrapped.description = opts.tool.description;
54
+ }
55
+ return wrapped;
56
+ }
57
+ var AuthRequestSchema = z.object({
58
+ userId: z.string().min(1, "userId is required"),
59
+ action: z.string().min(1, "action is required"),
60
+ resource: z.record(z.string()).optional()
61
+ });
62
+ var AgentContextSchema = z.object({
63
+ /** LLM confidence score — 0.0 to 1.0 */
64
+ confidence: z.number().min(0).max(1),
65
+ /** User the agent is acting on behalf of */
66
+ actingFor: z.string().optional(),
67
+ /** Requested agent scope */
68
+ scope: z.string().optional()
69
+ });
70
+ var AgentAuthRequestSchema = z.object({
71
+ actor: z.string().min(1, "actor is required"),
72
+ action: z.string().min(1, "action is required"),
73
+ resource: z.record(z.string()).optional(),
74
+ context: AgentContextSchema
75
+ });
76
+ var MintTokenRequestSchema = z.object({
77
+ scope: z.string().min(1),
78
+ actingFor: z.string().optional(),
79
+ ttlSeconds: z.number().int().positive().optional()
80
+ });
81
+ var DelegateScopeRequestSchema = z.object({
82
+ delegator: z.string().min(1, "delegator is required"),
83
+ delegatee: z.string().min(1, "delegatee is required"),
84
+ scopedTo: z.array(z.string().min(1)).optional(),
85
+ ttlSeconds: z.number().int().positive().optional(),
86
+ confidence: z.number().min(0).max(1).optional(),
87
+ actingFor: z.string().optional(),
88
+ tenantId: z.string().optional()
89
+ });
90
+ var AuthEngineError = class extends Error {
91
+ constructor(message, status, details) {
92
+ super(message);
93
+ this.status = status;
94
+ this.details = details;
95
+ this.name = "AuthEngineError";
96
+ }
97
+ };
98
+
99
+ // src/client.ts
100
+ var LeluClient = class {
101
+ baseUrl;
102
+ timeoutMs;
103
+ apiKey;
104
+ constructor(cfg = {}) {
105
+ this.baseUrl = (cfg.baseUrl ?? "http://localhost:8080").replace(/\/$/, "");
106
+ this.timeoutMs = cfg.timeoutMs ?? 5e3;
107
+ this.apiKey = cfg.apiKey;
108
+ }
109
+ // ── Human authorization ────────────────────────────────────────────────────
110
+ /**
111
+ * Checks whether a human user is permitted to perform an action.
112
+ */
113
+ async authorize(req) {
114
+ const validated = AuthRequestSchema.parse(req);
115
+ const body = {
116
+ user_id: validated.userId,
117
+ action: validated.action,
118
+ resource: validated.resource
119
+ };
120
+ const data = await this.post("/v1/authorize", body);
121
+ return {
122
+ allowed: data.allowed,
123
+ reason: data.reason,
124
+ traceId: data.trace_id
125
+ };
126
+ }
127
+ // ── Agent authorization ────────────────────────────────────────────────────
128
+ /**
129
+ * Checks whether an AI agent is permitted to perform an action, taking the
130
+ * confidence score into account (Confidence-Aware Auth ★).
131
+ */
132
+ async agentAuthorize(req) {
133
+ const validated = AgentAuthRequestSchema.parse(req);
134
+ const body = {
135
+ actor: validated.actor,
136
+ action: validated.action,
137
+ resource: validated.resource,
138
+ confidence: validated.context.confidence,
139
+ acting_for: validated.context.actingFor,
140
+ scope: validated.context.scope
141
+ };
142
+ const data = await this.post("/v1/agent/authorize", body);
143
+ return {
144
+ allowed: data.allowed,
145
+ reason: data.reason,
146
+ traceId: data.trace_id,
147
+ downgradedScope: data.downgraded_scope,
148
+ requiresHumanReview: data.requires_human_review,
149
+ confidenceUsed: data.confidence_used
150
+ };
151
+ }
152
+ // ── JIT Token minting ──────────────────────────────────────────────────────
153
+ /**
154
+ * Mints a scoped JWT for an agent with an optional TTL.
155
+ * Default TTL is 60 seconds.
156
+ */
157
+ async mintToken(req) {
158
+ const validated = MintTokenRequestSchema.parse(req);
159
+ const body = {
160
+ scope: validated.scope,
161
+ acting_for: validated.actingFor,
162
+ ttl_seconds: validated.ttlSeconds ?? 60
163
+ };
164
+ const data = await this.post("/v1/tokens/mint", body);
165
+ return {
166
+ token: data.token,
167
+ tokenId: data.token_id,
168
+ expiresAt: new Date(data.expires_at * 1e3)
169
+ };
170
+ }
171
+ // ── Token revocation ───────────────────────────────────────────────────────
172
+ /**
173
+ * Immediately revokes a JIT token by its ID.
174
+ */
175
+ async revokeToken(tokenId) {
176
+ const data = await this.delete(
177
+ `/v1/tokens/${encodeURIComponent(tokenId)}`
178
+ );
179
+ return { success: data.success };
180
+ }
181
+ // ── Multi-agent delegation ─────────────────────────────────────────────────
182
+ /**
183
+ * Delegates a constrained sub-scope from one agent to another.
184
+ *
185
+ * Validates the delegation rule in the loaded policy, caps the TTL to the
186
+ * policy maximum, and mints a child JIT token scoped to the granted actions.
187
+ *
188
+ * The delegator's `confidence` score is checked against the policy's
189
+ * `require_confidence_above` before delegation is granted.
190
+ */
191
+ async delegateScope(req) {
192
+ const validated = DelegateScopeRequestSchema.parse(req);
193
+ const body = {
194
+ delegator: validated.delegator,
195
+ delegatee: validated.delegatee,
196
+ scoped_to: validated.scopedTo ?? [],
197
+ ttl_seconds: validated.ttlSeconds ?? 60,
198
+ confidence: validated.confidence ?? 1,
199
+ acting_for: validated.actingFor ?? "",
200
+ tenant_id: validated.tenantId ?? ""
201
+ };
202
+ const data = await this.post("/v1/agent/delegate", body);
203
+ return {
204
+ token: data.token,
205
+ tokenId: data.token_id,
206
+ expiresAt: new Date(data.expires_at * 1e3),
207
+ delegator: data.delegator,
208
+ delegatee: data.delegatee,
209
+ grantedScopes: data.granted_scopes,
210
+ traceId: data.trace_id
211
+ };
212
+ }
213
+ // ── Health check ───────────────────────────────────────────────────────────
214
+ /**
215
+ * Returns true if the engine is reachable and healthy.
216
+ */
217
+ async isHealthy() {
218
+ try {
219
+ const data = await this.get("/healthz");
220
+ return data.status === "ok";
221
+ } catch {
222
+ return false;
223
+ }
224
+ }
225
+ // ── HTTP helpers ───────────────────────────────────────────────────────────
226
+ headers() {
227
+ const h = { "Content-Type": "application/json" };
228
+ if (this.apiKey) {
229
+ h["Authorization"] = `Bearer ${this.apiKey}`;
230
+ }
231
+ return h;
232
+ }
233
+ async post(path, body) {
234
+ const ctrl = new AbortController();
235
+ const timer = setTimeout(() => ctrl.abort(), this.timeoutMs);
236
+ try {
237
+ const res = await fetch(`${this.baseUrl}${path}`, {
238
+ method: "POST",
239
+ headers: this.headers(),
240
+ body: JSON.stringify(body),
241
+ signal: ctrl.signal
242
+ });
243
+ return this.parseResponse(res);
244
+ } finally {
245
+ clearTimeout(timer);
246
+ }
247
+ }
248
+ async delete(path) {
249
+ const ctrl = new AbortController();
250
+ const timer = setTimeout(() => ctrl.abort(), this.timeoutMs);
251
+ try {
252
+ const res = await fetch(`${this.baseUrl}${path}`, {
253
+ method: "DELETE",
254
+ headers: this.headers(),
255
+ signal: ctrl.signal
256
+ });
257
+ return this.parseResponse(res);
258
+ } finally {
259
+ clearTimeout(timer);
260
+ }
261
+ }
262
+ async get(path) {
263
+ const ctrl = new AbortController();
264
+ const timer = setTimeout(() => ctrl.abort(), this.timeoutMs);
265
+ try {
266
+ const res = await fetch(`${this.baseUrl}${path}`, {
267
+ method: "GET",
268
+ headers: this.headers(),
269
+ signal: ctrl.signal
270
+ });
271
+ return this.parseResponse(res);
272
+ } finally {
273
+ clearTimeout(timer);
274
+ }
275
+ }
276
+ async parseResponse(res) {
277
+ const json = await res.json();
278
+ if (!res.ok) {
279
+ throw new AuthEngineError(
280
+ json["error"] ?? "engine error",
281
+ res.status,
282
+ json
283
+ );
284
+ }
285
+ return json;
286
+ }
287
+ };
288
+
289
+ // src/index.ts
290
+ function createClient(config) {
291
+ return new LeluClient(config);
292
+ }
293
+
294
+ export { AgentAuthRequestSchema, AgentContextSchema, AuthEngineError, AuthRequestSchema, DelegateScopeRequestSchema, LeluClient, MintTokenRequestSchema, createClient, secureTool };
295
+ //# sourceMappingURL=index.mjs.map
296
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/vercel/index.ts","../src/types.ts","../src/client.ts","../src/index.ts"],"names":[],"mappings":";;;AAgGO,SAAS,WACd,IAAA,EAC+C;AAC/C,EAAA,MAAM,EAAE,MAAA,EAAQ,KAAA,EAAO,MAAA,EAAQ,WAAU,GAAI,IAAA;AAE7C,EAAA,MAAM,OAAA,GAAyD;AAAA,IAC7D,UAAA,EAAY,KAAK,IAAA,CAAK,UAAA;AAAA,IAEtB,MAAM,OAAA,CACJ,IAAA,EACA,OAAA,EACqC;AAErC,MAAA,MAAM,UAAA,GACJ,OAAO,IAAA,CAAK,UAAA,KAAe,UAAA,GACvB,KAAK,UAAA,CAAW,IAAI,CAAA,GACnB,IAAA,CAAK,UAAA,IAAc,CAAA;AAE1B,MAAA,IA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Q,IAAA,CAAK;AAAA,OACd,CAAA;AACD,MAAA,OAAO,IAAA,CAAK,cAAiB,GAAG,CAAA;AAAA,IAClC,CAAA,SAAE;AACA,MAAA,YAAA,CAAa,KAAK,CAAA;AAAA,IACpB;AAAA,EACF;AAAA,EAEA,MAAc,IAAO,IAAA,EAA0B;AAC7C,IAAA,MAAM,IAAA,GAAO,IAAI,eAAA,EAAgB;AACjC,IAAA,MAAM,QAAQ,UAAA,CAAW,MAAM,KAAK,KAAA,EAAM,EAAG,KAAK,SAAS,CAAA;AAC3D,IAAA,IAAI;AACF,MAAA,MAAM,GAAA,GAAM,MAAM,KAAA,CAAM,CAAA,EAAG,KAAK,OAAO,CAAA,EAAG,IAAI,CAAA,CAAA,EAAI;AAAA,QAChD,MAAA,EAAQ,KAAA;AAAA,QACR,OAAA,EAAS,KAAK,OAAA,EAAQ;AAAA,QACtB,QAAQ,IAAA,CAAK;AAAA,OACd,CAAA;AACD,MAAA,OAAO,IAAA,CAAK,cAAiB,GAAG,CAAA;AAAA,IAClC,CAAA,SAAE;AACA,MAAA,YAAA,CAAa,KAAK,CAAA;AAAA,IACpB;AAAA,EACF;AAAA,EAEA,MAAc,cAAiB,GAAA,EAA2B;AACxD,IAAA,MAAM,IAAA,GAAQ,MAAM,GAAA,CAAI,IAAA,EAAK;AAC7B,IAAA,IAAI,CAAC,IAAI,EAAA,EAAI;AACX,MAAA,MAAM,IAAI,eAAA;AAAA,QACP,IAAA,CAAK,OAAO,CAAA,IAAgB,cAAA;AAAA,QAC7B,GAAA,CAAI,MAAA;AAAA,QACJ;AAAA,OACF;AAAA,IACF;AACA,IAAA,OAAO,IAAA;AAAA,EACT;AACF;;;AC7NO,SAAS,aAAa,MAAA,EAAmC;AAC9D,EAAA,OAAO,IAAI,WAAW,MAAM,CAAA;AAC9B","file":"index.mjs","sourcesContent":["/**\n * Vercel AI SDK integration for Lelu — Confidence-Aware Auth.\n *\n * Wraps a Vercel AI SDK `tool()` definition with a Lelu authorization\n * gate. The wrapped tool runs the original `execute` function only when\n * Lelu allows it; otherwise it returns a structured refusal object that\n * the model can inspect and self-correct on.\n *\n * @example\n * ```ts\n * import { tool } from 'ai';\n * import { z } from 'zod';\n * import { LeluClient } from '@lelu-auth/lelu';\n * import { secureTool } from 'lelu/vercel';\n *\n * const lelu = new LeluClient({ baseUrl: 'http://localhost:8082' });\n *\n * const refundTool = secureTool({\n * client: lelu,\n * actor: 'invoice_bot',\n * action: 'invoice:refund',\n * confidence: 0.92,\n * tool: tool({\n * description: 'Process a customer refund',\n * parameters: z.object({ invoiceId: z.string() }),\n * execute: async ({ invoiceId }) => ({ refunded: invoiceId }),\n * }),\n * });\n *\n * // Use in streamText / generateText:\n * const result = await streamText({\n * model: openai('gpt-4o'),\n * tools: { refundTool },\n * });\n * ```\n */\n\nimport { LeluClient } from \"../client.js\";\n\n// ─── Minimal Vercel AI SDK tool type ──────────────────────────────────────────\n\n/**\n * The minimal shape of a Vercel AI SDK `tool()` return value.\n * We keep this local to avoid a hard dependency on `ai`.\n */\nexport interface VercelTool<TArgs = unknown, TResult = unknown> {\n description?: string;\n parameters: unknown;\n execute?: (args: TArgs, options?: unknown) => Promise<TResult>;\n}\n\n// ─── Denied / Review result shape ─────────────────────────────────────────────\n\nexport interface LeluDeniedResult {\n /** Always `false` when the tool was blocked. */\n allowed: false;\n /** Human/LLM-readable denial reason. */\n reason: string;\n /** Whether the action is queued for human review. */\n requiresHumanReview: boolean;\n /** The downgraded scope when confidence caused a downgrade. */\n downgradedScope?: string;\n}\n\n// ─── Options ──────────────────────────────────────────────────────────────────\n\nexport interface SecureToolOptions<TArgs = unknown, TResult = unknown> {\n /** Configured Lelu client. */\n client: LeluClient;\n /** The agent actor / scope registered in Lelu policy. */\n actor: string;\n /** The permission string being checked. */\n action: string;\n /**\n * LLM confidence score (0.0–1.0). Can be a static number or a function\n * receiving the parsed tool arguments, allowing dynamic confidence based\n * on the actual call. Defaults to `1.0`.\n */\n confidence?: number | ((args: TArgs) => number);\n /** Optional user ID the agent is acting on behalf of. */\n actingFor?: string;\n /** The original Vercel AI SDK tool to wrap. */\n tool: VercelTool<TArgs, TResult>;\n}\n\n// ─── secureTool ───────────────────────────────────────────────────────────────\n\n/**\n * Wraps a Vercel AI SDK `tool()` with Lelu Confidence-Aware Auth.\n *\n * Returns a new tool object with the same `description` and `parameters`\n * but with an `execute` function that gates through Lelu first.\n *\n * On denial the tool returns a `LeluDeniedResult` object (not a throw) so\n * the model sees a structured response it can reason about.\n */\nexport function secureTool<TArgs = unknown, TResult = unknown>(\n opts: SecureToolOptions<TArgs, TResult>\n): VercelTool<TArgs, TResult | LeluDeniedResult> {\n const { client, actor, action, actingFor } = opts;\n\n const wrapped: VercelTool<TArgs, TResult | LeluDeniedResult> = {\n parameters: opts.tool.parameters,\n\n async execute(\n args: TArgs,\n options?: unknown\n ): Promise<TResult | LeluDeniedResult> {\n // Resolve confidence — static number or dynamic function.\n const confidence =\n typeof opts.confidence === \"function\"\n ? opts.confidence(args)\n : (opts.confidence ?? 1.0);\n\n let decision;\n try {\n decision = await client.agentAuthorize({\n actor,\n action,\n context: { confidence, actingFor },\n });\n } catch (err) {\n // Fail open with a structured denial so the model can handle it.\n return {\n allowed: false,\n reason: `Lelu authorization check failed: ${String(err)}`,\n requiresHumanReview: false,\n };\n }\n\n // ── Human review required ──────────────────────────────────────────\n if (decision.requiresHumanReview) {\n return {\n allowed: false,\n reason:\n `Action '${action}' for agent '${actor}' is queued for human review. ` +\n `Reason: ${decision.reason}. Confidence: ${(confidence * 100).toFixed(0)}%.`,\n requiresHumanReview: true,\n };\n }\n\n // ── Hard deny ─────────────────────────────────────────────────────\n if (!decision.allowed) {\n const denied: LeluDeniedResult = {\n allowed: false,\n reason:\n `Action '${action}' was denied for agent '${actor}'. ` +\n `Reason: ${decision.reason}.`,\n requiresHumanReview: false,\n };\n if (decision.downgradedScope !== undefined) {\n denied.downgradedScope = decision.downgradedScope;\n }\n return {\n ...denied,\n };\n }\n\n // ── Authorized — run original execute ─────────────────────────────\n if (!opts.tool.execute) {\n throw new Error(\n `[Lelu] secureTool: the wrapped tool '${action}' has no execute function.`\n );\n }\n return opts.tool.execute(args, options);\n },\n };\n\n if (opts.tool.description !== undefined) {\n wrapped.description = opts.tool.description;\n }\n\n return wrapped;\n}\n","import { z } from \"zod\";\n\n// ─── Request / Response schemas ───────────────────────────────────────────────\n\nexport const AuthRequestSchema = z.object({\n userId: z.string().min(1, \"userId is required\"),\n action: z.string().min(1, \"action is required\"),\n resource: z.record(z.string()).optional(),\n});\n\nexport const AgentContextSchema = z.object({\n /** LLM confidence score — 0.0 to 1.0 */\n confidence: z.number().min(0).max(1),\n /** User the agent is acting on behalf of */\n actingFor: z.string().optional(),\n /** Requested agent scope */\n scope: z.string().optional(),\n});\n\nexport const AgentAuthRequestSchema = z.object({\n actor: z.string().min(1, \"actor is required\"),\n action: z.string().min(1, \"action is required\"),\n resource: z.record(z.string()).optional(),\n context: AgentContextSchema,\n});\n\nexport const MintTokenRequestSchema = z.object({\n scope: z.string().min(1),\n actingFor: z.string().optional(),\n ttlSeconds: z.number().int().positive().optional(),\n});\n\nexport const DelegateScopeRequestSchema = z.object({\n delegator: z.string().min(1, \"delegator is required\"),\n delegatee: z.string().min(1, \"delegatee is required\"),\n scopedTo: z.array(z.string().min(1)).optional(),\n ttlSeconds: z.number().int().positive().optional(),\n confidence: z.number().min(0).max(1).optional(),\n actingFor: z.string().optional(),\n tenantId: z.string().optional(),\n});\n\n// ─── Decision types ───────────────────────────────────────────────────────────\n\nexport interface AuthDecision {\n allowed: boolean;\n reason: string;\n traceId: string;\n}\n\nexport interface AgentAuthDecision {\n allowed: boolean;\n reason: string;\n traceId: string;\n downgradedScope: string | undefined;\n requiresHumanReview: boolean;\n confidenceUsed: number;\n}\n\nexport interface MintTokenResult {\n token: string;\n tokenId: string;\n expiresAt: Date;\n}\n\nexport interface DelegateScopeRequest {\n /** Agent delegating the scope */\n delegator: string;\n /** Agent receiving the constrained sub-scope */\n delegatee: string;\n /** Actions to grant (must be subset of policy's can_delegate.scoped_to) */\n scopedTo?: string[];\n /** Token TTL in seconds — capped by the policy's max_ttl_seconds */\n ttlSeconds?: number;\n /** Delegator's confidence score — checked against require_confidence_above */\n confidence?: number;\n /** User the delegated agent acts on behalf of */\n actingFor?: string;\n tenantId?: string;\n}\n\nexport interface DelegateScopeResult {\n token: string;\n tokenId: string;\n expiresAt: Date;\n delegator: string;\n delegatee: string;\n grantedScopes: string[];\n traceId: string;\n}\n\nexport interface RevokeTokenResult {\n success: boolean;\n}\n\n// ─── Typed Input types ───────────────────────────────────────────────────────\n\nexport type AuthRequest = z.infer<typeof AuthRequestSchema>;\nexport type AgentAuthRequest = z.infer<typeof AgentAuthRequestSchema>;\nexport type AgentContext = z.infer<typeof AgentContextSchema>;\nexport type MintTokenRequest = z.infer<typeof MintTokenRequestSchema>;\n\n// ─── Client config ────────────────────────────────────────────────────────────\n\nexport interface ClientConfig {\n /** Base URL of the Auth Permission Engine (default: http://localhost:8080) */\n baseUrl?: string;\n /** Request timeout in milliseconds (default: 5000) */\n timeoutMs?: number;\n /** Optional bearer token for authenticating with the engine */\n apiKey?: string;\n}\n\n// ─── Error type ───────────────────────────────────────────────────────────────\n\nexport class AuthEngineError extends Error {\n constructor(\n message: string,\n public readonly status?: number,\n public readonly details?: unknown\n ) {\n super(message);\n this.name = \"AuthEngineError\";\n }\n}\n","import {\n AuthEngineError,\n AuthRequestSchema,\n AgentAuthRequestSchema,\n MintTokenRequestSchema,\n DelegateScopeRequestSchema,\n type AuthDecision,\n type AgentAuthDecision,\n type MintTokenResult,\n type DelegateScopeResult,\n type DelegateScopeRequest,\n type RevokeTokenResult,\n type AuthRequest,\n type AgentAuthRequest,\n type MintTokenRequest,\n type ClientConfig,\n} from \"./types.js\";\n\n// ─── Client ───────────────────────────────────────────────────────────────────\n\n/**\n * LeluClient is the core SDK entry-point. It communicates with the local\n * Auth Permission Engine sidecar over HTTP/JSON.\n *\n * @example\n * ```ts\n * const lelu = new LeluClient({ baseUrl: \"http://localhost:8080\" });\n *\n * const decision = await lelu.agentAuthorize({\n * actor: \"invoice_bot\",\n * action: \"approve_refunds\",\n * context: { confidence: 0.92, actingFor: \"user_123\" },\n * });\n *\n * if (!decision.allowed) {\n * console.log(decision.reason);\n * }\n * ```\n */\nexport class LeluClient {\n private readonly baseUrl: string;\n private readonly timeoutMs: number;\n private readonly apiKey: string | undefined;\n\n constructor(cfg: ClientConfig = {}) {\n this.baseUrl = (cfg.baseUrl ?? \"http://localhost:8080\").replace(/\\/$/, \"\");\n this.timeoutMs = cfg.timeoutMs ?? 5_000;\n this.apiKey = cfg.apiKey;\n }\n\n // ── Human authorization ────────────────────────────────────────────────────\n\n /**\n * Checks whether a human user is permitted to perform an action.\n */\n async authorize(req: AuthRequest): Promise<AuthDecision> {\n const validated = AuthRequestSchema.parse(req);\n const body = {\n user_id: validated.userId,\n action: validated.action,\n resource: validated.resource,\n };\n const data = await this.post<{\n allowed: boolean;\n reason: string;\n trace_id: string;\n }>(\"/v1/authorize\", body);\n\n return {\n allowed: data.allowed,\n reason: data.reason,\n traceId: data.trace_id,\n };\n }\n\n // ── Agent authorization ────────────────────────────────────────────────────\n\n /**\n * Checks whether an AI agent is permitted to perform an action, taking the\n * confidence score into account (Confidence-Aware Auth ★).\n */\n async agentAuthorize(req: AgentAuthRequest): Promise<AgentAuthDecision> {\n const validated = AgentAuthRequestSchema.parse(req);\n const body = {\n actor: validated.actor,\n action: validated.action,\n resource: validated.resource,\n confidence: validated.context.confidence,\n acting_for: validated.context.actingFor,\n scope: validated.context.scope,\n };\n const data = await this.post<{\n allowed: boolean;\n reason: string;\n trace_id: string;\n downgraded_scope?: string;\n requires_human_review: boolean;\n confidence_used: number;\n }>(\"/v1/agent/authorize\", body);\n\n return {\n allowed: data.allowed,\n reason: data.reason,\n traceId: data.trace_id,\n downgradedScope: data.downgraded_scope,\n requiresHumanReview: data.requires_human_review,\n confidenceUsed: data.confidence_used,\n };\n }\n\n // ── JIT Token minting ──────────────────────────────────────────────────────\n\n /**\n * Mints a scoped JWT for an agent with an optional TTL.\n * Default TTL is 60 seconds.\n */\n async mintToken(req: MintTokenRequest): Promise<MintTokenResult> {\n const validated = MintTokenRequestSchema.parse(req);\n const body = {\n scope: validated.scope,\n acting_for: validated.actingFor,\n ttl_seconds: validated.ttlSeconds ?? 60,\n };\n const data = await this.post<{\n token: string;\n token_id: string;\n expires_at: number;\n }>(\"/v1/tokens/mint\", body);\n\n return {\n token: data.token,\n tokenId: data.token_id,\n expiresAt: new Date(data.expires_at * 1000),\n };\n }\n\n // ── Token revocation ───────────────────────────────────────────────────────\n\n /**\n * Immediately revokes a JIT token by its ID.\n */\n async revokeToken(tokenId: string): Promise<RevokeTokenResult> {\n const data = await this.delete<{ success: boolean }>(\n `/v1/tokens/${encodeURIComponent(tokenId)}`\n );\n return { success: data.success };\n }\n\n // ── Multi-agent delegation ─────────────────────────────────────────────────\n\n /**\n * Delegates a constrained sub-scope from one agent to another.\n *\n * Validates the delegation rule in the loaded policy, caps the TTL to the\n * policy maximum, and mints a child JIT token scoped to the granted actions.\n *\n * The delegator's `confidence` score is checked against the policy's\n * `require_confidence_above` before delegation is granted.\n */\n async delegateScope(req: DelegateScopeRequest): Promise<DelegateScopeResult> {\n const validated = DelegateScopeRequestSchema.parse(req);\n const body = {\n delegator: validated.delegator,\n delegatee: validated.delegatee,\n scoped_to: validated.scopedTo ?? [],\n ttl_seconds: validated.ttlSeconds ?? 60,\n confidence: validated.confidence ?? 1.0,\n acting_for: validated.actingFor ?? \"\",\n tenant_id: validated.tenantId ?? \"\",\n };\n const data = await this.post<{\n token: string;\n token_id: string;\n expires_at: number;\n delegator: string;\n delegatee: string;\n granted_scopes: string[];\n trace_id: string;\n }>(\"/v1/agent/delegate\", body);\n\n return {\n token: data.token,\n tokenId: data.token_id,\n expiresAt: new Date(data.expires_at * 1000),\n delegator: data.delegator,\n delegatee: data.delegatee,\n grantedScopes: data.granted_scopes,\n traceId: data.trace_id,\n };\n }\n\n // ── Health check ───────────────────────────────────────────────────────────\n\n /**\n * Returns true if the engine is reachable and healthy.\n */\n async isHealthy(): Promise<boolean> {\n try {\n const data = await this.get<{ status: string }>(\"/healthz\");\n return data.status === \"ok\";\n } catch {\n return false;\n }\n }\n\n // ── HTTP helpers ───────────────────────────────────────────────────────────\n\n private headers(): Record<string, string> {\n const h: Record<string, string> = { \"Content-Type\": \"application/json\" };\n if (this.apiKey) {\n h[\"Authorization\"] = `Bearer ${this.apiKey}`;\n }\n return h;\n }\n\n private async post<T>(path: string, body: unknown): Promise<T> {\n const ctrl = new AbortController();\n const timer = setTimeout(() => ctrl.abort(), this.timeoutMs);\n try {\n const res = await fetch(`${this.baseUrl}${path}`, {\n method: \"POST\",\n headers: this.headers(),\n body: JSON.stringify(body),\n signal: ctrl.signal,\n });\n return this.parseResponse<T>(res);\n } finally {\n clearTimeout(timer);\n }\n }\n\n private async delete<T>(path: string): Promise<T> {\n const ctrl = new AbortController();\n const timer = setTimeout(() => ctrl.abort(), this.timeoutMs);\n try {\n const res = await fetch(`${this.baseUrl}${path}`, {\n method: \"DELETE\",\n headers: this.headers(),\n signal: ctrl.signal,\n });\n return this.parseResponse<T>(res);\n } finally {\n clearTimeout(timer);\n }\n }\n\n private async get<T>(path: string): Promise<T> {\n const ctrl = new AbortController();\n const timer = setTimeout(() => ctrl.abort(), this.timeoutMs);\n try {\n const res = await fetch(`${this.baseUrl}${path}`, {\n method: \"GET\",\n headers: this.headers(),\n signal: ctrl.signal,\n });\n return this.parseResponse<T>(res);\n } finally {\n clearTimeout(timer);\n }\n }\n\n private async parseResponse<T>(res: Response): Promise<T> {\n const json = (await res.json()) as Record<string, unknown>;\n if (!res.ok) {\n throw new AuthEngineError(\n (json[\"error\"] as string) ?? \"engine error\",\n res.status,\n json\n );\n }\n return json as T;\n }\n}\n","// Auth Permission Engine — TypeScript SDK\n// Public API surface\n\n// ─── Vercel AI SDK integration ────────────────────────────────────────────────\n// Import via: import { secureTool } from 'lelu/vercel'\n// (tree-shakeable — does not add weight to non-Vercel users)\nexport { secureTool } from \"./vercel/index.js\";\nexport type { SecureToolOptions, LeluDeniedResult, VercelTool } from \"./vercel/index.js\";\n\nexport { LeluClient } from \"./client.js\";\n\nexport type {\n AuthRequest,\n AgentAuthRequest,\n AgentContext,\n MintTokenRequest,\n DelegateScopeRequest,\n AuthDecision,\n AgentAuthDecision,\n MintTokenResult,\n DelegateScopeResult,\n RevokeTokenResult,\n ClientConfig,\n} from \"./types.js\";\n\nexport {\n AuthEngineError,\n AuthRequestSchema,\n AgentAuthRequestSchema,\n AgentContextSchema,\n MintTokenRequestSchema,\n DelegateScopeRequestSchema,\n} from \"./types.js\";\n\n// ─── Convenience factory ──────────────────────────────────────────────────────\n\nimport { LeluClient } from \"./client.js\";\nimport type { ClientConfig } from \"./types.js\";\n\n/**\n * Creates a LeluClient with the given configuration.\n * Equivalent to `new LeluClient(config)`.\n *\n * @example\n * ```ts\n * import { createClient } from \"@lelu-auth/lelu\";\n *\n * const lelu = createClient({ baseUrl: \"http://localhost:8080\" });\n * const { allowed } = await lelu.agentAuthorize({ ... });\n * ```\n */\nexport function createClient(config?: ClientConfig): LeluClient {\n return new LeluClient(config);\n}\n"]}
@@ -0,0 +1,110 @@
1
+ import { L as LeluClient } from '../client-BD9h8CBT.mjs';
2
+ import 'zod';
3
+
4
+ /**
5
+ * SecureTool — LangChain tool wrapper with Confidence-Aware Auth
6
+ *
7
+ * Intercepts every tool call and gates it through the Lelu engine before
8
+ * execution. Returns a structured refusal string when denied so the LLM can
9
+ * self-correct, queue for human review, or escalate.
10
+ *
11
+ * Usage:
12
+ * ```typescript
13
+ * import { SecureTool } from 'lelu/langchain';
14
+ *
15
+ * const refundTool = new SecureTool({
16
+ * name: 'process_refund',
17
+ * description: 'Processes a customer refund',
18
+ * actor: 'invoice_bot',
19
+ * requiredPermission: 'invoice:refund',
20
+ * client: leluClient,
21
+ * func: async (input) => {
22
+ * // your tool implementation
23
+ * return `Refund processed for ${input}`;
24
+ * },
25
+ * });
26
+ * ```
27
+ */
28
+
29
+ interface SecureToolOptions {
30
+ /** Unique tool name (used as the action in authz request). */
31
+ name: string;
32
+ /** Human-readable description forwarded to the LLM. */
33
+ description: string;
34
+ /** The Lelu agent scope / actor name. */
35
+ actor: string;
36
+ /** The permission string being checked (e.g. "invoice:refund"). */
37
+ requiredPermission: string;
38
+ /** Configured LeluClient. */
39
+ client: LeluClient;
40
+ /** The actual tool function to execute when authorized. */
41
+ func: (input: string) => Promise<string>;
42
+ /**
43
+ * Optional: LLM confidence score for this invocation (0.0–1.0).
44
+ * If omitted, defaults to 1.0 (full confidence assumed).
45
+ */
46
+ confidence?: number;
47
+ /**
48
+ * Optional: the human user the agent is acting on behalf of.
49
+ */
50
+ actingFor?: string;
51
+ /**
52
+ * Optional: if true, throw an error when denied instead of returning
53
+ * a structured refusal string. Default: false (silent fail, structured msg).
54
+ */
55
+ throwOnDeny?: boolean;
56
+ }
57
+ interface ToolCallResult {
58
+ allowed: boolean;
59
+ output: string;
60
+ requiresHumanReview: boolean;
61
+ reviewId?: string;
62
+ reason: string;
63
+ }
64
+ /**
65
+ * SecureTool wraps a tool function with Lelu's Confidence-Aware Auth gate.
66
+ *
67
+ * Implements the LangChain Tool interface (name + description + invoke)
68
+ * so it can be dropped into any LangChain agent tool array:
69
+ * ```typescript
70
+ * const agent = await createOpenAIFunctionsAgent({ tools: [refundTool] });
71
+ * ```
72
+ */
73
+ declare class SecureTool {
74
+ readonly name: string;
75
+ readonly description: string;
76
+ private readonly opts;
77
+ constructor(opts: SecureToolOptions);
78
+ /**
79
+ * invoke — LangChain StructuredTool / DynamicTool compatible entry point.
80
+ *
81
+ * 1. Calls Lelu `agentAuthorize` with the confidence score
82
+ * 2a. Allowed → runs the wrapped tool function
83
+ * 2b. Requires human review → returns structured "pending" message
84
+ * 2c. Denied → returns structured refusal string (LLM can self-correct)
85
+ */
86
+ invoke(input: string): Promise<string>;
87
+ /** call — alias for older LangChain versions. */
88
+ call(input: string): Promise<string>;
89
+ /**
90
+ * Check authorization and run the tool if permitted.
91
+ * Returns a full ToolCallResult for programmatic consumers.
92
+ */
93
+ checkAndCall(input: string): Promise<ToolCallResult>;
94
+ private _check;
95
+ }
96
+
97
+ declare class SemanticPolicyGenerator {
98
+ private openai;
99
+ constructor(apiKey: string);
100
+ /**
101
+ * Converts a natural language description into a deterministic Rego policy.
102
+ *
103
+ * @param description Natural language description of the policy (e.g., "Don't let the bot refund more than $50 unless approved by a finance manager.")
104
+ * @param packageName The Rego package name (default: "lelu.authz")
105
+ * @returns The generated Rego policy string
106
+ */
107
+ generateRegoPolicy(description: string, packageName?: string): Promise<string>;
108
+ }
109
+
110
+ export { SecureTool, type SecureToolOptions, SemanticPolicyGenerator, type ToolCallResult };
@@ -0,0 +1,110 @@
1
+ import { L as LeluClient } from '../client-BD9h8CBT.js';
2
+ import 'zod';
3
+
4
+ /**
5
+ * SecureTool — LangChain tool wrapper with Confidence-Aware Auth
6
+ *
7
+ * Intercepts every tool call and gates it through the Lelu engine before
8
+ * execution. Returns a structured refusal string when denied so the LLM can
9
+ * self-correct, queue for human review, or escalate.
10
+ *
11
+ * Usage:
12
+ * ```typescript
13
+ * import { SecureTool } from 'lelu/langchain';
14
+ *
15
+ * const refundTool = new SecureTool({
16
+ * name: 'process_refund',
17
+ * description: 'Processes a customer refund',
18
+ * actor: 'invoice_bot',
19
+ * requiredPermission: 'invoice:refund',
20
+ * client: leluClient,
21
+ * func: async (input) => {
22
+ * // your tool implementation
23
+ * return `Refund processed for ${input}`;
24
+ * },
25
+ * });
26
+ * ```
27
+ */
28
+
29
+ interface SecureToolOptions {
30
+ /** Unique tool name (used as the action in authz request). */
31
+ name: string;
32
+ /** Human-readable description forwarded to the LLM. */
33
+ description: string;
34
+ /** The Lelu agent scope / actor name. */
35
+ actor: string;
36
+ /** The permission string being checked (e.g. "invoice:refund"). */
37
+ requiredPermission: string;
38
+ /** Configured LeluClient. */
39
+ client: LeluClient;
40
+ /** The actual tool function to execute when authorized. */
41
+ func: (input: string) => Promise<string>;
42
+ /**
43
+ * Optional: LLM confidence score for this invocation (0.0–1.0).
44
+ * If omitted, defaults to 1.0 (full confidence assumed).
45
+ */
46
+ confidence?: number;
47
+ /**
48
+ * Optional: the human user the agent is acting on behalf of.
49
+ */
50
+ actingFor?: string;
51
+ /**
52
+ * Optional: if true, throw an error when denied instead of returning
53
+ * a structured refusal string. Default: false (silent fail, structured msg).
54
+ */
55
+ throwOnDeny?: boolean;
56
+ }
57
+ interface ToolCallResult {
58
+ allowed: boolean;
59
+ output: string;
60
+ requiresHumanReview: boolean;
61
+ reviewId?: string;
62
+ reason: string;
63
+ }
64
+ /**
65
+ * SecureTool wraps a tool function with Lelu's Confidence-Aware Auth gate.
66
+ *
67
+ * Implements the LangChain Tool interface (name + description + invoke)
68
+ * so it can be dropped into any LangChain agent tool array:
69
+ * ```typescript
70
+ * const agent = await createOpenAIFunctionsAgent({ tools: [refundTool] });
71
+ * ```
72
+ */
73
+ declare class SecureTool {
74
+ readonly name: string;
75
+ readonly description: string;
76
+ private readonly opts;
77
+ constructor(opts: SecureToolOptions);
78
+ /**
79
+ * invoke — LangChain StructuredTool / DynamicTool compatible entry point.
80
+ *
81
+ * 1. Calls Lelu `agentAuthorize` with the confidence score
82
+ * 2a. Allowed → runs the wrapped tool function
83
+ * 2b. Requires human review → returns structured "pending" message
84
+ * 2c. Denied → returns structured refusal string (LLM can self-correct)
85
+ */
86
+ invoke(input: string): Promise<string>;
87
+ /** call — alias for older LangChain versions. */
88
+ call(input: string): Promise<string>;
89
+ /**
90
+ * Check authorization and run the tool if permitted.
91
+ * Returns a full ToolCallResult for programmatic consumers.
92
+ */
93
+ checkAndCall(input: string): Promise<ToolCallResult>;
94
+ private _check;
95
+ }
96
+
97
+ declare class SemanticPolicyGenerator {
98
+ private openai;
99
+ constructor(apiKey: string);
100
+ /**
101
+ * Converts a natural language description into a deterministic Rego policy.
102
+ *
103
+ * @param description Natural language description of the policy (e.g., "Don't let the bot refund more than $50 unless approved by a finance manager.")
104
+ * @param packageName The Rego package name (default: "lelu.authz")
105
+ * @returns The generated Rego policy string
106
+ */
107
+ generateRegoPolicy(description: string, packageName?: string): Promise<string>;
108
+ }
109
+
110
+ export { SecureTool, type SecureToolOptions, SemanticPolicyGenerator, type ToolCallResult };