@priors/plugin-elizaos 0.3.1 → 0.3.2

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 CHANGED
@@ -54,7 +54,7 @@ Example messages:
54
54
 
55
55
  - "Before we pay it, check agent #1234's record on Priors."
56
56
  - "Pay https://api.priors.trade/v1/report/1234 at most 0.05, borrow up to 5 if the wallet is short."
57
- - "Buy a chat completion from https://models.example/v1/chat/completions at most 0.02: {"model": "…", "messages": [{"role": "user", "content": "…"}]}" (the URL, the price and any borrow are read outside the JSON; `models.example` stands for an OpenAI-compatible model API you choose that charges over x402, its host in `PRIORS_PAY_HOSTS`)
57
+ - "Buy a chat completion from https://models.example/v1/chat/completions at most 0.02: {"model": "…", "messages": [{"role": "user", "content": "…"}]}" (the URL, the price and any borrow are read outside the JSON, which must be the message's only JSON object; `models.example` stands for an OpenAI-compatible model API you choose that charges over x402, its host in `PRIORS_PAY_HOSTS`)
58
58
  - "Repay loan 50900 now."
59
59
 
60
60
  ## How it works
@@ -67,7 +67,11 @@ allowlist and the signing are `@priors/mcp`'s own: a payment above `max_price_us
67
67
  URLs are read from the message of a sender listed in `PRIORS_OWNER_ENTITY_IDS`, never chosen by the model: the URL is
68
68
  taken out before the amounts are read, so nothing in a seller's path or query (`/pay1/borrow5.json`, `#123`) is ever
69
69
  read as a price, a borrow or an id, and a bare "pay 5" is a price only when no cap phrase ("at most", "max", "up to",
70
- "no more than") is named and it is an amount, not a count ("pay 5 items").
70
+ "no more than") is named and it is an amount, not a count ("pay 5 items"); a cap phrase's number is an amount too, not
71
+ a count ("up to 50 items"). The JSON body, and anything else the message writes in braces or a ``` block (a
72
+ template's `{model}`, an example), is taken out the same way wherever it stands, so nothing in a prompt is read as a
73
+ price or a borrow. A message must write exactly one JSON object for the body: with two (an example before the body),
74
+ one that does not parse, or one joined to the URL with no space, nothing is paid.
71
75
 
72
76
  ## Disclosure
73
77
 
@@ -2,7 +2,7 @@
2
2
  "id": "priors",
3
3
  "name": "@priors/plugin-elizaos",
4
4
  "description": "Priors credit for agents on Robinhood Chain: pay x402 APIs and borrow the gap in USDG from the agent's line, repay it, and read another agent's repayment record before paying it.",
5
- "version": "0.3.1",
5
+ "version": "0.3.2",
6
6
  "kind": "plugin",
7
7
  "requiredSecrets": [],
8
8
  "configSchema": {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@priors/plugin-elizaos",
3
- "version": "0.3.1",
3
+ "version": "0.3.2",
4
4
  "description": "Priors credit for elizaOS agents: pay x402 APIs and borrow the gap in USDG from the agent's Priors line on Robinhood Chain, repay it, and read another agent's repayment record before paying it.",
5
5
  "type": "module",
6
6
  "main": "./src/index.js",
@@ -20,7 +20,7 @@
20
20
  },
21
21
  "dependencies": {
22
22
  "@modelcontextprotocol/sdk": "^1.30.1",
23
- "@priors/mcp": "0.18.1"
23
+ "@priors/mcp": "0.19.1"
24
24
  },
25
25
  "peerDependencies": {
26
26
  "@elizaos/core": "^1.7.0"
package/src/index.js CHANGED
@@ -78,9 +78,11 @@ export const payX402Action = {
78
78
  handler: async (runtime, message, _state, _options, callback) => {
79
79
  if (!fromOperator(runtime, message)) return answer(callback, "PRIORS_PAY_X402", false, notOperator(runtime), {});
80
80
  const text = textOf(message);
81
+ // the URL and the amounts are read with the body and every other object of the message taken out, wherever they
82
+ // stand (parse.js); a message whose body cannot be told apart pays nothing (GHSA-p87w, GHSA-8pvh)
81
83
  const b = bodyOf(text);
82
- const rest = b ? text.replace(b.raw, " ") : text;
83
- const url = urlOf(rest), maxPrice = maxPriceOf(rest), borrow = borrowOf(rest);
84
+ if (b && b.error) return answer(callback, "PRIORS_PAY_X402", false, `Nothing was paid: ${b.error}.`, {});
85
+ const url = urlOf(text), maxPrice = maxPriceOf(text), borrow = borrowOf(text);
84
86
  if (!url) return answer(callback, "PRIORS_PAY_X402", false, "Nothing was paid. Name the https URL to pay outside the request body.", {});
85
87
  if (maxPrice === null || maxPrice <= 0) {
86
88
  return answer(callback, "PRIORS_PAY_X402", false, `Nothing was paid. Say the most to pay for ${url}, for example "at most 0.10", and if the wallet may borrow, the most to borrow, for example "borrow up to 1".`, { url });
package/src/parse.js CHANGED
@@ -6,13 +6,96 @@ const num = (s) => { const n = Number(String(s).replace(",", ".")); return Numbe
6
6
  // a URL as the prose holds it: everything up to the next whitespace. ")", "]", "'" and the rest are a URL's own
7
7
  // characters (RFC 3986), so stopping at one left the URL's tail in the prose (GHSA-mc89)
8
8
  const URL_RE = /https?:\/\/\S+/gi;
9
+ const urlSpans = (t) => [...t.matchAll(URL_RE)].map((m) => [m.index, m.index + m[0].length]);
10
+ const inSpans = (spans, i) => spans.some(([s, e]) => i >= s && i < e);
11
+
12
+ /** Where the {...} that opens at `start` closes (the index after its "}"), strings read as JSON spells them; -1 if never.
13
+ * `memo` keeps what one scan learns of every { it passes outside a string (a scan from there would read the same), so a
14
+ * run of braces that never close is scanned once, not once per brace. */
15
+ function closeOf(t, start, memo = new Map()) {
16
+ if (memo.has(start)) return memo.get(start);
17
+ const open = [];
18
+ let inStr = false, esc = false;
19
+ for (let i = start; i < t.length; i++) {
20
+ const c = t[i];
21
+ if (inStr) { if (esc) esc = false; else if (c === "\\") esc = true; else if (c === "\"") inStr = false; continue; }
22
+ if (c === "\"") inStr = true;
23
+ else if (c === "{") open.push(i);
24
+ else if (c === "}" && open.length) { memo.set(open.pop(), i + 1); if (!open.length) return i + 1; }
25
+ }
26
+ for (const j of open) memo.set(j, -1);
27
+ return -1;
28
+ }
29
+
30
+ /** A JSON object as compact JSON, or null. */
31
+ const objectOf = (s) => { try { const v = JSON.parse(s); return v && typeof v === "object" && !Array.isArray(v) ? JSON.stringify(v) : null; } catch { return null; } };
32
+
33
+ /**
34
+ * The message's objects: each ``` block, and each balanced {...} that opens outside a URL and outside a ``` block (one
35
+ * that never closes is skipped). Each is { start, end, json, body }: `json` when it is written as JSON (a block holding
36
+ * { or [, a {...} opening with a quote or closing at once), `body` the JSON object it holds as compact JSON, or null.
37
+ * None of their text is the message's own words: a request body, an example of one, a template's {model}. A brace
38
+ * inside a URL is the URL's (GHSA-p87w): it was taken for the body, and taking it out cut the URL in two.
39
+ */
40
+ function objectsOf(text) {
41
+ const t = String(text || ""), urls = urlSpans(t), out = [], memo = new Map();
42
+ for (let i = 0; i < t.length;) {
43
+ if (t.startsWith("```", i) && !inSpans(urls, i)) {
44
+ // a block holding an object ends after it, so a ``` inside one of its strings does not end the block
45
+ const open = /^```(?:json\b)?\s*/i.exec(t.slice(i))[0].length + i;
46
+ const objEnd = t[open] === "{" ? closeOf(t, open, memo) : -1;
47
+ const after = objEnd > 0 ? /^\s*```/.exec(t.slice(objEnd)) : null;
48
+ if (after) {
49
+ out.push({ start: i, end: objEnd + after[0].length, json: true, body: objectOf(t.slice(open, objEnd)) });
50
+ i = objEnd + after[0].length;
51
+ continue;
52
+ }
53
+ const close = t.indexOf("```", i + 3);
54
+ if (close >= 0) {
55
+ const inner = t.slice(i + 3, close).replace(/^json\b/i, "").trim();
56
+ out.push({ start: i, end: close + 3, json: /^[{[]/.test(inner), body: objectOf(inner) });
57
+ i = close + 3;
58
+ continue;
59
+ }
60
+ }
61
+ if (t[i] === "{" && !inSpans(urls, i)) {
62
+ const end = closeOf(t, i, memo);
63
+ if (end > 0) {
64
+ const raw = t.slice(i, end);
65
+ out.push({ start: i, end, json: /^\{\s*["}]/.test(raw), body: objectOf(raw) });
66
+ i = end;
67
+ continue;
68
+ }
69
+ }
70
+ i++;
71
+ }
72
+ return out;
73
+ }
74
+
75
+ /** The text with the given [start, end) spans each replaced by one space. */
76
+ function without(t, spans) {
77
+ let out = "", at = 0;
78
+ for (const [s, e] of [...spans].sort((a, b) => a[0] - b[0])) {
79
+ if (e <= at) continue;
80
+ out += t.slice(at, Math.max(at, s)) + " ";
81
+ at = e;
82
+ }
83
+ return out + t.slice(at);
84
+ }
85
+
9
86
  /** The message's own words: every URL taken out whole, so nothing in a seller's path, query or fragment
10
- * (/pay1/borrow5.json, #123, #)borrow$5) is ever read as an amount or an id (GHSA-rf6g, GHSA-62m7, GHSA-mc89). */
11
- export const proseOf = (text) => String(text || "").replace(URL_RE, " ");
87
+ * (/pay1/borrow5.json, #123, #)borrow$5) is ever read as an amount or an id (GHSA-rf6g, GHSA-62m7, GHSA-mc89), and every
88
+ * object (a request body, an example, a template's {model}: objectsOf) taken out too, wherever it stands, so nothing
89
+ * inside one is ever read either (GHSA-p87w, GHSA-8pvh). */
90
+ export const proseOf = (text) => {
91
+ const t = String(text || "");
92
+ return without(t, [...urlSpans(t), ...objectsOf(t).map((o) => [o.start, o.end])]);
93
+ };
12
94
 
13
- /** The first https URL in the text, or null. */
95
+ /** The first https URL in the text outside its objects (a URL inside a request body is the body's), or null. */
14
96
  export function urlOf(text) {
15
- const m = String(text || "").match(/https:\/\/[^\s<>"'`)\]]+/i);
97
+ const t = String(text || "");
98
+ const m = without(t, objectsOf(t).map((o) => [o.start, o.end])).match(/https:\/\/[^\s<>"'`)\]]+/i);
16
99
  if (!m) return null;
17
100
  try { return new URL(m[0].replace(/[.,;:!?]+$/, "")).href; } catch { return null; }
18
101
  }
@@ -33,22 +116,27 @@ export function loanIdOf(text) {
33
116
  }
34
117
 
35
118
  // The borrow phrase with its amount: "borrow up to 1", "borrow at most $0.50", "borrow 1" (whole words only), and an
36
- // aside right after it ("borrow 0.50 (up to 5)"), which is the borrow's too.
37
- const BORROW = /\bborrow(?:ing)?\s*(?:up to|at most|no more than|max(?:imum)?)?\s*\$?\s*(\d+(?:[.,]\d+)?)\b(?:\s*\([^)]*\))?/i;
38
- const AMOUNT = "\\$?\\s*(\\d+(?:[.,]\\d+)?)";
39
- // a cap phrase: "at most 0.10", "max $0.10", "up to 0.10", "no more than 0.10"
40
- const CAP = new RegExp(`\\b(?:at most|max(?:imum)?|up to|no more than)\\s*${AMOUNT}(?!\\d|[.,]\\d)`, "i");
119
+ // aside right after it, after its unit too ("borrow 0.50 (up to 5)", "borrow 0.50 USDG (up to 5)"), which is the
120
+ // borrow's (GHSA-p87w).
121
+ const BORROW = /\bborrow(?:ing)?\s*(?:up to|at most|no more than|max(?:imum)?)?\s*\$?\s*(\d+(?:[.,]\d+)?)\b(?:\s*(?:usdg|usd|dollars?)\b)?(?:\s*\([^)]*\))?/i;
122
+ const AMOUNT = "\\$?\\s*(\\d+(?:[.,]\\d+)?)(?!\\d|[.,]\\d)";
123
+ const UNIT = "(?:\\s*(?:usdg|usd|dollars?))?";
124
+ // a cap phrase: "at most 0.10", "max $0.10", "up to 0.10", "no more than 0.10". Its number is an amount, not a count
125
+ // ("up to 50 items", "max 100 tokens", GHSA-p87w): after "$", or followed by a unit, then the end, a line break,
126
+ // punctuation or a joining word, never by a noun.
127
+ const CAP_AFTER = `${UNIT}(?=[ \\t]*(?:$|[\\r\\n.,;:!?)\\]"'’]|(?:for|to|at|per|each|on|in|a|an|if|and|or|but|plus|then|so|please|with|without|unless|borrow(?:ing)?|do|don't|don’t|dont|never|no|not)\\b))`;
128
+ const CAP = new RegExp(`\\b(?:at most|max(?:imum)?|up to|no more than)\\s*(?:\\$\\s*(\\d+(?:[.,]\\d+)?)(?!\\d|[.,]\\d)|(\\d+(?:[.,]\\d+)?)(?!\\d|[.,]\\d)${CAP_AFTER})`, "i");
41
129
  // a bare "pay 0.10": an amount (USDG, USD, dollars, or nothing more) before the end, punctuation or a preposition, not a
42
130
  // count ("pay 5 items")
43
- const PAY = new RegExp(`\\bpay\\s+${AMOUNT}(?:\\s*(?:usdg|usd|dollars?))?(?=\\s*(?:$|[.,;:!?)]|(?:for|to|at|per|each|on)\\b))`, "i");
131
+ const PAY = new RegExp(`\\bpay\\s+${AMOUNT}${UNIT}(?=\\s*(?:$|[.,;:!?)]|(?:for|to|at|per|each|on)\\b))`, "i");
44
132
 
45
- /** The most to pay, from the message's own words (never its URL): a cap phrase ("at most 0.10", "max 0.10", "up to
46
- * $0.10 for it"), else a bare "pay 0.10"; null when none. The borrow phrase, wherever it stands, is taken out first, so
47
- * its amount is never read as the price. */
133
+ /** The most to pay, from the message's own words (never its URL, its request body or another object of it): a cap
134
+ * phrase ("at most 0.10", "max 0.10", "up to $0.10 for it"), else a bare "pay 0.10"; null when none. The borrow phrase,
135
+ * wherever it stands, is taken out first, so its amount is never read as the price. */
48
136
  export function maxPriceOf(text) {
49
137
  const t = proseOf(text).replace(new RegExp(BORROW.source, "gi"), " ");
50
138
  const m = t.match(CAP) || t.match(PAY);
51
- return m ? num(m[1]) : null;
139
+ return m ? num(m[1] ?? m[2]) : null;
52
140
  }
53
141
 
54
142
  /** The most to borrow: "borrow up to 1", "borrow at most $0.50", "borrow 1"; 0 when the message names none. */
@@ -66,24 +154,26 @@ export function borrowOf(text) {
66
154
  }
67
155
 
68
156
  /**
69
- * A JSON object the message gives as the request body (a chat completion's {"model", "messages"}): the first ``` fence
70
- * holding one, else the first balanced {...} that parses. { body: the object as compact JSON, raw: the text it was read
71
- * from }, or null. The caller reads the URL and the amounts from the rest of the message, so nothing inside the body
72
- * (a prompt that says "up to 3") is ever taken for the price or a borrow.
157
+ * The JSON object the message gives as the request body (a chat completion's {"model", "messages"}), in a ``` block or
158
+ * as a balanced {...} outside a URL, wherever it stands in the message: { body: the object as compact JSON, raw: the
159
+ * text it was read from }; null when the message writes none (a template's {model} is not one). The message must write
160
+ * exactly one, so nothing is guessed: two (an example before the body, a config object), one that does not parse as an
161
+ * object, or one joined to a URL with no space before it answer { error } and nothing is paid (GHSA-8pvh, GHSA-p87w).
162
+ * The URL and the amounts are read with every object taken out (proseOf, urlOf), so nothing inside the body (a prompt
163
+ * that says "up to 3") is ever taken for the price or a borrow.
73
164
  */
74
165
  export function bodyOf(text) {
75
166
  const t = String(text || "");
76
- const obj = (s) => { try { const v = JSON.parse(s); return v && typeof v === "object" && !Array.isArray(v) ? JSON.stringify(v) : null; } catch { return null; } };
77
- for (const m of t.matchAll(/```(?:json)?\s*([\s\S]*?)```/gi)) { const body = obj(m[1].trim()); if (body) return { body, raw: m[0] }; }
78
- const start = t.indexOf("{");
79
- if (start < 0) return null;
80
- let depth = 0, inStr = false, esc = false;
81
- for (let i = start; i < t.length; i++) {
82
- const c = t[i];
83
- if (inStr) { if (esc) esc = false; else if (c === "\\") esc = true; else if (c === "\"") inStr = false; continue; }
84
- if (c === "\"") inStr = true;
85
- else if (c === "{") depth++;
86
- else if (c === "}" && --depth === 0) { const raw = t.slice(start, i + 1); const body = obj(raw); return body ? { body, raw } : null; }
167
+ const objects = objectsOf(t), written = objects.filter((o) => o.json);
168
+ const inObject = (i) => objects.some((o) => i >= o.start && i < o.end);
169
+ if (urlSpans(t).some(([s, e]) => !inObject(s) && /\{\s*"/.test(t.slice(s, e)))) {
170
+ return { error: "a JSON object is joined to a URL in the message: put a space between the URL and the request body" };
87
171
  }
88
- return null;
172
+ if (objects.some((o) => !o.json && /\{\s*"/.test(t.slice(o.start + 1, o.end)))) {
173
+ return { error: "a JSON object is inside other braces or a ``` block that is not JSON: give the request body on its own" };
174
+ }
175
+ if (!written.length) return null;
176
+ if (written.some((o) => o.body === null)) return { error: "the message holds JSON that is not one JSON object: write the request body as one JSON object" };
177
+ if (written.length > 1) return { error: `the message holds ${written.length} JSON objects (in \`\`\` blocks or braces): give the request body as the only one` };
178
+ return { body: written[0].body, raw: t.slice(written[0].start, written[0].end) };
89
179
  }
package/src/priors.js CHANGED
@@ -6,7 +6,7 @@ import { InMemoryTransport } from "@modelcontextprotocol/sdk/inMemory.js";
6
6
  import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
7
7
 
8
8
  export const HOSTED_URL = "https://mcp.priors.trade/mcp";
9
- export const CLIENT = { name: "elizaos-plugin-priors", version: "0.3.1" };
9
+ export const CLIENT = { name: "elizaos-plugin-priors", version: "0.3.2" };
10
10
 
11
11
  /** The settings @priors/mcp reads, taken from the agent's settings (runtime.getSetting), else the process env. */
12
12
  export const SETTINGS = Object.freeze([