plures 1.7.1 → 1.10.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 +2 -0
- package/dist/cli.cjs +137 -17
- package/dist/cli.js +293 -9
- package/dist/index.cjs +31 -2
- package/dist/index.d.cts +113 -2
- package/dist/index.d.ts +113 -2
- package/dist/index.js +196 -6
- package/package.json +1 -1
- package/dist/chunk-N4GCKRA6.js +0 -174
package/dist/chunk-N4GCKRA6.js
DELETED
|
@@ -1,174 +0,0 @@
|
|
|
1
|
-
// src/index.ts
|
|
2
|
-
var VERSION = "1.7.1";
|
|
3
|
-
var PluresError = class extends Error {
|
|
4
|
-
constructor(message, status, type) {
|
|
5
|
-
super(message);
|
|
6
|
-
this.status = status;
|
|
7
|
-
this.type = type;
|
|
8
|
-
}
|
|
9
|
-
status;
|
|
10
|
-
type;
|
|
11
|
-
};
|
|
12
|
-
var Plures = class {
|
|
13
|
-
base;
|
|
14
|
-
apiKey;
|
|
15
|
-
/** The app tasks go to when they don't name one. From opts.app or PLURES_APP. */
|
|
16
|
-
app;
|
|
17
|
-
ua;
|
|
18
|
-
constructor(apiKey, opts = {}) {
|
|
19
|
-
const key = apiKey ?? (typeof process !== "undefined" ? process.env?.PLURES_API_KEY : void 0);
|
|
20
|
-
if (!key) throw new Error("Pass your Plures API key, or set PLURES_API_KEY. Get a free test key at https://plures.si/?for=developers#join");
|
|
21
|
-
this.apiKey = key;
|
|
22
|
-
this.ua = opts.userAgent ?? `plures-js/${VERSION}`;
|
|
23
|
-
this.app = opts.app ?? (typeof process !== "undefined" ? process.env?.PLURES_APP : void 0) ?? null;
|
|
24
|
-
this.base = (opts.baseUrl ?? (typeof process !== "undefined" ? process.env?.PLURES_BASE_URL : void 0) ?? "https://plures.si").replace(/\/$/, "");
|
|
25
|
-
}
|
|
26
|
-
async req(method, path, body, headers = {}) {
|
|
27
|
-
for (let attempt = 0; ; attempt++) {
|
|
28
|
-
const r = await fetch(this.base + path, {
|
|
29
|
-
method,
|
|
30
|
-
headers: { authorization: `Bearer ${this.apiKey}`, "user-agent": this.ua, ...body ? { "content-type": "application/json" } : {}, ...this.app ? { "plures-app": this.app } : {}, ...headers },
|
|
31
|
-
body: body ? JSON.stringify(body) : void 0
|
|
32
|
-
});
|
|
33
|
-
if (r.status >= 500 && attempt < 2) {
|
|
34
|
-
await sleep(500 * 2 ** attempt);
|
|
35
|
-
continue;
|
|
36
|
-
}
|
|
37
|
-
const data = await r.json().catch(() => ({}));
|
|
38
|
-
if (!r.ok) throw new PluresError(data?.error?.message ?? `HTTP ${r.status}`, r.status, data?.error?.type ?? "api_error");
|
|
39
|
-
return data;
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
tasks = {
|
|
43
|
-
/** Sends a question to verified people. Pass an idempotency key to make retries safe. */
|
|
44
|
-
create: (params, opts = {}) => this.req("POST", "/api/v1/tasks", params, opts.idempotencyKey ? { "idempotency-key": opts.idempotencyKey } : {}),
|
|
45
|
-
retrieve: (id) => this.req("GET", `/api/v1/tasks/${encodeURIComponent(id)}`),
|
|
46
|
-
list: (params = {}) => {
|
|
47
|
-
const q = new URLSearchParams(Object.entries(params).filter(([, v]) => v != null).map(([k, v]) => [k, String(v)]));
|
|
48
|
-
return this.req("GET", `/api/v1/tasks${q.size ? `?${q}` : ""}`);
|
|
49
|
-
},
|
|
50
|
-
cancel: (id) => this.req("POST", `/api/v1/tasks/${encodeURIComponent(id)}/cancel`),
|
|
51
|
-
/** Polls until the task is settled. Prefer webhooks in production. */
|
|
52
|
-
waitFor: async (id, opts = {}) => {
|
|
53
|
-
const end = Date.now() + (opts.timeoutMs ?? 10 * 6e4);
|
|
54
|
-
for (; ; ) {
|
|
55
|
-
const t = await this.tasks.retrieve(id);
|
|
56
|
-
if (t.status !== "pending") return t;
|
|
57
|
-
if (Date.now() > end) return t;
|
|
58
|
-
await sleep(opts.intervalMs ?? 3e3);
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
};
|
|
62
|
-
/**
|
|
63
|
-
* Test keys only. How test tasks behave when they don't set metadata.test_outcome,
|
|
64
|
-
* so an agent running unchanged sends a share of decisions to your team.
|
|
65
|
-
* A single task can override it with metadata.test_mix.
|
|
66
|
-
*/
|
|
67
|
-
testMix = {
|
|
68
|
-
get: () => this.req("GET", "/api/v1/test_mix"),
|
|
69
|
-
/** For example { unanimous: 70, split: 20, tie: 5, expired: 5 }. Pass null to turn it off. */
|
|
70
|
-
set: (mix) => this.req("PUT", "/api/v1/test_mix", { mix }),
|
|
71
|
-
clear: () => this.req("DELETE", "/api/v1/test_mix")
|
|
72
|
-
};
|
|
73
|
-
/**
|
|
74
|
-
* Where tasks go. set() replaces what you send (team, groups or rules) and keeps the rest.
|
|
75
|
-
* For example { groups: [{ name: "Finance", emails: ["ap@acme.com"] }], rules: [{ category: "refunds", route: "team", group: "Finance" }] }.
|
|
76
|
-
*/
|
|
77
|
-
routing = {
|
|
78
|
-
get: () => this.req("GET", "/api/v1/routing"),
|
|
79
|
-
set: (update) => this.req("PUT", "/api/v1/routing", update)
|
|
80
|
-
};
|
|
81
|
-
/** Your apps. Each agent you run can be its own app, with its own tasks, ledger view and map. */
|
|
82
|
-
apps = {
|
|
83
|
-
list: () => this.req("GET", "/api/v1/apps"),
|
|
84
|
-
/** { name: "Support agent" }, with an optional id like "support-agent". */
|
|
85
|
-
create: (params) => this.req("POST", "/api/v1/apps", params),
|
|
86
|
-
retrieve: (id) => this.req("GET", `/api/v1/apps/${encodeURIComponent(id)}`),
|
|
87
|
-
/** Rename it, change its id (its tasks move with it), or make it the default. */
|
|
88
|
-
update: (id, params) => this.req("PATCH", `/api/v1/apps/${encodeURIComponent(id)}`, params),
|
|
89
|
-
/** Only an app with no tasks can be deleted. */
|
|
90
|
-
del: (id) => this.req("DELETE", `/api/v1/apps/${encodeURIComponent(id)}`)
|
|
91
|
-
};
|
|
92
|
-
/**
|
|
93
|
-
* Agent maps: where each app's agent needs a person. Your coding agent reads your code and
|
|
94
|
-
* writes the map; Plures gets only the map. guide() returns the steps it follows.
|
|
95
|
-
*/
|
|
96
|
-
maps = {
|
|
97
|
-
guide: async () => {
|
|
98
|
-
const r = await fetch(`${this.base}/api/v1/map_guide`);
|
|
99
|
-
return r.text();
|
|
100
|
-
},
|
|
101
|
-
retrieve: (app) => this.req("GET", `/api/v1/apps/${encodeURIComponent(app)}/map`),
|
|
102
|
-
/** Uploads a new version. Checks you already added or dismissed keep that. */
|
|
103
|
-
upload: (app, map) => this.req("PUT", `/api/v1/apps/${encodeURIComponent(app)}/map`, map),
|
|
104
|
-
setCheckpoint: (app, id, status) => this.req("PATCH", `/api/v1/apps/${encodeURIComponent(app)}/map/checkpoints/${encodeURIComponent(id)}`, { status })
|
|
105
|
-
};
|
|
106
|
-
/**
|
|
107
|
-
* Quality: people grade a sample of your agent's outputs against a rubric, so you see drift
|
|
108
|
-
* early, and compare two versions blind before you ship. Personal details are taken out first.
|
|
109
|
-
*/
|
|
110
|
-
quality = {
|
|
111
|
-
/** Send every output; the rubric's sample rate decides which ones people grade. */
|
|
112
|
-
sample: (p) => this.req("POST", "/api/v1/samples", p),
|
|
113
|
-
/** A blind release check: a is the current version's output, b the new one's, for the same input. */
|
|
114
|
-
compare: (p) => this.req("POST", "/api/v1/comparisons", p),
|
|
115
|
-
/** How often people preferred the new version, with a 95% range and a verdict. */
|
|
116
|
-
release: (name, opts = {}) => this.req("GET", `/api/v1/releases/${encodeURIComponent(name)}${opts.app ? `?app=${encodeURIComponent(opts.app)}` : ""}`),
|
|
117
|
-
rubrics: {
|
|
118
|
-
list: (opts = {}) => this.req("GET", `/api/v1/rubrics${opts.app ? `?app=${encodeURIComponent(opts.app)}` : ""}`),
|
|
119
|
-
/** { name: "reply_quality", question: "Is this reply correct and kind?", options: ["Good", "Okay", "Bad"], sample_rate: 0.05 } */
|
|
120
|
-
create: (p) => this.req("POST", "/api/v1/rubrics", p),
|
|
121
|
-
retrieve: (name, opts = {}) => this.req("GET", `/api/v1/rubrics/${encodeURIComponent(name)}${opts.app ? `?app=${encodeURIComponent(opts.app)}` : ""}`),
|
|
122
|
-
update: (name, p) => this.req("PATCH", `/api/v1/rubrics/${encodeURIComponent(name)}${p.app ? `?app=${encodeURIComponent(p.app)}` : ""}`, p),
|
|
123
|
-
del: (name, opts = {}) => this.req("DELETE", `/api/v1/rubrics/${encodeURIComponent(name)}${opts.app ? `?app=${encodeURIComponent(opts.app)}` : ""}`)
|
|
124
|
-
},
|
|
125
|
-
/** Graded samples as JSON lines (input, output, grade, score, agreement), for fine tuning or an eval set. */
|
|
126
|
-
export: async (rubric, opts = {}) => {
|
|
127
|
-
const q = new URLSearchParams(Object.entries({ app: opts.app ?? this.app ?? void 0, since: opts.since }).filter(([, v]) => v));
|
|
128
|
-
const r = await fetch(`${this.base}/api/v1/rubrics/${encodeURIComponent(rubric)}/export${q.size ? `?${q}` : ""}`, { headers: { authorization: `Bearer ${this.apiKey}`, "user-agent": this.ua } });
|
|
129
|
-
if (!r.ok) {
|
|
130
|
-
const d = await r.json().catch(() => ({}));
|
|
131
|
-
throw new PluresError(d?.error?.message ?? `HTTP ${r.status}`, r.status, d?.error?.type ?? "api_error");
|
|
132
|
-
}
|
|
133
|
-
return (await r.text()).split("\n").filter(Boolean).map((l) => JSON.parse(l));
|
|
134
|
-
}
|
|
135
|
-
};
|
|
136
|
-
/** Your prepaid credit. A live task that could cost more than available_cents is refused with a 402 insufficient_credits error. */
|
|
137
|
-
balance = () => this.req("GET", "/api/v1/balance");
|
|
138
|
-
/** Creates a task and waits for people to answer it. */
|
|
139
|
-
async verify(params, opts = {}) {
|
|
140
|
-
const t = await this.tasks.create(params, { idempotencyKey: opts.idempotencyKey });
|
|
141
|
-
return t.status === "pending" ? this.tasks.waitFor(t.id, { timeoutMs: opts.timeoutMs }) : t;
|
|
142
|
-
}
|
|
143
|
-
/**
|
|
144
|
-
* Checks the Plures-Signature header on a webhook and returns the parsed event.
|
|
145
|
-
* Pass the raw request body exactly as received. Throws if the signature is wrong
|
|
146
|
-
* or older than toleranceSec.
|
|
147
|
-
*/
|
|
148
|
-
static async verifyWebhook(rawBody, signatureHeader, secret, toleranceSec = 300) {
|
|
149
|
-
const parts = Object.fromEntries((signatureHeader ?? "").split(",").map((p) => p.split("=", 2)));
|
|
150
|
-
const t = Number(parts.t);
|
|
151
|
-
if (!t || !parts.v1) throw new Error("Missing Plures-Signature header");
|
|
152
|
-
if (Math.abs(Date.now() / 1e3 - t) > toleranceSec) throw new Error("Webhook timestamp is too old");
|
|
153
|
-
const key = await crypto.subtle.importKey("raw", new TextEncoder().encode(secret), { name: "HMAC", hash: "SHA-256" }, false, ["sign"]);
|
|
154
|
-
const mac = new Uint8Array(await crypto.subtle.sign("HMAC", key, new TextEncoder().encode(`${t}.${rawBody}`)));
|
|
155
|
-
const expected = Array.from(mac, (b) => b.toString(16).padStart(2, "0")).join("");
|
|
156
|
-
if (!timingSafeEqual(expected, parts.v1)) throw new Error("Webhook signature does not match");
|
|
157
|
-
return JSON.parse(rawBody);
|
|
158
|
-
}
|
|
159
|
-
};
|
|
160
|
-
function timingSafeEqual(a, b) {
|
|
161
|
-
if (a.length !== b.length) return false;
|
|
162
|
-
let diff = 0;
|
|
163
|
-
for (let i = 0; i < a.length; i++) diff |= a.charCodeAt(i) ^ b.charCodeAt(i);
|
|
164
|
-
return diff === 0;
|
|
165
|
-
}
|
|
166
|
-
var sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
167
|
-
var index_default = Plures;
|
|
168
|
-
|
|
169
|
-
export {
|
|
170
|
-
VERSION,
|
|
171
|
-
PluresError,
|
|
172
|
-
Plures,
|
|
173
|
-
index_default
|
|
174
|
-
};
|