agentrein 1.0.8

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.
@@ -0,0 +1,63 @@
1
+ import { AgentameUnavailableError } from './errors';
2
+ export interface AgentameOptions {
3
+ apiKey: string;
4
+ serverUrl?: string;
5
+ failureMode?: 'open' | 'closed';
6
+ }
7
+ export interface SessionOptions {
8
+ agentId?: string;
9
+ intent?: string;
10
+ }
11
+ export interface UndoConfig {
12
+ type: 'slack-correction' | 'http-delete' | 'none';
13
+ url?: string;
14
+ headers?: Record<string, string>;
15
+ body?: Record<string, unknown>;
16
+ }
17
+ export interface Session {
18
+ id: string;
19
+ organizationId: string;
20
+ agentId: string;
21
+ intent: string | null;
22
+ status: string;
23
+ startedAt: string;
24
+ endedAt: string | null;
25
+ }
26
+ export { AgentameUnavailableError };
27
+ export declare class Agentame {
28
+ private readonly serverUrl;
29
+ private readonly apiKey;
30
+ private readonly failureMode;
31
+ private token;
32
+ constructor(options: AgentameOptions);
33
+ /**
34
+ * Obtain a JWT token from the Agentame server using the API key.
35
+ * Caches the token for subsequent requests.
36
+ */
37
+ private getToken;
38
+ /**
39
+ * Build authorization headers for server requests.
40
+ */
41
+ private authHeaders;
42
+ /**
43
+ * Create a new agent session on the Agentame server.
44
+ *
45
+ * @param options - Session options (agentId + optional intent).
46
+ * Can also pass a plain string for backward compat (agentId).
47
+ * If omitted, a random agentId is generated.
48
+ */
49
+ newSession(options?: SessionOptions | string): Promise<Session>;
50
+ /**
51
+ * Execute an API call under Agentame's protection.
52
+ *
53
+ * 1. Calls fn(...args)
54
+ * 2. Logs the action to the Agentame server (async, non-blocking)
55
+ * 3. On failure, triggers server-side rollback
56
+ *
57
+ * @param fn - The function to execute
58
+ * @param session - The active Agentame session
59
+ * @param args - Arguments forwarded to fn
60
+ */
61
+ call<T>(fn: Function, session: Session, ...args: any[]): Promise<T>;
62
+ call<T>(fn: Function, session: Session, undoConfig: UndoConfig, ...args: any[]): Promise<T>;
63
+ }
@@ -0,0 +1,92 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Agentame = exports.AgentameUnavailableError = void 0;
4
+ const axios_1 = require("axios");
5
+ const errors_1 = require("./errors");
6
+ Object.defineProperty(exports, "AgentameUnavailableError", { enumerable: true, get: function () { return errors_1.AgentameUnavailableError; } });
7
+ // ─── Agentame Client ─────────────────────────────────────
8
+ class Agentame {
9
+ constructor(options) {
10
+ this.token = null;
11
+ this.serverUrl = options.serverUrl || 'https://agentame.up.railway.app';
12
+ this.apiKey = options.apiKey;
13
+ this.failureMode = options.failureMode ?? 'open';
14
+ }
15
+ // ── Authentication ──────────────────────────────────
16
+ /**
17
+ * Obtain a JWT token from the Agentame server using the API key.
18
+ * Caches the token for subsequent requests.
19
+ */
20
+ async getToken() {
21
+ if (this.token)
22
+ return this.token;
23
+ try {
24
+ const res = await axios_1.default.post(`${this.serverUrl}/auth/token`, {
25
+ apiKey: this.apiKey,
26
+ });
27
+ this.token = res.data.data.token;
28
+ return this.token;
29
+ }
30
+ catch (err) {
31
+ throw new errors_1.AgentameUnavailableError(`Failed to authenticate with Agentame server: ${err instanceof Error ? err.message : String(err)}`);
32
+ }
33
+ }
34
+ /**
35
+ * Build authorization headers for server requests.
36
+ */
37
+ async authHeaders() {
38
+ const token = await this.getToken();
39
+ return { Authorization: `Bearer ${token}` };
40
+ }
41
+ // ── newSession ───────────────────────────────────────
42
+ /**
43
+ * Create a new agent session on the Agentame server.
44
+ *
45
+ * @param options - Session options (agentId + optional intent).
46
+ * Can also pass a plain string for backward compat (agentId).
47
+ * If omitted, a random agentId is generated.
48
+ */
49
+ async newSession(options) {
50
+ const resolved = typeof options === 'string'
51
+ ? { agentId: options, intent: undefined }
52
+ : options ?? {};
53
+ const agentId = resolved.agentId ?? crypto.randomUUID();
54
+ const intent = resolved.intent;
55
+ const headers = await this.authHeaders();
56
+ const res = await axios_1.default.post(`${this.serverUrl}/sessions`, { agentId, intent }, { headers });
57
+ return res.data.data;
58
+ }
59
+ async call(fn, session, ...args) {
60
+ // Detect if first extra arg is an UndoConfig object
61
+ let undoConfig;
62
+ let callArgs = args;
63
+ if (args.length > 0 &&
64
+ args[0] &&
65
+ typeof args[0] === 'object' &&
66
+ 'type' in args[0] &&
67
+ ['slack-correction', 'http-delete', 'none'].includes(args[0].type)) {
68
+ undoConfig = args[0];
69
+ callArgs = args.slice(1);
70
+ }
71
+ const headers = await this.authHeaders();
72
+ try {
73
+ const result = await fn(...callArgs);
74
+ // Log action to server (fire-and-forget)
75
+ axios_1.default.post(`${this.serverUrl}/sessions/${session.id}/actions`, {
76
+ apiName: fn.name || 'anonymous',
77
+ operationType: 'CREATE',
78
+ payload: callArgs[0] ?? {},
79
+ response: result,
80
+ status: 'SUCCESS',
81
+ undoConfig,
82
+ }, { headers }).catch(() => { });
83
+ return result;
84
+ }
85
+ catch (error) {
86
+ // Trigger server-side rollback
87
+ await axios_1.default.post(`${this.serverUrl}/sessions/${session.id}/rollback`, {}, { headers }).catch(() => { });
88
+ throw error;
89
+ }
90
+ }
91
+ }
92
+ exports.Agentame = Agentame;
@@ -0,0 +1,63 @@
1
+ import { AgentreinUnavailableError } from './errors';
2
+ export interface AgentreinOptions {
3
+ apiKey: string;
4
+ serverUrl?: string;
5
+ failureMode?: 'open' | 'closed';
6
+ }
7
+ export interface SessionOptions {
8
+ agentId?: string;
9
+ intent?: string;
10
+ }
11
+ export interface UndoConfig {
12
+ type: 'slack-correction' | 'http-delete' | 'none';
13
+ url?: string;
14
+ headers?: Record<string, string>;
15
+ body?: Record<string, unknown>;
16
+ }
17
+ export interface Session {
18
+ id: string;
19
+ organizationId: string;
20
+ agentId: string;
21
+ intent: string | null;
22
+ status: string;
23
+ startedAt: string;
24
+ endedAt: string | null;
25
+ }
26
+ export { AgentreinUnavailableError };
27
+ export declare class Agentrein {
28
+ private readonly serverUrl;
29
+ private readonly apiKey;
30
+ private readonly failureMode;
31
+ private token;
32
+ constructor(options: AgentreinOptions);
33
+ /**
34
+ * Obtain a JWT token from the Agentrein server using the API key.
35
+ * Caches the token for subsequent requests.
36
+ */
37
+ private getToken;
38
+ /**
39
+ * Build authorization headers for server requests.
40
+ */
41
+ private authHeaders;
42
+ /**
43
+ * Create a new agent session on the Agentrein server.
44
+ *
45
+ * @param options - Session options (agentId + optional intent).
46
+ * Can also pass a plain string for backward compat (agentId).
47
+ * If omitted, a random agentId is generated.
48
+ */
49
+ newSession(options?: SessionOptions | string): Promise<Session>;
50
+ /**
51
+ * Execute an API call under Agentrein's protection.
52
+ *
53
+ * 1. Calls fn(...args)
54
+ * 2. Logs the action to the Agentrein server (async, non-blocking)
55
+ * 3. On failure, triggers server-side rollback
56
+ *
57
+ * @param fn - The function to execute
58
+ * @param session - The active Agentrein session
59
+ * @param args - Arguments forwarded to fn
60
+ */
61
+ call<T>(fn: Function, session: Session, ...args: any[]): Promise<T>;
62
+ call<T>(fn: Function, session: Session, undoConfig: UndoConfig, ...args: any[]): Promise<T>;
63
+ }
@@ -0,0 +1,92 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Agentrein = exports.AgentreinUnavailableError = void 0;
4
+ const axios_1 = require("axios");
5
+ const errors_1 = require("./errors");
6
+ Object.defineProperty(exports, "AgentreinUnavailableError", { enumerable: true, get: function () { return errors_1.AgentreinUnavailableError; } });
7
+ // ─── Agentrein Client ─────────────────────────────────────
8
+ class Agentrein {
9
+ constructor(options) {
10
+ this.token = null;
11
+ this.serverUrl = options.serverUrl || 'https://api.agentrein.com';
12
+ this.apiKey = options.apiKey;
13
+ this.failureMode = options.failureMode ?? 'open';
14
+ }
15
+ // ── Authentication ──────────────────────────────────
16
+ /**
17
+ * Obtain a JWT token from the Agentrein server using the API key.
18
+ * Caches the token for subsequent requests.
19
+ */
20
+ async getToken() {
21
+ if (this.token)
22
+ return this.token;
23
+ try {
24
+ const res = await axios_1.default.post(`${this.serverUrl}/auth/token`, {
25
+ apiKey: this.apiKey,
26
+ });
27
+ this.token = res.data.data.token;
28
+ return this.token;
29
+ }
30
+ catch (err) {
31
+ throw new errors_1.AgentreinUnavailableError(`Failed to authenticate with Agentrein server: ${err instanceof Error ? err.message : String(err)}`);
32
+ }
33
+ }
34
+ /**
35
+ * Build authorization headers for server requests.
36
+ */
37
+ async authHeaders() {
38
+ const token = await this.getToken();
39
+ return { Authorization: `Bearer ${token}` };
40
+ }
41
+ // ── newSession ───────────────────────────────────────
42
+ /**
43
+ * Create a new agent session on the Agentrein server.
44
+ *
45
+ * @param options - Session options (agentId + optional intent).
46
+ * Can also pass a plain string for backward compat (agentId).
47
+ * If omitted, a random agentId is generated.
48
+ */
49
+ async newSession(options) {
50
+ const resolved = typeof options === 'string'
51
+ ? { agentId: options, intent: undefined }
52
+ : options ?? {};
53
+ const agentId = resolved.agentId ?? crypto.randomUUID();
54
+ const intent = resolved.intent;
55
+ const headers = await this.authHeaders();
56
+ const res = await axios_1.default.post(`${this.serverUrl}/sessions`, { agentId, intent }, { headers });
57
+ return res.data.data;
58
+ }
59
+ async call(fn, session, ...args) {
60
+ // Detect if first extra arg is an UndoConfig object
61
+ let undoConfig;
62
+ let callArgs = args;
63
+ if (args.length > 0 &&
64
+ args[0] &&
65
+ typeof args[0] === 'object' &&
66
+ 'type' in args[0] &&
67
+ ['slack-correction', 'http-delete', 'none'].includes(args[0].type)) {
68
+ undoConfig = args[0];
69
+ callArgs = args.slice(1);
70
+ }
71
+ const headers = await this.authHeaders();
72
+ try {
73
+ const result = await fn(...callArgs);
74
+ // Log action to server (fire-and-forget)
75
+ axios_1.default.post(`${this.serverUrl}/sessions/${session.id}/actions`, {
76
+ apiName: fn.name || 'anonymous',
77
+ operationType: 'CREATE',
78
+ payload: callArgs[0] ?? {},
79
+ response: result,
80
+ status: 'SUCCESS',
81
+ undoConfig,
82
+ }, { headers }).catch(() => { });
83
+ return result;
84
+ }
85
+ catch (error) {
86
+ // Trigger server-side rollback
87
+ await axios_1.default.post(`${this.serverUrl}/sessions/${session.id}/rollback`, {}, { headers }).catch(() => { });
88
+ throw error;
89
+ }
90
+ }
91
+ }
92
+ exports.Agentrein = Agentrein;
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Custom errors for the Agentrein SDK.
3
+ */
4
+ export declare class AgentreinUnavailableError extends Error {
5
+ constructor(message?: string);
6
+ }
7
+ export declare class ApprovalRejectedError extends Error {
8
+ constructor(apiName: string, reason?: string);
9
+ }
10
+ export declare class ApprovalTimeoutError extends Error {
11
+ constructor(apiName: string, timeoutMinutes: number);
12
+ }
package/dist/errors.js ADDED
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ /**
3
+ * Custom errors for the Agentrein SDK.
4
+ */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.ApprovalTimeoutError = exports.ApprovalRejectedError = exports.AgentreinUnavailableError = void 0;
7
+ class AgentreinUnavailableError extends Error {
8
+ constructor(message) {
9
+ super(message ?? 'Agentrein safety layer is unavailable');
10
+ this.name = 'AgentreinUnavailableError';
11
+ }
12
+ }
13
+ exports.AgentreinUnavailableError = AgentreinUnavailableError;
14
+ class ApprovalRejectedError extends Error {
15
+ constructor(apiName, reason) {
16
+ super(`Action "${apiName}" was rejected by a human reviewer${reason ? `: ${reason}` : ''}`);
17
+ this.name = 'ApprovalRejectedError';
18
+ }
19
+ }
20
+ exports.ApprovalRejectedError = ApprovalRejectedError;
21
+ class ApprovalTimeoutError extends Error {
22
+ constructor(apiName, timeoutMinutes) {
23
+ super(`Action "${apiName}" was not approved within ${timeoutMinutes} minutes — auto-rejected`);
24
+ this.name = 'ApprovalTimeoutError';
25
+ }
26
+ }
27
+ exports.ApprovalTimeoutError = ApprovalTimeoutError;
package/package.json ADDED
@@ -0,0 +1,20 @@
1
+ {
2
+ "name": "agentrein",
3
+ "version": "1.0.8",
4
+ "main": "dist/agentreinClient.js",
5
+ "types": "dist/agentreinClient.d.ts",
6
+ "files": [
7
+ "dist/",
8
+ "README.md"
9
+ ],
10
+ "scripts": {
11
+ "build": "tsc",
12
+ "prepublishOnly": "npm run build"
13
+ },
14
+ "dependencies": {
15
+ "axios": "^1.6.0"
16
+ },
17
+ "devDependencies": {
18
+ "typescript": "^5.0.0"
19
+ }
20
+ }