calmlens 0.3.1 → 0.5.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 +72 -39
- package/bin/calmlens.js +240 -79
- package/cjs/CalmLensTypes-DG_kHVqY.d.ts +3521 -0
- package/cjs/{CalmLensUtils-DocJkqQj.js → CalmLensUtils-0QaxbI3A.js} +105 -93
- package/cjs/index.d.ts +2607 -24
- package/cjs/index.js +548 -120
- package/cjs/testing.d.ts +3 -2
- package/cjs/testing.js +39 -45
- package/esm/CalmLensTypes-DG_kHVqY.d.ts +3521 -0
- package/esm/{CalmLensUtils--N_91gHC.js → CalmLensUtils-BQEgxGWk.js} +78 -88
- package/esm/index.d.ts +2607 -24
- package/esm/index.js +541 -120
- package/esm/testing.d.ts +3 -2
- package/esm/testing.js +38 -22
- package/package.json +2 -2
- package/cjs/CalmLensTypes-Bnq4u8HJ.d.ts +0 -1758
- package/esm/CalmLensTypes-Bnq4u8HJ.d.ts +0 -1758
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
> This package is the same as [`@calmlens/
|
|
1
|
+
> This package is the same as [`@calmlens/sdk`](https://www.npmjs.com/package/@calmlens/sdk), published as `calmlens` so `npx calmlens` runs the CLI without installing anything. In code, depend on `@calmlens/sdk`.
|
|
2
2
|
|
|
3
|
-
# @calmlens/
|
|
3
|
+
# @calmlens/sdk
|
|
4
4
|
|
|
5
5
|
Official JavaScript/TypeScript SDK for CalmLens API
|
|
6
6
|
|
|
@@ -9,7 +9,7 @@ CalmLens provides content moderation and classification for text, images, docume
|
|
|
9
9
|
## Installation
|
|
10
10
|
|
|
11
11
|
```bash
|
|
12
|
-
npm install @calmlens/
|
|
12
|
+
npm install @calmlens/sdk
|
|
13
13
|
```
|
|
14
14
|
|
|
15
15
|
Every public SDK operation accepts one options object. This keeps call sites stable as optional fields are added.
|
|
@@ -19,7 +19,7 @@ Every public SDK operation accepts one options object. This keeps call sites sta
|
|
|
19
19
|
Create an API key under **Developer → API Keys** in the CalmLens portal. The same page shows your **Project ID**. Keep both on your server.
|
|
20
20
|
|
|
21
21
|
```typescript
|
|
22
|
-
import { CalmLensClient } from "@calmlens/
|
|
22
|
+
import { CalmLensClient } from "@calmlens/sdk";
|
|
23
23
|
|
|
24
24
|
const client = new CalmLensClient({
|
|
25
25
|
apiKey: process.env.CALMLENS_API_KEY!,
|
|
@@ -27,10 +27,7 @@ const client = new CalmLensClient({
|
|
|
27
27
|
});
|
|
28
28
|
|
|
29
29
|
// Text (comments, captions, bios): waits for the decision
|
|
30
|
-
const comment = await client.text.moderate({
|
|
31
|
-
text: "Great photo!",
|
|
32
|
-
externalId: "comment-42", // your own ID
|
|
33
|
-
});
|
|
30
|
+
const comment = await client.text.moderate({ text: "Great photo!", externalId: "comment-42" });
|
|
34
31
|
|
|
35
32
|
if (comment.status === "approved") {
|
|
36
33
|
// publish it; if comment.report?.flagged is true, also queue it for review
|
|
@@ -77,21 +74,6 @@ new CalmLensClient(options: CalmLensClientOptions)
|
|
|
77
74
|
|
|
78
75
|
#### Methods
|
|
79
76
|
|
|
80
|
-
##### text.moderate(options)
|
|
81
|
-
|
|
82
|
-
Moderate a comment, caption, bio or message and wait for the decision.
|
|
83
|
-
|
|
84
|
-
```typescript
|
|
85
|
-
async text.moderate(options: ModerateTextOptions): Promise<Asset>
|
|
86
|
-
```
|
|
87
|
-
|
|
88
|
-
**Options:**
|
|
89
|
-
- `text` (string, required): The text to moderate
|
|
90
|
-
- `name` (string, optional): Asset name shown in the portal. Defaults to `"text"`
|
|
91
|
-
- `externalId`, `metadata`, `folderId`, `processingOptions` (optional): As for `assets.create`
|
|
92
|
-
|
|
93
|
-
This is shorthand for `assets.analyze` with `content: { plainText: text, type: "text/plain" }`.
|
|
94
|
-
|
|
95
77
|
##### assets.create(options)
|
|
96
78
|
|
|
97
79
|
Queue an asset for processing and return the created asset immediately.
|
|
@@ -126,11 +108,17 @@ const asset = await client.assets.create({
|
|
|
126
108
|
const asset = await client.assets.create({
|
|
127
109
|
name: "website-content",
|
|
128
110
|
url: "https://example.com/page",
|
|
129
|
-
description: "Web page to
|
|
111
|
+
description: "Web page to moderate"
|
|
130
112
|
});
|
|
131
113
|
```
|
|
132
114
|
|
|
133
|
-
|
|
115
|
+
##### assets.moderate(options)
|
|
116
|
+
|
|
117
|
+
Submit an asset and wait for classification to finish. Accepts the same options as `assets.create()` for text, images, documents, and URLs.
|
|
118
|
+
|
|
119
|
+
```typescript
|
|
120
|
+
async assets.moderate(options: SubmitAssetOptions): Promise<Asset>
|
|
121
|
+
```
|
|
134
122
|
|
|
135
123
|
##### assets.retrieve(options)
|
|
136
124
|
|
|
@@ -255,6 +243,17 @@ await client.assets.move({
|
|
|
255
243
|
});
|
|
256
244
|
```
|
|
257
245
|
|
|
246
|
+
##### assets.retry(options)
|
|
247
|
+
|
|
248
|
+
Process an asset that failed (status `error`) again. It returns to `pending` and is moderated like a
|
|
249
|
+
new upload, so handle the result the same way (webhook or polling). Its content must still be stored:
|
|
250
|
+
otherwise this throws `asset/content-unavailable`, and you need to upload it again.
|
|
251
|
+
|
|
252
|
+
```typescript
|
|
253
|
+
const asset = await client.assets.retry({ assetId: "asset-uuid" });
|
|
254
|
+
// asset.status === "pending"
|
|
255
|
+
```
|
|
256
|
+
|
|
258
257
|
##### folders.list()
|
|
259
258
|
|
|
260
259
|
List all folders in the current project.
|
|
@@ -388,7 +387,7 @@ const completed = await client.assets.uploadPart({
|
|
|
388
387
|
|
|
389
388
|
**Example (single-call synchronous analysis):**
|
|
390
389
|
```typescript
|
|
391
|
-
const analyzed = await client.assets.
|
|
390
|
+
const analyzed = await client.assets.moderate({
|
|
392
391
|
name: "large-image.png",
|
|
393
392
|
part: {
|
|
394
393
|
base64: finalPartBase64,
|
|
@@ -403,30 +402,36 @@ console.log(analyzed.status);
|
|
|
403
402
|
|
|
404
403
|
##### verifyWebhookSignature(options)
|
|
405
404
|
|
|
406
|
-
Verify webhook
|
|
405
|
+
Verify that a webhook delivery came from CalmLens and is fresh. Also exported as a standalone `verifyWebhookSignature` function.
|
|
407
406
|
|
|
408
407
|
```typescript
|
|
409
408
|
static async verifyWebhookSignature(options: {
|
|
410
|
-
payload: string | object;
|
|
411
|
-
|
|
409
|
+
payload: string | object; // raw request body (pass the string, not parsed JSON)
|
|
410
|
+
signature: string | null | undefined; // x-calmlens-signature header
|
|
411
|
+
timestamp: string | number | null | undefined; // x-calmlens-timestamp header
|
|
412
412
|
secret: string;
|
|
413
|
+
toleranceSeconds?: number; // default 300
|
|
414
|
+
now?: Date | number; // default Date.now(), for tests
|
|
413
415
|
}): Promise<boolean>
|
|
414
416
|
```
|
|
415
417
|
|
|
418
|
+
It returns `true` only when the `x-calmlens-signature` header contains a `v1=` HMAC-SHA256 of `"<timestamp>.<raw body>"` (one match in a comma-separated list is enough) and the timestamp is within `toleranceSeconds` of now.
|
|
419
|
+
|
|
416
420
|
**Example:**
|
|
417
421
|
```typescript
|
|
418
|
-
// In your webhook handler
|
|
419
|
-
const
|
|
420
|
-
const payload = req.body;
|
|
422
|
+
// In your webhook handler (Fetch API Request)
|
|
423
|
+
const payload = await request.text();
|
|
421
424
|
const isValid = await CalmLensClient.verifyWebhookSignature({
|
|
422
425
|
payload,
|
|
423
|
-
|
|
426
|
+
signature: request.headers.get('x-calmlens-signature'),
|
|
427
|
+
timestamp: request.headers.get('x-calmlens-timestamp'),
|
|
424
428
|
secret: WEBHOOK_SECRET,
|
|
425
429
|
});
|
|
426
430
|
|
|
427
431
|
if (!isValid) {
|
|
428
|
-
return
|
|
432
|
+
return new Response('Unauthorized', { status: 401 });
|
|
429
433
|
}
|
|
434
|
+
const event: WebhookEventPayload = JSON.parse(payload);
|
|
430
435
|
```
|
|
431
436
|
|
|
432
437
|
## Asset Types
|
|
@@ -488,12 +493,12 @@ Public API failures are normalized to `CalmLensError`. Use its stable `code` pro
|
|
|
488
493
|
|
|
489
494
|
## Testing
|
|
490
495
|
|
|
491
|
-
`@calmlens/
|
|
496
|
+
`@calmlens/sdk/testing` exports a matching object-parameter `MockCalmLensClient`, in-memory storage implementations, and mock API key helpers for local unit testing without network requests.
|
|
492
497
|
|
|
493
|
-
The main `@calmlens/
|
|
498
|
+
The main `@calmlens/sdk` entry point does not load or export testing helpers to keep production bundles minimal. Keep `/testing` imports in test files.
|
|
494
499
|
|
|
495
500
|
```typescript
|
|
496
|
-
import { MockCalmLensClient, createMockApiKey } from "@calmlens/
|
|
501
|
+
import { MockCalmLensClient, createMockApiKey } from "@calmlens/sdk/testing";
|
|
497
502
|
|
|
498
503
|
const client = new MockCalmLensClient({
|
|
499
504
|
apiKey: createMockApiKey(),
|
|
@@ -505,11 +510,31 @@ For configuration options (`mockOptions`), deterministic timestamps and IDs, sto
|
|
|
505
510
|
|
|
506
511
|
## Webhooks
|
|
507
512
|
|
|
508
|
-
|
|
513
|
+
A `webhook` connector POSTs each event to your URL as JSON:
|
|
514
|
+
|
|
515
|
+
```json
|
|
516
|
+
{
|
|
517
|
+
"id": "…",
|
|
518
|
+
"type": "asset.flagged",
|
|
519
|
+
"createdAt": 1760000000000,
|
|
520
|
+
"projectId": "…",
|
|
521
|
+
"data": { "severity": "high", "title": "…", "description": null, "assetId": "…", "asset": null, "metadata": null }
|
|
522
|
+
}
|
|
523
|
+
```
|
|
524
|
+
|
|
525
|
+
Each delivery has these headers:
|
|
526
|
+
|
|
527
|
+
| Header | Value |
|
|
528
|
+
|---|---|
|
|
529
|
+
| `x-calmlens-delivery` | Delivery ID (the same across retries) |
|
|
530
|
+
| `x-calmlens-timestamp` | Unix time in seconds when the delivery was signed |
|
|
531
|
+
| `x-calmlens-signature` | `v1=<lowercase hex HMAC-SHA256(secret, "<timestamp>.<raw body>")>` |
|
|
532
|
+
|
|
533
|
+
Verify the exact raw request body with `verifyWebhookSignature` before parsing it; re-serializing parsed JSON can change the signed bytes. Deliveries more than 5 minutes old (or ahead of your clock) are rejected to prevent replays. The body type is exported as `WebhookEventPayload`; narrow it on `type` to get that event type's typed `data.facts` (`EventType` and `EventFacts<Type>` are exported too, and [the webhooks guide](https://calmlens.com/docs/guides/webhooks#event-types) lists every type). The header names are exported as `WEBHOOK_SIGNATURE_HEADER`, `WEBHOOK_TIMESTAMP_HEADER` and `WEBHOOK_DELIVERY_HEADER`.
|
|
509
534
|
|
|
510
535
|
## Command-line tool
|
|
511
536
|
|
|
512
|
-
The SDK installs a `calmlens` command. Webhook
|
|
537
|
+
The SDK installs a `calmlens` command. Webhook connectors must use public HTTPS URLs, so during development use it to forward events to a local handler instead. Each event is sent with the same body, headers and signature as a real delivery:
|
|
513
538
|
|
|
514
539
|
```bash
|
|
515
540
|
npx calmlens listen --forward-to http://localhost:3000/api/webhooks/calmlens --env-file .env.local
|
|
@@ -522,3 +547,11 @@ The same feed is available in code: `client.events.list({ after, limit })` retur
|
|
|
522
547
|
## Releasing
|
|
523
548
|
|
|
524
549
|
Stable releases are published automatically from `main` when packed SDK contents differ from npm `latest` and an SDK Changeset is present. Contributors must run `bun run changeset` to choose the release type and write release notes. CI updates and commits this package's `CHANGELOG.md` automatically. See the repository's [.changeset release guide](../../.changeset/README.md).
|
|
550
|
+
|
|
551
|
+
### Request budgets and errors
|
|
552
|
+
|
|
553
|
+
Set `timeoutMs` (default 120000) or `signal` on the client. Use `client.withRequestOptions({ signal, timeoutMs: 30000 })` to scope them to an operation without changing the original client. Multipart state is shared between scoped clients. Deadlines include response-body reads; authenticated default REST, upload and CDN requests reject redirects. Custom non-Fetch backends own their redirect/cancellation policies.
|
|
554
|
+
|
|
555
|
+
`CalmLensError` exposes `code`, optional HTTP `status`, `details` and native `cause`. Transport failures use `sdk/transport-error`, deadlines `sdk/timeout`, cancellation `sdk/cancelled`, malformed successful uploads `sdk/invalid-response`, and unstructured HTTP failures `sdk/http-error`. API error codes/messages are preserved. Core model types (`Asset`, `Folder`, `Lens`, `AssetPage`, `EventsPage`, `ClassificationReport`) and request types are exported from the package root.
|
|
556
|
+
|
|
557
|
+
`text.moderate` is a convenience wrapper around asset moderation: it supplies `text/plain` content and defaults the asset name to `text`. `assets.create` queues processing; `assets.moderate` waits for classification. The old asset analyze method is removed.
|