@reefclaw/openclaw-plugin 0.1.48 → 0.1.50
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 +10 -6
- package/bridge/bridge.js +22 -16
- package/bridge/gateway/gateway-ws-client.d.ts +79 -1
- package/bridge/gateway/gateway-ws-client.js +99 -5
- package/bridge/gateway/heartbeat-cron.d.ts +64 -0
- package/bridge/gateway/heartbeat-cron.js +150 -1
- package/bridge/provider.d.ts +12 -0
- package/bridge/providers/connector-update-direct.d.ts +3 -0
- package/bridge/providers/connector-update-direct.js +11 -1
- package/bridge/providers/connector-update-openclaw.d.ts +268 -0
- package/bridge/providers/connector-update-openclaw.js +772 -0
- package/bridge/providers/connector-update.d.ts +115 -1
- package/bridge/providers/connector-update.js +228 -13
- package/bridge/providers/gateway.d.ts +52 -10
- package/bridge/providers/gateway.js +243 -34
- package/bridge/utils/skill-version.d.ts +6 -0
- package/bridge/utils/skill-version.js +10 -0
- package/config/first-start-venue.d.ts +147 -0
- package/config/first-start-venue.js +400 -0
- package/config/plugin-config-io.d.ts +9 -2
- package/config/plugin-config-io.js +29 -5
- package/connector-supervisor.d.ts +20 -0
- package/connector-supervisor.js +27 -1
- package/index.js +36 -1
- package/openclaw.plugin.json +333 -1
- package/package.json +1 -1
- package/release-integrity.json +10 -0
- package/release-notes.json +3 -1
- package/signals/stop-rules.d.ts +13 -0
- package/signals/stop-rules.js +35 -6
- package/signals/strategy-adapter.js +25 -6
- package/skills/reefclaw/SKILL.md +21 -8
- package/tools/assessment-validation.d.ts +10 -2
- package/tools/assessment-validation.js +19 -5
- package/tools/create-order.d.ts +4 -0
- package/tools/create-order.js +15 -0
- package/tools/intel-cache.d.ts +14 -0
- package/tools/intel-cache.js +59 -0
- package/tools/venue-quote-hint.d.ts +49 -0
- package/tools/venue-quote-hint.js +200 -0
package/tools/create-order.d.ts
CHANGED
|
@@ -77,6 +77,10 @@ export declare function createOrderTool(args: {
|
|
|
77
77
|
available: boolean;
|
|
78
78
|
active: boolean;
|
|
79
79
|
}>;
|
|
80
|
+
/** ATR(14) for the symbol (price units) for the protective-geometry
|
|
81
|
+
* floor's 0.3×ATR term. Optional and FAIL-OPEN: null/throw/slow (>2 s)
|
|
82
|
+
* → the fixed 30 bps floor alone, exactly the pre-2026-09-29 behaviour. */
|
|
83
|
+
readAtr14?: (symbol: string) => Promise<number | null>;
|
|
80
84
|
refreshPaperState?: () => void;
|
|
81
85
|
wave9TradingModeCheck?: () => string;
|
|
82
86
|
wave9VenueCheck?: () => string;
|
package/tools/create-order.js
CHANGED
|
@@ -1006,12 +1006,27 @@ export async function createOrderTool(args, deps) {
|
|
|
1006
1006
|
// by the stop-watcher seconds later (7 sub-5-minute kills on the 2026-07 HL
|
|
1007
1007
|
// soak — one spawned the −$117.86 journal fabrication of issue #199).
|
|
1008
1008
|
if (!wave9Claimed) {
|
|
1009
|
+
// ATR term of the floor (2026-09-29). Bounded so a slow intel read can
|
|
1010
|
+
// never delay an order by more than 2 s; any failure = no ATR = 30 bps only.
|
|
1011
|
+
let atr14;
|
|
1012
|
+
if (deps.readAtr14 && args.stopPrice !== undefined) {
|
|
1013
|
+
// Promise.resolve().then(...) also converts a SYNCHRONOUS throw from the
|
|
1014
|
+
// dep into a rejection — the outer catch would otherwise refuse the
|
|
1015
|
+
// entry (fail-closed) against this gate's fail-open contract.
|
|
1016
|
+
const reader = deps.readAtr14;
|
|
1017
|
+
const read = Promise.resolve().then(() => reader(args.symbol)).catch(() => null);
|
|
1018
|
+
const timeout = new Promise(resolve => { const t = setTimeout(() => resolve(null), 2_000); t.unref?.(); });
|
|
1019
|
+
const v = await Promise.race([read, timeout]);
|
|
1020
|
+
if (typeof v === 'number' && Number.isFinite(v) && v > 0)
|
|
1021
|
+
atr14 = v;
|
|
1022
|
+
}
|
|
1009
1023
|
const geometryError = validateProtectiveGeometry({
|
|
1010
1024
|
side: side,
|
|
1011
1025
|
refPrice: orderPrice,
|
|
1012
1026
|
stopPrice: args.stopPrice,
|
|
1013
1027
|
invalidationPrice: args.invalidation_price,
|
|
1014
1028
|
targetPrice: args.target_price,
|
|
1029
|
+
atr14,
|
|
1015
1030
|
});
|
|
1016
1031
|
if (geometryError) {
|
|
1017
1032
|
return { error: `create_order rejected (protective geometry): ${geometryError}` };
|
package/tools/intel-cache.d.ts
CHANGED
|
@@ -23,3 +23,17 @@ export declare function getStrategiesCached(deps: IntelApiDeps): Promise<Strateg
|
|
|
23
23
|
export declare const __testing__: {
|
|
24
24
|
resetCaches(): void;
|
|
25
25
|
};
|
|
26
|
+
export declare function _resetAtrLastKnownGoodForTests(): void;
|
|
27
|
+
/** ATR(14) for the symbol (price units), for create_order's stop-distance
|
|
28
|
+
* floor. Venue-aware symbol resolution mirrors get_setup_detail (the raw
|
|
29
|
+
* 'AVAX/USDC' form never hits the HL_ cache key). Order of preference:
|
|
30
|
+
* a fact returned within the budget → its atr14 (and remember it);
|
|
31
|
+
* otherwise the last-known-good value if younger than 30 min; otherwise
|
|
32
|
+
* null — the caller fails open to the fixed 30 bps floor. Never throws. */
|
|
33
|
+
export declare function readAtr14FromFacts(rawSymbol: string, deps: IntelApiDeps, opts?: {
|
|
34
|
+
getFact?: (symbol: string, deps: IntelApiDeps) => Promise<SymbolFact | {
|
|
35
|
+
error: string;
|
|
36
|
+
}>;
|
|
37
|
+
now?: () => number;
|
|
38
|
+
budgetMs?: number;
|
|
39
|
+
}): Promise<number | null>;
|
package/tools/intel-cache.js
CHANGED
|
@@ -146,3 +146,62 @@ export const __testing__ = {
|
|
|
146
146
|
symbolFactCache.clear();
|
|
147
147
|
},
|
|
148
148
|
};
|
|
149
|
+
/** Last-known-good ATR per intel symbol. The fact cache is only 30 s and the
|
|
150
|
+
* LLM's gap between scan_pairs and create_order is usually longer, so most
|
|
151
|
+
* entries would miss it and race a network fetch; ATR(14) on 1h bars barely
|
|
152
|
+
* moves in 30 minutes, so a recent value is a better floor input than none. */
|
|
153
|
+
const atrLastKnownGood = new Map();
|
|
154
|
+
const ATR_LKG_TTL_MS = 30 * 60_000;
|
|
155
|
+
/** Give the fact fetch this long before falling back to the last-known-good
|
|
156
|
+
* value; the fetch keeps running and warms the cache for the next call. */
|
|
157
|
+
const ATR_FETCH_BUDGET_MS = 1_500;
|
|
158
|
+
export function _resetAtrLastKnownGoodForTests() {
|
|
159
|
+
atrLastKnownGood.clear();
|
|
160
|
+
}
|
|
161
|
+
/** ATR(14) for the symbol (price units), for create_order's stop-distance
|
|
162
|
+
* floor. Venue-aware symbol resolution mirrors get_setup_detail (the raw
|
|
163
|
+
* 'AVAX/USDC' form never hits the HL_ cache key). Order of preference:
|
|
164
|
+
* a fact returned within the budget → its atr14 (and remember it);
|
|
165
|
+
* otherwise the last-known-good value if younger than 30 min; otherwise
|
|
166
|
+
* null — the caller fails open to the fixed 30 bps floor. Never throws. */
|
|
167
|
+
export async function readAtr14FromFacts(rawSymbol, deps, opts = {}) {
|
|
168
|
+
const now = opts.now ?? Date.now;
|
|
169
|
+
const budgetMs = opts.budgetMs ?? ATR_FETCH_BUDGET_MS;
|
|
170
|
+
const getFact = opts.getFact ?? getFactBySymbolCached;
|
|
171
|
+
let symbol;
|
|
172
|
+
try {
|
|
173
|
+
symbol = (deps.venue ?? 'binance') === 'hyperliquid'
|
|
174
|
+
? resolveIntelSymbol(deps, rawSymbol)
|
|
175
|
+
: String(rawSymbol ?? '').toUpperCase().replace(/[^A-Z0-9]/g, '');
|
|
176
|
+
}
|
|
177
|
+
catch {
|
|
178
|
+
return null;
|
|
179
|
+
}
|
|
180
|
+
const fromLkg = () => {
|
|
181
|
+
const lkg = atrLastKnownGood.get(symbol);
|
|
182
|
+
return lkg && now() - lkg.at <= ATR_LKG_TTL_MS ? lkg.atr : null;
|
|
183
|
+
};
|
|
184
|
+
try {
|
|
185
|
+
const fetch = Promise.resolve().then(() => getFact(symbol, deps)).then((fact) => {
|
|
186
|
+
if ('error' in fact)
|
|
187
|
+
return null;
|
|
188
|
+
const raw = fact.indicators?.atr14;
|
|
189
|
+
const n = typeof raw === 'number' ? raw : typeof raw === 'string' ? Number(raw) : Number.NaN;
|
|
190
|
+
if (!Number.isFinite(n) || n <= 0)
|
|
191
|
+
return null;
|
|
192
|
+
atrLastKnownGood.set(symbol, { atr: n, at: now() });
|
|
193
|
+
return n;
|
|
194
|
+
}).catch(() => null);
|
|
195
|
+
let timer;
|
|
196
|
+
const budget = new Promise((resolve) => { timer = setTimeout(() => resolve('timeout'), budgetMs); timer.unref?.(); });
|
|
197
|
+
const raced = await Promise.race([fetch, budget]);
|
|
198
|
+
if (timer)
|
|
199
|
+
clearTimeout(timer);
|
|
200
|
+
if (raced === 'timeout')
|
|
201
|
+
return fromLkg();
|
|
202
|
+
return raced ?? fromLkg();
|
|
203
|
+
}
|
|
204
|
+
catch {
|
|
205
|
+
return fromLkg();
|
|
206
|
+
}
|
|
207
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/** Generic wording: used whenever a specific pair cannot be named safely. */
|
|
2
|
+
export declare const HL_GENERIC_QUOTE_HINT: string;
|
|
3
|
+
/** Plain bases Hyperliquid lists only per 1000, as 'k' + base ('kPEPE' = 1000
|
|
4
|
+
* PEPE). Checked against Hyperliquid's live `meta` universe on 2026-09-30:
|
|
5
|
+
* kPEPE, kSHIB, kBONK, kFLOKI, kLUNC and kNEIRO trade, none of the plain
|
|
6
|
+
* bases does (kDOGS is in the universe but delisted, so DOGS is not here).
|
|
7
|
+
* Wording only, both ways: a k-coin this list misses keeps the plain
|
|
8
|
+
* '<base>/USDC' wording, and an entry delisted later names a pair that no
|
|
9
|
+
* longer trades; the call itself is never touched. */
|
|
10
|
+
export declare const HL_PER_1000_BASES: ReadonlySet<string>;
|
|
11
|
+
export type UsdtSymbolVerdict = {
|
|
12
|
+
kind: 'not-usdt';
|
|
13
|
+
}
|
|
14
|
+
/** USDT-quoted, and `base` is the Hyperliquid coin safe to name (verbatim,
|
|
15
|
+
* or 'k' + base for a coin listed per 1000). */
|
|
16
|
+
| {
|
|
17
|
+
kind: 'usdt';
|
|
18
|
+
base: string;
|
|
19
|
+
}
|
|
20
|
+
/** Looks USDT-quoted, but the base is empty or unusual: generic hint only. */
|
|
21
|
+
| {
|
|
22
|
+
kind: 'usdt-unclear';
|
|
23
|
+
};
|
|
24
|
+
/** Does this argument look like a Binance USDT-quoted pair? Pure, never throws
|
|
25
|
+
* (non-strings are simply not USDT-quoted). */
|
|
26
|
+
export declare function classifyUsdtSymbol(raw: unknown): UsdtSymbolVerdict;
|
|
27
|
+
/** The hint for a call's arguments, or null when none of its `symbol` /
|
|
28
|
+
* `symbols` look USDT-quoted. Pure. Only those two fields are read (never
|
|
29
|
+
* any other argument, some of which are credentials). */
|
|
30
|
+
export declare function buildHyperliquidQuoteHint(params: unknown): string | null;
|
|
31
|
+
/** A tool result's details that report a failure: a plain object whose own
|
|
32
|
+
* `error` is a non-empty string, `true`, or an object. `error: null`,
|
|
33
|
+
* `false` or `''` is not a failure. */
|
|
34
|
+
export declare function carriesError(details: unknown): boolean;
|
|
35
|
+
/** The result, with the hint added when every condition holds; otherwise the
|
|
36
|
+
* SAME object, untouched. Only a result in index.ts jsonResult's exact shape
|
|
37
|
+
* (one text block that is JSON.stringify(details)) is ever rebuilt, so no
|
|
38
|
+
* hand-built result can be mangled. Never throws. */
|
|
39
|
+
export declare function addHyperliquidQuoteHint<R>(result: R, params: unknown, venue: unknown): R;
|
|
40
|
+
/** Anything with a name and an execute(toolCallId, params, signal). */
|
|
41
|
+
export interface HintablePluginTool {
|
|
42
|
+
name: string;
|
|
43
|
+
execute: (...args: any[]) => any;
|
|
44
|
+
}
|
|
45
|
+
/** Wrap a tool's execute with the hint. `getVenue` is called per call, AFTER
|
|
46
|
+
* the tool returned, and only feeds the hint path (a throwing getter = no
|
|
47
|
+
* hint). The tool's own promise rejection, or a non-promise return, passes
|
|
48
|
+
* through untouched; every other property of the tool is kept. */
|
|
49
|
+
export declare function wrapToolWithHyperliquidQuoteHint<T extends HintablePluginTool>(tool: T, getVenue: () => unknown): T;
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
// Hyperliquid quote-asset hint on FAILED tool calls (2026-09-30).
|
|
2
|
+
//
|
|
3
|
+
// Why: on a Hyperliquid box the agent keeps reaching for Binance-shaped
|
|
4
|
+
// symbols (get_market_structure("BTC/USDT"), fetch_ticker("BTCUSDT")), and the
|
|
5
|
+
// call fails with a bare "Failed to fetch candle data for BTC/USDT" /
|
|
6
|
+
// "Failed to fetch current price for BTCUSDT" (tools/helpers.ts,
|
|
7
|
+
// tools/get-market-structure.ts, tools/create-order.ts; ~44 tools take a
|
|
8
|
+
// symbol) that never says the pair is on the wrong quote asset. The agent then
|
|
9
|
+
// retries the same symbol, or concludes the market-data path is down.
|
|
10
|
+
//
|
|
11
|
+
// What this does: ONE additive field, applied centrally at the tool dispatch
|
|
12
|
+
// wrapper (index.ts, inside the ToolGate + entitlement gate). When a call has
|
|
13
|
+
// ALREADY failed (its result carries a non-empty `error`), the running venue
|
|
14
|
+
// is hyperliquid, and the call's `symbol` / `symbols` arguments look
|
|
15
|
+
// USDT-quoted ("X/USDT", "X/USDT:USDT", "XUSDT"), the result gains
|
|
16
|
+
// hint: "Hyperliquid pairs are quoted in USDC: use X/USDC"
|
|
17
|
+
//
|
|
18
|
+
// What it never does (CLAUDE.md: never silently translate quote assets):
|
|
19
|
+
// - translate, rewrite or retry the call: the agent reads the hint and
|
|
20
|
+
// decides what to call next;
|
|
21
|
+
// - touch a successful result (get_regime('BTCUSDT') works on both venues);
|
|
22
|
+
// - change the error text, or a `hint` a tool already set;
|
|
23
|
+
// - throw: any exception on this path returns the original result as is,
|
|
24
|
+
// and a tool's own rejection passes through untouched.
|
|
25
|
+
//
|
|
26
|
+
// Deriving the base is deliberately conservative. Hyperliquid coin names are
|
|
27
|
+
// case-sensitive ('kPEPE' is 1000 PEPE; venues/symbols.ts), so only a trailing
|
|
28
|
+
// exact-case "USDT" / "/USDT" / "/USDT:USDT" is stripped and the base is kept
|
|
29
|
+
// verbatim. A lowercase quote ("btcusdt"), a base that starts with a digit, or
|
|
30
|
+
// anything else unusual gets the generic hint, which names no pair. The one
|
|
31
|
+
// mapping is for the coins Hyperliquid lists per 1000 under a 'k' prefix
|
|
32
|
+
// (HL_PER_1000_BASES): "PEPEUSDT" and Binance's "1000PEPEUSDT" both name
|
|
33
|
+
// "kPEPE/USDC", because "PEPE/USDC" does not trade there, and a hint naming it
|
|
34
|
+
// would send the agent into a second failure that carries no hint (the retried
|
|
35
|
+
// symbol is no longer USDT-quoted). That is still wording only: the call the
|
|
36
|
+
// agent made is never rewritten.
|
|
37
|
+
import { VENUE_QUOTE_ASSET } from '../venues/symbols.js';
|
|
38
|
+
const HL_QUOTE = VENUE_QUOTE_ASSET.hyperliquid; // 'USDC'
|
|
39
|
+
/** Generic wording: used whenever a specific pair cannot be named safely. */
|
|
40
|
+
export const HL_GENERIC_QUOTE_HINT = `Hyperliquid pairs are quoted in ${HL_QUOTE} (e.g. BTC/${HL_QUOTE})`;
|
|
41
|
+
/** At most this many pairs are named in one hint. */
|
|
42
|
+
const MAX_NAMED_PAIRS = 5;
|
|
43
|
+
/** At most this many `symbols` entries are inspected (bounded work). */
|
|
44
|
+
const MAX_SYMBOLS_INSPECTED = 50;
|
|
45
|
+
/** A base we are willing to echo back as "<base>/USDC": starts with a letter,
|
|
46
|
+
* letters and digits only, short. Case is preserved, never changed. */
|
|
47
|
+
const SAFE_BASE = /^[A-Za-z][A-Za-z0-9]{0,19}$/;
|
|
48
|
+
/** Plain bases Hyperliquid lists only per 1000, as 'k' + base ('kPEPE' = 1000
|
|
49
|
+
* PEPE). Checked against Hyperliquid's live `meta` universe on 2026-09-30:
|
|
50
|
+
* kPEPE, kSHIB, kBONK, kFLOKI, kLUNC and kNEIRO trade, none of the plain
|
|
51
|
+
* bases does (kDOGS is in the universe but delisted, so DOGS is not here).
|
|
52
|
+
* Wording only, both ways: a k-coin this list misses keeps the plain
|
|
53
|
+
* '<base>/USDC' wording, and an entry delisted later names a pair that no
|
|
54
|
+
* longer trades; the call itself is never touched. */
|
|
55
|
+
export const HL_PER_1000_BASES = new Set(['PEPE', 'SHIB', 'BONK', 'FLOKI', 'LUNC', 'NEIRO']);
|
|
56
|
+
/** The Hyperliquid coin to name for a USDT base: 'k' + base for a coin listed
|
|
57
|
+
* per 1000 (from the plain 'PEPE' or Binance's '1000PEPE'), the base itself
|
|
58
|
+
* otherwise; null when the base is not safe to name at all. */
|
|
59
|
+
function hyperliquidBaseFor(base) {
|
|
60
|
+
const thousands = /^1000([A-Z]+)$/.exec(base);
|
|
61
|
+
if (thousands)
|
|
62
|
+
return HL_PER_1000_BASES.has(thousands[1]) ? `k${thousands[1]}` : null;
|
|
63
|
+
if (!SAFE_BASE.test(base))
|
|
64
|
+
return null;
|
|
65
|
+
const upper = base.toUpperCase();
|
|
66
|
+
if (upper === 'USDT' || upper === HL_QUOTE)
|
|
67
|
+
return null;
|
|
68
|
+
return HL_PER_1000_BASES.has(base) ? `k${base}` : base;
|
|
69
|
+
}
|
|
70
|
+
/** Does this argument look like a Binance USDT-quoted pair? Pure, never throws
|
|
71
|
+
* (non-strings are simply not USDT-quoted). */
|
|
72
|
+
export function classifyUsdtSymbol(raw) {
|
|
73
|
+
if (typeof raw !== 'string')
|
|
74
|
+
return { kind: 'not-usdt' };
|
|
75
|
+
const s = raw.trim();
|
|
76
|
+
if (s === '')
|
|
77
|
+
return { kind: 'not-usdt' };
|
|
78
|
+
// Exact case: canonical "X/USDT", CCXT "X/USDT:USDT", Binance-native "XUSDT".
|
|
79
|
+
const m = /^(.*)\/USDT(?::USDT)?$/.exec(s) ?? /^([^/:]*)USDT$/.exec(s);
|
|
80
|
+
if (m) {
|
|
81
|
+
const base = hyperliquidBaseFor(m[1]);
|
|
82
|
+
return base !== null ? { kind: 'usdt', base } : { kind: 'usdt-unclear' };
|
|
83
|
+
}
|
|
84
|
+
// Any other casing of the same shapes: still USDT-quoted, but the base's
|
|
85
|
+
// true Hyperliquid casing is unknown, so it is never echoed back.
|
|
86
|
+
if (/^.*\/usdt(?::usdt)?$/i.test(s) || /^[^/:]*usdt$/i.test(s))
|
|
87
|
+
return { kind: 'usdt-unclear' };
|
|
88
|
+
return { kind: 'not-usdt' };
|
|
89
|
+
}
|
|
90
|
+
/** The hint for a call's arguments, or null when none of its `symbol` /
|
|
91
|
+
* `symbols` look USDT-quoted. Pure. Only those two fields are read (never
|
|
92
|
+
* any other argument, some of which are credentials). */
|
|
93
|
+
export function buildHyperliquidQuoteHint(params) {
|
|
94
|
+
if (!params || typeof params !== 'object' || Array.isArray(params))
|
|
95
|
+
return null;
|
|
96
|
+
const p = params;
|
|
97
|
+
const candidates = [p.symbol];
|
|
98
|
+
if (Array.isArray(p.symbols))
|
|
99
|
+
candidates.push(...p.symbols.slice(0, MAX_SYMBOLS_INSPECTED));
|
|
100
|
+
const bases = [];
|
|
101
|
+
let unclear = false;
|
|
102
|
+
for (const c of candidates) {
|
|
103
|
+
const v = classifyUsdtSymbol(c);
|
|
104
|
+
if (v.kind === 'usdt') {
|
|
105
|
+
if (!bases.includes(v.base))
|
|
106
|
+
bases.push(v.base);
|
|
107
|
+
}
|
|
108
|
+
else if (v.kind === 'usdt-unclear') {
|
|
109
|
+
unclear = true;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
if (bases.length === 0 && !unclear)
|
|
113
|
+
return null;
|
|
114
|
+
// One unclear symbol makes the whole hint generic: naming some pairs and
|
|
115
|
+
// not others would read as "these are the only fix".
|
|
116
|
+
if (unclear)
|
|
117
|
+
return HL_GENERIC_QUOTE_HINT;
|
|
118
|
+
const shown = bases.slice(0, MAX_NAMED_PAIRS);
|
|
119
|
+
const named = shown.map((b) => `${b}/${HL_QUOTE}`);
|
|
120
|
+
const more = bases.length > MAX_NAMED_PAIRS ? ', …' : '';
|
|
121
|
+
// A per-1000 coin says so: one unit is 1000 of the plain coin (1000x the price).
|
|
122
|
+
const per1000 = shown.flatMap((b) => {
|
|
123
|
+
const plain = b.startsWith('k') ? b.slice(1) : '';
|
|
124
|
+
return HL_PER_1000_BASES.has(plain) ? [`${b} = 1000 ${plain}`] : [];
|
|
125
|
+
});
|
|
126
|
+
const note = per1000.length > 0 ? ` (${per1000.join(', ')})` : '';
|
|
127
|
+
return `Hyperliquid pairs are quoted in ${HL_QUOTE}: use ${named.join(', ')}${more}${note}`;
|
|
128
|
+
}
|
|
129
|
+
/** A tool result's details that report a failure: a plain object whose own
|
|
130
|
+
* `error` is a non-empty string, `true`, or an object. `error: null`,
|
|
131
|
+
* `false` or `''` is not a failure. */
|
|
132
|
+
export function carriesError(details) {
|
|
133
|
+
if (!details || typeof details !== 'object' || Array.isArray(details))
|
|
134
|
+
return false;
|
|
135
|
+
if (!Object.prototype.hasOwnProperty.call(details, 'error'))
|
|
136
|
+
return false;
|
|
137
|
+
const e = details.error;
|
|
138
|
+
if (typeof e === 'string')
|
|
139
|
+
return e.trim() !== '';
|
|
140
|
+
return e === true || (typeof e === 'object' && e !== null);
|
|
141
|
+
}
|
|
142
|
+
/** The result, with the hint added when every condition holds; otherwise the
|
|
143
|
+
* SAME object, untouched. Only a result in index.ts jsonResult's exact shape
|
|
144
|
+
* (one text block that is JSON.stringify(details)) is ever rebuilt, so no
|
|
145
|
+
* hand-built result can be mangled. Never throws. */
|
|
146
|
+
export function addHyperliquidQuoteHint(result, params, venue) {
|
|
147
|
+
try {
|
|
148
|
+
if (venue !== 'hyperliquid')
|
|
149
|
+
return result;
|
|
150
|
+
const r = result;
|
|
151
|
+
if (!r || typeof r !== 'object' || !Array.isArray(r.content) || r.content.length !== 1)
|
|
152
|
+
return result;
|
|
153
|
+
const details = r.details;
|
|
154
|
+
if (!carriesError(details))
|
|
155
|
+
return result;
|
|
156
|
+
if (Object.prototype.hasOwnProperty.call(details, 'hint'))
|
|
157
|
+
return result;
|
|
158
|
+
const block = r.content[0];
|
|
159
|
+
if (!block || block.type !== 'text' || block.text !== JSON.stringify(details))
|
|
160
|
+
return result;
|
|
161
|
+
const hint = buildHyperliquidQuoteHint(params);
|
|
162
|
+
if (hint === null)
|
|
163
|
+
return result;
|
|
164
|
+
const next = { ...details, hint };
|
|
165
|
+
return {
|
|
166
|
+
...r,
|
|
167
|
+
content: [{ type: 'text', text: JSON.stringify(next) }],
|
|
168
|
+
details: next,
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
catch {
|
|
172
|
+
return result;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
/** Wrap a tool's execute with the hint. `getVenue` is called per call, AFTER
|
|
176
|
+
* the tool returned, and only feeds the hint path (a throwing getter = no
|
|
177
|
+
* hint). The tool's own promise rejection, or a non-promise return, passes
|
|
178
|
+
* through untouched; every other property of the tool is kept. */
|
|
179
|
+
export function wrapToolWithHyperliquidQuoteHint(tool, getVenue) {
|
|
180
|
+
const originalExecute = tool.execute.bind(tool);
|
|
181
|
+
return {
|
|
182
|
+
...tool,
|
|
183
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
184
|
+
execute: (...args) => {
|
|
185
|
+
const out = originalExecute(...args);
|
|
186
|
+
if (!out || typeof out.then !== 'function')
|
|
187
|
+
return out;
|
|
188
|
+
return out.then((result) => {
|
|
189
|
+
let venue;
|
|
190
|
+
try {
|
|
191
|
+
venue = getVenue();
|
|
192
|
+
}
|
|
193
|
+
catch {
|
|
194
|
+
return result;
|
|
195
|
+
}
|
|
196
|
+
return addHyperliquidQuoteHint(result, args[1], venue);
|
|
197
|
+
});
|
|
198
|
+
},
|
|
199
|
+
};
|
|
200
|
+
}
|