Skip to content

Commit ddad641

Browse files
Load env vars from a node hook so they're loaded before the logging and tracing init
1 parent 64052bd commit ddad641

35 files changed

+414
-185
lines changed

src/agent/completionHandlerRegistry.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import { ConsoleCompletedHandler } from '#agent/autonomous/agentCompletion';
2-
import { SlackChatBotService } from '#modules/slack/slackChatBotService';
32
import { logger } from '#o11y/logger';
43
import { GitLabNoteCompletedHandler } from '#routes/webhooks/gitlab/gitlabNoteHandler';
54
import type { AgentCompleted } from '#shared/agent/agent.model';
65

76
// Use a Map for easier addition/removal during tests
87
let handlersMap = new Map<string, new () => AgentCompleted>();
98

10-
// Initialize with default handlers
11-
handlersMap.set(new ConsoleCompletedHandler().agentCompletedHandlerId(), ConsoleCompletedHandler);
12-
handlersMap.set(new SlackChatBotService().agentCompletedHandlerId(), SlackChatBotService);
13-
handlersMap.set(new GitLabNoteCompletedHandler().agentCompletedHandlerId(), GitLabNoteCompletedHandler);
9+
function initHandlers() {
10+
// Initialize with default handlers
11+
handlersMap.set(new ConsoleCompletedHandler().agentCompletedHandlerId(), ConsoleCompletedHandler);
12+
handlersMap.set(new GitLabNoteCompletedHandler().agentCompletedHandlerId(), GitLabNoteCompletedHandler);
13+
}
1414

1515
/**
1616
* Return the AgentCompleted callback object from its id.
@@ -20,6 +20,8 @@ handlersMap.set(new GitLabNoteCompletedHandler().agentCompletedHandlerId(), GitL
2020
export function getCompletedHandler(handlerId: string): AgentCompleted | null {
2121
if (!handlerId) return null;
2222

23+
if (handlersMap.size === 0) initHandlers();
24+
2325
const HandlerCtor = handlersMap.get(handlerId);
2426
if (HandlerCtor) return new HandlerCtor();
2527

@@ -47,5 +49,4 @@ export function clearCompletedHandlers(): void {
4749
handlersMap = new Map<string, new () => AgentCompleted>();
4850
// Re-initialize with default handlers
4951
handlersMap.set(new ConsoleCompletedHandler().agentCompletedHandlerId(), ConsoleCompletedHandler);
50-
handlersMap.set(new SlackChatBotService().agentCompletedHandlerId(), SlackChatBotService);
5152
}

src/cli/agent.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,9 @@ import { logger } from '#o11y/logger';
1414
import type { AgentContext } from '#shared/agent/agent.model';
1515
import { registerErrorHandlers } from '../errorHandlers';
1616
import { parseProcessArgs, saveAgentId } from './cli';
17-
import { loadCliEnvironment } from './envLoader';
1817
import { resolveFunctionClasses } from './functionAliases';
1918

2019
export async function main(): Promise<void> {
21-
loadCliEnvironment();
2220
registerErrorHandlers();
2321
await initApplicationContext();
2422
const llms = defaultLLMs();

src/cli/chat.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@ import { getMarkdownFormatPrompt } from '#routes/chat/chatPromptUtils';
99
import { LLM, LlmMessage, UserContentExt, contentText, messageText, user } from '#shared/llm/llm.model';
1010
import { currentUser } from '#user/userContext';
1111
import { parseProcessArgs, saveAgentId } from './cli';
12-
import { loadCliEnvironment } from './envLoader';
1312
import { LLM_CLI_ALIAS } from './llmAliases';
1413

1514
async function main() {
16-
loadCliEnvironment();
1715
await initApplicationContext();
1816

1917
const { initialPrompt: rawPrompt, resumeAgentId, flags } = parseProcessArgs();

src/cli/code.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,9 @@ import { contentText, messageText } from '#shared/llm/llm.model';
1111
import { CodeEditingAgent } from '#swe/codeEditingAgent';
1212
import { beep } from '#utils/beep';
1313
import { parseProcessArgs, saveAgentId } from './cli';
14-
import { loadCliEnvironment } from './envLoader';
1514
import { parsePromptWithImages } from './promptParser';
1615

1716
async function main() {
18-
loadCliEnvironment();
1917
await initApplicationContext();
2018
const agentLlms: AgentLLMs = defaultLLMs();
2119

src/cli/codeAgent.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import { CodeFunctions } from '#swe/codeFunctions';
2121
import { MorphEditor } from '#swe/morph/morphEditor';
2222
import { registerErrorHandlers } from '../errorHandlers';
2323
import { parseProcessArgs, saveAgentId } from './cli';
24-
import { loadCliEnvironment } from './envLoader';
2524
import { resolveFunctionClasses } from './functionAliases';
2625

2726
async function resumeAgent(resumeAgentId: string, initialPrompt: string) {
@@ -44,7 +43,6 @@ async function resumeAgent(resumeAgentId: string, initialPrompt: string) {
4443
}
4544

4645
export async function main(): Promise<void> {
47-
loadCliEnvironment();
4846
registerErrorHandlers();
4947
await initApplicationContext();
5048
const llms = defaultLLMs();

src/cli/commit.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@ import { shutdownTrace } from '#fastify/trace-init/trace-init';
55
import { Git } from '#functions/scm/git';
66
import { FileSystemRead } from '#functions/storage/fileSystemRead';
77
import { defaultLLMs } from '#llm/services/defaultLlms';
8-
import { loadCliEnvironment } from './envLoader';
98

109
async function main() {
11-
loadCliEnvironment();
1210
await initApplicationContext();
1311
console.log('Commit command starting...');
1412

src/cli/debate.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,9 @@ import { logger } from '#o11y/logger';
1212
import type { AgentLLMs } from '#shared/agent/agent.model';
1313
import { messageText } from '#shared/llm/llm.model';
1414
import { parseProcessArgs } from './cli';
15-
import { loadCliEnvironment } from './envLoader';
1615
import { parsePromptWithImages } from './promptParser';
1716

1817
async function main() {
19-
loadCliEnvironment();
2018
await initApplicationContext();
2119
const agentLLMs: AgentLLMs = defaultLLMs();
2220
const { initialPrompt: rawPrompt, resumeAgentId, flags } = parseProcessArgs();

src/cli/easy.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,12 @@ import { mockLLMs } from '#llm/services/mock-llm';
99
import { vertexGemini_2_5_Flash } from '#llm/services/vertexai';
1010
import type { AgentContext } from '#shared/agent/agent.model';
1111
import { parseProcessArgs } from './cli';
12-
import { loadCliEnvironment } from './envLoader';
1312

1413
// See https://arxiv.org/html/2405.19616v1 https://github.com/autogenai/easy-problems-that-llms-get-wrong
1514
// Usage:
1615
// npm run easy
1716

1817
async function main() {
19-
loadCliEnvironment();
2018
await initApplicationContext();
2119

2220
const context: AgentContext = createContext({

src/cli/envLoader.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44
* When using git worktrees enables using the local.env from the main repository
55
* Extracted from startLocal.ts to be shared across all CLI tools.
66
*/
7-
87
import { existsSync, readFileSync } from 'node:fs';
98
import { isAbsolute, resolve } from 'node:path';
10-
import { logger } from '#o11y/logger';
119

1210
interface ResolveEnvFileOptions {
1311
envFile?: string | null;
@@ -21,6 +19,8 @@ interface ApplyEnvOptions {
2119

2220
type ParsedEnv = Record<string, string>;
2321

22+
export let loadedEnvFilePath: string | undefined;
23+
2424
/**
2525
* Builds an absolute path from a potential relative path.
2626
* @param value The path value (can be null or undefined).
@@ -125,9 +125,12 @@ export function applyEnvFile(filePath: string, options: ApplyEnvOptions = {}): v
125125
export function loadCliEnvironment(options: ApplyEnvOptions = {}): void {
126126
try {
127127
const envFilePath = resolveEnvFilePath();
128+
loadedEnvFilePath = envFilePath;
128129
applyEnvFile(envFilePath, options);
129-
logger.debug(`Loaded environment from ${envFilePath}`);
130+
console.log(`Loaded environment from ${envFilePath}`);
130131
} catch (err) {
131-
logger.debug(err, 'No environment file found; continuing with existing process.env');
132+
console.log(err, 'No environment file found; continuing with existing process.env');
132133
}
133134
}
135+
136+
loadCliEnvironment();

src/cli/export.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,13 @@ import micromatch from 'micromatch';
77
import { FileSystemService } from '#functions/storage/fileSystemService';
88
import { countTokens } from '#llm/tokens';
99
import { logger } from '#o11y/logger';
10-
import { loadCliEnvironment } from './envLoader';
1110

1211
/**
1312
* If there are no arguments then only write the exported contents to the console
1413
* If there is the -v arg then write the additional debug info
1514
* If there is the -f arg write it to a file. Default to export.xml. If a value is provided, e.g. -f=export2.xml then write to export2.xml
1615
*/
1716
async function main() {
18-
loadCliEnvironment();
1917
const fileSystemService = new FileSystemService();
2018
const basePath = fileSystemService.getBasePath();
2119

0 commit comments

Comments
 (0)