@oracle-agent/oracle 0.9.7 → 0.10.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 +9 -13
- package/package.json +3 -2
- package/public/oracle-splash/index.html +223 -46
- package/src/auth/oauth.mjs +672 -0
- package/src/cli/commands/auth.mjs +193 -0
- package/src/cli/commands/bootstrap.mjs +2 -2
- package/src/cli/commands/chat.mjs +97 -75
- package/src/cli/commands/model.mjs +79 -23
- package/src/cli/kernel.mjs +4 -3
- package/src/cli/model-config.mjs +70 -0
- package/src/router/best-execution.mjs +105 -0
- package/src/router/index.mjs +42 -1
- package/src/router/prepare-route.mjs +17 -1
- package/src/tui/app.mjs +18 -7
- package/src/tui/backend.mjs +145 -0
- package/src/tui/standalone-client.mjs +666 -0
- package/protocols/templates/safe-erc20/README.md +0 -9
- package/src/cli/__pycache__/oracle-harness.cpython-311.pyc +0 -0
|
@@ -99,6 +99,111 @@ export function normalizeRoute(raw) {
|
|
|
99
99
|
};
|
|
100
100
|
}
|
|
101
101
|
|
|
102
|
+
/**
|
|
103
|
+
* Measure how badly a route's price decays with size.
|
|
104
|
+
*
|
|
105
|
+
* WHY THIS EXISTS: a quote against a drained pool is indistinguishable, in shape,
|
|
106
|
+
* from a quote against a deep one -- same fields, no error, just a smaller number.
|
|
107
|
+
* Verified on Robinhood 2026-08-02: a dead Uniswap V3 SQUEEZE pool holding 7.03
|
|
108
|
+
* USDG happily quoted a 205,374-token sell, and every layer downstream treated
|
|
109
|
+
* that as truth. `minOut` does NOT protect against this: it guards the price
|
|
110
|
+
* MOVING between quote and fill, so against a garbage quote it faithfully locks
|
|
111
|
+
* in the garbage.
|
|
112
|
+
*
|
|
113
|
+
* The probe: quote a small slice of the same trade, compare per-unit prices. Real
|
|
114
|
+
* slippage decays smoothly with size; a drained pool falls off a cliff.
|
|
115
|
+
*
|
|
116
|
+
* `probeFn(amountIn) -> amountOut | null`. Returns null when the comparison could
|
|
117
|
+
* not be made, which callers MUST treat as unproven rather than as a pass.
|
|
118
|
+
*/
|
|
119
|
+
export async function measurePriceImpact(probeFn, amountIn, { divisor = 1000n } = {}) {
|
|
120
|
+
const full = bn(amountIn);
|
|
121
|
+
if (full == null || full <= 0n) return null;
|
|
122
|
+
|
|
123
|
+
const probeAmt = full / divisor > 0n ? full / divisor : 1n;
|
|
124
|
+
if (probeAmt >= full) return null; // too small to compare against itself
|
|
125
|
+
|
|
126
|
+
let smallOut;
|
|
127
|
+
let fullOut;
|
|
128
|
+
try {
|
|
129
|
+
[smallOut, fullOut] = await Promise.all([probeFn(probeAmt), probeFn(full)]);
|
|
130
|
+
} catch {
|
|
131
|
+
return null;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const s = bn(smallOut);
|
|
135
|
+
const f = bn(fullOut);
|
|
136
|
+
if (s == null || f == null || s <= 0n) return null;
|
|
137
|
+
|
|
138
|
+
// Scale to compare per-unit prices without floating point until the last step.
|
|
139
|
+
const smallPx = Number(s) / Number(probeAmt);
|
|
140
|
+
const fullPx = Number(f) / Number(full);
|
|
141
|
+
if (!Number.isFinite(smallPx) || !Number.isFinite(fullPx) || smallPx <= 0) return null;
|
|
142
|
+
|
|
143
|
+
return {
|
|
144
|
+
impactPct: (1 - fullPx / smallPx) * 100,
|
|
145
|
+
probeAmountIn: probeAmt.toString(),
|
|
146
|
+
probeAmountOut: s.toString(),
|
|
147
|
+
fullAmountOut: f.toString(),
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Reject routes whose price impact exceeds a ceiling.
|
|
153
|
+
*
|
|
154
|
+
* Applied AFTER ranking so the rejection is visible: a caller can see that the
|
|
155
|
+
* nominal winner was dropped and why, rather than silently receiving second place.
|
|
156
|
+
* A route with no impact measurement is NOT dropped here -- absence of evidence is
|
|
157
|
+
* surfaced through `unmeasured` so the caller decides, but it is never presented
|
|
158
|
+
* as a passing measurement.
|
|
159
|
+
*/
|
|
160
|
+
export function applyImpactCeiling(ranked, impacts, { maxImpactPct = 25 } = {}) {
|
|
161
|
+
const rejected = [];
|
|
162
|
+
const unmeasured = [];
|
|
163
|
+
|
|
164
|
+
const kept = (ranked.routes ?? []).filter((r) => {
|
|
165
|
+
const m = impacts?.[r.source];
|
|
166
|
+
if (!m) {
|
|
167
|
+
unmeasured.push(r.source);
|
|
168
|
+
return true;
|
|
169
|
+
}
|
|
170
|
+
if (m.impactPct > maxImpactPct) {
|
|
171
|
+
rejected.push({
|
|
172
|
+
source: r.source,
|
|
173
|
+
impactPct: Number(m.impactPct.toFixed(2)),
|
|
174
|
+
reason:
|
|
175
|
+
`price impact ${m.impactPct.toFixed(1)}% exceeds ${maxImpactPct}% ceiling -- ` +
|
|
176
|
+
"the venue cannot absorb this size near the quoted price",
|
|
177
|
+
});
|
|
178
|
+
return false;
|
|
179
|
+
}
|
|
180
|
+
return true;
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
const warnings = [...(ranked.warnings ?? [])];
|
|
184
|
+
if (rejected.length) {
|
|
185
|
+
warnings.push(
|
|
186
|
+
`dropped ${rejected.length} route(s) on price impact: ` +
|
|
187
|
+
rejected.map((r) => `${r.source} (${r.impactPct}%)`).join(", "),
|
|
188
|
+
);
|
|
189
|
+
}
|
|
190
|
+
if (unmeasured.length) {
|
|
191
|
+
warnings.push(
|
|
192
|
+
`price impact NOT measured for: ${unmeasured.join(", ")}. These are unproven ` +
|
|
193
|
+
"against thin liquidity, not proven safe.",
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
return {
|
|
198
|
+
...ranked,
|
|
199
|
+
routes: kept,
|
|
200
|
+
best: kept[0] ?? null,
|
|
201
|
+
impactRejected: rejected,
|
|
202
|
+
impactUnmeasured: unmeasured,
|
|
203
|
+
warnings,
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
|
|
102
207
|
/**
|
|
103
208
|
* Rank normalized routes by NET output.
|
|
104
209
|
*
|
package/src/router/index.mjs
CHANGED
|
@@ -5,7 +5,9 @@
|
|
|
5
5
|
// a one-source "best route" is not a comparison at all, so that case is labelled
|
|
6
6
|
// rather than presented as if it beat something.
|
|
7
7
|
|
|
8
|
-
import {
|
|
8
|
+
import {
|
|
9
|
+
gatherRoutes, rankRoutes, QUALITY, measurePriceImpact, applyImpactCeiling,
|
|
10
|
+
} from "./best-execution.mjs";
|
|
9
11
|
import { swapCandidates, bridgeCandidates, nativeUsd } from "./route-sources.mjs";
|
|
10
12
|
import { llamaPrices } from "../data/providers/defillama.mjs";
|
|
11
13
|
|
|
@@ -93,6 +95,45 @@ export async function bestSwapRoute(p, opts = {}) {
|
|
|
93
95
|
destDecimals: p.decimalsOut ?? dest.decimals,
|
|
94
96
|
});
|
|
95
97
|
|
|
98
|
+
// Liquidity check. Ranking alone will happily crown a quote taken against a
|
|
99
|
+
// drained pool, because a thin venue returns a well-formed number rather than
|
|
100
|
+
// an error. Re-quote the winner at 1/1000th size and compare per-unit price:
|
|
101
|
+
// real slippage decays smoothly, a dead pool falls off a cliff.
|
|
102
|
+
//
|
|
103
|
+
// Opt out with `maxImpactPct: null` for callers that genuinely want the raw
|
|
104
|
+
// ranking (analytics, spread display) rather than an executable route.
|
|
105
|
+
const maxImpactPct = opts.maxImpactPct === undefined ? 25 : opts.maxImpactPct;
|
|
106
|
+
if (maxImpactPct != null && ranked.best) {
|
|
107
|
+
const winner = ranked.best.source;
|
|
108
|
+
const probe = async (amt) => {
|
|
109
|
+
const sub = swapCandidates({
|
|
110
|
+
...p,
|
|
111
|
+
amountIn: amt.toString(),
|
|
112
|
+
nativePriceUsd: nativePricePromise,
|
|
113
|
+
decimalsOut: p.decimalsOut,
|
|
114
|
+
destDecimalsPromise: destPromise,
|
|
115
|
+
opts,
|
|
116
|
+
}).filter((c) => c.source === winner);
|
|
117
|
+
if (!sub.length) return null;
|
|
118
|
+
const got = await gatherRoutes(sub, { timeoutMs: opts.timeoutMs ?? 12_000 });
|
|
119
|
+
return got?.[0]?.amountOut ?? null;
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
const impact = await measurePriceImpact(probe, amountIn);
|
|
123
|
+
if (impact) {
|
|
124
|
+
const guarded = applyImpactCeiling(ranked, { [winner]: impact }, { maxImpactPct });
|
|
125
|
+
return {
|
|
126
|
+
kind: "swap",
|
|
127
|
+
chainId,
|
|
128
|
+
tokenIn,
|
|
129
|
+
tokenOut,
|
|
130
|
+
amountIn: String(amountIn),
|
|
131
|
+
...guarded,
|
|
132
|
+
priceImpact: { [winner]: Number(impact.impactPct.toFixed(2)) },
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
96
137
|
return {
|
|
97
138
|
kind: "swap",
|
|
98
139
|
chainId,
|
|
@@ -196,7 +196,18 @@ export async function prepareBestRoute(p, opts = {}) {
|
|
|
196
196
|
|
|
197
197
|
const comparison = await bestSwapRoute(p, opts);
|
|
198
198
|
if (!comparison.best) {
|
|
199
|
-
|
|
199
|
+
// A liquidity rejection is a DIFFERENT failure from "nobody answered", and
|
|
200
|
+
// conflating them hides the reason a live venue was refused.
|
|
201
|
+
const blocked = comparison.impactRejected ?? [];
|
|
202
|
+
return {
|
|
203
|
+
ok: false,
|
|
204
|
+
reason: blocked.length
|
|
205
|
+
? blocked[0].reason
|
|
206
|
+
: "no source returned a usable route",
|
|
207
|
+
priceImpactBlocked: blocked.length > 0,
|
|
208
|
+
impactRejected: blocked,
|
|
209
|
+
comparison,
|
|
210
|
+
};
|
|
200
211
|
}
|
|
201
212
|
|
|
202
213
|
// Honour an explicit override, but never silently: picking a non-winner is a
|
|
@@ -331,6 +342,11 @@ export async function prepareBestRoute(p, opts = {}) {
|
|
|
331
342
|
grossOut: chosen.grossOut,
|
|
332
343
|
gasUsd: chosen.gasUsd,
|
|
333
344
|
},
|
|
345
|
+
// Carry the liquidity measurement through to the caller. Without this the
|
|
346
|
+
// guard runs but its result is invisible, so a consumer cannot tell a route
|
|
347
|
+
// that PASSED the ceiling from one that was never measured at all.
|
|
348
|
+
priceImpact: comparison.priceImpact ?? null,
|
|
349
|
+
impactUnmeasured: comparison.impactUnmeasured ?? [],
|
|
334
350
|
...prepared,
|
|
335
351
|
unsigned: true,
|
|
336
352
|
signedBy: "user-wallet",
|
package/src/tui/app.mjs
CHANGED
|
@@ -371,23 +371,34 @@ export function createOracleTui(options = {}) {
|
|
|
371
371
|
});
|
|
372
372
|
}
|
|
373
373
|
|
|
374
|
-
export async function runOracleTui({
|
|
374
|
+
export async function runOracleTui({
|
|
375
|
+
hermesPython,
|
|
376
|
+
client,
|
|
377
|
+
tuiFactory = createOracleTui,
|
|
378
|
+
args = [],
|
|
379
|
+
env = process.env,
|
|
380
|
+
cwd = process.cwd(),
|
|
381
|
+
stdout = process.stdout,
|
|
382
|
+
stdin = process.stdin,
|
|
383
|
+
} = {}) {
|
|
375
384
|
const parsed = parseChatArgs(args);
|
|
376
385
|
if (parsed.query) return { native: false, pass: parsed.pass };
|
|
377
386
|
if (!stdin.isTTY || !stdout.isTTY) return { native: false, pass: parsed.pass };
|
|
378
|
-
|
|
379
|
-
if (env.ORACLE_NATIVE_TUI !== "1") return { native: false, pass: parsed.pass };
|
|
387
|
+
if (env.ORACLE_NATIVE_TUI === "0") return { native: false, pass: parsed.pass };
|
|
380
388
|
const invocation = hermesPython;
|
|
381
|
-
if (!invocation) return { native: false, pass: parsed.pass };
|
|
389
|
+
if (!client && !invocation) return { native: false, pass: parsed.pass };
|
|
382
390
|
const cleanEnv = activeChainEnv({
|
|
383
391
|
...env,
|
|
384
392
|
ORACLE_CHAT_SURFACE: "1",
|
|
385
393
|
ORACLE_PROFILE: "oracle",
|
|
386
394
|
ORACLE_NODE_BIN: process.execPath,
|
|
387
395
|
});
|
|
388
|
-
const tui =
|
|
389
|
-
|
|
390
|
-
|
|
396
|
+
const tui = tuiFactory({
|
|
397
|
+
...(client ? { client } : {}),
|
|
398
|
+
...(invocation ? {
|
|
399
|
+
python: invocation.command,
|
|
400
|
+
pythonArgs: invocation.prefix || [],
|
|
401
|
+
} : {}),
|
|
391
402
|
cwd,
|
|
392
403
|
env: cleanEnv,
|
|
393
404
|
stdin,
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
const PROVIDERS = Object.freeze({
|
|
2
|
+
openrouter: {
|
|
3
|
+
key: "OPENROUTER_API_KEY",
|
|
4
|
+
baseUrl: "https://openrouter.ai/api/v1",
|
|
5
|
+
model: "openrouter/auto",
|
|
6
|
+
},
|
|
7
|
+
openai: {
|
|
8
|
+
key: "OPENAI_API_KEY",
|
|
9
|
+
baseUrl: "https://api.openai.com/v1",
|
|
10
|
+
model: "gpt-4.1-mini",
|
|
11
|
+
},
|
|
12
|
+
xai: {
|
|
13
|
+
key: "XAI_API_KEY",
|
|
14
|
+
baseUrl: "https://api.x.ai/v1",
|
|
15
|
+
model: "grok-4-latest",
|
|
16
|
+
},
|
|
17
|
+
deepseek: {
|
|
18
|
+
key: "DEEPSEEK_API_KEY",
|
|
19
|
+
baseUrl: "https://api.deepseek.com/v1",
|
|
20
|
+
model: "deepseek-chat",
|
|
21
|
+
},
|
|
22
|
+
gemini: {
|
|
23
|
+
key: "GEMINI_API_KEY",
|
|
24
|
+
alternateKey: "GOOGLE_API_KEY",
|
|
25
|
+
baseUrl: "https://generativelanguage.googleapis.com/v1beta/openai",
|
|
26
|
+
model: "gemini-2.5-flash",
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
const OAUTH_PROVIDERS = Object.freeze({
|
|
31
|
+
"anthropic-oauth": {
|
|
32
|
+
baseUrl: "https://api.anthropic.com/v1",
|
|
33
|
+
model: "claude-sonnet-4-6",
|
|
34
|
+
protocol: "anthropic-messages",
|
|
35
|
+
},
|
|
36
|
+
"openai-codex": {
|
|
37
|
+
baseUrl: "https://chatgpt.com/backend-api/codex",
|
|
38
|
+
model: "gpt-5.6-sol",
|
|
39
|
+
protocol: "responses",
|
|
40
|
+
},
|
|
41
|
+
"xai-oauth": {
|
|
42
|
+
baseUrl: "https://api.x.ai/v1",
|
|
43
|
+
model: "grok-4.5",
|
|
44
|
+
protocol: "responses",
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
function positiveInteger(value, fallback) {
|
|
49
|
+
const parsed = Number.parseInt(String(value || ""), 10);
|
|
50
|
+
return Number.isFinite(parsed) && parsed > 0 ? parsed : fallback;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function providerFromEnv(env, stored = {}) {
|
|
54
|
+
const explicit = String(env.ORACLE_PROVIDER || stored.provider || "").trim().toLowerCase();
|
|
55
|
+
if (explicit) return explicit;
|
|
56
|
+
if (env.ORACLE_BASE_URL) return "custom";
|
|
57
|
+
for (const [name, preset] of Object.entries(PROVIDERS)) {
|
|
58
|
+
if (env[preset.key] || (preset.alternateKey && env[preset.alternateKey])) return name;
|
|
59
|
+
}
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function standaloneConfigFromEnv(
|
|
64
|
+
env = process.env,
|
|
65
|
+
stored = {},
|
|
66
|
+
oauthAvailable = () => false,
|
|
67
|
+
apiKeyResolver = () => "",
|
|
68
|
+
) {
|
|
69
|
+
const provider = providerFromEnv(env, stored);
|
|
70
|
+
if (!provider) return null;
|
|
71
|
+
|
|
72
|
+
const oauthPreset = OAUTH_PROVIDERS[provider];
|
|
73
|
+
const preset = PROVIDERS[provider] || oauthPreset;
|
|
74
|
+
const apiKeyEnv = String(env.ORACLE_API_KEY_ENV || stored.apiKeyEnv || "");
|
|
75
|
+
const apiKey = String(
|
|
76
|
+
env.ORACLE_API_KEY ||
|
|
77
|
+
(apiKeyEnv && env[apiKeyEnv]) ||
|
|
78
|
+
(preset && (env[preset.key] || (preset.alternateKey && env[preset.alternateKey]))) ||
|
|
79
|
+
apiKeyResolver(provider) ||
|
|
80
|
+
"",
|
|
81
|
+
);
|
|
82
|
+
const baseUrl = String(
|
|
83
|
+
oauthPreset
|
|
84
|
+
? oauthPreset.baseUrl
|
|
85
|
+
: provider === "custom"
|
|
86
|
+
? (env.ORACLE_BASE_URL || stored.baseUrl || "")
|
|
87
|
+
: (preset?.baseUrl || ""),
|
|
88
|
+
).replace(/\/+$/, "");
|
|
89
|
+
const model = String(env.ORACLE_MODEL || stored.model || preset?.model || "");
|
|
90
|
+
if (!baseUrl || !model) return null;
|
|
91
|
+
const hasOAuth = Boolean(oauthPreset && oauthAvailable(provider));
|
|
92
|
+
if (oauthPreset && !hasOAuth) return null;
|
|
93
|
+
if (!oauthPreset && provider !== "custom" && !apiKey) return null;
|
|
94
|
+
|
|
95
|
+
return {
|
|
96
|
+
kind: "standalone",
|
|
97
|
+
provider,
|
|
98
|
+
model,
|
|
99
|
+
baseUrl,
|
|
100
|
+
...(hasOAuth ? { authType: "oauth", protocol: oauthPreset.protocol } : { apiKey }),
|
|
101
|
+
contextLength: positiveInteger(env.ORACLE_CONTEXT_LENGTH || stored.contextLength, 128000),
|
|
102
|
+
reasoningEffort: String(env.ORACLE_REASONING_EFFORT || stored.reasoningEffort || "high"),
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function setupReason(extra = "") {
|
|
107
|
+
return [
|
|
108
|
+
"Oracle standalone chat does not require Hermes.",
|
|
109
|
+
"Set OPENROUTER_API_KEY, run `oracle auth login claude|codex|grok`, or configure a custom OpenAI-compatible endpoint.",
|
|
110
|
+
"If Hermes is installed, Oracle will reuse it automatically.",
|
|
111
|
+
extra,
|
|
112
|
+
].filter(Boolean).join("\n");
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export function resolveChatBackend({
|
|
116
|
+
env = process.env,
|
|
117
|
+
hermes = { ok: false },
|
|
118
|
+
storedConfig = null,
|
|
119
|
+
oauthAvailable = () => false,
|
|
120
|
+
apiKeyResolver = () => "",
|
|
121
|
+
} = {}) {
|
|
122
|
+
const requested = String(env.ORACLE_CHAT_BACKEND || storedConfig?.backend || "auto").trim().toLowerCase();
|
|
123
|
+
const standalone = standaloneConfigFromEnv(env, storedConfig || {}, oauthAvailable, apiKeyResolver);
|
|
124
|
+
|
|
125
|
+
if (requested === "standalone") {
|
|
126
|
+
return standalone
|
|
127
|
+
? { kind: "standalone", config: standalone }
|
|
128
|
+
: { kind: "unconfigured", reason: setupReason("ORACLE_CHAT_BACKEND=standalone was requested, but no standalone model is configured.") };
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (requested === "hermes") {
|
|
132
|
+
return hermes?.ok
|
|
133
|
+
? { kind: "hermes", bin: hermes.bin }
|
|
134
|
+
: { kind: "unconfigured", reason: setupReason("ORACLE_CHAT_BACKEND=hermes was requested, but Hermes was not found.") };
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
if (requested !== "auto") {
|
|
138
|
+
return { kind: "unconfigured", reason: setupReason(`Unknown ORACLE_CHAT_BACKEND value: ${requested}`) };
|
|
139
|
+
}
|
|
140
|
+
if (hermes?.ok) return { kind: "hermes", bin: hermes.bin };
|
|
141
|
+
if (standalone) return { kind: "standalone", config: standalone };
|
|
142
|
+
return { kind: "unconfigured", reason: setupReason() };
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export default { resolveChatBackend, standaloneConfigFromEnv };
|