apow-cli 0.11.8 → 0.11.10
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/dist/errors.js +25 -1
- package/dist/index.js +2 -2
- package/dist/mint.js +78 -45
- package/dist/preflight.js +41 -4
- package/dist/smhl.js +84 -41
- package/package.json +1 -1
package/dist/errors.js
CHANGED
|
@@ -62,13 +62,37 @@ const patterns = [
|
|
|
62
62
|
},
|
|
63
63
|
},
|
|
64
64
|
{
|
|
65
|
-
test: (m) => /\b40[13]\b/.test(m)
|
|
65
|
+
test: (m) => /\b40[13]\b/.test(m)
|
|
66
|
+
&& (m.includes("anthropic") || m.includes("openai") || m.toLowerCase().includes("unauthorized") || m.toLowerCase().includes("forbidden"))
|
|
67
|
+
&& !m.toLowerCase().includes("codex")
|
|
68
|
+
&& !m.toLowerCase().includes("claude")
|
|
69
|
+
&& !m.toLowerCase().includes("clawrouter"),
|
|
66
70
|
classify: () => ({
|
|
67
71
|
category: "setup",
|
|
68
72
|
userMessage: "LLM API key is invalid or expired",
|
|
69
73
|
recovery: "Run `apow setup` to configure a valid key",
|
|
70
74
|
}),
|
|
71
75
|
},
|
|
76
|
+
{
|
|
77
|
+
test: (m) => m.includes("SMHL solve failed with")
|
|
78
|
+
|| m.includes("Failed to authenticate")
|
|
79
|
+
|| m.includes("Request not allowed")
|
|
80
|
+
|| m.includes("country, region, or territory not supported"),
|
|
81
|
+
classify: (msg) => {
|
|
82
|
+
const lower = msg.toLowerCase();
|
|
83
|
+
const provider = lower.includes("codex") ? "codex"
|
|
84
|
+
: lower.includes("claude") ? "claude-code"
|
|
85
|
+
: lower.includes("clawrouter") ? "clawrouter"
|
|
86
|
+
: "your configured LLM";
|
|
87
|
+
return {
|
|
88
|
+
category: "setup",
|
|
89
|
+
userMessage: `${provider} is not usable for minting`,
|
|
90
|
+
recovery: provider === "clawrouter"
|
|
91
|
+
? "Check Base USDC balance and x402 connectivity, or switch to another LLM provider in `apow setup`"
|
|
92
|
+
: "Authenticate that CLI/provider, or switch to LLM_PROVIDER=clawrouter in `apow setup`",
|
|
93
|
+
};
|
|
94
|
+
},
|
|
95
|
+
},
|
|
72
96
|
{
|
|
73
97
|
test: (m) => m.includes("x402 GPU payment failed: insufficient USDC balance"),
|
|
74
98
|
classify: () => ({
|
package/dist/index.js
CHANGED
|
@@ -284,7 +284,7 @@ async function setupWizard() {
|
|
|
284
284
|
ui.ok(`Ollama at ${ollamaUrl}`);
|
|
285
285
|
}
|
|
286
286
|
else if (provider === "claude-code" || provider === "codex") {
|
|
287
|
-
ui.ok(`Using local ${provider} CLI —
|
|
287
|
+
ui.ok(`Using local ${provider} CLI — make sure you're already authenticated`);
|
|
288
288
|
}
|
|
289
289
|
else {
|
|
290
290
|
const apiKey = await ui.promptSecret("API key");
|
|
@@ -294,7 +294,7 @@ async function setupWizard() {
|
|
|
294
294
|
}
|
|
295
295
|
else {
|
|
296
296
|
ui.fail("No API key provided");
|
|
297
|
-
ui.hint(
|
|
297
|
+
ui.hint("Set LLM_API_KEY in .env later");
|
|
298
298
|
}
|
|
299
299
|
}
|
|
300
300
|
const defaultModel = (0, config_1.resolveDefaultModel)(provider);
|
package/dist/mint.js
CHANGED
|
@@ -49,6 +49,10 @@ const wallet_1 = require("./wallet");
|
|
|
49
49
|
const miningAgentAbi = MiningAgent_json_1.default;
|
|
50
50
|
const ZERO_SEED = `0x${"0".repeat(64)}`;
|
|
51
51
|
const MINT_GAS_RESERVE_ETH = (0, viem_1.parseEther)("0.003");
|
|
52
|
+
function isExpiredMintError(error) {
|
|
53
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
54
|
+
return message.includes("Expired");
|
|
55
|
+
}
|
|
52
56
|
function deriveChallengeFromSeed(seed) {
|
|
53
57
|
const bytes = (0, viem_1.hexToBytes)(seed);
|
|
54
58
|
const firstNChars = 5 + (bytes[0] % 6);
|
|
@@ -148,59 +152,88 @@ async function runMintFlow(options = {}) {
|
|
|
148
152
|
return null;
|
|
149
153
|
}
|
|
150
154
|
console.log("");
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
155
|
+
let receipt = null;
|
|
156
|
+
for (let attempt = 1; attempt <= 2; attempt += 1) {
|
|
157
|
+
// Request challenge
|
|
158
|
+
const challengeSpinner = ui.spinner(`Requesting challenge${attempt > 1 ? ` (retry ${attempt}/2)` : ""}...`);
|
|
159
|
+
const challengeTx = await walletClient.writeContract({
|
|
160
|
+
address: config_1.config.miningAgentAddress,
|
|
161
|
+
abi: miningAgentAbi,
|
|
162
|
+
account,
|
|
163
|
+
functionName: "getChallenge",
|
|
164
|
+
args: [account.address],
|
|
165
|
+
});
|
|
166
|
+
const challengeReceipt = await wallet_1.publicClient.waitForTransactionReceipt({ hash: challengeTx });
|
|
167
|
+
if (challengeReceipt.status === "reverted") {
|
|
168
|
+
throw new Error("Challenge request reverted on-chain");
|
|
169
|
+
}
|
|
170
|
+
challengeSpinner.stop(`Requesting challenge${attempt > 1 ? ` (retry ${attempt}/2)` : ""}... done`);
|
|
171
|
+
// Read the seed at the exact receipt block so we do not burn the 20s window
|
|
172
|
+
// waiting for latest-state propagation on slower RPCs.
|
|
173
|
+
let challengeSeed = (await wallet_1.publicClient.readContract({
|
|
169
174
|
address: config_1.config.miningAgentAddress,
|
|
170
175
|
abi: miningAgentAbi,
|
|
171
176
|
functionName: "challengeSeeds",
|
|
172
177
|
args: [account.address],
|
|
178
|
+
blockNumber: challengeReceipt.blockNumber,
|
|
173
179
|
}));
|
|
174
|
-
if (challengeSeed.toLowerCase()
|
|
180
|
+
if (challengeSeed.toLowerCase() === ZERO_SEED.toLowerCase()) {
|
|
181
|
+
// Fall back to latest-state polling if the provider does not expose the
|
|
182
|
+
// just-mined storage view reliably.
|
|
183
|
+
for (let retry = 0; retry < 3; retry++) {
|
|
184
|
+
challengeSeed = (await wallet_1.publicClient.readContract({
|
|
185
|
+
address: config_1.config.miningAgentAddress,
|
|
186
|
+
abi: miningAgentAbi,
|
|
187
|
+
functionName: "challengeSeeds",
|
|
188
|
+
args: [account.address],
|
|
189
|
+
}));
|
|
190
|
+
if (challengeSeed.toLowerCase() !== ZERO_SEED.toLowerCase())
|
|
191
|
+
break;
|
|
192
|
+
await new Promise((r) => setTimeout(r, 1000));
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
if (challengeSeed.toLowerCase() === ZERO_SEED.toLowerCase()) {
|
|
196
|
+
throw new Error("Challenge seed not found after confirmation. The RPC may be lagging — try again.");
|
|
197
|
+
}
|
|
198
|
+
// Solve SMHL
|
|
199
|
+
const challenge = deriveChallengeFromSeed(challengeSeed);
|
|
200
|
+
const smhlSpinner = ui.spinner("Solving SMHL...");
|
|
201
|
+
const solution = await (0, smhl_1.solveSmhlChallenge)(challenge, (smhlAttempt) => {
|
|
202
|
+
smhlSpinner.update(`Solving SMHL... attempt ${smhlAttempt}/5`);
|
|
203
|
+
});
|
|
204
|
+
smhlSpinner.stop("Solving SMHL... done");
|
|
205
|
+
// Mint
|
|
206
|
+
const mintSpinner = ui.spinner("Minting...");
|
|
207
|
+
try {
|
|
208
|
+
const mintTx = await walletClient.writeContract({
|
|
209
|
+
address: config_1.config.miningAgentAddress,
|
|
210
|
+
abi: miningAgentAbi,
|
|
211
|
+
account,
|
|
212
|
+
functionName: "mint",
|
|
213
|
+
args: [solution],
|
|
214
|
+
value: mintPrice,
|
|
215
|
+
});
|
|
216
|
+
mintSpinner.update("Waiting for confirmation...");
|
|
217
|
+
receipt = await wallet_1.publicClient.waitForTransactionReceipt({ hash: mintTx });
|
|
218
|
+
if (receipt.status === "reverted") {
|
|
219
|
+
throw new Error("Mint transaction reverted on-chain");
|
|
220
|
+
}
|
|
221
|
+
mintSpinner.stop("Minting... confirmed");
|
|
175
222
|
break;
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
223
|
+
}
|
|
224
|
+
catch (error) {
|
|
225
|
+
mintSpinner.fail("Minting... failed");
|
|
226
|
+
if (isExpiredMintError(error) && attempt < 2) {
|
|
227
|
+
ui.warn("Challenge expired before mint submission. Retrying with a fresh challenge...");
|
|
228
|
+
console.log("");
|
|
229
|
+
continue;
|
|
230
|
+
}
|
|
231
|
+
throw error;
|
|
232
|
+
}
|
|
180
233
|
}
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
const smhlSpinner = ui.spinner("Solving SMHL...");
|
|
184
|
-
const solution = await (0, smhl_1.solveSmhlChallenge)(challenge, (attempt) => {
|
|
185
|
-
smhlSpinner.update(`Solving SMHL... attempt ${attempt}/5`);
|
|
186
|
-
});
|
|
187
|
-
smhlSpinner.stop("Solving SMHL... done");
|
|
188
|
-
// Mint
|
|
189
|
-
const mintSpinner = ui.spinner("Minting...");
|
|
190
|
-
const mintTx = await walletClient.writeContract({
|
|
191
|
-
address: config_1.config.miningAgentAddress,
|
|
192
|
-
abi: miningAgentAbi,
|
|
193
|
-
account,
|
|
194
|
-
functionName: "mint",
|
|
195
|
-
args: [solution],
|
|
196
|
-
value: mintPrice,
|
|
197
|
-
});
|
|
198
|
-
mintSpinner.update("Waiting for confirmation...");
|
|
199
|
-
const receipt = await wallet_1.publicClient.waitForTransactionReceipt({ hash: mintTx });
|
|
200
|
-
if (receipt.status === "reverted") {
|
|
201
|
-
throw new Error("Mint transaction reverted on-chain");
|
|
234
|
+
if (!receipt) {
|
|
235
|
+
throw new Error("Mint failed before confirmation.");
|
|
202
236
|
}
|
|
203
|
-
mintSpinner.stop("Minting... confirmed");
|
|
204
237
|
// Parse token ID from Transfer event in receipt (avoids stale RPC reads)
|
|
205
238
|
const TRANSFER_TOPIC = "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef";
|
|
206
239
|
const mintLog = receipt.logs.find((log) => log.address.toLowerCase() === config_1.config.miningAgentAddress.toLowerCase() &&
|
package/dist/preflight.js
CHANGED
|
@@ -37,6 +37,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
37
37
|
};
|
|
38
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
39
|
exports.runPreflight = runPreflight;
|
|
40
|
+
const node_child_process_1 = require("node:child_process");
|
|
40
41
|
const viem_1 = require("viem");
|
|
41
42
|
const chains_1 = require("viem/chains");
|
|
42
43
|
const AgentCoin_json_1 = __importDefault(require("./abi/AgentCoin.json"));
|
|
@@ -59,6 +60,15 @@ const erc20BalanceAbi = [
|
|
|
59
60
|
stateMutability: "view",
|
|
60
61
|
},
|
|
61
62
|
];
|
|
63
|
+
function hasLocalCli(command) {
|
|
64
|
+
try {
|
|
65
|
+
(0, node_child_process_1.execFileSync)(command, ["--version"], { stdio: "ignore" });
|
|
66
|
+
return true;
|
|
67
|
+
}
|
|
68
|
+
catch {
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
62
72
|
async function runPreflight(level) {
|
|
63
73
|
const results = [];
|
|
64
74
|
// Check 1: Contract addresses set
|
|
@@ -207,7 +217,7 @@ async function runPreflight(level) {
|
|
|
207
217
|
});
|
|
208
218
|
}
|
|
209
219
|
}
|
|
210
|
-
// Check 5: LLM
|
|
220
|
+
// Check 5: LLM provider readiness (required for minting only)
|
|
211
221
|
if (level === "wallet") {
|
|
212
222
|
if (config_1.config.llmProvider === "clawrouter") {
|
|
213
223
|
if (!wallet_1.account) {
|
|
@@ -219,7 +229,7 @@ async function runPreflight(level) {
|
|
|
219
229
|
}
|
|
220
230
|
else {
|
|
221
231
|
results.push({
|
|
222
|
-
label:
|
|
232
|
+
label: "LLM provider: clawrouter (x402, same wallet as mining)",
|
|
223
233
|
passed: true,
|
|
224
234
|
});
|
|
225
235
|
}
|
|
@@ -227,8 +237,35 @@ async function runPreflight(level) {
|
|
|
227
237
|
else if (config_1.config.llmProvider === "ollama") {
|
|
228
238
|
results.push({ label: `LLM provider: ollama (${config_1.config.ollamaUrl})`, passed: true });
|
|
229
239
|
}
|
|
230
|
-
else if (config_1.config.llmProvider === "claude-code"
|
|
231
|
-
|
|
240
|
+
else if (config_1.config.llmProvider === "claude-code") {
|
|
241
|
+
if (hasLocalCli("claude")) {
|
|
242
|
+
results.push({
|
|
243
|
+
label: "LLM provider: claude-code (local CLI detected; auth checked at mint time)",
|
|
244
|
+
passed: true,
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
else {
|
|
248
|
+
results.push({
|
|
249
|
+
label: "LLM provider claude-code not installed",
|
|
250
|
+
passed: false,
|
|
251
|
+
fix: "Install the Claude CLI or switch to LLM_PROVIDER=clawrouter",
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
else if (config_1.config.llmProvider === "codex") {
|
|
256
|
+
if (hasLocalCli("codex")) {
|
|
257
|
+
results.push({
|
|
258
|
+
label: "LLM provider: codex (local CLI detected; auth/region checked at mint time)",
|
|
259
|
+
passed: true,
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
else {
|
|
263
|
+
results.push({
|
|
264
|
+
label: "LLM provider codex not installed",
|
|
265
|
+
passed: false,
|
|
266
|
+
fix: "Install the Codex CLI or switch to LLM_PROVIDER=clawrouter",
|
|
267
|
+
});
|
|
268
|
+
}
|
|
232
269
|
}
|
|
233
270
|
else if (config_1.config.llmApiKey) {
|
|
234
271
|
results.push({ label: `LLM provider: ${config_1.config.llmProvider} (key set)`, passed: true });
|
package/dist/smhl.js
CHANGED
|
@@ -184,10 +184,10 @@ function sanitizeResponse(text) {
|
|
|
184
184
|
}
|
|
185
185
|
return cleaned;
|
|
186
186
|
}
|
|
187
|
-
async function
|
|
187
|
+
async function requestOpenAiSolutionForModel(prompt, model) {
|
|
188
188
|
const client = new openai_1.default({ apiKey: (0, config_1.requireLlmApiKey)() });
|
|
189
189
|
const response = await client.chat.completions.create({
|
|
190
|
-
model
|
|
190
|
+
model,
|
|
191
191
|
temperature: 0.7,
|
|
192
192
|
messages: [
|
|
193
193
|
{
|
|
@@ -199,7 +199,7 @@ async function requestOpenAiSolution(prompt) {
|
|
|
199
199
|
}, { timeout: 15_000 });
|
|
200
200
|
return response.choices[0]?.message.content ?? "";
|
|
201
201
|
}
|
|
202
|
-
async function
|
|
202
|
+
async function requestAnthropicSolutionForModel(prompt, model) {
|
|
203
203
|
const response = await fetch("https://api.anthropic.com/v1/messages", {
|
|
204
204
|
method: "POST",
|
|
205
205
|
signal: AbortSignal.timeout(15_000),
|
|
@@ -209,7 +209,7 @@ async function requestAnthropicSolution(prompt) {
|
|
|
209
209
|
"anthropic-version": "2023-06-01",
|
|
210
210
|
},
|
|
211
211
|
body: JSON.stringify({
|
|
212
|
-
model
|
|
212
|
+
model,
|
|
213
213
|
max_tokens: 200,
|
|
214
214
|
temperature: 0.7,
|
|
215
215
|
system: "You generate short lowercase word sequences that match exact constraints. Return only the words separated by spaces. Nothing else.",
|
|
@@ -220,7 +220,7 @@ async function requestAnthropicSolution(prompt) {
|
|
|
220
220
|
const retryAfter = response.headers.get("retry-after");
|
|
221
221
|
const waitMs = retryAfter ? parseInt(retryAfter) * 1000 : 5000;
|
|
222
222
|
await new Promise((r) => setTimeout(r, waitMs));
|
|
223
|
-
throw new Error(
|
|
223
|
+
throw new Error("Rate limited by Anthropic — retrying");
|
|
224
224
|
}
|
|
225
225
|
if (!response.ok) {
|
|
226
226
|
throw new Error(`Anthropic request failed: ${response.status} ${response.statusText}`);
|
|
@@ -228,13 +228,13 @@ async function requestAnthropicSolution(prompt) {
|
|
|
228
228
|
const data = (await response.json());
|
|
229
229
|
return data.content?.find((item) => item.type === "text")?.text ?? "";
|
|
230
230
|
}
|
|
231
|
-
async function
|
|
231
|
+
async function requestOllamaSolutionForModel(prompt, model) {
|
|
232
232
|
const response = await fetch(`${config_1.config.ollamaUrl}/api/generate`, {
|
|
233
233
|
method: "POST",
|
|
234
234
|
signal: AbortSignal.timeout(15_000),
|
|
235
235
|
headers: { "content-type": "application/json" },
|
|
236
236
|
body: JSON.stringify({
|
|
237
|
-
model
|
|
237
|
+
model,
|
|
238
238
|
prompt: [
|
|
239
239
|
"You generate short lowercase word sequences that match exact constraints.",
|
|
240
240
|
"Return only the words separated by spaces. Nothing else.",
|
|
@@ -249,7 +249,7 @@ async function requestOllamaSolution(prompt) {
|
|
|
249
249
|
const retryAfter = response.headers.get("retry-after");
|
|
250
250
|
const waitMs = retryAfter ? parseInt(retryAfter) * 1000 : 5000;
|
|
251
251
|
await new Promise((r) => setTimeout(r, waitMs));
|
|
252
|
-
throw new Error(
|
|
252
|
+
throw new Error("Rate limited by Ollama — retrying");
|
|
253
253
|
}
|
|
254
254
|
if (!response.ok) {
|
|
255
255
|
throw new Error(`Ollama request failed: ${response.status} ${response.statusText}`);
|
|
@@ -257,8 +257,7 @@ async function requestOllamaSolution(prompt) {
|
|
|
257
257
|
const data = (await response.json());
|
|
258
258
|
return data.response ?? "";
|
|
259
259
|
}
|
|
260
|
-
async function
|
|
261
|
-
const model = config_1.config.llmModel || "gemini-2.5-flash";
|
|
260
|
+
async function requestGeminiSolutionForModel(prompt, model) {
|
|
262
261
|
const response = await fetch(`https://generativelanguage.googleapis.com/v1beta/models/${model}:generateContent?key=${(0, config_1.requireLlmApiKey)()}`, {
|
|
263
262
|
method: "POST",
|
|
264
263
|
signal: AbortSignal.timeout(15_000),
|
|
@@ -275,7 +274,7 @@ async function requestGeminiSolution(prompt) {
|
|
|
275
274
|
const retryAfter = response.headers.get("retry-after");
|
|
276
275
|
const waitMs = retryAfter ? parseInt(retryAfter) * 1000 : 5000;
|
|
277
276
|
await new Promise((r) => setTimeout(r, waitMs));
|
|
278
|
-
throw new Error(
|
|
277
|
+
throw new Error("Rate limited by Gemini — retrying");
|
|
279
278
|
}
|
|
280
279
|
if (!response.ok) {
|
|
281
280
|
throw new Error(`Gemini request failed: ${response.status} ${response.statusText}`);
|
|
@@ -283,13 +282,13 @@ async function requestGeminiSolution(prompt) {
|
|
|
283
282
|
const data = (await response.json());
|
|
284
283
|
return data.candidates?.[0]?.content?.parts?.[0]?.text ?? "";
|
|
285
284
|
}
|
|
286
|
-
async function
|
|
285
|
+
async function requestDeepSeekSolutionForModel(prompt, model) {
|
|
287
286
|
const client = new openai_1.default({
|
|
288
287
|
apiKey: (0, config_1.requireLlmApiKey)(),
|
|
289
288
|
baseURL: "https://api.deepseek.com",
|
|
290
289
|
});
|
|
291
290
|
const response = await client.chat.completions.create({
|
|
292
|
-
model
|
|
291
|
+
model,
|
|
293
292
|
temperature: 0.7,
|
|
294
293
|
messages: [
|
|
295
294
|
{
|
|
@@ -301,13 +300,13 @@ async function requestDeepSeekSolution(prompt) {
|
|
|
301
300
|
}, { timeout: 15_000 });
|
|
302
301
|
return response.choices[0]?.message.content ?? "";
|
|
303
302
|
}
|
|
304
|
-
async function
|
|
303
|
+
async function requestQwenSolutionForModel(prompt, model) {
|
|
305
304
|
const client = new openai_1.default({
|
|
306
305
|
apiKey: (0, config_1.requireLlmApiKey)(),
|
|
307
306
|
baseURL: "https://dashscope.aliyuncs.com/compatible-mode/v1",
|
|
308
307
|
});
|
|
309
308
|
const response = await client.chat.completions.create({
|
|
310
|
-
model
|
|
309
|
+
model,
|
|
311
310
|
temperature: 0.7,
|
|
312
311
|
messages: [
|
|
313
312
|
{
|
|
@@ -319,7 +318,7 @@ async function requestQwenSolution(prompt) {
|
|
|
319
318
|
}, { timeout: 15_000 });
|
|
320
319
|
return response.choices[0]?.message.content ?? "";
|
|
321
320
|
}
|
|
322
|
-
async function requestClawRouterSolution(prompt) {
|
|
321
|
+
async function requestClawRouterSolution(prompt, model) {
|
|
323
322
|
const { ensureClawRouter, getClawRouterBaseUrl } = await Promise.resolve().then(() => __importStar(require("./clawrouter")));
|
|
324
323
|
const { requirePrivateKey } = await Promise.resolve().then(() => __importStar(require("./config")));
|
|
325
324
|
await ensureClawRouter(requirePrivateKey());
|
|
@@ -328,7 +327,7 @@ async function requestClawRouterSolution(prompt) {
|
|
|
328
327
|
baseURL: `${getClawRouterBaseUrl()}/v1`,
|
|
329
328
|
});
|
|
330
329
|
const response = await client.chat.completions.create({
|
|
331
|
-
model
|
|
330
|
+
model,
|
|
332
331
|
temperature: 0.7,
|
|
333
332
|
messages: [
|
|
334
333
|
{
|
|
@@ -356,34 +355,62 @@ async function requestCodexSolution(prompt) {
|
|
|
356
355
|
return new Promise((resolve, reject) => {
|
|
357
356
|
(0, node_child_process_1.execFile)("codex", ["exec", prompt, "--full-auto"], { timeout: 15_000 }, (error, stdout, stderr) => {
|
|
358
357
|
if (error) {
|
|
359
|
-
reject(new Error(`Codex error: ${error.message}`));
|
|
358
|
+
reject(new Error(`Codex error: ${error.message}${stderr ? `\nstderr: ${stderr}` : ""}${stdout ? `\nstdout: ${stdout}` : ""}`));
|
|
360
359
|
return;
|
|
361
360
|
}
|
|
362
361
|
resolve(stdout.trim());
|
|
363
362
|
});
|
|
364
363
|
});
|
|
365
364
|
}
|
|
366
|
-
|
|
367
|
-
|
|
365
|
+
function resolveModelForProvider(provider) {
|
|
366
|
+
return provider === config_1.config.llmProvider ? config_1.config.llmModel : (0, config_1.resolveDefaultModel)(provider);
|
|
367
|
+
}
|
|
368
|
+
function resolveProviderOrder() {
|
|
369
|
+
const providers = [config_1.config.llmProvider];
|
|
370
|
+
if (config_1.config.useX402 && config_1.config.privateKey && config_1.config.llmProvider !== "clawrouter") {
|
|
371
|
+
providers.push("clawrouter");
|
|
372
|
+
}
|
|
373
|
+
return providers;
|
|
374
|
+
}
|
|
375
|
+
function describeProvider(provider) {
|
|
376
|
+
return provider === "claude-code" ? "Claude Code" : provider === "clawrouter" ? "ClawRouter" : provider;
|
|
377
|
+
}
|
|
378
|
+
function isPermanentProviderError(message) {
|
|
379
|
+
const normalized = message.toLowerCase();
|
|
380
|
+
return normalized.includes("request not allowed")
|
|
381
|
+
|| normalized.includes("failed to authenticate")
|
|
382
|
+
|| normalized.includes("country, region, or territory not supported")
|
|
383
|
+
|| normalized.includes("invalid api key")
|
|
384
|
+
|| normalized.includes("api key is invalid")
|
|
385
|
+
|| normalized.includes("not authenticated")
|
|
386
|
+
|| normalized.includes("unauthorized")
|
|
387
|
+
|| normalized.includes("forbidden")
|
|
388
|
+
|| normalized.includes("not logged in")
|
|
389
|
+
|| normalized.includes("command not found")
|
|
390
|
+
|| normalized.includes("enoent");
|
|
391
|
+
}
|
|
392
|
+
async function requestProviderSolution(provider, prompt) {
|
|
393
|
+
const model = resolveModelForProvider(provider);
|
|
394
|
+
switch (provider) {
|
|
368
395
|
case "anthropic":
|
|
369
|
-
return
|
|
396
|
+
return requestAnthropicSolutionForModel(prompt, model);
|
|
370
397
|
case "gemini":
|
|
371
|
-
return
|
|
398
|
+
return requestGeminiSolutionForModel(prompt, model);
|
|
372
399
|
case "ollama":
|
|
373
|
-
return
|
|
400
|
+
return requestOllamaSolutionForModel(prompt, model);
|
|
374
401
|
case "claude-code":
|
|
375
402
|
return requestClaudeCodeSolution(prompt);
|
|
376
403
|
case "codex":
|
|
377
404
|
return requestCodexSolution(prompt);
|
|
378
405
|
case "deepseek":
|
|
379
|
-
return
|
|
406
|
+
return requestDeepSeekSolutionForModel(prompt, model);
|
|
380
407
|
case "qwen":
|
|
381
|
-
return
|
|
408
|
+
return requestQwenSolutionForModel(prompt, model);
|
|
382
409
|
case "clawrouter":
|
|
383
|
-
return requestClawRouterSolution(prompt);
|
|
410
|
+
return requestClawRouterSolution(prompt, model);
|
|
384
411
|
case "openai":
|
|
385
412
|
default:
|
|
386
|
-
return
|
|
413
|
+
return requestOpenAiSolutionForModel(prompt, model);
|
|
387
414
|
}
|
|
388
415
|
}
|
|
389
416
|
/**
|
|
@@ -415,27 +442,43 @@ function solveSmhlAlgorithmic(challenge) {
|
|
|
415
442
|
return words.join(" ");
|
|
416
443
|
}
|
|
417
444
|
async function solveSmhlChallenge(challenge, onAttempt) {
|
|
445
|
+
const providers = resolveProviderOrder();
|
|
446
|
+
let providerIndex = 0;
|
|
418
447
|
let feedback;
|
|
419
448
|
let lastIssues = "provider did not return a valid response";
|
|
420
449
|
for (let attempt = 1; attempt <= 5; attempt += 1) {
|
|
421
450
|
if (onAttempt)
|
|
422
451
|
onAttempt(attempt);
|
|
423
|
-
|
|
452
|
+
while (providerIndex < providers.length) {
|
|
453
|
+
const provider = providers[providerIndex];
|
|
424
454
|
const prompt = buildSmhlPrompt(challenge, feedback);
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
455
|
+
try {
|
|
456
|
+
const raw = await requestProviderSolution(provider, prompt);
|
|
457
|
+
const sanitized = sanitizeResponse(raw);
|
|
458
|
+
const adjusted = adjustSolution(sanitized, challenge);
|
|
459
|
+
const issues = validateSmhlSolution(adjusted, challenge);
|
|
460
|
+
if (issues.length === 0) {
|
|
461
|
+
return adjusted;
|
|
462
|
+
}
|
|
463
|
+
feedback = issues.join(", ");
|
|
464
|
+
lastIssues = `attempt ${attempt} (${describeProvider(provider)}): ${feedback}`;
|
|
465
|
+
break;
|
|
466
|
+
}
|
|
467
|
+
catch (err) {
|
|
468
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
469
|
+
if (isPermanentProviderError(msg) && providerIndex < providers.length - 1) {
|
|
470
|
+
providerIndex += 1;
|
|
471
|
+
feedback = undefined;
|
|
472
|
+
lastIssues = `${describeProvider(provider)} unavailable: ${msg}`;
|
|
473
|
+
continue;
|
|
474
|
+
}
|
|
475
|
+
if (isPermanentProviderError(msg)) {
|
|
476
|
+
throw new Error(`SMHL solve failed with ${describeProvider(provider)}: ${msg}`);
|
|
477
|
+
}
|
|
478
|
+
feedback = msg;
|
|
479
|
+
lastIssues = `attempt ${attempt} (${describeProvider(provider)}): ${msg}`;
|
|
480
|
+
break;
|
|
431
481
|
}
|
|
432
|
-
feedback = issues.join(", ");
|
|
433
|
-
lastIssues = `attempt ${attempt}: ${feedback}`;
|
|
434
|
-
}
|
|
435
|
-
catch (err) {
|
|
436
|
-
const msg = err instanceof Error ? err.message : String(err);
|
|
437
|
-
feedback = msg;
|
|
438
|
-
lastIssues = `attempt ${attempt}: ${msg}`;
|
|
439
482
|
}
|
|
440
483
|
}
|
|
441
484
|
throw new Error(`SMHL solve failed after 5 attempts: ${lastIssues}`);
|