@xyz-credit/agent-cli 1.1.2 → 1.2.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.
@@ -0,0 +1,131 @@
1
+ /**
2
+ * Risk Scoring Service — Autonomous background risk calculation
3
+ *
4
+ * Computes an agent's Initial Risk Score based on:
5
+ * 1. Device Flow verification status
6
+ * 2. Wallet age/activity
7
+ * 3. LLM API key verification
8
+ * 4. MCP tools count
9
+ * 5. Services registered
10
+ *
11
+ * Score is sent to Central Platform; read-only for the user.
12
+ */
13
+ const fetch = require('node-fetch');
14
+ const chalk = require('chalk');
15
+ const { config, isAuthenticated, getCredentials } = require('../config');
16
+
17
+ async function computeRiskScore() {
18
+ if (!isAuthenticated()) return null;
19
+
20
+ const creds = getCredentials();
21
+ const deviceVerified = !!creds.agentId;
22
+ const walletProvided = !!creds.walletAddress;
23
+ const llmVerified = !!config.get('llmApiKey') || config.get('llmProvider') === 'ollama';
24
+ const mcpToolsCount = (config.get('discoveredTools') || []).length;
25
+ const servicesRegistered = (config.get('registeredServices') || []).length;
26
+
27
+ const payload = {
28
+ agent_id: creds.agentId,
29
+ api_key: creds.apiKey,
30
+ device_verified: deviceVerified,
31
+ wallet_provided: walletProvided,
32
+ llm_verified: llmVerified,
33
+ mcp_tools_count: mcpToolsCount,
34
+ services_registered: servicesRegistered,
35
+ };
36
+
37
+ try {
38
+ const res = await fetch(`${creds.platformUrl}/api/cli/risk-score`, {
39
+ method: 'POST',
40
+ headers: { 'Content-Type': 'application/json' },
41
+ body: JSON.stringify(payload),
42
+ timeout: 10000,
43
+ });
44
+
45
+ if (res.ok) {
46
+ const data = await res.json();
47
+ config.set('riskScore', data.risk_score);
48
+ return data;
49
+ }
50
+ } catch (e) {
51
+ // Silently compute locally
52
+ }
53
+
54
+ // Local fallback computation
55
+ let score = 0;
56
+ if (deviceVerified) score += 30;
57
+ if (walletProvided) score += 20;
58
+ if (llmVerified) score += 25;
59
+ if (mcpToolsCount > 0) score += Math.min(15, mcpToolsCount * 3);
60
+ if (servicesRegistered > 0) score += Math.min(10, servicesRegistered * 5);
61
+
62
+ config.set('riskScore', score);
63
+ return { risk_score: score, max_score: 100, local: true };
64
+ }
65
+
66
+ /**
67
+ * Discovery Script — reads LOCAL_MCP_SOURCE tools and auto-registers as Services
68
+ */
69
+ async function discoverAndRegisterServices() {
70
+ if (!isAuthenticated()) return null;
71
+
72
+ const creds = getCredentials();
73
+ const mcpUrl = config.get('localMcpUrl');
74
+ if (!mcpUrl) return null;
75
+
76
+ // Discover tools
77
+ const { discoverMcpTools } = require('../commands/connect');
78
+ let tools;
79
+ try {
80
+ tools = await discoverMcpTools(mcpUrl);
81
+ } catch {
82
+ return null;
83
+ }
84
+
85
+ if (!tools || tools.length === 0) return null;
86
+
87
+ // Store discovered tools
88
+ config.set('discoveredTools', tools.map(t => ({
89
+ name: t.name,
90
+ description: (t.description || '').split('\n')[0].slice(0, 200),
91
+ input_schema: t.inputSchema || {},
92
+ })));
93
+
94
+ // Auto-register on platform
95
+ try {
96
+ const res = await fetch(`${creds.platformUrl}/api/services/register`, {
97
+ method: 'POST',
98
+ headers: { 'Content-Type': 'application/json' },
99
+ body: JSON.stringify({
100
+ agent_id: creds.agentId,
101
+ api_key: creds.apiKey,
102
+ service_name: `${creds.agentName}-auto-service`,
103
+ description: `${tools.length} tools auto-discovered from local MCP`,
104
+ tools: tools.map(t => ({
105
+ name: t.name,
106
+ description: (t.description || '').split('\n')[0].slice(0, 200),
107
+ input_schema: t.inputSchema || {},
108
+ })),
109
+ fee_usdc: 0.10,
110
+ fee_eurc: 0,
111
+ category: 'general',
112
+ tags: tools.slice(0, 3).map(t => t.name),
113
+ }),
114
+ timeout: 15000,
115
+ });
116
+
117
+ if (res.ok) {
118
+ const data = await res.json();
119
+ const services = config.get('registeredServices') || [];
120
+ services.push({ id: data.service_id, name: `${creds.agentName}-auto-service` });
121
+ config.set('registeredServices', services);
122
+ return { registered: true, service_id: data.service_id, tools_count: tools.length };
123
+ }
124
+ } catch {
125
+ // Silent fail
126
+ }
127
+
128
+ return { registered: false, tools_count: tools.length };
129
+ }
130
+
131
+ module.exports = { computeRiskScore, discoverAndRegisterServices };