@clampd/sdk 0.5.2 → 0.6.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 +76 -23
- package/dist/client.d.ts +29 -0
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +104 -39
- package/dist/client.js.map +1 -1
- package/dist/index.d.ts +63 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +226 -22
- package/dist/index.js.map +1 -1
- package/dist/stream-guard.d.ts +39 -0
- package/dist/stream-guard.d.ts.map +1 -0
- package/dist/stream-guard.js +245 -0
- package/dist/stream-guard.js.map +1 -0
- package/dist/tool-verify.js +1 -1
- package/dist/tool-verify.js.map +1 -1
- package/package.json +9 -5
package/README.md
CHANGED
|
@@ -36,32 +36,41 @@ const response = await client.chat.completions.create({
|
|
|
36
36
|
// Prompts scanned before LLM, responses scanned after
|
|
37
37
|
```
|
|
38
38
|
|
|
39
|
-
## What's New in 0.
|
|
39
|
+
## What's New in 0.5.0
|
|
40
40
|
|
|
41
|
-
-
|
|
42
|
-
-
|
|
43
|
-
-
|
|
41
|
+
- **Per-agent JWT identity** — each agent in a multi-agent system authenticates independently. Kill/rate-limit/EMA operate per-agent.
|
|
42
|
+
- **Streaming guard** — opt-in tool call interception for streaming responses (`guardStream: true`)
|
|
43
|
+
- **Circuit breaker & retry** — automatic retry with exponential backoff, circuit breaker for gateway failures
|
|
44
|
+
- **CrewAI, ADK, Vercel AI wrappers** — guard tool calls across all major frameworks
|
|
45
|
+
- **216 detection rules** with Aho-Corasick prefilter (22μs at 10K rules)
|
|
44
46
|
|
|
45
47
|
## Configuration
|
|
46
48
|
|
|
47
|
-
Three ways to configure (pick one):
|
|
48
|
-
|
|
49
49
|
```typescript
|
|
50
|
-
// Option 1:
|
|
51
|
-
|
|
52
|
-
// CLAMPD_API_KEY=ag_live_...
|
|
53
|
-
// CLAMPD_AGENT_ID=my-agent
|
|
54
|
-
// JWT_SECRET=ags_...
|
|
55
|
-
|
|
56
|
-
// Option 2: Global init (recommended)
|
|
57
|
-
clampd.init({ agentId: "my-agent", secret: "ags_...", gatewayUrl: "...", apiKey: "..." });
|
|
58
|
-
|
|
59
|
-
// Option 3: Inline per-call
|
|
60
|
-
const safeFn = clampd.guard(myFn, {
|
|
61
|
-
toolName: "db.query",
|
|
50
|
+
// Option 1: Single agent (simple)
|
|
51
|
+
clampd.init({
|
|
62
52
|
agentId: "my-agent",
|
|
63
|
-
secret: "ags_...",
|
|
53
|
+
secret: "ags_...", // from dashboard → Agent → Secret
|
|
54
|
+
gatewayUrl: "http://localhost:8080",
|
|
55
|
+
apiKey: "ag_live_...",
|
|
64
56
|
});
|
|
57
|
+
|
|
58
|
+
// Option 2: Multi-agent (per-agent identity)
|
|
59
|
+
clampd.init({
|
|
60
|
+
agentId: "orchestrator",
|
|
61
|
+
apiKey: "ag_live_...",
|
|
62
|
+
agents: {
|
|
63
|
+
"orchestrator": process.env.CLAMPD_SECRET_orchestrator,
|
|
64
|
+
"research-agent": process.env.CLAMPD_SECRET_research_agent,
|
|
65
|
+
"writer-agent": process.env.CLAMPD_SECRET_writer_agent,
|
|
66
|
+
},
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
// Option 3: Environment variables
|
|
70
|
+
// CLAMPD_GATEWAY_URL=http://localhost:8080
|
|
71
|
+
// CLAMPD_API_KEY=ag_live_...
|
|
72
|
+
// CLAMPD_SECRET_orchestrator=ags_...
|
|
73
|
+
// CLAMPD_SECRET_research_agent=ags_...
|
|
65
74
|
```
|
|
66
75
|
|
|
67
76
|
## Anthropic / Claude
|
|
@@ -116,6 +125,49 @@ const client = clampd.openai(new OpenAI(), {
|
|
|
116
125
|
});
|
|
117
126
|
```
|
|
118
127
|
|
|
128
|
+
## Multi-Agent (A2A Delegation)
|
|
129
|
+
|
|
130
|
+
```typescript
|
|
131
|
+
import clampd from "@clampd/sdk";
|
|
132
|
+
|
|
133
|
+
// Each agent authenticates with its own secret.
|
|
134
|
+
// Delegation chains are tracked automatically via AsyncLocalStorage.
|
|
135
|
+
clampd.init({
|
|
136
|
+
agentId: "orchestrator",
|
|
137
|
+
apiKey: "ag_live_...",
|
|
138
|
+
agents: {
|
|
139
|
+
"orchestrator": process.env.CLAMPD_SECRET_orchestrator,
|
|
140
|
+
"research-agent": process.env.CLAMPD_SECRET_research_agent,
|
|
141
|
+
},
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
// research-agent gets its own JWT (sub=research-agent).
|
|
145
|
+
// Kill "research-agent" from dashboard → only this agent is blocked.
|
|
146
|
+
const search = clampd.guard(searchFn, {
|
|
147
|
+
agentId: "research-agent",
|
|
148
|
+
toolName: "web.search",
|
|
149
|
+
});
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
## Streaming Guard (opt-in)
|
|
153
|
+
|
|
154
|
+
```typescript
|
|
155
|
+
// Stream tool calls are guarded only when guardStream is enabled.
|
|
156
|
+
const client = clampd.openai(new OpenAI(), {
|
|
157
|
+
agentId: "my-agent",
|
|
158
|
+
guardStream: true, // buffer + guard tool call chunks before release
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
const stream = await client.chat.completions.create({
|
|
162
|
+
model: "gpt-4o",
|
|
163
|
+
stream: true,
|
|
164
|
+
tools: [...],
|
|
165
|
+
messages: [{ role: "user", content: "..." }],
|
|
166
|
+
});
|
|
167
|
+
// Tool calls in the stream are buffered, guarded, then released.
|
|
168
|
+
// Text chunks pass through immediately with zero added latency.
|
|
169
|
+
```
|
|
170
|
+
|
|
119
171
|
## Tool Definitions Wrapper
|
|
120
172
|
|
|
121
173
|
```typescript
|
|
@@ -144,11 +196,12 @@ try {
|
|
|
144
196
|
|
|
145
197
|
| Function | Description |
|
|
146
198
|
|----------|-------------|
|
|
147
|
-
| `clampd.init(opts)` | Configure global client
|
|
148
|
-
| `clampd.openai(client, opts?)` | Wrap OpenAI client
|
|
149
|
-
| `clampd.anthropic(client, opts?)` | Wrap Anthropic client
|
|
150
|
-
| `clampd.guard(fn, opts)` | Wrap any async function |
|
|
199
|
+
| `clampd.init(opts)` | Configure global client. `agents` for per-agent secrets. |
|
|
200
|
+
| `clampd.openai(client, opts?)` | Wrap OpenAI client. `guardStream: true` for streaming. |
|
|
201
|
+
| `clampd.anthropic(client, opts?)` | Wrap Anthropic client. `guardStream: true` for streaming. |
|
|
202
|
+
| `clampd.guard(fn, opts)` | Wrap any async function. `agentId` for per-agent identity. |
|
|
151
203
|
| `clampd.tools(defs, opts)` | Wrap OpenAI tool definitions |
|
|
204
|
+
| `clampd.agent(agentId, fn)` | Run function in agent's delegation scope |
|
|
152
205
|
|
|
153
206
|
## Requirements
|
|
154
207
|
|
package/dist/client.d.ts
CHANGED
|
@@ -48,12 +48,28 @@ export interface VerifyRequest {
|
|
|
48
48
|
params: Record<string, unknown>;
|
|
49
49
|
target_url?: string;
|
|
50
50
|
}
|
|
51
|
+
export interface CircuitBreakerOptions {
|
|
52
|
+
/** Number of consecutive failures before opening the circuit. Default: 5. */
|
|
53
|
+
threshold?: number;
|
|
54
|
+
/** Time in ms to keep circuit open before allowing a probe. Default: 30000. */
|
|
55
|
+
resetTimeoutMs?: number;
|
|
56
|
+
}
|
|
57
|
+
export interface RetryOptions {
|
|
58
|
+
/** Max retry attempts (0 = no retries). Default: 0. */
|
|
59
|
+
maxRetries?: number;
|
|
60
|
+
/** Base delay in ms for exponential backoff. Default: 500. */
|
|
61
|
+
baseDelayMs?: number;
|
|
62
|
+
}
|
|
51
63
|
export interface ClampdClientOptions {
|
|
52
64
|
gatewayUrl?: string;
|
|
53
65
|
agentId: string;
|
|
54
66
|
apiKey?: string;
|
|
55
67
|
secret?: string;
|
|
56
68
|
timeoutMs?: number;
|
|
69
|
+
/** Retry options for transient gateway errors. */
|
|
70
|
+
retry?: RetryOptions;
|
|
71
|
+
/** Circuit breaker to avoid hammering a failing gateway. */
|
|
72
|
+
circuitBreaker?: CircuitBreakerOptions;
|
|
57
73
|
}
|
|
58
74
|
/**
|
|
59
75
|
* Synchronous-style (async/await) client for the Clampd gateway proxy API.
|
|
@@ -66,7 +82,20 @@ export declare class ClampdClient {
|
|
|
66
82
|
private readonly apiKey;
|
|
67
83
|
private readonly jwt;
|
|
68
84
|
private readonly timeoutMs;
|
|
85
|
+
private readonly maxRetries;
|
|
86
|
+
private readonly baseDelayMs;
|
|
87
|
+
private readonly cbThreshold;
|
|
88
|
+
private readonly cbResetTimeoutMs;
|
|
89
|
+
private cbFailures;
|
|
90
|
+
private cbOpenedAt;
|
|
91
|
+
private cbState;
|
|
69
92
|
constructor(opts: ClampdClientOptions);
|
|
93
|
+
/** Check if circuit breaker allows a request. */
|
|
94
|
+
private cbAllowRequest;
|
|
95
|
+
/** Record a successful request. */
|
|
96
|
+
private cbRecordSuccess;
|
|
97
|
+
/** Record a failed request. */
|
|
98
|
+
private cbRecordFailure;
|
|
70
99
|
private headers;
|
|
71
100
|
/**
|
|
72
101
|
* Send a tool call through the Clampd gateway for evaluation.
|
package/dist/client.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA;;GAEG;AAOH,MAAM,WAAW,aAAa;IAC5B,UAAU,EAAE,MAAM,CAAC;IACnB,OAAO,EAAE,OAAO,CAAC;IACjB,iDAAiD;IACjD,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,UAAU,EAAE,MAAM,CAAC;IACnB,aAAa,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC9B,aAAa,CAAC,EAAE,OAAO,GAAG,IAAI,CAAC;IAC/B,aAAa,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC9B,yDAAyD;IACzD,SAAS,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B,kEAAkE;IAClE,aAAa,CAAC,EAAE,MAAM,EAAE,CAAC;IACzB,UAAU,EAAE,MAAM,CAAC;IACnB,eAAe,EAAE,MAAM,EAAE,CAAC;IAC1B,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.js";
|
|
6
6
|
// ── Synthesized error response ─────────────────────────────────────
|
|
7
|
-
function blockedResponse(reason) {
|
|
7
|
+
function blockedResponse(reason, gatewayError = false) {
|
|
8
8
|
return {
|
|
9
9
|
request_id: "error",
|
|
10
10
|
allowed: false,
|
|
@@ -14,6 +14,7 @@ function blockedResponse(reason) {
|
|
|
14
14
|
latency_ms: 0,
|
|
15
15
|
degraded_stages: [],
|
|
16
16
|
session_flags: [],
|
|
17
|
+
_gatewayError: gatewayError,
|
|
17
18
|
};
|
|
18
19
|
}
|
|
19
20
|
// ── Client ─────────────────────────────────────────────────────────
|
|
@@ -28,12 +29,54 @@ export class ClampdClient {
|
|
|
28
29
|
apiKey;
|
|
29
30
|
jwt;
|
|
30
31
|
timeoutMs;
|
|
32
|
+
// Retry config
|
|
33
|
+
maxRetries;
|
|
34
|
+
baseDelayMs;
|
|
35
|
+
// Circuit breaker state
|
|
36
|
+
cbThreshold;
|
|
37
|
+
cbResetTimeoutMs;
|
|
38
|
+
cbFailures = 0;
|
|
39
|
+
cbOpenedAt = 0;
|
|
40
|
+
cbState = "closed";
|
|
31
41
|
constructor(opts) {
|
|
32
42
|
this.gatewayUrl = (opts.gatewayUrl ?? "http://localhost:8080").replace(/\/$/, "");
|
|
33
43
|
this.agentId = opts.agentId;
|
|
34
44
|
this.apiKey = opts.apiKey ?? process.env.CLAMPD_API_KEY ?? "";
|
|
35
45
|
this.jwt = makeAgentJwt(this.agentId, { secret: opts.secret });
|
|
36
46
|
this.timeoutMs = opts.timeoutMs ?? 30_000;
|
|
47
|
+
// Retry
|
|
48
|
+
this.maxRetries = opts.retry?.maxRetries ?? 0;
|
|
49
|
+
this.baseDelayMs = opts.retry?.baseDelayMs ?? 500;
|
|
50
|
+
// Circuit breaker
|
|
51
|
+
this.cbThreshold = opts.circuitBreaker?.threshold ?? 5;
|
|
52
|
+
this.cbResetTimeoutMs = opts.circuitBreaker?.resetTimeoutMs ?? 30_000;
|
|
53
|
+
}
|
|
54
|
+
/** Check if circuit breaker allows a request. */
|
|
55
|
+
cbAllowRequest() {
|
|
56
|
+
if (this.cbState === "closed")
|
|
57
|
+
return true;
|
|
58
|
+
if (this.cbState === "open") {
|
|
59
|
+
if (Date.now() - this.cbOpenedAt >= this.cbResetTimeoutMs) {
|
|
60
|
+
this.cbState = "half-open";
|
|
61
|
+
return true; // Allow one probe request
|
|
62
|
+
}
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
// half-open: already allowing one probe
|
|
66
|
+
return true;
|
|
67
|
+
}
|
|
68
|
+
/** Record a successful request. */
|
|
69
|
+
cbRecordSuccess() {
|
|
70
|
+
this.cbFailures = 0;
|
|
71
|
+
this.cbState = "closed";
|
|
72
|
+
}
|
|
73
|
+
/** Record a failed request. */
|
|
74
|
+
cbRecordFailure() {
|
|
75
|
+
this.cbFailures++;
|
|
76
|
+
if (this.cbFailures >= this.cbThreshold) {
|
|
77
|
+
this.cbState = "open";
|
|
78
|
+
this.cbOpenedAt = Date.now();
|
|
79
|
+
}
|
|
37
80
|
}
|
|
38
81
|
headers(tools) {
|
|
39
82
|
const h = {
|
|
@@ -69,10 +112,11 @@ export class ClampdClient {
|
|
|
69
112
|
if (toolDescriptorHash) {
|
|
70
113
|
body.tool_descriptor_hash = toolDescriptorHash;
|
|
71
114
|
}
|
|
72
|
-
// Auto-include delegation context if present
|
|
115
|
+
// Auto-include delegation context if present.
|
|
116
|
+
// SDK sends chain as-is — the gateway appends the current agent (from JWT sub)
|
|
117
|
+
// and computes caller_agent_id from the complete chain.
|
|
73
118
|
const delegation = getDelegation();
|
|
74
|
-
if (delegation && delegation.chain.length >
|
|
75
|
-
body.caller_agent_id = delegation.chain[delegation.chain.length - 2];
|
|
119
|
+
if (delegation && delegation.chain.length > 0) {
|
|
76
120
|
body.delegation_chain = delegation.chain;
|
|
77
121
|
body.delegation_trace_id = delegation.traceId;
|
|
78
122
|
}
|
|
@@ -134,44 +178,65 @@ export class ClampdClient {
|
|
|
134
178
|
}
|
|
135
179
|
// ── Internal ────────────────────────────────────────────────────
|
|
136
180
|
async post(path, body, authorizedTools) {
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
resp = await fetch(url, {
|
|
141
|
-
method: "POST",
|
|
142
|
-
headers: this.headers(authorizedTools),
|
|
143
|
-
body: JSON.stringify(body),
|
|
144
|
-
signal: AbortSignal.timeout(this.timeoutMs),
|
|
145
|
-
});
|
|
181
|
+
// Circuit breaker check
|
|
182
|
+
if (!this.cbAllowRequest()) {
|
|
183
|
+
return blockedResponse("Circuit breaker open: gateway unavailable, requests are being short-circuited", true);
|
|
146
184
|
}
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
if (typeof json === "object" && json !== null && "degraded_stages" in json) {
|
|
155
|
-
const pr = json;
|
|
156
|
-
return {
|
|
157
|
-
...json,
|
|
158
|
-
degraded_stages: pr.degraded_stages ?? [],
|
|
159
|
-
session_flags: pr.session_flags ?? [],
|
|
160
|
-
};
|
|
185
|
+
const url = `${this.gatewayUrl}${path}`;
|
|
186
|
+
let lastError = "";
|
|
187
|
+
for (let attempt = 0; attempt <= this.maxRetries; attempt++) {
|
|
188
|
+
if (attempt > 0) {
|
|
189
|
+
// Exponential backoff: 500ms, 1000ms, 2000ms, ...
|
|
190
|
+
const delay = this.baseDelayMs * Math.pow(2, attempt - 1);
|
|
191
|
+
await new Promise((r) => setTimeout(r, delay));
|
|
161
192
|
}
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
193
|
+
let resp;
|
|
194
|
+
try {
|
|
195
|
+
resp = await fetch(url, {
|
|
196
|
+
method: "POST",
|
|
197
|
+
headers: this.headers(authorizedTools),
|
|
198
|
+
body: JSON.stringify(body),
|
|
199
|
+
signal: AbortSignal.timeout(this.timeoutMs),
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
catch (err) {
|
|
203
|
+
lastError = err instanceof Error ? err.message : "Unknown fetch error";
|
|
204
|
+
this.cbRecordFailure();
|
|
205
|
+
continue; // Retry on network errors
|
|
206
|
+
}
|
|
207
|
+
if (resp.ok) {
|
|
208
|
+
this.cbRecordSuccess();
|
|
209
|
+
const json = (await resp.json());
|
|
210
|
+
// Ensure array fields are always present for ProxyResponse shape
|
|
211
|
+
if (typeof json === "object" && json !== null && "degraded_stages" in json) {
|
|
212
|
+
const pr = json;
|
|
213
|
+
return {
|
|
214
|
+
...json,
|
|
215
|
+
degraded_stages: pr.degraded_stages ?? [],
|
|
216
|
+
session_flags: pr.session_flags ?? [],
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
return json;
|
|
220
|
+
}
|
|
221
|
+
// Don't retry on 4xx client errors (except 429 rate limit)
|
|
222
|
+
if (resp.status >= 400 && resp.status < 500 && resp.status !== 429) {
|
|
223
|
+
this.cbRecordSuccess(); // Client error is not a gateway failure
|
|
224
|
+
let errorText = await resp.text().catch(() => `HTTP ${resp.status}`);
|
|
225
|
+
if (resp.status === 401 &&
|
|
226
|
+
(errorText.includes("InvalidSignature") || errorText.includes("JWT validation failed"))) {
|
|
227
|
+
errorText =
|
|
228
|
+
"agent_auth_failed: Agent authentication failed. This usually means the agent is suspended " +
|
|
229
|
+
"or the signing secret is incorrect. Check your agent status in the dashboard " +
|
|
230
|
+
"or verify JWT_SECRET / secret parameter.";
|
|
231
|
+
}
|
|
232
|
+
return blockedResponse(errorText, true);
|
|
233
|
+
}
|
|
234
|
+
// 5xx or 429 — retry
|
|
235
|
+
lastError = await resp.text().catch(() => `HTTP ${resp.status}`);
|
|
236
|
+
this.cbRecordFailure();
|
|
173
237
|
}
|
|
174
|
-
|
|
238
|
+
// All retries exhausted
|
|
239
|
+
return blockedResponse(`Fetch error: ${lastError}`, true);
|
|
175
240
|
}
|
|
176
241
|
/**
|
|
177
242
|
* Like `post`, but throws on network errors and non-OK responses
|
package/dist/client.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AACzC,OAAO,EAAE,aAAa,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AACzC,OAAO,EAAE,aAAa,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAgFnE,sEAAsE;AAEtE,SAAS,eAAe,CAAC,MAAc,EAAE,YAAY,GAAG,KAAK;IAC3D,OAAO;QACL,UAAU,EAAE,OAAO;QACnB,OAAO,EAAE,KAAK;QACd,UAAU,EAAE,GAAG;QACf,aAAa,EAAE,MAAM;QACrB,aAAa,EAAE,EAAE;QACjB,UAAU,EAAE,CAAC;QACb,eAAe,EAAE,EAAE;QACnB,aAAa,EAAE,EAAE;QACjB,aAAa,EAAE,YAAY;KACX,CAAC;AACrB,CAAC;AAED,sEAAsE;AAEtE;;;;GAIG;AACH,MAAM,OAAO,YAAY;IACN,UAAU,CAAS;IACpB,OAAO,CAAS;IACf,MAAM,CAAS;IACf,GAAG,CAAS;IACZ,SAAS,CAAS;IAEnC,eAAe;IACE,UAAU,CAAS;IACnB,WAAW,CAAS;IAErC,wBAAwB;IACP,WAAW,CAAS;IACpB,gBAAgB,CAAS;IAClC,UAAU,GAAW,CAAC,CAAC;IACvB,UAAU,GAAW,CAAC,CAAC;IACvB,OAAO,GAAoC,QAAQ,CAAC;IAE5D,YAAY,IAAyB;QACnC,IAAI,CAAC,UAAU,GAAG,CAAC,IAAI,CAAC,UAAU,IAAI,uBAAuB,CAAC,CAAC,OA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AAG,EAAE,CAAC,QAAQ,IAAI,CAAC,MAAM,EAAE,CAAC,CAAC;YAClE,MAAM,IAAI,KAAK,CAAC,wBAAwB,IAAI,EAAE,CAAC,CAAC;QAClD,CAAC;QAED,OAAO,CAAC,MAAM,IAAI,CAAC,IAAI,EAAE,CAAM,CAAC;IAClC,CAAC;CACF"}
|
package/dist/index.d.ts
CHANGED
|
@@ -29,11 +29,26 @@ export { withDelegation, getDelegation, getCallerAgentId, delegationHeaders, typ
|
|
|
29
29
|
export { scanForSchemaInjection, type SchemaInjectionWarning } from "./schema-injection.js";
|
|
30
30
|
export { verifyScopeToken, requireScope, getCurrentScopeToken, ScopeVerificationError, withScopeToken, setScopeToken, fetchJwks, invalidateJwksCache } from "./tool-verify.js";
|
|
31
31
|
export type { ScopeTokenClaims } from "./tool-verify.js";
|
|
32
|
+
export { guardOpenAIStream, guardAnthropicStream } from "./stream-guard.js";
|
|
33
|
+
export type { CircuitBreakerOptions, RetryOptions } from "./client.js";
|
|
32
34
|
interface InitOptions {
|
|
33
35
|
agentId: string;
|
|
34
36
|
gatewayUrl?: string;
|
|
35
37
|
apiKey?: string;
|
|
36
38
|
secret?: string;
|
|
39
|
+
/** Per-agent secrets for multi-agent setups.
|
|
40
|
+
* Each agent gets its own JWT signed with its own ags_ secret.
|
|
41
|
+
* Kill/rate-limit/EMA operate independently per agent.
|
|
42
|
+
* @example
|
|
43
|
+
* clampd.init({
|
|
44
|
+
* agentId: "orchestrator",
|
|
45
|
+
* agents: {
|
|
46
|
+
* "orchestrator": process.env.ORCHESTRATOR_SECRET,
|
|
47
|
+
* "research-agent": process.env.RESEARCHER_SECRET,
|
|
48
|
+
* }
|
|
49
|
+
* });
|
|
50
|
+
*/
|
|
51
|
+
agents?: Record<string, string | undefined>;
|
|
37
52
|
}
|
|
38
53
|
interface GuardOptions {
|
|
39
54
|
agentId?: string;
|
|
@@ -49,6 +64,10 @@ interface WrapOptions {
|
|
|
49
64
|
checkResponse?: boolean;
|
|
50
65
|
scanInput?: boolean;
|
|
51
66
|
scanOutput?: boolean;
|
|
67
|
+
/** Enable stream interception for tool calls. When true, streaming tool call
|
|
68
|
+
* chunks are buffered and guarded before release. Default: false.
|
|
69
|
+
* When false and streaming with tools, a warning is logged. */
|
|
70
|
+
guardStream?: boolean;
|
|
52
71
|
schemaRegistry?: Record<string, string>;
|
|
53
72
|
}
|
|
54
73
|
declare function init(opts: InitOptions): ClampdClient;
|
|
@@ -67,12 +86,56 @@ declare function anthropic<T extends {
|
|
|
67
86
|
create: AnyFunction;
|
|
68
87
|
};
|
|
69
88
|
}>(client: T, opts: WrapOptions): T;
|
|
89
|
+
interface AdkOptions {
|
|
90
|
+
agentId?: string;
|
|
91
|
+
targetUrl?: string;
|
|
92
|
+
failOpen?: boolean;
|
|
93
|
+
checkResponse?: boolean;
|
|
94
|
+
secret?: string;
|
|
95
|
+
}
|
|
96
|
+
interface AdkCallbacks {
|
|
97
|
+
beforeTool: (toolName: string, args: Record<string, unknown>) => Promise<null | {
|
|
98
|
+
error: string;
|
|
99
|
+
}>;
|
|
100
|
+
afterTool?: (toolName: string, response: unknown) => Promise<null | {
|
|
101
|
+
error: string;
|
|
102
|
+
}>;
|
|
103
|
+
}
|
|
104
|
+
declare function adk(opts: AdkOptions): AdkCallbacks;
|
|
105
|
+
interface VercelAITool {
|
|
106
|
+
description?: string;
|
|
107
|
+
parameters?: unknown;
|
|
108
|
+
execute?: (args: Record<string, unknown>) => Promise<unknown>;
|
|
109
|
+
[key: string]: unknown;
|
|
110
|
+
}
|
|
111
|
+
declare function vercelAI<T extends Record<string, VercelAITool>>(toolDefs: T, opts: WrapOptions): T;
|
|
112
|
+
/**
|
|
113
|
+
* Run a function within an agent's delegation scope.
|
|
114
|
+
*
|
|
115
|
+
* Instead of manually using `withDelegation()`, wrap your agent logic
|
|
116
|
+
* with `clampd.agent()` — all `guard()` calls inside automatically
|
|
117
|
+
* inherit the delegation chain.
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* ```ts
|
|
121
|
+
* // Before (manual):
|
|
122
|
+
* return withDelegation("orchestrator", async () => { ... });
|
|
123
|
+
*
|
|
124
|
+
* // After (automatic):
|
|
125
|
+
* return clampd.agent("orchestrator", async () => { ... });
|
|
126
|
+
* ```
|
|
127
|
+
*/
|
|
128
|
+
declare function agent<T>(agentId: string, fn: () => Promise<T>): Promise<T>;
|
|
129
|
+
export type { AdkOptions, AdkCallbacks, VercelAITool };
|
|
70
130
|
declare const clampd: {
|
|
71
131
|
init: typeof init;
|
|
72
132
|
guard: typeof guard;
|
|
73
133
|
tools: typeof tools;
|
|
74
134
|
openai: typeof openai;
|
|
75
135
|
anthropic: typeof anthropic;
|
|
136
|
+
adk: typeof adk;
|
|
137
|
+
vercelAI: typeof vercelAI;
|
|
138
|
+
agent: typeof agent;
|
|
76
139
|
delegationHeaders: typeof delegationHeaders;
|
|
77
140
|
scanForSchemaInjection: typeof scanForSchemaInjection;
|
|
78
141
|
};
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAGH,OAAO,EAAE,YAAY,EAA4F,MAAM,aAAa,CAAC;AAErI,OAAO,EAAsB,KAAK,UAAU,EAAE,MAAM,kBAAkB,CAAC;AACvE,OAAO,EAAmD,iBAAiB,EAA0B,MAAM,iBAAiB,CAAC;AAC7H,OAAO,EAAE,sBAAsB,EAA+B,MAAM,uBAAuB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAGH,OAAO,EAAE,YAAY,EAA4F,MAAM,aAAa,CAAC;AAErI,OAAO,EAAsB,KAAK,UAAU,EAAE,MAAM,kBAAkB,CAAC;AACvE,OAAO,EAAmD,iBAAiB,EAA0B,MAAM,iBAAi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