-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrecommend.js
More file actions
198 lines (173 loc) · 7.48 KB
/
recommend.js
File metadata and controls
198 lines (173 loc) · 7.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/**
* Agent Connect — Decision Engine for Autonomous Agents
*
* An autonomous agent calls recommend() BEFORE connect().
* It receives structured recommendations with alternatives,
* cost estimates, warnings, and fallback strategies.
*
* The agent makes the final decision — the SDK never decides for it.
*/
import {
queryOnlineNodes,
fetchActiveNodes,
formatP2P,
IS_ADMIN,
WG_AVAILABLE,
} from 'blue-js-sdk';
import { toCountryCode, filterByProtocol, filterByCountry } from './recommend-filters.js';
import { rankNodes, formatNode } from './recommend-scoring.js';
// ─── recommend() ─────────────────────────────────────────────────────────────
/**
* Generate structured recommendations for an autonomous AI agent.
*
* The agent provides its preferences. The SDK returns ranked options
* with cost estimates, warnings, and fallback strategies.
* The agent makes the final decision.
*
* @param {object} preferences
* @param {string} [preferences.country] - Preferred country (name or ISO code)
* @param {number} [preferences.budget] - Available budget in udvpn
* @param {'reliability'|'cost'|'speed'|'location'} [preferences.priority='reliability'] - What matters most
* @param {number} [preferences.gigabytes=1] - Planned data usage
* @param {string} [preferences.protocol] - Force 'wireguard' or 'v2ray'
* @param {boolean} [preferences.strictCountry=false] - If true, fail if exact country unavailable
* @param {number} [preferences.maxNodes=50] - Max nodes to evaluate
*
* @returns {Promise<{
* action: 'connect'|'connect-fallback'|'cannot-connect',
* confidence: number,
* primary: object|null,
* alternatives: object[],
* fallbackStrategy: string,
* estimatedCost: { udvpn: number, p2p: string },
* warnings: string[],
* reasoning: string[],
* capabilities: { wireguard: boolean, v2ray: boolean, admin: boolean },
* }>}
*/
export async function recommend(preferences = {}) {
if (preferences && typeof preferences !== 'object') {
throw new Error('recommend(): preferences must be an object or undefined');
}
const {
country = null,
budget = 0,
priority = 'reliability',
gigabytes = 1,
protocol = null,
strictCountry = false,
maxNodes = 50,
} = preferences;
const warnings = [];
const reasoning = [];
const countryCode = toCountryCode(country);
// ─── Capabilities assessment ───────────────────────────────────────────
const canWG = WG_AVAILABLE && IS_ADMIN;
const canV2 = true; // V2Ray always available if binary exists
const capabilities = { wireguard: canWG, v2ray: canV2, admin: IS_ADMIN };
if (protocol === 'wireguard' && !canWG) {
warnings.push('WireGuard requested but not available (need admin + WireGuard installed). Falling back to V2Ray.');
reasoning.push('Protocol constraint: WireGuard unavailable, using V2Ray');
}
if (!IS_ADMIN && WG_AVAILABLE) {
warnings.push('WireGuard installed but not admin — running as admin unlocks faster WireGuard nodes');
}
// ─── Fetch nodes ───────────────────────────────────────────────────────
reasoning.push('Fetching active nodes from chain...');
let allNodes;
try {
// fetchActiveNodes returns raw chain data (no country/location).
// If country filter needed, we need enriched data from queryOnlineNodes.
if (countryCode) {
reasoning.push('Country filter requested — probing nodes for location data...');
allNodes = await queryOnlineNodes({ maxNodes: maxNodes * 3 });
} else {
allNodes = await fetchActiveNodes();
}
reasoning.push(`Found ${allNodes.length} active nodes`);
} catch (err) {
return {
action: 'cannot-connect',
confidence: 0,
primary: null,
alternatives: [],
fallbackStrategy: 'none',
estimatedCost: { udvpn: 0, p2p: '0 P2P' },
warnings: [`Chain query failed: ${err.message}`],
reasoning: ['Cannot fetch nodes — network may be unreachable'],
capabilities,
};
}
// ─── Filter by protocol ────────────────────────────────────────────────
const { filtered: candidates } = filterByProtocol(allNodes, protocol, capabilities, reasoning);
// ─── Filter by country ─────────────────────────────────────────────────
const { exactCountryNodes, nearbyNodes } = filterByCountry(
candidates, countryCode, country, strictCountry, reasoning,
);
// ─── Score and rank ────────────────────────────────────────────────────
const top = rankNodes(exactCountryNodes, nearbyNodes, candidates, priority, maxNodes);
// ─── Build recommendation ──────────────────────────────────────────────
if (top.length === 0) {
return {
action: 'cannot-connect',
confidence: 0,
primary: null,
alternatives: [],
fallbackStrategy: strictCountry ? 'fail' : 'none',
estimatedCost: { udvpn: 0, p2p: '0 P2P' },
warnings: [`No nodes available${country ? ` for ${country}` : ''}`],
reasoning,
capabilities,
};
}
if (countryCode && exactCountryNodes.length === 0 && strictCountry) {
return {
action: 'cannot-connect',
confidence: 0,
primary: null,
alternatives: top.slice(0, 5).map(formatNode),
fallbackStrategy: 'fail — strictCountry is true',
estimatedCost: { udvpn: 0, p2p: '0 P2P' },
warnings: [`No nodes in ${country} (${countryCode}). strictCountry=true prevents fallback.`],
reasoning,
capabilities,
};
}
const primary = top[0];
const alternatives = top.slice(1, 6).map(formatNode);
const gasCost = 40000;
const sessionCost = (primary._price || 100000) * gigabytes;
const totalCost = sessionCost + gasCost;
// Budget check
if (budget > 0 && budget < totalCost) {
warnings.push(`Budget (${formatP2P(budget)}) may be insufficient for ${gigabytes} GB (estimated ${formatP2P(totalCost)})`);
}
// Determine action
let action = 'connect';
let confidence = 0.9;
if (countryCode && exactCountryNodes.length === 0) {
action = 'connect-fallback';
confidence = 0.7;
const fc = primary._fallbackCountry || 'nearest available';
warnings.push(`Exact country ${country} not available. Recommending ${fc} as fallback.`);
reasoning.push(`Fallback: ${country} → ${fc}`);
}
// Determine fallback strategy
let fallbackStrategy = 'auto — SDK tries next node on failure';
if (countryCode && exactCountryNodes.length > 0) {
fallbackStrategy = `${exactCountryNodes.length} nodes in ${country}; SDK retries within country`;
} else if (nearbyNodes.length > 0) {
fallbackStrategy = `nearest-country — ${nearbyNodes.length} nodes in nearby countries`;
}
return {
action,
confidence,
primary: formatNode(primary),
alternatives,
fallbackStrategy,
estimatedCost: { udvpn: totalCost, p2p: formatP2P(totalCost) },
warnings,
reasoning,
capabilities,
};
}