@letsping/sdk 0.1.2 → 0.1.5
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 +25 -2
- package/dist/index.d.ts +23 -8
- package/dist/index.js +363 -48
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +353 -47
- package/dist/index.mjs.map +1 -1
- package/dist-tsc/index.d.ts +68 -0
- package/package.json +37 -28
- package/src/index.ts +390 -84
package/README.md
CHANGED
|
@@ -2,7 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
The official Node.js/TypeScript SDK for [LetsPing](https://letsping.co).
|
|
4
4
|
|
|
5
|
-
LetsPing is
|
|
5
|
+
LetsPing is a behavioral firewall and Human-in-the-Loop (HITL) infrastructure layer for Agentic AI. It provides mathematically secure state-parking (Cryo-Sleep) and execution governance for autonomous agents built on frameworks like LangGraph, Vercel AI SDK, and custom architectures.
|
|
6
|
+
|
|
7
|
+
### Features
|
|
8
|
+
- **The Behavioral Shield:** Silently profiles your agent's execution paths via Markov Chains. Automatically intercepts 0-probability reasoning anomalies (hallucinations/prompt injections).
|
|
9
|
+
- **Cryo-Sleep State Parking:** Pauses execution and securely uploads massive agent states directly to storage using Signed URLs, entirely bypassing serverless timeouts and webhook payload limits.
|
|
10
|
+
- **Smart-Accept Drift Adaptation:** Approval decisions mathematically alter the baseline. Old unused reasoning paths decay automatically via Exponential Moving Average (EMA).
|
|
6
11
|
|
|
7
12
|
## Requirements
|
|
8
13
|
|
|
@@ -78,12 +83,30 @@ const { id } = await lp.defer({
|
|
|
78
83
|
subject: "Your invoice is ready",
|
|
79
84
|
amount: 249.99
|
|
80
85
|
},
|
|
81
|
-
priority: "medium"
|
|
86
|
+
priority: "medium",
|
|
87
|
+
// Optional: Pass the full LangGraph/Vercel state dict.
|
|
88
|
+
// It will be encrypted client-side and uploaded directly to S3.
|
|
89
|
+
state_snapshot: agentState
|
|
82
90
|
});
|
|
83
91
|
|
|
84
92
|
console.log(`Approval request queued → ${id}`);
|
|
85
93
|
```
|
|
86
94
|
|
|
95
|
+
### Webhook Rehydration (Framework Agnostic)
|
|
96
|
+
LetsPing does **not** magically inject state back into your framework natively. You must handle the webhook and rehydrate your specific framework manually.
|
|
97
|
+
|
|
98
|
+
```typescript
|
|
99
|
+
// Example Webhook Route Handler
|
|
100
|
+
if (body.status === "APPROVED") {
|
|
101
|
+
let hydratedState = null;
|
|
102
|
+
if (body.state_download_url) {
|
|
103
|
+
const res = await fetch(body.state_download_url);
|
|
104
|
+
hydratedState = lp._decrypt(await res.json());
|
|
105
|
+
}
|
|
106
|
+
// Manually push `hydratedState` back into your LangGraph/Vercel thread
|
|
107
|
+
}
|
|
108
|
+
```
|
|
109
|
+
|
|
87
110
|
## API Reference
|
|
88
111
|
|
|
89
112
|
### `new LetsPing(apiKey, options?)`
|
package/dist/index.d.ts
CHANGED
|
@@ -1,37 +1,52 @@
|
|
|
1
|
-
type Priority = "low" | "medium" | "high" | "critical";
|
|
2
|
-
interface RequestOptions {
|
|
1
|
+
export type Priority = "low" | "medium" | "high" | "critical";
|
|
2
|
+
export interface RequestOptions {
|
|
3
3
|
service: string;
|
|
4
4
|
action: string;
|
|
5
5
|
payload: Record<string, any>;
|
|
6
6
|
priority?: Priority;
|
|
7
7
|
schema?: Record<string, any>;
|
|
8
|
+
state_snapshot?: Record<string, any>;
|
|
8
9
|
timeoutMs?: number;
|
|
10
|
+
role?: string;
|
|
9
11
|
}
|
|
10
|
-
interface Decision {
|
|
11
|
-
status: "APPROVED" | "REJECTED";
|
|
12
|
+
export interface Decision {
|
|
13
|
+
status: "APPROVED" | "REJECTED" | "APPROVED_WITH_MODIFICATIONS";
|
|
12
14
|
payload: any;
|
|
13
15
|
patched_payload?: any;
|
|
16
|
+
diff_summary?: any;
|
|
14
17
|
metadata?: {
|
|
15
18
|
resolved_at: string;
|
|
16
19
|
actor_id: string;
|
|
17
20
|
method?: string;
|
|
18
21
|
};
|
|
19
22
|
}
|
|
20
|
-
declare class LetsPingError extends Error {
|
|
23
|
+
export declare class LetsPingError extends Error {
|
|
21
24
|
status?: number | undefined;
|
|
22
25
|
constructor(message: string, status?: number | undefined);
|
|
23
26
|
}
|
|
24
|
-
declare
|
|
27
|
+
declare function computeDiff(original: any, patched: any): any;
|
|
28
|
+
export declare class LetsPing {
|
|
25
29
|
private readonly apiKey;
|
|
26
30
|
private readonly baseUrl;
|
|
31
|
+
private readonly encryptionKey;
|
|
27
32
|
constructor(apiKey?: string, options?: {
|
|
28
33
|
baseUrl?: string;
|
|
34
|
+
encryptionKey?: string;
|
|
29
35
|
});
|
|
36
|
+
private _encrypt;
|
|
37
|
+
private _decrypt;
|
|
38
|
+
private _prepareStateUpload;
|
|
30
39
|
ask(options: RequestOptions): Promise<Decision>;
|
|
31
40
|
defer(options: RequestOptions): Promise<{
|
|
32
41
|
id: string;
|
|
33
42
|
}>;
|
|
34
43
|
private request;
|
|
44
|
+
tool(service: string, action: string, priority?: Priority): (context: string | Record<string, any>) => Promise<string>;
|
|
45
|
+
webhookHandler(payloadStr: string, signatureHeader: string, webhookSecret: string): Promise<{
|
|
46
|
+
id: string;
|
|
47
|
+
event: string;
|
|
48
|
+
data: Decision;
|
|
49
|
+
state_snapshot?: Record<string, any>;
|
|
50
|
+
}>;
|
|
35
51
|
}
|
|
36
|
-
|
|
37
|
-
export { type Decision, LetsPing, LetsPingError, type Priority, type RequestOptions };
|
|
52
|
+
export { computeDiff };
|
package/dist/index.js
CHANGED
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
2
3
|
var __defProp = Object.defineProperty;
|
|
3
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
5
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __commonJS = (cb, mod) => function __require() {
|
|
9
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
10
|
+
};
|
|
6
11
|
var __export = (target, all) => {
|
|
7
12
|
for (var name in all)
|
|
8
13
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
@@ -15,15 +20,85 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
15
20
|
}
|
|
16
21
|
return to;
|
|
17
22
|
};
|
|
23
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
24
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
25
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
26
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
27
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
28
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
29
|
+
mod
|
|
30
|
+
));
|
|
18
31
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
32
|
|
|
33
|
+
// package.json
|
|
34
|
+
var require_package = __commonJS({
|
|
35
|
+
"package.json"(exports2, module2) {
|
|
36
|
+
module2.exports = {
|
|
37
|
+
name: "@letsping/sdk",
|
|
38
|
+
version: "0.1.5",
|
|
39
|
+
description: "Behavioral Firewall and Cryo-Sleep State Parking for Autonomous Agents",
|
|
40
|
+
main: "./dist/index.js",
|
|
41
|
+
module: "./dist/index.mjs",
|
|
42
|
+
types: "./dist/index.d.ts",
|
|
43
|
+
exports: {
|
|
44
|
+
".": {
|
|
45
|
+
types: "./dist/index.d.ts",
|
|
46
|
+
require: "./dist/index.js",
|
|
47
|
+
import: "./dist/index.mjs"
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
scripts: {
|
|
51
|
+
build: "tsup && tsc --emitDeclarationOnly --outDir dist",
|
|
52
|
+
dev: "tsup --watch",
|
|
53
|
+
clean: "rm -rf dist .turbo"
|
|
54
|
+
},
|
|
55
|
+
dependencies: {},
|
|
56
|
+
peerDependencies: {
|
|
57
|
+
"@opentelemetry/api": "^1.0.0"
|
|
58
|
+
},
|
|
59
|
+
peerDependenciesMeta: {
|
|
60
|
+
"@opentelemetry/api": {
|
|
61
|
+
optional: true
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
devDependencies: {
|
|
65
|
+
tsup: "^8.0.0",
|
|
66
|
+
typescript: "^5.7.2",
|
|
67
|
+
"@types/node": "^22.0.0",
|
|
68
|
+
"@opentelemetry/api": "^1.9.0"
|
|
69
|
+
},
|
|
70
|
+
publishConfig: {
|
|
71
|
+
access: "public"
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
|
|
20
77
|
// src/index.ts
|
|
21
78
|
var index_exports = {};
|
|
22
79
|
__export(index_exports, {
|
|
23
80
|
LetsPing: () => LetsPing,
|
|
24
|
-
LetsPingError: () => LetsPingError
|
|
81
|
+
LetsPingError: () => LetsPingError,
|
|
82
|
+
computeDiff: () => computeDiff
|
|
25
83
|
});
|
|
26
84
|
module.exports = __toCommonJS(index_exports);
|
|
85
|
+
var import_node_crypto = require("crypto");
|
|
86
|
+
var SDK_VERSION = "0.1.5";
|
|
87
|
+
try {
|
|
88
|
+
SDK_VERSION = require_package().version;
|
|
89
|
+
} catch {
|
|
90
|
+
}
|
|
91
|
+
var otelApi = null;
|
|
92
|
+
var otelTried = false;
|
|
93
|
+
async function getOtel() {
|
|
94
|
+
if (otelTried) return otelApi;
|
|
95
|
+
otelTried = true;
|
|
96
|
+
try {
|
|
97
|
+
otelApi = await import("@opentelemetry/api");
|
|
98
|
+
} catch {
|
|
99
|
+
}
|
|
100
|
+
return otelApi;
|
|
101
|
+
}
|
|
27
102
|
var LetsPingError = class extends Error {
|
|
28
103
|
constructor(message, status) {
|
|
29
104
|
super(message);
|
|
@@ -31,66 +106,250 @@ var LetsPingError = class extends Error {
|
|
|
31
106
|
this.name = "LetsPingError";
|
|
32
107
|
}
|
|
33
108
|
};
|
|
109
|
+
function isEncEnvelope(v) {
|
|
110
|
+
return typeof v === "object" && v !== null && v._lp_enc === true && typeof v.iv === "string" && typeof v.ct === "string";
|
|
111
|
+
}
|
|
112
|
+
function encryptPayload(keyBase64, payload) {
|
|
113
|
+
const keyBuf = Buffer.from(keyBase64, "base64");
|
|
114
|
+
const iv = (0, import_node_crypto.randomBytes)(12);
|
|
115
|
+
const cipher = (0, import_node_crypto.createCipheriv)("aes-256-gcm", keyBuf, iv);
|
|
116
|
+
const plain = Buffer.from(JSON.stringify(payload), "utf8");
|
|
117
|
+
const ct = Buffer.concat([cipher.update(plain), cipher.final(), cipher.getAuthTag()]);
|
|
118
|
+
return {
|
|
119
|
+
_lp_enc: true,
|
|
120
|
+
iv: iv.toString("base64"),
|
|
121
|
+
ct: ct.toString("base64")
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
function decryptPayload(keyBase64, envelope) {
|
|
125
|
+
const keyBuf = Buffer.from(keyBase64, "base64");
|
|
126
|
+
const iv = Buffer.from(envelope.iv, "base64");
|
|
127
|
+
const ctFull = Buffer.from(envelope.ct, "base64");
|
|
128
|
+
const authTag = ctFull.subarray(ctFull.length - 16);
|
|
129
|
+
const ct = ctFull.subarray(0, ctFull.length - 16);
|
|
130
|
+
const decipher = (0, import_node_crypto.createDecipheriv)("aes-256-gcm", keyBuf, iv);
|
|
131
|
+
decipher.setAuthTag(authTag);
|
|
132
|
+
const plain = Buffer.concat([decipher.update(ct), decipher.final()]);
|
|
133
|
+
return JSON.parse(plain.toString("utf8"));
|
|
134
|
+
}
|
|
135
|
+
function computeDiff(original, patched) {
|
|
136
|
+
if (original === patched) return null;
|
|
137
|
+
if (typeof original !== "object" || typeof patched !== "object" || original === null || patched === null || Array.isArray(original) || Array.isArray(patched)) {
|
|
138
|
+
if (JSON.stringify(original) !== JSON.stringify(patched)) {
|
|
139
|
+
return { from: original, to: patched };
|
|
140
|
+
}
|
|
141
|
+
return null;
|
|
142
|
+
}
|
|
143
|
+
const changes = {};
|
|
144
|
+
let hasChanges = false;
|
|
145
|
+
const allKeys = /* @__PURE__ */ new Set([...Object.keys(original), ...Object.keys(patched)]);
|
|
146
|
+
for (const key of allKeys) {
|
|
147
|
+
const oV = original[key];
|
|
148
|
+
const pV = patched[key];
|
|
149
|
+
if (!(key in original)) {
|
|
150
|
+
changes[key] = { from: void 0, to: pV };
|
|
151
|
+
hasChanges = true;
|
|
152
|
+
} else if (!(key in patched)) {
|
|
153
|
+
changes[key] = { from: oV, to: void 0 };
|
|
154
|
+
hasChanges = true;
|
|
155
|
+
} else {
|
|
156
|
+
const nestedDiff = computeDiff(oV, pV);
|
|
157
|
+
if (nestedDiff) {
|
|
158
|
+
changes[key] = nestedDiff;
|
|
159
|
+
hasChanges = true;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
return hasChanges ? changes : null;
|
|
164
|
+
}
|
|
34
165
|
var LetsPing = class {
|
|
35
166
|
constructor(apiKey, options) {
|
|
36
167
|
const key = apiKey || process.env.LETSPING_API_KEY;
|
|
37
168
|
if (!key) throw new Error("LetsPing: API Key is required. Pass it to the constructor or set LETSPING_API_KEY env var.");
|
|
38
169
|
this.apiKey = key;
|
|
39
170
|
this.baseUrl = options?.baseUrl || "https://letsping.co/api";
|
|
171
|
+
this.encryptionKey = options?.encryptionKey ?? process.env.LETSPING_ENCRYPTION_KEY ?? null;
|
|
172
|
+
}
|
|
173
|
+
_encrypt(payload) {
|
|
174
|
+
if (!this.encryptionKey) return payload;
|
|
175
|
+
return encryptPayload(this.encryptionKey, payload);
|
|
176
|
+
}
|
|
177
|
+
_decrypt(val) {
|
|
178
|
+
if (!this.encryptionKey || !isEncEnvelope(val)) return val;
|
|
179
|
+
try {
|
|
180
|
+
return decryptPayload(this.encryptionKey, val);
|
|
181
|
+
} catch {
|
|
182
|
+
return val;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
_prepareStateUpload(stateSnapshot, fallbackDek) {
|
|
186
|
+
if (this.encryptionKey) {
|
|
187
|
+
return {
|
|
188
|
+
data: this._encrypt(stateSnapshot),
|
|
189
|
+
contentType: "application/json"
|
|
190
|
+
};
|
|
191
|
+
} else if (fallbackDek) {
|
|
192
|
+
const keyBuf = Buffer.from(fallbackDek, "base64");
|
|
193
|
+
const iv = (0, import_node_crypto.randomBytes)(12);
|
|
194
|
+
const cipher = (0, import_node_crypto.createCipheriv)("aes-256-gcm", keyBuf, iv);
|
|
195
|
+
const plain = Buffer.from(JSON.stringify(stateSnapshot), "utf8");
|
|
196
|
+
const ct = Buffer.concat([cipher.update(plain), cipher.final(), cipher.getAuthTag()]);
|
|
197
|
+
const finalPayload = Buffer.concat([iv, ct]);
|
|
198
|
+
return {
|
|
199
|
+
data: finalPayload,
|
|
200
|
+
contentType: "application/octet-stream"
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
return {
|
|
204
|
+
data: stateSnapshot,
|
|
205
|
+
contentType: "application/json"
|
|
206
|
+
};
|
|
40
207
|
}
|
|
41
208
|
async ask(options) {
|
|
42
209
|
if (options.schema && options.schema._def) {
|
|
43
210
|
throw new LetsPingError("LetsPing Error: Raw Zod schema detected. You must convert it to JSON Schema (e.g. using 'zod-to-json-schema') before passing it to the SDK.");
|
|
44
211
|
}
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
212
|
+
const otel = await getOtel();
|
|
213
|
+
let span = null;
|
|
214
|
+
if (otel && otel.trace) {
|
|
215
|
+
const tracer = otel.trace.getTracer("letsping-sdk");
|
|
216
|
+
span = tracer.startSpan(`letsping.ask`, {
|
|
217
|
+
attributes: {
|
|
218
|
+
"letsping.service": options.service,
|
|
219
|
+
"letsping.action": options.action,
|
|
220
|
+
"letsping.priority": options.priority || "medium"
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
try {
|
|
225
|
+
const res = await this.request("POST", "/ingest", {
|
|
226
|
+
service: options.service,
|
|
227
|
+
action: options.action,
|
|
228
|
+
payload: this._encrypt(options.payload),
|
|
229
|
+
priority: options.priority || "medium",
|
|
230
|
+
schema: options.schema,
|
|
231
|
+
metadata: { role: options.role, sdk: "node" }
|
|
232
|
+
});
|
|
233
|
+
const { id, uploadUrl, dek } = res;
|
|
234
|
+
if (uploadUrl && options.state_snapshot) {
|
|
235
|
+
try {
|
|
236
|
+
const { data, contentType } = this._prepareStateUpload(options.state_snapshot, dek);
|
|
237
|
+
const putRes = await fetch(uploadUrl, {
|
|
238
|
+
method: "PUT",
|
|
239
|
+
headers: { "Content-Type": contentType },
|
|
240
|
+
body: Buffer.isBuffer(data) ? data : JSON.stringify(data)
|
|
241
|
+
});
|
|
242
|
+
if (!putRes.ok) {
|
|
243
|
+
console.warn("LetsPing: Failed to upload state_snapshot to storage", await putRes.text());
|
|
244
|
+
}
|
|
245
|
+
} catch (e) {
|
|
246
|
+
console.warn("LetsPing: Exception uploading state_snapshot", e.message);
|
|
247
|
+
}
|
|
54
248
|
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
249
|
+
if (span) span.setAttribute("letsping.request_id", id);
|
|
250
|
+
const timeout = options.timeoutMs || 24 * 60 * 60 * 1e3;
|
|
251
|
+
const start = Date.now();
|
|
252
|
+
let delay = 1e3;
|
|
253
|
+
const maxDelay = 1e4;
|
|
254
|
+
while (Date.now() - start < timeout) {
|
|
255
|
+
try {
|
|
256
|
+
const check = await this.request("GET", `/status/${id}`);
|
|
257
|
+
if (check.status === "APPROVED" || check.status === "REJECTED") {
|
|
258
|
+
const decryptedPayload = this._decrypt(check.payload) ?? options.payload;
|
|
259
|
+
const decryptedPatched = check.patched_payload ? this._decrypt(check.patched_payload) : void 0;
|
|
260
|
+
let diff_summary;
|
|
261
|
+
let finalStatus = check.status;
|
|
262
|
+
if (check.status === "APPROVED" && decryptedPatched !== void 0) {
|
|
263
|
+
finalStatus = "APPROVED_WITH_MODIFICATIONS";
|
|
264
|
+
const diff = computeDiff(decryptedPayload, decryptedPatched);
|
|
265
|
+
diff_summary = diff ? { changes: diff } : { changes: "Unknown structure changes" };
|
|
71
266
|
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
267
|
+
if (span) {
|
|
268
|
+
span.setAttribute("letsping.status", finalStatus);
|
|
269
|
+
if (check.actor_id) span.setAttribute("letsping.actor_id", check.actor_id);
|
|
270
|
+
span.end();
|
|
271
|
+
}
|
|
272
|
+
return {
|
|
273
|
+
status: finalStatus,
|
|
274
|
+
payload: decryptedPayload,
|
|
275
|
+
patched_payload: decryptedPatched,
|
|
276
|
+
diff_summary,
|
|
277
|
+
metadata: {
|
|
278
|
+
resolved_at: check.resolved_at,
|
|
279
|
+
actor_id: check.actor_id
|
|
280
|
+
}
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
} catch (e) {
|
|
284
|
+
const s = e.status;
|
|
285
|
+
if (s && s >= 400 && s < 500 && s !== 404 && s !== 429) throw e;
|
|
78
286
|
}
|
|
287
|
+
const jitter = Math.random() * 200;
|
|
288
|
+
await new Promise((r) => setTimeout(r, delay + jitter));
|
|
289
|
+
delay = Math.min(delay * 1.5, maxDelay);
|
|
79
290
|
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
291
|
+
throw new LetsPingError(`Request ${id} timed out waiting for approval.`);
|
|
292
|
+
} catch (error) {
|
|
293
|
+
if (span) {
|
|
294
|
+
span.recordException(error);
|
|
295
|
+
span.setStatus({ code: otel.SpanStatusCode.ERROR });
|
|
296
|
+
span.end();
|
|
297
|
+
}
|
|
298
|
+
throw error;
|
|
83
299
|
}
|
|
84
|
-
throw new LetsPingError(`Request ${id} timed out waiting for approval.`);
|
|
85
300
|
}
|
|
86
301
|
async defer(options) {
|
|
87
|
-
|
|
302
|
+
const otel = await getOtel();
|
|
303
|
+
let span = null;
|
|
304
|
+
if (otel && otel.trace) {
|
|
305
|
+
const tracer = otel.trace.getTracer("letsping-sdk");
|
|
306
|
+
span = tracer.startSpan(`letsping.defer`, {
|
|
307
|
+
attributes: {
|
|
308
|
+
"letsping.service": options.service,
|
|
309
|
+
"letsping.action": options.action,
|
|
310
|
+
"letsping.priority": options.priority || "medium"
|
|
311
|
+
}
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
try {
|
|
315
|
+
const res = await this.request("POST", "/ingest", {
|
|
316
|
+
...options,
|
|
317
|
+
payload: this._encrypt(options.payload)
|
|
318
|
+
});
|
|
319
|
+
if (res.uploadUrl && options.state_snapshot) {
|
|
320
|
+
try {
|
|
321
|
+
const { data, contentType } = this._prepareStateUpload(options.state_snapshot, res.dek);
|
|
322
|
+
const putRes = await fetch(res.uploadUrl, {
|
|
323
|
+
method: "PUT",
|
|
324
|
+
headers: { "Content-Type": contentType },
|
|
325
|
+
body: Buffer.isBuffer(data) ? data : JSON.stringify(data)
|
|
326
|
+
});
|
|
327
|
+
if (!putRes.ok) {
|
|
328
|
+
console.warn("LetsPing: Failed to upload state_snapshot to storage", await putRes.text());
|
|
329
|
+
}
|
|
330
|
+
} catch (e) {
|
|
331
|
+
console.warn("LetsPing: Exception uploading state_snapshot", e.message);
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
if (span) {
|
|
335
|
+
span.setAttribute("letsping.request_id", res.id);
|
|
336
|
+
span.end();
|
|
337
|
+
}
|
|
338
|
+
return { id: res.id };
|
|
339
|
+
} catch (error) {
|
|
340
|
+
if (span) {
|
|
341
|
+
span.recordException(error);
|
|
342
|
+
span.setStatus({ code: otel.SpanStatusCode.ERROR });
|
|
343
|
+
span.end();
|
|
344
|
+
}
|
|
345
|
+
throw error;
|
|
346
|
+
}
|
|
88
347
|
}
|
|
89
348
|
async request(method, path, body) {
|
|
90
349
|
const headers = {
|
|
91
350
|
"Authorization": `Bearer ${this.apiKey}`,
|
|
92
351
|
"Content-Type": "application/json",
|
|
93
|
-
"User-Agent":
|
|
352
|
+
"User-Agent": `letsping-node/${SDK_VERSION}`
|
|
94
353
|
};
|
|
95
354
|
try {
|
|
96
355
|
const response = await fetch(`${this.baseUrl}${path}`, {
|
|
@@ -129,26 +388,82 @@ var LetsPing = class {
|
|
|
129
388
|
} else {
|
|
130
389
|
payload = { raw_context: String(context) };
|
|
131
390
|
}
|
|
132
|
-
const result = await this.ask({
|
|
133
|
-
service,
|
|
134
|
-
action,
|
|
135
|
-
payload,
|
|
136
|
-
priority
|
|
137
|
-
});
|
|
391
|
+
const result = await this.ask({ service, action, payload, priority });
|
|
138
392
|
if (result.status === "REJECTED") {
|
|
139
|
-
return
|
|
393
|
+
return "STOP: Action Rejected by Human.";
|
|
140
394
|
}
|
|
141
|
-
|
|
142
|
-
|
|
395
|
+
if (result.status === "APPROVED_WITH_MODIFICATIONS") {
|
|
396
|
+
return JSON.stringify({
|
|
397
|
+
status: "APPROVED_WITH_MODIFICATIONS",
|
|
398
|
+
message: "The human reviewer authorized this action but modified your original payload. Please review the diff_summary to learn from this correction.",
|
|
399
|
+
diff_summary: result.diff_summary,
|
|
400
|
+
original_payload: result.payload,
|
|
401
|
+
executed_payload: result.patched_payload
|
|
402
|
+
});
|
|
403
|
+
}
|
|
404
|
+
return JSON.stringify({
|
|
405
|
+
status: "APPROVED",
|
|
406
|
+
executed_payload: result.payload
|
|
407
|
+
});
|
|
143
408
|
} catch (e) {
|
|
144
409
|
return `ERROR: System Failure: ${e.message}`;
|
|
145
410
|
}
|
|
146
411
|
};
|
|
147
412
|
}
|
|
413
|
+
async webhookHandler(payloadStr, signatureHeader, webhookSecret) {
|
|
414
|
+
const hmac = (0, import_node_crypto.createHmac)("sha256", webhookSecret).update(payloadStr).digest("hex");
|
|
415
|
+
const sigParts = signatureHeader.split(",").map((p) => p.split("="));
|
|
416
|
+
const sigMap = Object.fromEntries(sigParts);
|
|
417
|
+
if (sigMap["v1"] !== hmac) {
|
|
418
|
+
throw new LetsPingError("LetsPing Error: Invalid webhook signature", 401);
|
|
419
|
+
}
|
|
420
|
+
const payload = JSON.parse(payloadStr);
|
|
421
|
+
const data = payload.data;
|
|
422
|
+
let state_snapshot = void 0;
|
|
423
|
+
if (data && data.state_download_url) {
|
|
424
|
+
try {
|
|
425
|
+
const res = await fetch(data.state_download_url);
|
|
426
|
+
if (res.ok) {
|
|
427
|
+
const contentType = res.headers.get("content-type") || "";
|
|
428
|
+
if (contentType.includes("application/octet-stream")) {
|
|
429
|
+
const fallbackDek = data.dek;
|
|
430
|
+
if (fallbackDek) {
|
|
431
|
+
const buffer = Buffer.from(await res.arrayBuffer());
|
|
432
|
+
const keyBuf = Buffer.from(fallbackDek, "base64");
|
|
433
|
+
const iv = buffer.subarray(0, 12);
|
|
434
|
+
const ctFull = buffer.subarray(12);
|
|
435
|
+
const authTag = ctFull.subarray(ctFull.length - 16);
|
|
436
|
+
const ct = ctFull.subarray(0, ctFull.length - 16);
|
|
437
|
+
const decipher = (0, import_node_crypto.createDecipheriv)("aes-256-gcm", keyBuf, iv);
|
|
438
|
+
decipher.setAuthTag(authTag);
|
|
439
|
+
const plain = Buffer.concat([decipher.update(ct), decipher.final()]);
|
|
440
|
+
state_snapshot = JSON.parse(plain.toString("utf8"));
|
|
441
|
+
} else {
|
|
442
|
+
console.warn("LetsPing: Missing fallback DEK to decrypt octet-stream storage file");
|
|
443
|
+
}
|
|
444
|
+
} else {
|
|
445
|
+
const encState = await res.json();
|
|
446
|
+
state_snapshot = this._decrypt(encState);
|
|
447
|
+
}
|
|
448
|
+
} else {
|
|
449
|
+
console.warn("LetsPing: Could not fetch state_snapshot from storage", await res.text());
|
|
450
|
+
}
|
|
451
|
+
} catch (e) {
|
|
452
|
+
console.warn("LetsPing: Exception downloading state_snapshot from webhook url", e.message);
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
return {
|
|
456
|
+
id: payload.id,
|
|
457
|
+
event: payload.event,
|
|
458
|
+
data,
|
|
459
|
+
state_snapshot
|
|
460
|
+
};
|
|
461
|
+
}
|
|
148
462
|
};
|
|
149
463
|
// Annotate the CommonJS export names for ESM import in node:
|
|
150
464
|
0 && (module.exports = {
|
|
151
465
|
LetsPing,
|
|
152
|
-
LetsPingError
|
|
466
|
+
LetsPingError,
|
|
467
|
+
computeDiff
|
|
153
468
|
});
|
|
154
469
|
//# sourceMappingURL=index.js.map
|