@priors/plugin-elizaos 0.0.0-stage → 0.1.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 +90 -2
- package/elizaos.plugin.json +23 -0
- package/package.json +38 -4
- package/src/index.js +93 -0
- package/src/parse.js +43 -0
- package/src/priors.js +71 -0
package/README.md
CHANGED
|
@@ -1,3 +1,91 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @priors/plugin-elizaos
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Priors credit for [elizaOS](https://github.com/elizaOS/eliza) agents. An agent pays HTTP 402 (x402) APIs in USDG
|
|
4
|
+
from its wallet on Robinhood Chain; when the wallet is short, it borrows the gap from its
|
|
5
|
+
[Priors](https://priors.trade) credit line and repays it. Before paying another ERC-8004 agent, it can read that
|
|
6
|
+
agent's repayment record.
|
|
7
|
+
|
|
8
|
+
## Install
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install @priors/plugin-elizaos
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
Add it to the character's plugins (elizaOS 1.7 or later):
|
|
15
|
+
|
|
16
|
+
```json
|
|
17
|
+
{
|
|
18
|
+
"name": "MyAgent",
|
|
19
|
+
"plugins": ["@elizaos/plugin-sql", "@elizaos/plugin-bootstrap", "@elizaos/plugin-openrouter", "@priors/plugin-elizaos"],
|
|
20
|
+
"settings": {
|
|
21
|
+
"secrets": {
|
|
22
|
+
"PRIORS_KEY_FILE": "/path/to/agent-wallet.key",
|
|
23
|
+
"PRIORS_AGENT_ID": "1234",
|
|
24
|
+
"PRIORS_PAY_HOSTS": "check.blockpin.dev"
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
| Setting | What it is |
|
|
31
|
+
|---|---|
|
|
32
|
+
| `PRIORS_KEY_FILE` | a file holding the agent wallet's private key, readable by this user only (`chmod 600`); a dedicated wallet, never a main one (`PRIORS_KEY` works too) |
|
|
33
|
+
| `PRIORS_AGENT_ID` | the agent's ERC-8004 id on Robinhood Chain |
|
|
34
|
+
| `PRIORS_PAY_HOSTS` | the only hosts a payment may go to, comma-separated |
|
|
35
|
+
| `PRIORS_MAX_PRICE_USD` | most one x402 call may cost (default 1.00) |
|
|
36
|
+
| `PRIORS_STATE_DIR` | where the daily caps and sent payments are kept: a directory only this user can write to, with no group- or world-writable parent (otherwise nothing is signed) |
|
|
37
|
+
| `PRIORS_MAX_SPEND_DAY_USD`, `PRIORS_MAX_BORROW_DAY_USD` | optional daily caps |
|
|
38
|
+
|
|
39
|
+
Without a key, only `PRIORS_AGENT_RECORD` works.
|
|
40
|
+
|
|
41
|
+
## Actions
|
|
42
|
+
|
|
43
|
+
| Action | What it does | Moves money |
|
|
44
|
+
|---|---|---|
|
|
45
|
+
| `PRIORS_AGENT_RECORD` | reads another agent's record on the hosted read-only Priors server: its line, repaid and late loans, defaults, score. The message names the agent, as `#1234` | no |
|
|
46
|
+
| `PRIORS_CREDIT_STATUS` | reads this agent's own line: available, drawn, open loans and due dates | no |
|
|
47
|
+
| `PRIORS_PAY_X402` | pays an x402 URL at most the price the message names; if the wallet is short, borrows the gap only when the message names the most to borrow (`borrow up to 5`; a loan is at least the pool's minimum, 5 USDG today) | yes |
|
|
48
|
+
| `PRIORS_REPAY` | repays the loan the message names (`loan 50900`), principal and fee | yes |
|
|
49
|
+
|
|
50
|
+
Example messages:
|
|
51
|
+
|
|
52
|
+
- "Before we pay it, check agent #9233's record on Priors."
|
|
53
|
+
- "Pay https://check.blockpin.dev/check at most 0.10, borrow up to 5 if the wallet is short."
|
|
54
|
+
- "Repay loan 50900 now."
|
|
55
|
+
|
|
56
|
+
## How it works
|
|
57
|
+
|
|
58
|
+
The plugin runs [`@priors/mcp`](https://www.npmjs.com/package/@priors/mcp) in process for the agent's own wallet,
|
|
59
|
+
and reads records from the hosted read-only server at `https://mcp.priors.trade/mcp`. Every cap, the host
|
|
60
|
+
allowlist and the signing are `@priors/mcp`'s own: a payment above `max_price_usd` or to a host outside
|
|
61
|
+
`PRIORS_PAY_HOSTS` is refused before anything is signed, and the server's totals (`PRIORS_MAX_SPEND_USD` 5 and
|
|
62
|
+
`PRIORS_MAX_BORROW_TOTAL_USD` 25 by default while it runs) bound what one process can spend or borrow. Amounts and
|
|
63
|
+
URLs are read from the user's message, never chosen by the model.
|
|
64
|
+
|
|
65
|
+
## Disclosure
|
|
66
|
+
|
|
67
|
+
- Payments and loans are real money: USDG on Robinhood Chain mainnet, signed with the key in `PRIORS_KEY_FILE`.
|
|
68
|
+
- A loan more than three days late can be marked defaulted, which ends the agent's record on Priors.
|
|
69
|
+
- The hosted server counts each connection by its client's runtime name only (https://mcp.priors.trade/privacy).
|
|
70
|
+
|
|
71
|
+
## Demo
|
|
72
|
+
|
|
73
|
+

|
|
74
|
+
|
|
75
|
+
A production run in elizaOS 1.7.2 on 2026-10-30 (agent #437, Robinhood Chain mainnet), its own output lines with waits
|
|
76
|
+
over 6 seconds shortened. The agent reads the seller's record (Blockpin, agent #9233), then pays its x402 check (0.10
|
|
77
|
+
USDG) from an empty wallet: PRIORS_PAY_X402 borrows 5 USDG, the pool's minimum loan (loan #50896). The agent's income
|
|
78
|
+
then arrives from another wallet, and PRIORS_REPAY closes the loan for 5.011666 USDG.
|
|
79
|
+
Transactions: [borrow](https://robinhoodchain.blockscout.com/tx/0x1a17ff6a4f4329d4a16fee0c7654f43104b037a0d9109f9c7ee4c503cbdd2d81),
|
|
80
|
+
[payment](https://robinhoodchain.blockscout.com/tx/0xc508fd46c613aa642bf8fe3c609c2b93eea00f93c173bdda5945e461e0c060c1),
|
|
81
|
+
[repayment](https://robinhoodchain.blockscout.com/tx/0x4186f11d1db84408740dbf7154f71978ef8301d54c7c1a6ef80704737f87c342).
|
|
82
|
+
|
|
83
|
+
## Test
|
|
84
|
+
|
|
85
|
+
```bash
|
|
86
|
+
npm test
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## License
|
|
90
|
+
|
|
91
|
+
MIT
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"id": "priors",
|
|
3
|
+
"name": "@priors/plugin-elizaos",
|
|
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.1.0",
|
|
6
|
+
"kind": "plugin",
|
|
7
|
+
"requiredSecrets": [],
|
|
8
|
+
"configSchema": {
|
|
9
|
+
"type": "object",
|
|
10
|
+
"properties": {
|
|
11
|
+
"PRIORS_KEY_FILE": { "type": "string", "description": "Path of a file holding the agent wallet's private key (chmod 600). Without a key, only records can be read." },
|
|
12
|
+
"PRIORS_AGENT_ID": { "type": "string", "description": "The agent's ERC-8004 id on Robinhood Chain." },
|
|
13
|
+
"PRIORS_PAY_HOSTS": { "type": "string", "description": "The only hosts a payment may go to, comma-separated." },
|
|
14
|
+
"PRIORS_MAX_PRICE_USD": { "type": "string", "description": "Most one x402 call may cost (default 1.00)." },
|
|
15
|
+
"PRIORS_MAX_SPEND_DAY_USD": { "type": "string", "description": "Most the agent may pay per UTC day (optional)." },
|
|
16
|
+
"PRIORS_MAX_BORROW_DAY_USD": { "type": "string", "description": "Most the agent may borrow per UTC day (optional)." }
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"uiHints": {
|
|
20
|
+
"PRIORS_KEY_FILE": { "sensitive": false, "label": "Agent wallet key file" },
|
|
21
|
+
"PRIORS_PAY_HOSTS": { "label": "Hosts it may pay" }
|
|
22
|
+
}
|
|
23
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,40 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@priors/plugin-elizaos",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
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
|
+
"type": "module",
|
|
6
|
+
"main": "./src/index.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./src/index.js",
|
|
9
|
+
"./package.json": "./package.json"
|
|
10
|
+
},
|
|
11
|
+
"files": ["src", "elizaos.plugin.json", "README.md"],
|
|
12
|
+
"packageType": "plugin",
|
|
13
|
+
"keywords": ["elizaos", "elizaos-plugin", "plugin", "priors", "credit", "x402", "usdg", "erc-8004", "robinhood-chain"],
|
|
14
|
+
"homepage": "https://priors.trade",
|
|
15
|
+
"repository": { "type": "git", "url": "git+https://github.com/priors-agents/priors.git", "directory": "integrations/elizaos" },
|
|
16
|
+
"license": "MIT",
|
|
17
|
+
"engines": { "node": ">=20" },
|
|
18
|
+
"scripts": {
|
|
19
|
+
"test": "node --test test/*.test.mjs"
|
|
20
|
+
},
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"@modelcontextprotocol/sdk": "^1.30.1",
|
|
23
|
+
"@priors/mcp": "0.14.1"
|
|
24
|
+
},
|
|
25
|
+
"peerDependencies": {
|
|
26
|
+
"@elizaos/core": "^1.7.0"
|
|
27
|
+
},
|
|
28
|
+
"agentConfig": {
|
|
29
|
+
"pluginType": "elizaos:plugin:1.0.0",
|
|
30
|
+
"pluginParameters": {
|
|
31
|
+
"PRIORS_KEY_FILE": { "type": "string", "description": "Path of a file holding the agent wallet's private key, readable by this user only (chmod 600). A dedicated wallet." },
|
|
32
|
+
"PRIORS_KEY": { "type": "string", "description": "The agent wallet's private key, if no key file is used." },
|
|
33
|
+
"PRIORS_AGENT_ID": { "type": "string", "description": "The agent's ERC-8004 id on Robinhood Chain." },
|
|
34
|
+
"PRIORS_PAY_HOSTS": { "type": "string", "description": "The only hosts a payment may go to, comma-separated." },
|
|
35
|
+
"PRIORS_MAX_PRICE_USD": { "type": "string", "description": "Most one x402 call may cost (default 1.00)." },
|
|
36
|
+
"PRIORS_MAX_SPEND_DAY_USD": { "type": "string", "description": "Most the agent may pay per UTC day (optional)." },
|
|
37
|
+
"PRIORS_MAX_BORROW_DAY_USD": { "type": "string", "description": "Most the agent may borrow per UTC day (optional)." }
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
// @priors/plugin-elizaos: Priors credit for elizaOS agents. Four actions over the two Priors MCP servers (priors.js):
|
|
2
|
+
// read another agent's repayment record, read the agent's own line, pay an x402 URL that borrows the gap when the
|
|
3
|
+
// wallet is short, and repay a loan. Every amount and URL comes from the user's own message (parse.js); a payment
|
|
4
|
+
// borrows only when the message names the most to borrow. The caps, the host allowlist and the signing are
|
|
5
|
+
// @priors/mcp's own.
|
|
6
|
+
import { priorsOf } from "./priors.js";
|
|
7
|
+
import { urlOf, agentIdOf, loanIdOf, maxPriceOf, maxBorrowOf } from "./parse.js";
|
|
8
|
+
|
|
9
|
+
const textOf = (message) => String((message && message.content && message.content.text) || "");
|
|
10
|
+
|
|
11
|
+
/** Answer through the callback and as the action's result. */
|
|
12
|
+
async function answer(callback, action, ok, text, data = {}) {
|
|
13
|
+
if (typeof callback === "function") await callback({ text, actions: [action] });
|
|
14
|
+
return { success: ok, text, data: { actionName: action, ...data } };
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export const agentRecordAction = {
|
|
18
|
+
name: "PRIORS_AGENT_RECORD",
|
|
19
|
+
similes: ["CHECK_AGENT_RECORD", "AGENT_REPAYMENT_RECORD", "PRIORS_RECORD"],
|
|
20
|
+
description: "Read an ERC-8004 agent's repayment record on Priors (its line, repaid and late loans, defaults, score) before paying or hiring it. Needs the agent id in the message, as #1234.",
|
|
21
|
+
validate: async (_runtime, message) => agentIdOf(textOf(message)) !== null && /record|repa|default|reliab|trust|check|score/i.test(textOf(message)),
|
|
22
|
+
handler: async (runtime, message, _state, _options, callback) => {
|
|
23
|
+
const id = agentIdOf(textOf(message));
|
|
24
|
+
const r = await priorsOf(runtime).read("agent_record", { agent_id: id });
|
|
25
|
+
return answer(callback, "PRIORS_AGENT_RECORD", r.ok, r.text, { agentId: id });
|
|
26
|
+
},
|
|
27
|
+
examples: [[
|
|
28
|
+
{ name: "{{user1}}", content: { text: "Before we pay it, check agent #9233's record on Priors." } },
|
|
29
|
+
{ name: "{{agentName}}", content: { text: "Agent #9233 has repaid 2 loans and never defaulted.", actions: ["PRIORS_AGENT_RECORD"] } },
|
|
30
|
+
]],
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export const creditStatusAction = {
|
|
34
|
+
name: "PRIORS_CREDIT_STATUS",
|
|
35
|
+
similes: ["MY_CREDIT_LINE", "PRIORS_LINE", "OPEN_LOANS"],
|
|
36
|
+
description: "Read this agent's own Priors credit line on Robinhood Chain: what is available, what is drawn, its open loans and their due dates.",
|
|
37
|
+
validate: async (_runtime, message) => !urlOf(textOf(message)) && /credit line|my line|\bline\b|open loans?|what (do )?i owe|due/i.test(textOf(message)),
|
|
38
|
+
handler: async (runtime, _message, _state, _options, callback) => {
|
|
39
|
+
const r = await priorsOf(runtime).wallet("credit_status", {});
|
|
40
|
+
return answer(callback, "PRIORS_CREDIT_STATUS", r.ok, r.text);
|
|
41
|
+
},
|
|
42
|
+
examples: [[
|
|
43
|
+
{ name: "{{user1}}", content: { text: "How much is left on my Priors line, and what is due?" } },
|
|
44
|
+
{ name: "{{agentName}}", content: { text: "Your line is 5 USDG; 4.90 is available and one loan is due on 6 November.", actions: ["PRIORS_CREDIT_STATUS"] } },
|
|
45
|
+
]],
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export const payX402Action = {
|
|
49
|
+
name: "PRIORS_PAY_X402",
|
|
50
|
+
similes: ["PAY_X402", "PAY_HTTP_402", "BUY_API_CALL"],
|
|
51
|
+
description: "Pay an HTTP 402 (x402) URL in USDG from this agent's wallet. If the wallet is short, it borrows the gap from the agent's Priors line, only when the message names the most to borrow. The message must name the URL and the most to pay. Real money.",
|
|
52
|
+
validate: async (_runtime, message) => !!urlOf(textOf(message)) && /\bpay|buy|call|fetch/i.test(textOf(message)),
|
|
53
|
+
handler: async (runtime, message, _state, _options, callback) => {
|
|
54
|
+
const text = textOf(message);
|
|
55
|
+
const url = urlOf(text), maxPrice = maxPriceOf(text), maxBorrow = maxBorrowOf(text);
|
|
56
|
+
if (maxPrice === null || maxPrice <= 0) {
|
|
57
|
+
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 });
|
|
58
|
+
}
|
|
59
|
+
const args = { url, max_price_usd: maxPrice, ...(maxBorrow > 0 ? { max_borrow_usd: maxBorrow } : {}) };
|
|
60
|
+
const r = await priorsOf(runtime).wallet("pay_url", args);
|
|
61
|
+
return answer(callback, "PRIORS_PAY_X402", r.ok, r.text, { url, maxPrice, maxBorrow });
|
|
62
|
+
},
|
|
63
|
+
examples: [[
|
|
64
|
+
{ name: "{{user1}}", content: { text: "Pay https://check.blockpin.dev/check at most 0.10, borrow up to 5 if the wallet is short." } },
|
|
65
|
+
{ name: "{{agentName}}", content: { text: "Paid 0.10 USDG; the wallet was short, so it borrowed 5 USDG, the pool's minimum loan, from the Priors line (loan #50900, due 6 November).", actions: ["PRIORS_PAY_X402"] } },
|
|
66
|
+
]],
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
export const repayAction = {
|
|
70
|
+
name: "PRIORS_REPAY",
|
|
71
|
+
similes: ["REPAY_LOAN", "PAY_BACK_LOAN"],
|
|
72
|
+
description: "Repay one of this agent's Priors loans in full (principal and fee) from its wallet. Needs the loan id in the message, as loan 50900. Real money.",
|
|
73
|
+
validate: async (_runtime, message) => loanIdOf(textOf(message)) !== null && /repay|pay (it )?back/i.test(textOf(message)),
|
|
74
|
+
handler: async (runtime, message, _state, _options, callback) => {
|
|
75
|
+
const loan = loanIdOf(textOf(message));
|
|
76
|
+
const r = await priorsOf(runtime).wallet("repay", { loan_id: loan });
|
|
77
|
+
return answer(callback, "PRIORS_REPAY", r.ok, r.text, { loanId: loan });
|
|
78
|
+
},
|
|
79
|
+
examples: [[
|
|
80
|
+
{ name: "{{user1}}", content: { text: "Repay loan 50900 now." } },
|
|
81
|
+
{ name: "{{agentName}}", content: { text: "Loan #50900 repaid: 5.011666 USDG.", actions: ["PRIORS_REPAY"] } },
|
|
82
|
+
]],
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
export const priorsPlugin = {
|
|
86
|
+
name: "priors",
|
|
87
|
+
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.",
|
|
88
|
+
actions: [agentRecordAction, creditStatusAction, payX402Action, repayAction],
|
|
89
|
+
providers: [],
|
|
90
|
+
evaluators: [],
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
export default priorsPlugin;
|
package/src/parse.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// What an action reads from the user's message: plain patterns, never a model's guess. A payment needs its URL and
|
|
2
|
+
// the most to pay named in the message; it borrows only when the message names the most to borrow.
|
|
3
|
+
|
|
4
|
+
const num = (s) => { const n = Number(String(s).replace(",", ".")); return Number.isFinite(n) && n >= 0 ? n : null; };
|
|
5
|
+
|
|
6
|
+
/** The first https URL in the text, or null. */
|
|
7
|
+
export function urlOf(text) {
|
|
8
|
+
const m = String(text || "").match(/https:\/\/[^\s<>"'`)\]]+/i);
|
|
9
|
+
if (!m) return null;
|
|
10
|
+
try { return new URL(m[0].replace(/[.,;:!?]+$/, "")).href; } catch { return null; }
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** An agent id: "agent 9233" or "agent #9233" first, else a bare "#9233" that is not a loan's; null when none. */
|
|
14
|
+
export function agentIdOf(text) {
|
|
15
|
+
const t = String(text || "");
|
|
16
|
+
const named = t.match(/agent\s*#?\s*(\d{1,12})\b/i);
|
|
17
|
+
if (named) return Number(named[1]);
|
|
18
|
+
for (const m of t.matchAll(/#(\d{1,12})\b/g)) if (!/loan\s*$/i.test(t.slice(0, m.index))) return Number(m[1]);
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** A loan id: "loan 50327" or "loan #50327"; null when none. */
|
|
23
|
+
export function loanIdOf(text) {
|
|
24
|
+
const m = String(text || "").match(/loan\s*#?\s*(\d{1,12})\b/i);
|
|
25
|
+
return m ? Number(m[1]) : null;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// The borrow phrase with its amount: "borrow up to 1", "borrow at most $0.50", "borrow 1".
|
|
29
|
+
const BORROW = /borrow(?:ing)?\s*(?:up to|at most|no more than|max(?:imum)?)?\s*\$?\s*(\d+(?:[.,]\d+)?)/i;
|
|
30
|
+
|
|
31
|
+
/** The most to pay: "at most 0.10", "max 0.10", "up to $0.10 for it", "pay 0.10"; null when none. The borrow phrase,
|
|
32
|
+
* wherever it stands, is taken out first, so its amount is never read as the price. */
|
|
33
|
+
export function maxPriceOf(text) {
|
|
34
|
+
const t = String(text || "").replace(new RegExp(BORROW.source, "gi"), " ");
|
|
35
|
+
const m = t.match(/(?:at most|max(?:imum)?|up to|no more than|pay)\s*\$?\s*(\d+(?:[.,]\d+)?)/i);
|
|
36
|
+
return m ? num(m[1]) : null;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** The most to borrow: "borrow up to 1", "borrow at most $0.50", "borrow 1"; 0 when the message names none. */
|
|
40
|
+
export function maxBorrowOf(text) {
|
|
41
|
+
const m = String(text || "").match(BORROW);
|
|
42
|
+
return m ? num(m[1]) ?? 0 : 0;
|
|
43
|
+
}
|
package/src/priors.js
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
// The two Priors MCP servers an elizaOS agent talks to: @priors/mcp in process (the agent's own wallet: its line, x402
|
|
2
|
+
// payments that borrow the gap, repayments; every cap and the host allowlist are that server's own), and the hosted
|
|
3
|
+
// read-only server (another agent's record). Each is connected once per runtime, on first use.
|
|
4
|
+
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
5
|
+
import { InMemoryTransport } from "@modelcontextprotocol/sdk/inMemory.js";
|
|
6
|
+
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
|
|
7
|
+
|
|
8
|
+
export const HOSTED_URL = "https://mcp.priors.trade/mcp";
|
|
9
|
+
export const CLIENT = { name: "elizaos-plugin-priors", version: "0.1.0" };
|
|
10
|
+
|
|
11
|
+
/** The settings @priors/mcp reads, taken from the agent's settings (runtime.getSetting), else the process env. */
|
|
12
|
+
export const SETTINGS = Object.freeze([
|
|
13
|
+
"PRIORS_KEY_FILE", "PRIORS_KEY", "PRIORS_AGENT_ID", "PRIORS_PAY_HOSTS", "PRIORS_RPC", "PRIORS_STATE_DIR",
|
|
14
|
+
"PRIORS_MAX_PRICE_USD", "PRIORS_MAX_BORROW_USD", "PRIORS_MAX_SPEND_USD", "PRIORS_MAX_BORROW_TOTAL_USD",
|
|
15
|
+
"PRIORS_MAX_SPEND_DAY_USD", "PRIORS_MAX_BORROW_DAY_USD",
|
|
16
|
+
]);
|
|
17
|
+
|
|
18
|
+
/** The env @priors/mcp is built with: the process env, with every Priors setting the agent has replacing it. */
|
|
19
|
+
export function envOf(runtime, base = process.env) {
|
|
20
|
+
const env = { ...base };
|
|
21
|
+
for (const k of SETTINGS) {
|
|
22
|
+
const v = runtime && typeof runtime.getSetting === "function" ? runtime.getSetting(k) : undefined;
|
|
23
|
+
if (v !== undefined && v !== null && String(v) !== "") env[k] = String(v);
|
|
24
|
+
}
|
|
25
|
+
return env;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** A tool answer as text (what the server says, never more). */
|
|
29
|
+
const textOf = (r) => (r && Array.isArray(r.content) ? r.content.filter((c) => c && c.type === "text").map((c) => c.text).join("\n") : "");
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* The Priors calls of one runtime: `wallet(tool, args)` on @priors/mcp in process, `read(tool, args)` on the hosted
|
|
33
|
+
* server. Each answers { ok, text }. `connect` replaces both connections for tests: ({ kind, env }) => client with
|
|
34
|
+
* callTool.
|
|
35
|
+
*/
|
|
36
|
+
export function priorsFor(runtime, { connect = defaultConnect, base = process.env } = {}) {
|
|
37
|
+
const clients = {};
|
|
38
|
+
const clientOf = (kind) => (clients[kind] ||= connect({ kind, env: envOf(runtime, base) }));
|
|
39
|
+
const call = (kind) => async (name, args = {}) => {
|
|
40
|
+
try {
|
|
41
|
+
const r = await (await clientOf(kind)).callTool({ name, arguments: args }, undefined, { timeout: 180_000 });
|
|
42
|
+
return { ok: !r.isError, text: textOf(r) };
|
|
43
|
+
} catch (e) {
|
|
44
|
+
clients[kind] = null; // a broken connection is opened again on the next call
|
|
45
|
+
return { ok: false, text: `Priors (${kind}): ${String((e && e.message) || e).slice(0, 300)}` };
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
return { wallet: call("wallet"), read: call("read") };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async function defaultConnect({ kind, env }) {
|
|
52
|
+
const client = new Client(CLIENT);
|
|
53
|
+
if (kind === "read") {
|
|
54
|
+
await client.connect(new StreamableHTTPClientTransport(new URL(HOSTED_URL)));
|
|
55
|
+
return client;
|
|
56
|
+
}
|
|
57
|
+
const { createPriorsMcpServer } = await import("@priors/mcp");
|
|
58
|
+
const server = await createPriorsMcpServer({ env });
|
|
59
|
+
const [a, b] = InMemoryTransport.createLinkedPair();
|
|
60
|
+
await server.connect(a);
|
|
61
|
+
await client.connect(b);
|
|
62
|
+
return client;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** One Priors per runtime (an agent's runtime object), connected lazily. */
|
|
66
|
+
const byRuntime = new WeakMap();
|
|
67
|
+
export function priorsOf(runtime, opts) {
|
|
68
|
+
if (!runtime || typeof runtime !== "object") return priorsFor(runtime, opts);
|
|
69
|
+
if (!byRuntime.has(runtime)) byRuntime.set(runtime, priorsFor(runtime, opts));
|
|
70
|
+
return byRuntime.get(runtime);
|
|
71
|
+
}
|