feat: suppress console.info in headless
This commit is contained in:
parent
fcf0dd2a33
commit
8177eef6a0
8 changed files with 139 additions and 40 deletions
|
|
@ -6,7 +6,7 @@ import { isAuthenticated, login } from "./workos.js";
|
|||
* Returns true if authentication is successful, false otherwise
|
||||
*/
|
||||
export async function ensureAuthenticated(
|
||||
requireAuth: boolean = true,
|
||||
requireAuth: boolean = true
|
||||
): Promise<boolean> {
|
||||
if (isAuthenticated()) {
|
||||
return true;
|
||||
|
|
@ -16,7 +16,7 @@ export async function ensureAuthenticated(
|
|||
return false;
|
||||
}
|
||||
|
||||
console.log(chalk.yellow("Authentication required."));
|
||||
console.info(chalk.yellow("Authentication required."));
|
||||
|
||||
try {
|
||||
await login();
|
||||
|
|
|
|||
|
|
@ -170,7 +170,7 @@ export async function login(
|
|||
): Promise<AuthConfig> {
|
||||
// If CONTINUE_API_KEY environment variable exists, use that instead
|
||||
if (process.env.CONTINUE_API_KEY) {
|
||||
console.log(
|
||||
console.info(
|
||||
chalk.green("Using CONTINUE_API_KEY from environment variables")
|
||||
);
|
||||
return {
|
||||
|
|
@ -179,24 +179,24 @@ export async function login(
|
|||
}
|
||||
|
||||
try {
|
||||
console.log(chalk.cyan("\nStarting authentication with Continue..."));
|
||||
console.info(chalk.cyan("\nStarting authentication with Continue..."));
|
||||
|
||||
// Get auth URL using the direct implementation
|
||||
const authUrl = getAuthUrlForTokenPage(useOnboarding);
|
||||
console.log(chalk.green(`Opening browser to sign in at: ${authUrl}`));
|
||||
console.info(chalk.green(`Opening browser to sign in at: ${authUrl}`));
|
||||
await open(authUrl);
|
||||
|
||||
console.log(chalk.yellow("\nAfter signing in, you'll receive a token."));
|
||||
console.info(chalk.yellow("\nAfter signing in, you'll receive a token."));
|
||||
|
||||
// Get token from user
|
||||
const token = await prompt(chalk.yellow("Paste your sign-in token here: "));
|
||||
|
||||
console.log(chalk.cyan("Verifying token..."));
|
||||
console.info(chalk.cyan("Verifying token..."));
|
||||
|
||||
// Exchange token for session
|
||||
const response = await refreshToken(token);
|
||||
|
||||
console.log(chalk.green("\nAuthentication successful!"));
|
||||
console.info(chalk.green("\nAuthentication successful!"));
|
||||
|
||||
return response;
|
||||
} catch (error: any) {
|
||||
|
|
@ -213,7 +213,7 @@ export async function login(
|
|||
*/
|
||||
export function logout(): void {
|
||||
if (process.env.CONTINUE_API_KEY) {
|
||||
console.log(
|
||||
console.info(
|
||||
chalk.yellow(
|
||||
"Using CONTINUE_API_KEY from environment variables, nothing to log out"
|
||||
)
|
||||
|
|
@ -223,8 +223,8 @@ export function logout(): void {
|
|||
|
||||
if (fs.existsSync(AUTH_CONFIG_PATH)) {
|
||||
fs.unlinkSync(AUTH_CONFIG_PATH);
|
||||
console.log(chalk.green("Successfully logged out"));
|
||||
console.info(chalk.green("Successfully logged out"));
|
||||
} else {
|
||||
console.log(chalk.yellow("No active session found"));
|
||||
console.info(chalk.yellow("No active session found"));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
11
src/index.ts
11
src/index.ts
|
|
@ -11,12 +11,16 @@ import { introMessage } from "./intro.js";
|
|||
import { MCPService } from "./mcp.js";
|
||||
import { handleSlashCommands } from "./slashCommands.js";
|
||||
import { streamChatResponse } from "./streamChatResponse.js";
|
||||
import { configureLogger } from "./logger.js";
|
||||
import * as fs from "fs";
|
||||
import { ContinueClient } from "@continuedev/sdk";
|
||||
|
||||
// Parse command line arguments
|
||||
const args = parseArgs();
|
||||
|
||||
// Configure logger based on headless mode
|
||||
configureLogger(args.isHeadless);
|
||||
|
||||
async function chat() {
|
||||
const isAuthenticated = await ensureAuthenticated(true);
|
||||
|
||||
|
|
@ -36,7 +40,7 @@ async function chat() {
|
|||
|
||||
// if (fs.existsSync(args.assistantPath)) {
|
||||
// // If it's a file, load it directly
|
||||
// console.log(
|
||||
// console.info(
|
||||
// chalk.yellow(`Loading assistant from file: ${args.assistantPath}`)
|
||||
// );
|
||||
// // We need to extract the assistant slug from the yaml to use with the SDK
|
||||
|
|
@ -111,6 +115,7 @@ async function chat() {
|
|||
break;
|
||||
}
|
||||
|
||||
// Note that `console.log` is shown in headless mode, `console.info` is not
|
||||
console.log(`\n${chalk.italic.gray(commandResult.output ?? "")}`);
|
||||
|
||||
if (commandResult.newInput) {
|
||||
|
|
@ -125,14 +130,14 @@ async function chat() {
|
|||
|
||||
// Get AI response with potential tool usage
|
||||
if (!args.isHeadless) {
|
||||
console.log(`\n${chalk.bold.blue("Assistant:")}`);
|
||||
console.info(`\n${chalk.bold.blue("Assistant:")}`);
|
||||
}
|
||||
|
||||
try {
|
||||
await streamChatResponse(chatHistory, assistant!, client!);
|
||||
} catch (e: any) {
|
||||
console.error(`\n${chalk.red(`Error: ${e.message}`)}`);
|
||||
console.log(
|
||||
console.info(
|
||||
chalk.dim(`Chat history:\n${JSON.stringify(chatHistory, null, 2)}`)
|
||||
);
|
||||
}
|
||||
|
|
|
|||
44
src/intro.ts
44
src/intro.ts
|
|
@ -20,49 +20,49 @@ export function introMessage(assistant: Assistant, mcpService: MCPService) {
|
|||
const mcpTools = mcpService.getTools() ?? [];
|
||||
const mcpPrompts = mcpService.getPrompts() ?? [];
|
||||
|
||||
console.log(chalk.cyan(CONTINUE_ASCII_ART));
|
||||
console.info(chalk.cyan(CONTINUE_ASCII_ART));
|
||||
|
||||
console.log(`\n${chalk.bold.blue(`Assistant: ${assistantConfig.name}`)}`);
|
||||
console.log(`${chalk.blue(`Model: ${assistant.getModel()}`)}\n`);
|
||||
console.info(`\n${chalk.bold.blue(`Assistant: ${assistantConfig.name}`)}`);
|
||||
console.info(`${chalk.blue(`Model: ${assistant.getModel()}`)}\n`);
|
||||
|
||||
console.log(chalk.yellow("Available tools:"));
|
||||
console.info(chalk.yellow("Available tools:"));
|
||||
getAllTools().forEach((tool) => {
|
||||
console.log(
|
||||
console.info(
|
||||
`- ${chalk.green(tool.function.name)}: ${tool.function.description ?? ""}`
|
||||
);
|
||||
});
|
||||
mcpTools.forEach((tool) => {
|
||||
console.log(`- ${chalk.green(tool.name)}: ${tool.description}`);
|
||||
console.info(`- ${chalk.green(tool.name)}: ${tool.description}`);
|
||||
});
|
||||
console.log("");
|
||||
console.info("");
|
||||
|
||||
console.log(chalk.yellow("\nAvailable slash commands:"));
|
||||
console.log("- /exit: Exit the chat");
|
||||
console.log("- /clear: Clear the chat history");
|
||||
console.log("- /help: Show help message");
|
||||
console.log("- /models: Show available models");
|
||||
console.log("- /login: Authenticate with your account");
|
||||
console.log("- /logout: Sign out of your current session");
|
||||
console.log("- /whoami: Check who you're currently logged in as");
|
||||
console.info(chalk.yellow("\nAvailable slash commands:"));
|
||||
console.info("- /exit: Exit the chat");
|
||||
console.info("- /clear: Clear the chat history");
|
||||
console.info("- /help: Show help message");
|
||||
console.info("- /models: Show available models");
|
||||
console.info("- /login: Authenticate with your account");
|
||||
console.info("- /logout: Sign out of your current session");
|
||||
console.info("- /whoami: Check who you're currently logged in as");
|
||||
for (const prompt of assistantConfig.prompts ?? []) {
|
||||
console.log(`- /${prompt?.name}: ${prompt?.description}`);
|
||||
console.info(`- /${prompt?.name}: ${prompt?.description}`);
|
||||
}
|
||||
for (const prompt of mcpPrompts) {
|
||||
console.log(`- /${prompt.name}: ${prompt.description}`);
|
||||
console.info(`- /${prompt.name}: ${prompt.description}`);
|
||||
}
|
||||
console.log("");
|
||||
console.info("");
|
||||
|
||||
if (assistantConfig.rules?.length) {
|
||||
console.log(
|
||||
console.info(
|
||||
chalk.yellow("\nAssistant rules: " + assistantConfig.rules.length)
|
||||
);
|
||||
}
|
||||
|
||||
if (assistantConfig.mcpServers?.length) {
|
||||
console.log(chalk.yellow("\nMCP Servers:"));
|
||||
console.info(chalk.yellow("\nMCP Servers:"));
|
||||
assistantConfig.mcpServers.forEach((server: any) => {
|
||||
console.log(`- ${chalk.cyan(server?.name)}`);
|
||||
console.info(`- ${chalk.cyan(server?.name)}`);
|
||||
});
|
||||
}
|
||||
console.log("");
|
||||
console.info("");
|
||||
}
|
||||
|
|
|
|||
20
src/logger.ts
Normal file
20
src/logger.ts
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
/**
|
||||
* Configures the logger based on the application mode (headless or interactive)
|
||||
* When in headless mode, console.info is suppressed to avoid verbose output
|
||||
*/
|
||||
export function configureLogger(isHeadless: boolean): void {
|
||||
if (isHeadless) {
|
||||
// Save the original console.info function
|
||||
const originalConsoleInfo = console.info;
|
||||
|
||||
// Override console.info to be a no-op in headless mode
|
||||
console.info = function() {
|
||||
// Do nothing in headless mode
|
||||
};
|
||||
|
||||
// Add a method to restore original behavior if needed
|
||||
(console as any).restoreInfo = function() {
|
||||
console.info = originalConsoleInfo;
|
||||
};
|
||||
}
|
||||
}
|
||||
74
src/logging.ts
Normal file
74
src/logging.ts
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
import chalk from "chalk";
|
||||
|
||||
// Track if we're in headless mode (default to false)
|
||||
let isHeadlessMode = false;
|
||||
|
||||
/**
|
||||
* Configure the logger to enable/disable headless mode
|
||||
*/
|
||||
export function configureLogger(options: { headless: boolean }) {
|
||||
isHeadlessMode = options.headless;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if logging is currently enabled
|
||||
* This will return false if in headless mode unless specifically exempted
|
||||
*/
|
||||
export function isLoggingEnabled(forceEnable = false): boolean {
|
||||
return !isHeadlessMode || forceEnable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper for console.info that respects headless mode
|
||||
* If forceLog is true, it will log even in headless mode
|
||||
*/
|
||||
export function log(message: any, forceLog = false): void {
|
||||
if (isLoggingEnabled(forceLog)) {
|
||||
console.info(message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper for console.error that respects headless mode
|
||||
* Errors are generally important, so we default forceLog to true
|
||||
*/
|
||||
export function error(message: any, forceLog = true): void {
|
||||
if (isLoggingEnabled(forceLog)) {
|
||||
console.error(message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper for console.info that respects headless mode
|
||||
*/
|
||||
export function info(message: any, forceLog = false): void {
|
||||
if (isLoggingEnabled(forceLog)) {
|
||||
console.info(message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper for console.warn that respects headless mode
|
||||
*/
|
||||
export function warn(message: any, forceLog = false): void {
|
||||
if (isLoggingEnabled(forceLog)) {
|
||||
console.warn(message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Colored logging helpers that respect headless mode
|
||||
*/
|
||||
export const loggers = {
|
||||
success: (message: string, forceLog = false) =>
|
||||
log(chalk.green(message), forceLog),
|
||||
info: (message: string, forceLog = false) =>
|
||||
log(chalk.blue(message), forceLog),
|
||||
warning: (message: string, forceLog = false) =>
|
||||
log(chalk.yellow(message), forceLog),
|
||||
error: (message: string, forceLog = true) =>
|
||||
error(chalk.red(message), forceLog),
|
||||
debug: (message: string, forceLog = false) =>
|
||||
log(chalk.gray(message), forceLog),
|
||||
command: (message: string) => log(message, true), // Always show command outputs
|
||||
};
|
||||
|
|
@ -44,7 +44,7 @@ export function handleSlashCommands(
|
|||
case "login":
|
||||
login()
|
||||
.then((config) => {
|
||||
console.log(
|
||||
console.info(
|
||||
chalk.green(`\nLogged in as ${config.userEmail || config.userId}`)
|
||||
);
|
||||
})
|
||||
|
|
|
|||
|
|
@ -148,7 +148,7 @@ export async function streamChatResponse(
|
|||
}
|
||||
}
|
||||
|
||||
console.log(); // Add a newline after the response
|
||||
console.info(); // Add a newline after the response
|
||||
|
||||
// Add the tool calls to the chat history
|
||||
if (currentToolCalls.length > 0) {
|
||||
|
|
@ -186,7 +186,7 @@ export async function streamChatResponse(
|
|||
content: toolResult,
|
||||
});
|
||||
|
||||
console.log(chalk.green(toolResult) + "\n");
|
||||
console.info(chalk.green(toolResult) + "\n");
|
||||
} catch (error) {
|
||||
const errorMessage = `Error executing tool ${toolCall.name}: ${
|
||||
error instanceof Error ? error.message : String(error)
|
||||
|
|
@ -196,7 +196,7 @@ export async function streamChatResponse(
|
|||
tool_call_id: toolCall.id,
|
||||
content: errorMessage,
|
||||
});
|
||||
console.log(
|
||||
console.info(
|
||||
`${chalk.red("[Tool error:")} ${chalk.red(errorMessage)}${chalk.red(
|
||||
")"
|
||||
)}`
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue