feat: handle environment.json file in cn serve (#7984)

* feat: handle environment.json file in cn serve

* fix: address feedback
This commit is contained in:
Nate Sesti 2025-09-25 17:12:55 -07:00 • committed by GitHub
parent b201072e1b
commit 1ac8d45f9e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 129 additions and 0 deletions

View file

@ -3,6 +3,7 @@ import type { ChatHistoryItem } from "core/index.js";
import express, { Request, Response } from "express";
import { getAccessToken, getAssistantSlug } from "../auth/workos.js";
import { runEnvironmentInstallSafe } from "../environment/environmentHandler.js";
import { processCommandFlags } from "../flags/flagProcessor.js";
import { toolPermissionManager } from "../permissions/permissionManager.js";
import {
@ -55,6 +56,8 @@ export async function serve(prompt?: string, options: ServeOptions = {}) {
const timeoutMs = timeoutSeconds * 1000;
const port = parseInt(options.port || "8000", 10);
// Environment install script will be deferred until after server startup to avoid blocking
// Initialize services with tool permission overrides
const { permissionOverrides } = processCommandFlags(options);
@ -348,6 +351,9 @@ export async function serve(prompt?: string, options: ServeOptions = {}) {
),
);
// Run environment install script after server startup
runEnvironmentInstallSafe();
// If initial prompt provided, queue it for processing
if (actualPrompt) {
console.log(chalk.dim("\nProcessing initial prompt..."));

View file

@ -0,0 +1,123 @@
import { exec } from "child_process";
import { existsSync, readFileSync } from "fs";
import path from "path";
import { promisify } from "util";
import chalk from "chalk";
const execAsync = promisify(exec);
import { formatError } from "../util/formatError.js";
import { logger } from "../util/logger.js";
interface EnvironmentConfig {
install?: string;
}
const ENVIRONMENT_FILE_NAME = "environment.json";
const ENVIRONMENT_SEARCH_PATHS = [".continue"];
/**
* Finds and reads the environment.json file
*/
function findEnvironmentFile(): EnvironmentConfig | null {
for (const searchPath of ENVIRONMENT_SEARCH_PATHS) {
const environmentPath = path.join(
process.cwd(),
searchPath,
ENVIRONMENT_FILE_NAME,
);
if (existsSync(environmentPath)) {
try {
const content = readFileSync(environmentPath, "utf-8");
const config = JSON.parse(content) as EnvironmentConfig;
logger.debug(`Found environment.json at: ${environmentPath}`);
return config;
} catch (error) {
logger.error(
`Failed to parse environment.json at ${environmentPath}: ${formatError(error)}`,
);
return null;
}
}
}
logger.debug("No environment.json file found");
return null;
}
/**
* Runs the install script from environment.json if present
* Now runs asynchronously to avoid blocking server startup
*/
export async function runEnvironmentInstall(): Promise<void> {
const environmentConfig = findEnvironmentFile();
if (!environmentConfig || !environmentConfig.install) {
logger.debug("No install script found in environment.json, skipping...");
return;
}
const installScript = environmentConfig.install;
logger.debug(
chalk.blue(
`\nRunning environment install script: ${chalk.dim(installScript)}`,
),
);
try {
await execAsync(installScript, {
cwd: process.cwd(),
encoding: "utf-8",
});
logger.debug(
chalk.green("✓ Environment install script completed successfully"),
);
} catch (error) {
logger.error(`Environment install script failed: ${formatError(error)}`);
throw new Error(
`Failed to run environment install script: ${formatError(error)}`,
);
}
}
/**
* Runs the install script from environment.json if present, with error handling
* This version doesn't throw errors to avoid blocking server startup
*/
export async function runEnvironmentInstallSafe(): Promise<void> {
const environmentConfig = findEnvironmentFile();
if (!environmentConfig || !environmentConfig.install) {
logger.debug("No environment install script to run");
return;
}
logger.debug(
chalk.blue("Running environment install script in background..."),
);
try {
await runEnvironmentInstall();
logger.debug(
chalk.green("✓ Environment install script completed successfully"),
);
} catch (error) {
logger.error(
chalk.yellow("Warning: Environment install script failed:"),
formatError(error),
);
logger.error(
chalk.dim("Server will continue to run without the install script."),
);
}
}
/**
* Gets the environment configuration if available
*/
export function getEnvironmentConfig(): EnvironmentConfig | null {
return findEnvironmentFile();
}