fix: guard channel-tools listActions against plugin crashes
Wraps plugin.actions.listActions() in a try/catch so a single broken channel plugin cannot crash the entire agent boot sequence. Errors are logged once per plugin+message (deduped) via defaultRuntime.error() and the call gracefully returns an empty array instead of propagating the exception. Fixes: 'Cannot read properties of undefined (reading listActions)' after the clawdbot→moltbot rename left some plugin state undefined.
This commit is contained in:
@@ -1,8 +1,13 @@
|
||||
import { getChannelDock } from "../channels/dock.js";
|
||||
import { getChannelPlugin, listChannelPlugins } from "../channels/plugins/index.js";
|
||||
import { normalizeAnyChannelId } from "../channels/registry.js";
|
||||
import type { ChannelAgentTool, ChannelMessageActionName } from "../channels/plugins/types.js";
|
||||
import type {
|
||||
ChannelAgentTool,
|
||||
ChannelMessageActionName,
|
||||
ChannelPlugin,
|
||||
} from "../channels/plugins/types.js";
|
||||
import type { MoltbotConfig } from "../config/config.js";
|
||||
import { defaultRuntime } from "../runtime.js";
|
||||
|
||||
/**
|
||||
* Get the list of supported message actions for a specific channel.
|
||||
@@ -16,7 +21,7 @@ export function listChannelSupportedActions(params: {
|
||||
const plugin = getChannelPlugin(params.channel as Parameters<typeof getChannelPlugin>[0]);
|
||||
if (!plugin?.actions?.listActions) return [];
|
||||
const cfg = params.cfg ?? ({} as MoltbotConfig);
|
||||
return plugin.actions.listActions({ cfg });
|
||||
return runPluginListActions(plugin, cfg);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -29,7 +34,7 @@ export function listAllChannelSupportedActions(params: {
|
||||
for (const plugin of listChannelPlugins()) {
|
||||
if (!plugin.actions?.listActions) continue;
|
||||
const cfg = params.cfg ?? ({} as MoltbotConfig);
|
||||
const channelActions = plugin.actions.listActions({ cfg });
|
||||
const channelActions = runPluginListActions(plugin, cfg);
|
||||
for (const action of channelActions) {
|
||||
actions.add(action);
|
||||
}
|
||||
@@ -64,3 +69,35 @@ export function resolveChannelMessageToolHints(params: {
|
||||
.map((entry) => entry.trim())
|
||||
.filter(Boolean);
|
||||
}
|
||||
|
||||
const loggedListActionErrors = new Set<string>();
|
||||
|
||||
function runPluginListActions(
|
||||
plugin: ChannelPlugin,
|
||||
cfg: MoltbotConfig,
|
||||
): ChannelMessageActionName[] {
|
||||
if (!plugin.actions?.listActions) return [];
|
||||
try {
|
||||
const listed = plugin.actions.listActions({ cfg });
|
||||
return Array.isArray(listed) ? listed : [];
|
||||
} catch (err) {
|
||||
logListActionsError(plugin.id, err);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
function logListActionsError(pluginId: string, err: unknown) {
|
||||
const message = err instanceof Error ? err.message : String(err);
|
||||
const key = `${pluginId}:${message}`;
|
||||
if (loggedListActionErrors.has(key)) return;
|
||||
loggedListActionErrors.add(key);
|
||||
const stack = err instanceof Error && err.stack ? err.stack : null;
|
||||
const details = stack ?? message;
|
||||
defaultRuntime.error?.(`[channel-tools] ${pluginId}.actions.listActions failed: ${details}`);
|
||||
}
|
||||
|
||||
export const __testing = {
|
||||
resetLoggedListActionErrors() {
|
||||
loggedListActionErrors.clear();
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user