cornerstone-autonomous-agent 1.3.0 → 2.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.
@@ -1,53 +1,77 @@
1
- /**
2
- * LangChain.js ReAct agent: MCP tools + local tools, Hugging Face LLM.
3
- */
4
-
5
- import { createReactAgent } from '@langchain/langgraph/prebuilt';
6
- import { createLLM } from './llm.js';
7
- import { createMcpTools } from './tools/mcpTools.js';
8
- import { createLocalTools } from './tools/localTools.js';
9
-
10
- const SYSTEM_MESSAGE = `You are an autonomous agent that can create, fund, and use your own Aptos and EVM wallets (optionally testnet or mainnet), then run stock predictions, backtests, and link bank accounts via paid MCP tools.
11
-
12
- Before trying any paid or wallet-dependent action: use get_wallet_addresses to see which wallets are configured. It returns lists: aptos: [{ address, network? }, ...], evm: [{ address, network? }, ...]. You can have multiple Aptos and multiple EVM wallets (e.g. one testnet and one mainnet each). If none exist, use create_aptos_wallet and create_evm_wallet (optionally pass network: "testnet" or "mainnet"; default is testnet). Use force: true to add another wallet when one already exists. Then tell the user to whitelist all addresses at http://localhost:4024/flow.html — the whitelist form supports multiple EVM and multiple Aptos rows with an optional testnet/mainnet tag per address. Use credit_aptos_wallet to fund the Aptos wallet (devnet programmatic or testnet instructions). Use fund_evm_wallet to get funding instructions for the EVM wallet.
13
-
14
- When the user asks to create or produce wallets: use create_aptos_wallet and create_evm_wallet (with network if they want testnet or mainnet), then credit_aptos_wallet and fund_evm_wallet for instructions, and remind the user to whitelist every address at http://localhost:4024/flow.html (add multiple EVM and Aptos rows as needed; tag testnet/mainnet if relevant).
15
-
16
- Use run_prediction for stock predictions (symbol, horizon in days). Use run_backtest for backtesting a strategy.
17
- Use link_bank_account to start the bank linking flow.
18
- Use get_agent_reputation_score and get_borrower_score to query scores for allowlisted wallets; use get_agent_reputation_score_by_email and get_borrower_score_by_email when you have an email (requires SCORE_BY_EMAIL_ENABLED on server). Score tools can be paid via x402 or lender credits (payer_wallet).
19
- Use balance_aptos and balance_evm to check wallet balances.
20
- When you need to pay for a tool (402), the payment is handled automatically; just call the tool.`;
21
-
22
- /**
23
- * Create agent graph: llm + tools (MCP + local).
24
- * @param {{ llm?: import('@langchain/core/language_models/chat_models').BaseChatModel; tools?: import('@langchain/core/tools').StructuredToolInterface[] }} options - llm and tools (if omitted, created from env and mcpClient)
25
- * @param {{ callTool: (name: string, args: Object) => Promise<Object> }} [options.mcpClient] - required if tools not provided
26
- * @returns {Promise<{ agent: import('@langchain/langgraph').CompiledStateGraph; runAgent: (message: string) => Promise<Object> }>}
27
- */
28
- export async function createAgent(options = {}) {
29
- const llm = options.llm || createLLM();
30
- let tools = options.tools;
31
- if (!tools && options.mcpClient) {
32
- tools = [...createMcpTools(options.mcpClient), ...createLocalTools()];
33
- }
34
- if (!tools) {
35
- throw new Error('Provide options.tools or options.mcpClient to createAgent.');
36
- }
37
-
38
- const agent = createReactAgent({
39
- llm,
40
- tools,
41
- stateModifier: (state) => [{ role: 'system', content: SYSTEM_MESSAGE }, ...(state.messages || [])],
42
- });
43
-
44
- async function runAgent(userMessage) {
45
- const result = await agent.invoke(
46
- { messages: [{ role: 'user', content: userMessage }] },
47
- { recursionLimit: 50 }
48
- );
49
- return result;
50
- }
51
-
52
- return { agent, runAgent };
53
- }
1
+ /**
2
+ * LangChain.js ReAct runner for the skill: MCP tools + local tools, Hugging Face LLM.
3
+ */
4
+
5
+ import { createReactAgent } from '@langchain/langgraph/prebuilt';
6
+ import { createLLM } from './llm.js';
7
+ import { createMcpTools } from './tools/mcpTools.js';
8
+ import { createLocalTools } from './tools/localTools.js';
9
+
10
+ const SYSTEM_MESSAGE = `You are an agent with a skill that provides tools for wallet management and x402-paid MCP tools (predictions, backtests, bank linking, scores). Payment is automatic just call the tool.
11
+
12
+ ## ALWAYS DO FIRST
13
+ Before any paid or wallet action, call get_wallet_addresses (no args). It returns:
14
+ { aptos: [{ address, network }], evm: [{ address, network }] }
15
+ - If aptos is empty and you need Aptos tools: create_aptos_wallet -> credit_aptos_wallet -> tell user to whitelist at https://arnstein.ch/flow.html
16
+ - If evm is empty and you need EVM tools: create_evm_wallet -> fund_evm_wallet -> tell user to whitelist at https://arnstein.ch/flow.html
17
+ - If wallets exist: check balance before paid calls. balance_aptos (for prediction/backtest/scores) or balance_evm({ chain: "baseSepolia" }) (for link_bank_account).
18
+
19
+ ## TOOLS
20
+
21
+ Wallet tools:
22
+ - get_wallet_addresses() -> { aptos: [...], evm: [...] }
23
+ - create_aptos_wallet({ force?, network? }) -> { success, address, network }
24
+ - create_evm_wallet({ force?, network? }) -> { success, address, network }
25
+ - credit_aptos_wallet({ amount_octas? }) -> devnet: funds directly; testnet: returns faucet_url + instructions
26
+ - fund_evm_wallet() -> returns faucet_url + address (manual funding)
27
+ - balance_aptos() -> { address, balances: { usdc, apt } }
28
+ - balance_evm({ chain? }) -> { address, chain, balance, symbol }. Chains: base, baseSepolia, ethereum, polygon, arbitrum, optimism
29
+
30
+ Paid MCP tools (payment handled for you — just call them):
31
+ - run_prediction({ symbol, horizon? }) — stock prediction, ~6c USDC. Example: run_prediction({ symbol: "AAPL", horizon: 30 })
32
+ - run_backtest({ symbol, startDate?, endDate?, strategy? }) — backtest, ~6c. Dates "YYYY-MM-DD", strategy default "chronos"
33
+ - link_bank_account() — Plaid bank link, ~5c on Base. Needs funded EVM wallet (baseSepolia for testnet)
34
+ - get_agent_reputation_score({ agent_address?, payer_wallet? }) — reputation score
35
+ - get_borrower_score({ agent_address?, payer_wallet? }) borrower score
36
+ - get_agent_reputation_score_by_email({ email, payer_wallet? }) — by email (needs SCORE_BY_EMAIL_ENABLED)
37
+ - get_borrower_score_by_email({ email, payer_wallet? }) — by email
38
+
39
+ ## ERROR RECOVERY
40
+ - "No Aptos/EVM wallet" -> create the missing wallet
41
+ - "already exist" -> use existing wallet, or pass force: true to add another
42
+ - "Payment verification failed" -> insufficient funds; check balance, tell user to fund
43
+ - "403" / "not allowlisted" -> tell user to whitelist at https://arnstein.ch/flow.html
44
+ - Timeout -> retry once`;
45
+
46
+ /**
47
+ * Create agent graph: llm + tools (MCP + local).
48
+ * @param {{ llm?: import('@langchain/core/language_models/chat_models').BaseChatModel; tools?: import('@langchain/core/tools').StructuredToolInterface[] }} options - llm and tools (if omitted, created from env and mcpClient)
49
+ * @param {{ callTool: (name: string, args: Object) => Promise<Object> }} [options.mcpClient] - required if tools not provided
50
+ * @returns {Promise<{ agent: import('@langchain/langgraph').CompiledStateGraph; runAgent: (message: string) => Promise<Object> }>}
51
+ */
52
+ export async function createAgent(options = {}) {
53
+ const llm = options.llm || createLLM();
54
+ let tools = options.tools;
55
+ if (!tools && options.mcpClient) {
56
+ tools = [...createMcpTools(options.mcpClient), ...createLocalTools()];
57
+ }
58
+ if (!tools) {
59
+ throw new Error('Provide options.tools or options.mcpClient to createAgent.');
60
+ }
61
+
62
+ const agent = createReactAgent({
63
+ llm,
64
+ tools,
65
+ stateModifier: (state) => [{ role: 'system', content: SYSTEM_MESSAGE }, ...(state.messages || [])],
66
+ });
67
+
68
+ async function runAgent(userMessage) {
69
+ const result = await agent.invoke(
70
+ { messages: [{ role: 'user', content: userMessage }] },
71
+ { recursionLimit: 50 }
72
+ );
73
+ return result;
74
+ }
75
+
76
+ return { agent, runAgent };
77
+ }