Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Eterna CLI

## Releases

This repo uses [Release Please](https://github.com/googleapis/release-please) for automated releases and changelog generation.

**Always use [Conventional Commits](https://www.conventionalcommits.org/)** so Release Please can determine the correct semantic version bump:

- `fix:` → patch (0.0.X)
- `feat:` → minor (0.X.0)
- `feat!:` or `BREAKING CHANGE:` → major (X.0.0)

Examples:
```
fix: parse JSON error bodies in execute failures
feat: add `eterna status` command
feat: add inline code execution via `-e` flag
```
15 changes: 13 additions & 2 deletions src/api/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,25 @@ export class ApiClient {
body: JSON.stringify({ code }),
});
if (!retryRes.ok) {
throw new Error(`Execution failed: ${retryRes.statusText}`);
const errBody = await retryRes.text();
throw new Error(`Execution failed (${retryRes.status}): ${errBody}`);
}
return retryRes.json() as Promise<ExecutionResult>;
}

if (!res.ok) {
const errBody = await res.text();
throw new Error(`Execution failed (${res.status}): ${errBody}`);
let detail = errBody;
try {
const parsed = JSON.parse(errBody) as Record<string, unknown>;
detail = (parsed.error as string)
?? (parsed.message as string)
?? (parsed.detail as string)
?? errBody;
} catch { /* use raw body */ }
throw new Error(
`Execution failed (${res.status}): ${detail}`,
);
}

return res.json() as Promise<ExecutionResult>;
Expand Down
51 changes: 42 additions & 9 deletions src/auth/localhost-flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,38 @@ const CALLBACK_PORT = 9876;
const REDIRECT_URI = `http://127.0.0.1:${CALLBACK_PORT}/callback`;
const SCOPE = "mcp:full";

function callbackPage(title: string, message: string, success: boolean): string {
const iconColor = success ? "#a299ff" : "#f87171";
const icon = success ? "&#10003;" : "&#10007;";
return `<!DOCTYPE html>
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
<title>${title} — Eterna CLI</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link href="https://fonts.googleapis.com/css2?family=JetBrains+Mono:wght@400;500;700&display=swap" rel="stylesheet">
<style>
*{margin:0;padding:0;box-sizing:border-box}
body{font-family:"JetBrains Mono",monospace;background:#000;color:#fff;
display:flex;align-items:center;justify-content:center;min-height:100vh;padding:1rem}
.card{text-align:center;padding:3rem 2.5rem;background:#0c0c0c;
border:1px solid #1a1a1a;max-width:28rem;width:100%;
transition:border-color .2s}
.card:hover{border-color:rgba(255,255,255,.1)}
.icon{font-size:2.5rem;width:64px;height:64px;line-height:64px;
color:${iconColor};margin:0 auto 2rem;display:block;letter-spacing:-0.05em}
h1{font-size:1.25rem;margin-bottom:0.75rem;color:#fff;font-weight:700;
letter-spacing:0.02em}
p{color:#b0b0b0;line-height:1.6;font-size:0.875rem}
.brand{margin-top:2.5rem;font-size:0.625rem;color:#555;
letter-spacing:0.1em;text-transform:uppercase;font-weight:500}
</style></head>
<body><div class="card">
<span class="icon">${icon}</span>
<h1>${title}</h1>
<p>${message}</p>
<div class="brand">Eterna</div>
</div></body></html>`;
}

export function generateCodeVerifier(): string {
return randomBytes(32).toString("base64url");
}
Expand Down Expand Up @@ -73,27 +105,27 @@ export async function localhostAuthFlow(

if (error) {
res.writeHead(200, { "Content-Type": "text/html" });
res.end(
"<html><body><h1>Authorization Failed</h1><p>You can close this window.</p></body></html>",
);
res.end(callbackPage("Authorization Failed", "Something went wrong. You can close this window.", false));
server.closeAllConnections();
server.close();
reject(new Error(`OAuth error: ${error}`));
return;
}

if (!code || !receivedState) {
res.writeHead(400, { "Content-Type": "text/html" });
res.end("<html><body><h1>Missing parameters</h1></body></html>");
res.end(callbackPage("Missing Parameters", "The callback was missing required parameters.", false));
server.closeAllConnections();
server.close();
reject(new Error("Missing code or state in callback"));
return;
}

res.writeHead(200, { "Content-Type": "text/html" });
res.end(
"<html><body><h1>Authorized!</h1><p>You can close this window and return to your terminal.</p></body></html>",
);
server.close();
res.end(callbackPage("Authorized!", "You can close this window and return to your terminal.", true), () => {
server.closeAllConnections();
server.close();
});
resolve({ code, receivedState });
}
});
Expand All @@ -106,10 +138,11 @@ export async function localhostAuthFlow(
});
});

setTimeout(() => {
const timeout = setTimeout(() => {
server.close();
reject(new Error("Authorization timed out after 5 minutes"));
}, 300_000);
timeout.unref();
});

if (receivedState !== state) {
Expand Down
2 changes: 2 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { executeCommand } from "./commands/execute.js";
import { sdkCommand } from "./commands/sdk.js";
import { balanceCommand } from "./commands/balance.js";
import { positionsCommand } from "./commands/positions.js";
import { statusCommand } from "./commands/status.js";

const program = new Command();

Expand All @@ -15,6 +16,7 @@ program

program.addCommand(loginCommand);
program.addCommand(logoutCommand);
program.addCommand(statusCommand);
program.addCommand(executeCommand);
program.addCommand(sdkCommand);
program.addCommand(balanceCommand);
Expand Down
11 changes: 8 additions & 3 deletions src/commands/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,20 @@ async function readStdin(): Promise<string> {
export const executeCommand = new Command("execute")
.description("Execute trading code in the Eterna sandbox")
.argument("[file]", "TypeScript file to execute, or - for stdin")
.action(async (file?: string) => {
.option("-e, --eval <code>", "Execute inline code instead of a file")
.action(async (file: string | undefined, opts: { eval?: string }) => {
let code: string;

if (file === "-" || (!file && !process.stdin.isTTY)) {
if (opts.eval) {
code = opts.eval;
} else if (file === "-" || (!file && !process.stdin.isTTY)) {
code = await readStdin();
} else if (file) {
code = await readCodeFromFile(file);
} else {
console.error("Usage: eterna execute <file.ts> or pipe code via stdin");
console.error(
"Usage: eterna execute <file.ts>, eterna execute -e '<code>', or pipe via stdin",
);
process.exitCode = 1;
return;
}
Expand Down
29 changes: 29 additions & 0 deletions src/commands/status.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Command } from "commander";
import { TokenManager } from "../auth/token-manager.js";
import { readCredentials, readConfig } from "../auth/config.js";

export const statusCommand = new Command("status")
.description("Show authentication status")
.action(async () => {
const tokenManager = new TokenManager();
const creds = readCredentials();

if (!creds || !tokenManager.isAuthenticated()) {
console.log("Not authenticated. Run `eterna login` to get started.");
return;
}

const config = readConfig();
const expired = tokenManager.needsRefresh();

console.log(`Authenticated`);
console.log(` Endpoint: ${config.endpoint}`);

const expiresAt = new Date(creds.expiresAt);
if (expired) {
console.log(` Token: expired (${expiresAt.toLocaleString()})`);
} else {
const remaining = Math.round((creds.expiresAt - Date.now()) / 60_000);
console.log(` Token: valid (expires in ${remaining}m)`);
}
});
Loading