@auctra/sdk 0.3.5 → 0.6.1

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/esm/index.js CHANGED
@@ -1,331 +1,9 @@
1
- export const AUCTRA_SDK_VERSION = "0.3.5";
2
- export class AuctraApiError extends Error {
3
- status;
4
- code;
5
- details;
6
- requestId;
7
- constructor(message, options) {
8
- super(message);
9
- this.name = "AuctraApiError";
10
- this.status = options.status;
11
- this.code = options.code;
12
- this.details = options.details;
13
- this.requestId = options.requestId;
14
- }
15
- }
16
- function retryDelay(response, attempt) {
17
- const retryAfter = response?.headers.get("retry-after");
18
- if (retryAfter) {
19
- const seconds = Number(retryAfter);
20
- if (Number.isFinite(seconds))
21
- return Math.min(30_000, Math.max(0, seconds * 1_000));
22
- const dateDelay = Date.parse(retryAfter) - Date.now();
23
- if (Number.isFinite(dateDelay))
24
- return Math.min(30_000, Math.max(0, dateDelay));
25
- }
26
- return Math.min(5_000, 150 * 2 ** attempt);
27
- }
28
- function wait(ms, signal) {
29
- if (signal?.aborted)
30
- return Promise.reject(signal.reason);
31
- return new Promise((resolve, reject) => {
32
- const timeout = setTimeout(resolve, ms);
33
- signal?.addEventListener("abort", () => {
34
- clearTimeout(timeout);
35
- reject(signal.reason);
36
- }, { once: true });
37
- });
38
- }
39
- export class Auctra {
40
- apiKey;
41
- baseUrl;
42
- timeoutMs;
43
- maxRetries;
44
- fetchImpl;
45
- constructor(options) {
46
- if (!options.apiKey.trim())
47
- throw new Error("apiKey is required");
48
- if (options.timeoutMs !== undefined && options.timeoutMs <= 0) {
49
- throw new Error("timeoutMs must be greater than zero");
50
- }
51
- if (options.maxRetries !== undefined &&
52
- (!Number.isInteger(options.maxRetries) || options.maxRetries < 0)) {
53
- throw new Error("maxRetries must be a non-negative integer");
54
- }
55
- this.apiKey = options.apiKey;
56
- this.baseUrl = (options.baseUrl ?? "https://console.auctra.tech").replace(/\/$/, "");
57
- this.timeoutMs = options.timeoutMs ?? 10_000;
58
- this.maxRetries = options.maxRetries ?? 2;
59
- this.fetchImpl = options.fetch ?? globalThis.fetch;
60
- if (!this.fetchImpl)
61
- throw new Error("A fetch implementation is required");
62
- }
63
- async request(method, path, body, options = {}) {
64
- const retryableRequest = method === "GET" || Boolean(options.idempotencyKey);
65
- let lastError;
66
- for (let attempt = 0; attempt <= this.maxRetries; attempt += 1) {
67
- if (options.signal?.aborted)
68
- throw options.signal.reason;
69
- const timeoutSignal = AbortSignal.timeout(this.timeoutMs);
70
- const signal = options.signal
71
- ? AbortSignal.any([options.signal, timeoutSignal])
72
- : timeoutSignal;
73
- let response;
74
- try {
75
- response = await this.fetchImpl(`${this.baseUrl}${path}`, {
76
- method,
77
- signal,
78
- headers: {
79
- ...(body !== undefined ? { "content-type": "application/json" } : {}),
80
- ...(options.idempotencyKey ? { "idempotency-key": options.idempotencyKey } : {}),
81
- authorization: `Bearer ${this.apiKey}`,
82
- "x-auctra-sdk-version": AUCTRA_SDK_VERSION,
83
- },
84
- ...(body !== undefined ? { body: JSON.stringify(body) } : {}),
85
- });
86
- const data = (await response.json().catch(() => ({})));
87
- if (response.ok)
88
- return data;
89
- if (retryableRequest &&
90
- (response.status === 429 || response.status >= 500) &&
91
- attempt < this.maxRetries) {
92
- await wait(retryDelay(response, attempt), options.signal);
93
- continue;
94
- }
95
- throw new AuctraApiError(typeof data.error === "string" ? data.error : `Auctra API error (${response.status})`, {
96
- status: response.status,
97
- code: typeof data.code === "string" ? data.code : undefined,
98
- details: data.details,
99
- requestId: response.headers.get("x-request-id") ?? undefined,
100
- });
101
- }
102
- catch (error) {
103
- lastError = error;
104
- if (options.signal?.aborted)
105
- throw options.signal.reason;
106
- if (error instanceof AuctraApiError || !retryableRequest || attempt >= this.maxRetries) {
107
- throw error;
108
- }
109
- await wait(retryDelay(response, attempt), options.signal);
110
- }
111
- }
112
- throw lastError;
113
- }
114
- async evaluateAction(input, options = {}) {
115
- const idempotencyKey = options.idempotencyKey ?? crypto.randomUUID();
116
- return this.request("POST", "/v1/action-requests/evaluate", {
117
- agent_id: input.agentId,
118
- action_type: input.actionType,
119
- payload: input.payload ?? {},
120
- claimed_intent_id: input.claimedIntentId,
121
- intent_anchor_token: input.intentAnchorToken,
122
- parent_action_id: input.parentActionId,
123
- actor: input.actor
124
- ? {
125
- id: input.actor.id,
126
- type: input.actor.type,
127
- name: input.actor.name,
128
- }
129
- : undefined,
130
- action: input.action
131
- ? {
132
- target: input.action.target,
133
- description: input.action.description,
134
- risk_level: input.action.riskLevel,
135
- metadata: input.action.metadata,
136
- }
137
- : undefined,
138
- }, { ...options, idempotencyKey });
139
- }
140
- async listIntents() {
141
- return this.request("GET", "/v1/intents");
142
- }
143
- async createIntent(input) {
144
- return this.request("POST", "/v1/intents", {
145
- title: input.title,
146
- description: input.description,
147
- intent_type: input.intentType,
148
- risk_level: input.riskLevel,
149
- max_risk_level: input.maxRiskLevel,
150
- expires_at: input.expiresAt,
151
- allowed_action_types: input.allowedActionTypes,
152
- target_pattern: input.targetPattern,
153
- resource_allowlist: input.resourceAllowlist,
154
- });
155
- }
156
- async getIntent(intentId) {
157
- return this.request("GET", `/v1/intents/${encodeURIComponent(intentId)}`);
158
- }
159
- async updateIntentStatus(intentId, status) {
160
- return this.updateIntent(intentId, { status });
161
- }
162
- async updateIntent(intentId, input) {
163
- return this.request("PATCH", `/v1/intents/${encodeURIComponent(intentId)}`, {
164
- status: input.status,
165
- title: input.title,
166
- description: input.description,
167
- intent_type: input.intentType,
168
- risk_level: input.riskLevel,
169
- max_risk_level: input.maxRiskLevel,
170
- expires_at: input.expiresAt,
171
- });
172
- }
173
- async getAuthorityGraph() {
174
- return this.request("GET", "/v1/authority/graph");
175
- }
176
- async createAuthorityEdge(input) {
177
- return this.request("POST", "/v1/authority/edges", {
178
- from_actor_id: input.fromActorId,
179
- from_actor_type: input.fromActorType,
180
- to_actor_id: input.toActorId,
181
- to_actor_type: input.toActorType,
182
- authority_scope: input.authorityScope,
183
- conditions: input.conditions,
184
- max_risk_level: input.maxRiskLevel,
185
- expires_at: input.expiresAt,
186
- });
187
- }
188
- async getActionEvaluation(actionRequestId) {
189
- return this.request("GET", `/v1/action-requests/${encodeURIComponent(actionRequestId)}/evaluation`);
190
- }
191
- async getRootIntentChain(actionRequestId) {
192
- return this.request("GET", `/v1/action-requests/${encodeURIComponent(actionRequestId)}/root-intent`);
193
- }
194
- async listAgents() {
195
- return this.request("GET", "/v1/agents");
196
- }
197
- async getAgent(agentId) {
198
- return this.request("GET", `/v1/agents/${encodeURIComponent(agentId)}`);
199
- }
200
- async createAgent(input) {
201
- return this.request("POST", "/v1/agents", {
202
- name: input.name,
203
- model_provider: input.modelProvider,
204
- model_name: input.modelName,
205
- description: input.description,
206
- environment: input.environment,
207
- authority_level: input.authorityLevel,
208
- sponsor_user_id: input.sponsorUserId,
209
- });
210
- }
211
- async updateAgentStatus(agentId, input) {
212
- return this.request("PATCH", `/v1/agents/${encodeURIComponent(agentId)}`, {
213
- status: input.status,
214
- reason: input.reason,
215
- revoke_delegations: input.revokeDelegations,
216
- restore_delegations: input.restoreDelegations,
217
- });
218
- }
219
- async deleteAgent(agentId) {
220
- return this.request("DELETE", `/v1/agents/${encodeURIComponent(agentId)}`);
221
- }
222
- async listDelegations() {
223
- return this.request("GET", "/v1/delegations");
224
- }
225
- async getDelegation(delegationId) {
226
- return this.request("GET", `/v1/delegations/${encodeURIComponent(delegationId)}`);
227
- }
228
- async createDelegation(input) {
229
- return this.request("POST", "/v1/delegations", {
230
- agent_id: input.agentId,
231
- action_types: input.actionTypes,
232
- valid_until: input.validUntil,
233
- max_amount: input.maxAmount,
234
- max_count: input.maxCount,
235
- environment: input.environment,
236
- currency: input.currency,
237
- delegator_user_id: input.delegatorUserId,
238
- max_actions_per_window: input.maxActionsPerWindow
239
- ? {
240
- count: input.maxActionsPerWindow.count,
241
- window_seconds: input.maxActionsPerWindow.windowSeconds,
242
- action_type: input.maxActionsPerWindow.actionType,
243
- }
244
- : undefined,
245
- max_amount_per_window: input.maxAmountPerWindow
246
- ? {
247
- amount: input.maxAmountPerWindow.amount,
248
- window_seconds: input.maxAmountPerWindow.windowSeconds,
249
- }
250
- : undefined,
251
- });
252
- }
253
- async revokeDelegation(delegationId) {
254
- return this.request("POST", `/v1/delegations/${encodeURIComponent(delegationId)}/revoke`, {});
255
- }
256
- async listActionRequests() {
257
- return this.request("GET", "/v1/action-requests");
258
- }
259
- async approveActionRequest(actionRequestId, approverUserId, reason) {
260
- return this.resolveActionRequest(actionRequestId, "approve", approverUserId, reason);
261
- }
262
- async rejectActionRequest(actionRequestId, approverUserId, reason) {
263
- return this.resolveActionRequest(actionRequestId, "reject", approverUserId, reason);
264
- }
265
- async escalateActionRequest(actionRequestId, approverUserId, reason) {
266
- return this.resolveActionRequest(actionRequestId, "escalate", approverUserId, reason);
267
- }
268
- async resolveActionRequest(actionRequestId, action, approverUserId, reason) {
269
- return this.request("POST", `/v1/action-requests/${encodeURIComponent(actionRequestId)}/${action}`, { reason, approver_user_id: approverUserId });
270
- }
271
- async listPolicies() {
272
- return this.request("GET", "/v1/policies");
273
- }
274
- async createPolicy(input) {
275
- return this.request("POST", "/v1/policies", {
276
- name: input.name,
277
- description: input.description,
278
- policy_type: input.policyType,
279
- status: input.status,
280
- priority: input.priority,
281
- action_type: input.actionType,
282
- amount_greater_than: input.amountGreaterThan,
283
- resource_sensitivity: input.resourceSensitivity,
284
- external_recipient: input.externalRecipient,
285
- environment: input.environment,
286
- decision: input.decision,
287
- approver_role: input.approverRole,
288
- max_actions_per_window: input.maxActionsPerWindow
289
- ? {
290
- count: input.maxActionsPerWindow.count,
291
- window_seconds: input.maxActionsPerWindow.windowSeconds,
292
- action_type: input.maxActionsPerWindow.actionType,
293
- }
294
- : undefined,
295
- max_amount_per_window: input.maxAmountPerWindow
296
- ? {
297
- amount: input.maxAmountPerWindow.amount,
298
- window_seconds: input.maxAmountPerWindow.windowSeconds,
299
- }
300
- : undefined,
301
- });
302
- }
303
- async simulatePolicy(policyId, input = {}) {
304
- return this.request("POST", `/v1/policies/${encodeURIComponent(policyId)}/simulate`, input);
305
- }
306
- async publishPolicy(policyId, input = {}) {
307
- return this.request("POST", `/v1/policies/${encodeURIComponent(policyId)}/publish`, input);
308
- }
309
- async listActionTypes() {
310
- return this.listCustomActionTypes();
311
- }
312
- async listCustomActionTypes() {
313
- return this.request("GET", "/v1/action-types");
314
- }
315
- async createCustomActionType(input) {
316
- return this.request("POST", "/v1/action-types", {
317
- id: input.id,
318
- label: input.label,
319
- description: input.description,
320
- category: input.category,
321
- policy_type: input.policyType,
322
- payload_schema: input.payloadSchema ?? {},
323
- });
324
- }
325
- async listAuditEvents() {
326
- return this.request("GET", "/v1/audit-events");
327
- }
328
- async listApiKeys() {
329
- return this.request("GET", "/v1/api-keys");
330
- }
331
- }
1
+ /**
2
+ * @auctra/sdk Stage 1.0 public surface.
3
+ *
4
+ * Modular layout under src/{types,artifacts,client,authority,action,evidence}.ts.
5
+ * Canonical API: authority.* / action.* / evidence.* — no createDelegation / evaluateAction.
6
+ */
7
+ export { AUCTRA_SDK_VERSION } from "./types.js";
8
+ export { AuctraApiError, verifyMandateArtifact, verifyDecisionArtifact } from "./artifacts.js";
9
+ export { Auctra } from "./client.js";