Skip to content
Open
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
27 changes: 26 additions & 1 deletion packages/cli/src/utils/nonInteractiveHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,33 @@ export async function buildSystemMessage(
sessionId: string,
permissionMode: PermissionMode,
): Promise<CLISystemMessage> {
// Ensure ToolRegistry is initialized before accessing it
// In headless mode, config.initialize() should have been called before this,
// but we need to handle the case where it hasn't been called yet
const toolRegistry = config.getToolRegistry();
const tools = toolRegistry ? toolRegistry.getAllToolNames() : [];
if (!toolRegistry) {
// Log a warning and return empty tools list instead of throwing
// This can happen if config.initialize() hasn't been called yet
if (config.getDebugMode()) {
console.warn(
'[buildSystemMessage] ToolRegistry is not initialized. Tools will not be available until config.initialize() is called.',
);
}
return {
type: 'system',
subtype: 'init',
uuid: '',
session_id: sessionId,
model: config.getModel(),
permission_mode: permissionMode,
tools: [],
mcp_servers: [],
slash_commands: [],
subagents: [],
timestamp: new Date().toISOString(),
};
}
const tools = toolRegistry.getAllToolNames();

const mcpServers = config.getMcpServers();
const mcpServerList = mcpServers
Expand Down
11 changes: 11 additions & 0 deletions packages/core/src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1161,13 +1161,24 @@ export class Config {
if (isToolEnabled(toolName, coreToolsConfig, excludeToolsConfig)) {
try {
registry.registerTool(new ToolClass(...args));
if (this.debugMode) {
console.debug(
`[Config] Registered tool: ${className} (${toolName})`,
);
}
} catch (error) {
console.error(
`[Config] Failed to register tool ${className} (${toolName}):`,
error,
);
throw error; // Re-throw after logging context
}
} else {
if (this.debugMode) {
console.debug(
`[Config] Tool ${className} (${toolName}) is disabled by configuration`,
);
}
}
};

Expand Down
Loading