x402-pqs 1.0.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.
Files changed (4) hide show
  1. package/README.md +55 -0
  2. package/index.d.ts +21 -0
  3. package/index.js +61 -0
  4. package/package.json +26 -0
package/README.md ADDED
@@ -0,0 +1,55 @@
1
+ # x402-pqs
2
+
3
+ Express middleware that scores prompts via [PQS (Prompt Quality Score)](https://pqs.onchainintel.net) before they hit any LLM endpoint.
4
+
5
+ Built for x402-enabled agent workflows. Free tier — no wallet required.
6
+
7
+ ## Install
8
+ ```bash
9
+ npm install x402-pqs
10
+ ```
11
+
12
+ ## Usage
13
+ ```javascript
14
+ const express = require("express");
15
+ const { pqsMiddleware } = require("x402-pqs");
16
+
17
+ const app = express();
18
+ app.use(express.json());
19
+
20
+ // Score all prompts, warn if below 10/40
21
+ app.use(pqsMiddleware({
22
+ threshold: 10,
23
+ vertical: "crypto",
24
+ onLowScore: "warn",
25
+ }));
26
+
27
+ app.post("/api/chat", (req, res) => {
28
+ // req.pqs contains the score result
29
+ console.log("Prompt score:", req.pqs.score, req.pqs.grade);
30
+ res.json({ message: "ok" });
31
+ });
32
+ ```
33
+
34
+ ## Options
35
+
36
+ | Option | Type | Default | Description |
37
+ |--------|------|---------|-------------|
38
+ | threshold | number | 0 | Block/warn prompts scoring below this |
39
+ | vertical | string | "general" | Scoring vertical: crypto, content, research, general |
40
+ | onLowScore | string | "warn" | warn, block, or ignore low scores |
41
+ | promptField | string | "prompt" | Request body field containing the prompt |
42
+ | onScore | function | null | Callback with score result |
43
+
44
+ ## Response Headers
45
+
46
+ Every request gets these headers added:
47
+ - `X-PQS-Score` — numeric score (0-40)
48
+ - `X-PQS-Grade` — letter grade (A-F)
49
+ - `X-PQS-Out-Of` — maximum score (40)
50
+
51
+ ## Powered by
52
+
53
+ [PQS — Prompt Quality Score](https://pqs.onchainintel.net) | Built by [OnChainIntel](https://onchainintel.net)
54
+
55
+ The world's first named AI prompt quality score. 8 attributes. 5 frameworks. 1 score.
package/index.d.ts ADDED
@@ -0,0 +1,21 @@
1
+ export interface PQSResult {
2
+ pqs_version: string;
3
+ prompt: string;
4
+ vertical: string;
5
+ score: number;
6
+ out_of: number;
7
+ grade: "A" | "B" | "C" | "D" | "F";
8
+ upgrade: string;
9
+ powered_by: string;
10
+ }
11
+
12
+ export interface PQSMiddlewareOptions {
13
+ threshold?: number;
14
+ vertical?: "crypto" | "content" | "research" | "general";
15
+ onLowScore?: "warn" | "block" | "ignore";
16
+ promptField?: string;
17
+ onScore?: (result: PQSResult, req: any, res: any) => void;
18
+ }
19
+
20
+ export function pqsMiddleware(options?: PQSMiddlewareOptions): (req: any, res: any, next: any) => Promise<void>;
21
+ export function scorePrompt(prompt: string, vertical?: string): Promise<PQSResult | null>;
package/index.js ADDED
@@ -0,0 +1,61 @@
1
+ const PQS_FREE_ENDPOINT = "https://pqs.onchainintel.net/api/score/free";
2
+
3
+ async function scorePrompt(prompt, vertical = "general") {
4
+ const res = await fetch(PQS_FREE_ENDPOINT, {
5
+ method: "POST",
6
+ headers: { "Content-Type": "application/json" },
7
+ body: JSON.stringify({ prompt, vertical }),
8
+ });
9
+ if (!res.ok) return null;
10
+ return await res.json();
11
+ }
12
+
13
+ function pqsMiddleware(options = {}) {
14
+ const {
15
+ threshold = 0,
16
+ vertical = "general",
17
+ onLowScore = "warn",
18
+ promptField = "prompt",
19
+ onScore = null,
20
+ } = options;
21
+
22
+ return async function (req, res, next) {
23
+ const prompt = req.body && req.body[promptField];
24
+ if (!prompt) return next();
25
+
26
+ try {
27
+ const result = await scorePrompt(prompt, vertical);
28
+ if (!result) return next();
29
+
30
+ req.pqs = result;
31
+ res.setHeader("X-PQS-Score", result.score);
32
+ res.setHeader("X-PQS-Grade", result.grade);
33
+ res.setHeader("X-PQS-Out-Of", result.out_of);
34
+
35
+ if (onScore && typeof onScore === "function") {
36
+ onScore(result, req, res);
37
+ }
38
+
39
+ if (result.score < threshold) {
40
+ if (onLowScore === "block") {
41
+ return res.status(400).json({
42
+ error: "Prompt quality too low",
43
+ pqs_score: result.score,
44
+ pqs_grade: result.grade,
45
+ pqs_out_of: result.out_of,
46
+ upgrade: result.upgrade,
47
+ });
48
+ } else if (onLowScore === "warn") {
49
+ console.warn(`[x402-pqs] Low prompt score: ${result.score}/${result.out_of} (${result.grade})`);
50
+ }
51
+ }
52
+
53
+ return next();
54
+ } catch (err) {
55
+ console.error("[x402-pqs] Scoring error:", err.message);
56
+ return next();
57
+ }
58
+ };
59
+ }
60
+
61
+ module.exports = { pqsMiddleware, scorePrompt };
package/package.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "name": "x402-pqs",
3
+ "version": "1.0.0",
4
+ "description": "Express middleware that scores prompts via PQS (Prompt Quality Score) before they hit any LLM endpoint",
5
+ "main": "index.js",
6
+ "types": "index.d.ts",
7
+ "keywords": [
8
+ "x402",
9
+ "pqs",
10
+ "prompt-quality",
11
+ "llm",
12
+ "ai",
13
+ "middleware",
14
+ "express"
15
+ ],
16
+ "author": "OnChainIntel",
17
+ "license": "MIT",
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "https://github.com/OnChainAIIntel/x402-pqs"
21
+ },
22
+ "homepage": "https://pqs.onchainintel.net",
23
+ "dependencies": {
24
+ "x402-pqs": "github:OnChainAIIntel/x402-pqs"
25
+ }
26
+ }