@priors/plugin-elizaos 0.3.1 → 0.3.3

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,13 @@ 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 ``` block that is never closed, or a body cut short (a `{` opened by a quote that never closes),
74
+ runs to the end of the message, and a `}` inside a quoted string, single or double, does not close its braces. A
75
+ message must write exactly one JSON object for the body: with two (an example before the body), one that does not
76
+ parse (a body cut short too), or one joined to the URL with no space, nothing is paid.
71
77
 
72
78
  ## Disclosure
73
79
 
@@ -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.3",
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.3",
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,103 @@ 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, and a
13
+ * single-quoted one too where a string can open (after {, [, "," or ":"), so a } inside 'a } b' does not close the braces
14
+ * while an apostrophe in words ({it's}) is no string; -1 if never. `memo` keeps what one scan learns of every
15
+ * { it passes outside a string (a scan from there would read the same), so a run of braces that never close is scanned
16
+ * once, not once per brace. */
17
+ function closeOf(t, start, memo = new Map()) {
18
+ if (memo.has(start)) return memo.get(start);
19
+ const open = [];
20
+ let quote = null, esc = false, prev = "";
21
+ for (let i = start; i < t.length; i++) {
22
+ const c = t[i];
23
+ if (quote) { if (esc) esc = false; else if (c === "\\") esc = true; else if (c === quote) { quote = null; prev = c; } continue; }
24
+ if (c === "\"" || (c === "'" && prev !== "" && "{[,:".includes(prev))) quote = c;
25
+ else if (c === "{") open.push(i);
26
+ else if (c === "}" && open.length) { memo.set(open.pop(), i + 1); if (!open.length) return i + 1; }
27
+ if (c.trim()) prev = c;
28
+ }
29
+ for (const j of open) memo.set(j, -1);
30
+ return -1;
31
+ }
32
+
33
+ /** A JSON object as compact JSON, or null. */
34
+ const objectOf = (s) => { try { const v = JSON.parse(s); return v && typeof v === "object" && !Array.isArray(v) ? JSON.stringify(v) : null; } catch { return null; } };
35
+
36
+ /**
37
+ * The message's objects: each ``` block, and each balanced {...} that opens outside a URL and outside a ``` block. A
38
+ * ``` block that never closes runs to the end of the message, as Markdown reads it, and so does a { that never closes
39
+ * when a quote opens it (a JSON body cut short: its words are the body's); any other { that never closes is
40
+ * skipped (a lone brace hides nothing). Each is { start, end, json, body }: `json` when it is written as JSON (a block
41
+ * holding { or [, a {...} opening with a double quote or closing at once), `body` the JSON object it holds as compact
42
+ * JSON, or null. None of their text is the message's own words: a request body, an example of one, a template's
43
+ * {model}. A brace inside a URL is the URL's (GHSA-p87w): it was taken for the body, and taking it out cut the URL in two.
44
+ */
45
+ function objectsOf(text) {
46
+ const t = String(text || ""), urls = urlSpans(t), out = [], memo = new Map();
47
+ for (let i = 0; i < t.length;) {
48
+ if (t.startsWith("```", i) && !inSpans(urls, i)) {
49
+ // a block holding an object ends after it, so a ``` inside one of its strings does not end the block
50
+ const open = /^```(?:json\b)?\s*/i.exec(t.slice(i))[0].length + i;
51
+ const objEnd = t[open] === "{" ? closeOf(t, open, memo) : -1;
52
+ const after = objEnd > 0 ? /^\s*```/.exec(t.slice(objEnd)) : null;
53
+ if (after) {
54
+ out.push({ start: i, end: objEnd + after[0].length, json: true, body: objectOf(t.slice(open, objEnd)) });
55
+ i = objEnd + after[0].length;
56
+ continue;
57
+ }
58
+ const close = t.indexOf("```", i + 3), end = close >= 0 ? close + 3 : t.length;
59
+ const inner = t.slice(i + 3, close >= 0 ? close : t.length).replace(/^json\b/i, "").trim();
60
+ out.push({ start: i, end, json: /^[{[]/.test(inner), body: objectOf(inner) });
61
+ i = end;
62
+ continue;
63
+ }
64
+ if (t[i] === "{" && !inSpans(urls, i)) {
65
+ const end = closeOf(t, i, memo);
66
+ if (end > 0) {
67
+ const raw = t.slice(i, end);
68
+ out.push({ start: i, end, json: /^\{\s*["}]/.test(raw), body: objectOf(raw) });
69
+ i = end;
70
+ continue;
71
+ }
72
+ const opened = /\{\s*(["'])/y;
73
+ opened.lastIndex = i;
74
+ const q = opened.exec(t);
75
+ if (q) { out.push({ start: i, end: t.length, json: q[1] === "\"", body: null }); break; }
76
+ }
77
+ i++;
78
+ }
79
+ return out;
80
+ }
81
+
82
+ /** The text with the given [start, end) spans each replaced by one space. */
83
+ function without(t, spans) {
84
+ let out = "", at = 0;
85
+ for (const [s, e] of [...spans].sort((a, b) => a[0] - b[0])) {
86
+ if (e <= at) continue;
87
+ out += t.slice(at, Math.max(at, s)) + " ";
88
+ at = e;
89
+ }
90
+ return out + t.slice(at);
91
+ }
92
+
9
93
  /** 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, " ");
94
+ * (/pay1/borrow5.json, #123, #)borrow$5) is ever read as an amount or an id (GHSA-rf6g, GHSA-62m7, GHSA-mc89), and every
95
+ * object (a request body, an example, a template's {model}: objectsOf) taken out too, wherever it stands, so nothing
96
+ * inside one is ever read either (GHSA-p87w, GHSA-8pvh). */
97
+ export const proseOf = (text) => {
98
+ const t = String(text || "");
99
+ return without(t, [...urlSpans(t), ...objectsOf(t).map((o) => [o.start, o.end])]);
100
+ };
12
101
 
13
- /** The first https URL in the text, or null. */
102
+ /** The first https URL in the text outside its objects (a URL inside a request body is the body's), or null. */
14
103
  export function urlOf(text) {
15
- const m = String(text || "").match(/https:\/\/[^\s<>"'`)\]]+/i);
104
+ const t = String(text || "");
105
+ const m = without(t, objectsOf(t).map((o) => [o.start, o.end])).match(/https:\/\/[^\s<>"'`)\]]+/i);
16
106
  if (!m) return null;
17
107
  try { return new URL(m[0].replace(/[.,;:!?]+$/, "")).href; } catch { return null; }
18
108
  }
@@ -33,22 +123,27 @@ export function loanIdOf(text) {
33
123
  }
34
124
 
35
125
  // 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");
126
+ // aside right after it, after its unit too ("borrow 0.50 (up to 5)", "borrow 0.50 USDG (up to 5)"), which is the
127
+ // borrow's (GHSA-p87w).
128
+ 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;
129
+ const AMOUNT = "\\$?\\s*(\\d+(?:[.,]\\d+)?)(?!\\d|[.,]\\d)";
130
+ const UNIT = "(?:\\s*(?:usdg|usd|dollars?))?";
131
+ // 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
132
+ // ("up to 50 items", "max 100 tokens", GHSA-p87w): after "$", or followed by a unit, then the end, a line break,
133
+ // punctuation or a joining word, never by a noun.
134
+ 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))`;
135
+ 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
136
  // a bare "pay 0.10": an amount (USDG, USD, dollars, or nothing more) before the end, punctuation or a preposition, not a
42
137
  // 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");
138
+ const PAY = new RegExp(`\\bpay\\s+${AMOUNT}${UNIT}(?=\\s*(?:$|[.,;:!?)]|(?:for|to|at|per|each|on)\\b))`, "i");
44
139
 
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. */
140
+ /** The most to pay, from the message's own words (never its URL, its request body or another object of it): a cap
141
+ * 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,
142
+ * wherever it stands, is taken out first, so its amount is never read as the price. */
48
143
  export function maxPriceOf(text) {
49
144
  const t = proseOf(text).replace(new RegExp(BORROW.source, "gi"), " ");
50
145
  const m = t.match(CAP) || t.match(PAY);
51
- return m ? num(m[1]) : null;
146
+ return m ? num(m[1] ?? m[2]) : null;
52
147
  }
53
148
 
54
149
  /** The most to borrow: "borrow up to 1", "borrow at most $0.50", "borrow 1"; 0 when the message names none. */
@@ -66,24 +161,26 @@ export function borrowOf(text) {
66
161
  }
67
162
 
68
163
  /**
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.
164
+ * The JSON object the message gives as the request body (a chat completion's {"model", "messages"}), in a ``` block or
165
+ * as a balanced {...} outside a URL, wherever it stands in the message: { body: the object as compact JSON, raw: the
166
+ * text it was read from }; null when the message writes none (a template's {model} is not one). The message must write
167
+ * exactly one, so nothing is guessed: two (an example before the body, a config object), one that does not parse as an
168
+ * object, or one joined to a URL with no space before it answer { error } and nothing is paid (GHSA-8pvh, GHSA-p87w).
169
+ * The URL and the amounts are read with every object taken out (proseOf, urlOf), so nothing inside the body (a prompt
170
+ * that says "up to 3") is ever taken for the price or a borrow.
73
171
  */
74
172
  export function bodyOf(text) {
75
173
  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; }
174
+ const objects = objectsOf(t), written = objects.filter((o) => o.json);
175
+ const inObject = (i) => objects.some((o) => i >= o.start && i < o.end);
176
+ if (urlSpans(t).some(([s, e]) => !inObject(s) && /\{\s*"/.test(t.slice(s, e)))) {
177
+ return { error: "a JSON object is joined to a URL in the message: put a space between the URL and the request body" };
87
178
  }
88
- return null;
179
+ if (objects.some((o) => !o.json && /\{\s*"/.test(t.slice(o.start + 1, o.end)))) {
180
+ return { error: "a JSON object is inside other braces or a ``` block that is not JSON: give the request body on its own" };
181
+ }
182
+ if (!written.length) return null;
183
+ 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" };
184
+ 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` };
185
+ return { body: written[0].body, raw: t.slice(written[0].start, written[0].end) };
89
186
  }
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.3" };
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([