Merge pull request #9070 from continuedev/nate/refactor-info-screen-extract-functions
Refactor info command into separate functions and add usage statistics
This commit is contained in:
commit
710f67569f
2 changed files with 94 additions and 45 deletions
|
|
@ -141,7 +141,7 @@ Common slash commands available in CLI:
|
|||
- `/exit` - Exit the chat
|
||||
- `/fork` - Start a forked chat session from the current history
|
||||
- `/help` - Show help message
|
||||
- `/info` - Show session information
|
||||
- `/info` - Show session information including usage statistics (cost, tokens, cache usage)
|
||||
- `/init` - Create an AGENTS.md file
|
||||
- `/login` - Authenticate with your account
|
||||
- `/logout` - Sign out of your current session
|
||||
|
|
|
|||
|
|
@ -8,97 +8,139 @@ import {
|
|||
loadAuthConfig,
|
||||
} from "./auth/workos.js";
|
||||
import { services } from "./services/index.js";
|
||||
import { getCurrentSession, getSessionFilePath } from "./session.js";
|
||||
import {
|
||||
getCurrentSession,
|
||||
getSessionFilePath,
|
||||
getSessionUsage,
|
||||
} from "./session.js";
|
||||
import { logger } from "./util/logger.js";
|
||||
import { getVersion } from "./version.js";
|
||||
|
||||
export async function handleInfoSlashCommand() {
|
||||
const infoLines = [];
|
||||
|
||||
// Version and working directory info
|
||||
function getVersionInfo(): string[] {
|
||||
const version = getVersion();
|
||||
const cwd = process.cwd();
|
||||
|
||||
infoLines.push(
|
||||
return [
|
||||
chalk.white("CLI Information:"),
|
||||
` Version: ${chalk.green(version)}`,
|
||||
` Working Directory: ${chalk.blue(cwd)}`,
|
||||
);
|
||||
];
|
||||
}
|
||||
|
||||
async function getAuthInfo(): Promise<string[]> {
|
||||
const lines: string[] = ["", chalk.white("Authentication:")];
|
||||
|
||||
// Auth info
|
||||
if (await isAuthenticated()) {
|
||||
const config = loadAuthConfig();
|
||||
if (config && isAuthenticatedConfig(config)) {
|
||||
const email = config.userEmail || config.userId;
|
||||
const orgId = config.organizationId;
|
||||
infoLines.push(
|
||||
"",
|
||||
chalk.white("Authentication:"),
|
||||
lines.push(
|
||||
` Email: ${chalk.green(email)}`,
|
||||
` Org ID: ${chalk.cyan(orgId)}`,
|
||||
);
|
||||
} else {
|
||||
infoLines.push(
|
||||
"",
|
||||
chalk.white("Authentication:"),
|
||||
` ${chalk.yellow("Authenticated via environment variable")}`,
|
||||
);
|
||||
lines.push(` ${chalk.yellow("Authenticated via environment variable")}`);
|
||||
}
|
||||
} else {
|
||||
infoLines.push(
|
||||
"",
|
||||
chalk.white("Authentication:"),
|
||||
` ${chalk.red("Not logged in")}`,
|
||||
);
|
||||
lines.push(` ${chalk.red("Not logged in")}`);
|
||||
}
|
||||
|
||||
// Config info
|
||||
return lines;
|
||||
}
|
||||
|
||||
function getConfigInfo(): string[] {
|
||||
const lines: string[] = ["", chalk.white("Configuration:")];
|
||||
|
||||
try {
|
||||
const configState = services.config.getState();
|
||||
infoLines.push("", chalk.white("Configuration:"));
|
||||
if (configState.config) {
|
||||
infoLines.push(` ${chalk.gray(`Using ${configState.config?.name}`)}`);
|
||||
lines.push(` ${chalk.gray(`Using ${configState.config?.name}`)}`);
|
||||
} else {
|
||||
infoLines.push(` ${chalk.red(`Config not found`)}`);
|
||||
lines.push(` ${chalk.red(`Config not found`)}`);
|
||||
}
|
||||
if (configState.configPath) {
|
||||
infoLines.push(` Path: ${chalk.blue(configState.configPath)}`);
|
||||
lines.push(` Path: ${chalk.blue(configState.configPath)}`);
|
||||
}
|
||||
|
||||
// Add current model info
|
||||
try {
|
||||
const modelInfo = services.model?.getModelInfo();
|
||||
if (modelInfo) {
|
||||
infoLines.push(` Model: ${chalk.cyan(modelInfo.name)}`);
|
||||
lines.push(` Model: ${chalk.cyan(modelInfo.name)}`);
|
||||
} else {
|
||||
infoLines.push(` Model: ${chalk.red("Not available")}`);
|
||||
lines.push(` Model: ${chalk.red("Not available")}`);
|
||||
}
|
||||
} catch {
|
||||
infoLines.push(` Model: ${chalk.red("Error retrieving model info")}`);
|
||||
lines.push(` Model: ${chalk.red("Error retrieving model info")}`);
|
||||
}
|
||||
} catch {
|
||||
infoLines.push(
|
||||
"",
|
||||
chalk.white("Configuration:"),
|
||||
` ${chalk.red("Configuration service not available")}`,
|
||||
);
|
||||
lines.push(` ${chalk.red("Configuration service not available")}`);
|
||||
}
|
||||
|
||||
// Session info
|
||||
infoLines.push("", chalk.white("Session:"));
|
||||
return lines;
|
||||
}
|
||||
|
||||
function getSessionInfo(): string[] {
|
||||
const lines: string[] = ["", chalk.white("Session:")];
|
||||
|
||||
try {
|
||||
const currentSession = getCurrentSession();
|
||||
const sessionFilePath = getSessionFilePath();
|
||||
infoLines.push(
|
||||
lines.push(
|
||||
` Title: ${chalk.green(currentSession.title)}`,
|
||||
` ID: ${chalk.gray(currentSession.sessionId)}`,
|
||||
` File: ${chalk.blue(sessionFilePath)}`,
|
||||
);
|
||||
} catch {
|
||||
infoLines.push(` ${chalk.red("Session not available")}`);
|
||||
lines.push(` ${chalk.red("Session not available")}`);
|
||||
}
|
||||
|
||||
// Runtime diagnostic info
|
||||
return lines;
|
||||
}
|
||||
|
||||
function getUsageInfo(): string[] {
|
||||
const lines: string[] = ["", chalk.white("Usage:")];
|
||||
|
||||
try {
|
||||
const usage = getSessionUsage();
|
||||
if (usage.totalCost > 0) {
|
||||
lines.push(
|
||||
` Total Cost: ${chalk.green(`$${usage.totalCost.toFixed(6)}`)}`,
|
||||
);
|
||||
lines.push(
|
||||
` Input Tokens: ${chalk.cyan(usage.promptTokens.toLocaleString())}`,
|
||||
);
|
||||
lines.push(
|
||||
` Output Tokens: ${chalk.cyan(usage.completionTokens.toLocaleString())}`,
|
||||
);
|
||||
|
||||
if (usage.promptTokensDetails?.cachedTokens) {
|
||||
lines.push(
|
||||
` Cache Read Tokens: ${chalk.cyan(usage.promptTokensDetails.cachedTokens.toLocaleString())}`,
|
||||
);
|
||||
}
|
||||
|
||||
if (usage.promptTokensDetails?.cacheWriteTokens) {
|
||||
lines.push(
|
||||
` Cache Write Tokens: ${chalk.cyan(usage.promptTokensDetails.cacheWriteTokens.toLocaleString())}`,
|
||||
);
|
||||
}
|
||||
|
||||
const totalTokens = usage.promptTokens + usage.completionTokens;
|
||||
lines.push(` Total Tokens: ${chalk.cyan(totalTokens.toLocaleString())}`);
|
||||
} else {
|
||||
lines.push(` ${chalk.gray("No usage data yet")}`);
|
||||
}
|
||||
} catch (error) {
|
||||
logger.warn("Failed to get session usage:", error);
|
||||
lines.push(` ${chalk.red("Usage data not available")}`);
|
||||
}
|
||||
|
||||
return lines;
|
||||
}
|
||||
|
||||
function getDiagnosticInfo(): string[] {
|
||||
const nodePath = process.execPath;
|
||||
const invokedPath = process.argv[1];
|
||||
|
||||
|
|
@ -111,20 +153,27 @@ export async function handleInfoSlashCommand() {
|
|||
stdio: ["pipe", "pipe", "ignore"],
|
||||
}).trim();
|
||||
} catch (error) {
|
||||
// If npm command fails, fallback to "unknown"
|
||||
logger.warn("Failed to get npm version:", error);
|
||||
}
|
||||
|
||||
// Diagnostic info
|
||||
infoLines.push(
|
||||
return [
|
||||
"",
|
||||
chalk.white("Diagnostic Info"),
|
||||
` Currently running: npm-global (${chalk.green(npmVersion)})`,
|
||||
` Path: ${chalk.blue(nodePath)}`,
|
||||
` Invoked: ${chalk.blue(invokedPath)}`,
|
||||
);
|
||||
];
|
||||
}
|
||||
|
||||
// TODO add global settings like auto update etc.
|
||||
export async function handleInfoSlashCommand() {
|
||||
const infoLines = [
|
||||
...getVersionInfo(),
|
||||
...(await getAuthInfo()),
|
||||
...getConfigInfo(),
|
||||
...getSessionInfo(),
|
||||
...getUsageInfo(),
|
||||
...getDiagnosticInfo(),
|
||||
];
|
||||
|
||||
return {
|
||||
exit: false,
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue