fix(hooks): replace console logging with proper subsystem logging in loader (openclaw#11029) thanks @shadril238
Verified: - pnpm build - pnpm check - pnpm test Co-authored-by: shadril238 <63901551+shadril238@users.noreply.github.com> Co-authored-by: Tak Hoffman <781889+Takhoffman@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
05524bb5ef
commit
1c928e493d
@@ -165,6 +165,7 @@ Docs: https://docs.openclaw.ai
|
||||
- Agents: keep followup-runner session `totalTokens` aligned with post-compaction context by using last-call usage and shared token-accounting logic. (#14979) Thanks @shtse8.
|
||||
- Hooks/Plugins: wire 9 previously unwired plugin lifecycle hooks into core runtime paths (session, compaction, gateway, and outbound message hooks). (#14882) Thanks @shtse8.
|
||||
- Hooks/Tools: dispatch `before_tool_call` and `after_tool_call` hooks from both tool execution paths with rebased conflict fixes. (#15012) Thanks @Patrick-Barletta, @Takhoffman.
|
||||
- Hooks: replace loader `console.*` output with subsystem logger messages so hook loading errors/warnings route through standard logging. (#11029) Thanks @shadril238.
|
||||
- Discord: allow channel-edit to archive/lock threads and set auto-archive duration. (#5542) Thanks @stumct.
|
||||
- Discord tests: use a partial @buape/carbon mock in slash command coverage. (#13262) Thanks @arosstale.
|
||||
- Tests: update thread ID handling in Slack message collection tests. (#14108) Thanks @swizzmagik.
|
||||
|
||||
@@ -5,6 +5,7 @@ import type { ModelRow } from "./list.types.js";
|
||||
import { ensureAuthProfileStore } from "../../agents/auth-profiles.js";
|
||||
import { resolveForwardCompatModel } from "../../agents/model-forward-compat.js";
|
||||
import { parseModelRef } from "../../agents/model-selection.js";
|
||||
import { resolveModel } from "../../agents/pi-embedded-runner/model.js";
|
||||
import { loadConfig } from "../../config/config.js";
|
||||
import { resolveConfiguredEntries } from "./list.configured.js";
|
||||
import { formatErrorWithStack } from "./list.errors.js";
|
||||
@@ -109,6 +110,9 @@ export async function modelsListCommand(
|
||||
modelByKey.set(entry.key, forwardCompat);
|
||||
}
|
||||
}
|
||||
if (!model) {
|
||||
model = resolveModel(entry.ref.provider, entry.ref.model, undefined, cfg).model;
|
||||
}
|
||||
if (opts.local && model && !isLocalBaseUrl(model.baseUrl)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import fs from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
||||
import type { OpenClawConfig } from "../config/config.js";
|
||||
import {
|
||||
clearInternalHooks,
|
||||
@@ -151,8 +151,6 @@ describe("loader", () => {
|
||||
});
|
||||
|
||||
it("should handle module loading errors gracefully", async () => {
|
||||
const consoleError = vi.spyOn(console, "error").mockImplementation(() => {});
|
||||
|
||||
const cfg: OpenClawConfig = {
|
||||
hooks: {
|
||||
internal: {
|
||||
@@ -167,19 +165,12 @@ describe("loader", () => {
|
||||
},
|
||||
};
|
||||
|
||||
// Should not throw and should return 0 (handler failed to load)
|
||||
const count = await loadInternalHooks(cfg, tmpDir);
|
||||
expect(count).toBe(0);
|
||||
expect(consoleError).toHaveBeenCalledWith(
|
||||
expect.stringContaining("Failed to load hook handler"),
|
||||
expect.any(String),
|
||||
);
|
||||
|
||||
consoleError.mockRestore();
|
||||
});
|
||||
|
||||
it("should handle non-function exports", async () => {
|
||||
const consoleError = vi.spyOn(console, "error").mockImplementation(() => {});
|
||||
|
||||
// Create a module with a non-function export
|
||||
const handlerPath = path.join(tmpDir, "bad-export.js");
|
||||
await fs.writeFile(handlerPath, 'export default "not a function";', "utf-8");
|
||||
@@ -198,11 +189,9 @@ describe("loader", () => {
|
||||
},
|
||||
};
|
||||
|
||||
// Should not throw and should return 0 (handler is not a function)
|
||||
const count = await loadInternalHooks(cfg, tmpDir);
|
||||
expect(count).toBe(0);
|
||||
expect(consoleError).toHaveBeenCalledWith(expect.stringContaining("is not a function"));
|
||||
|
||||
consoleError.mockRestore();
|
||||
});
|
||||
|
||||
it("should handle relative paths", async () => {
|
||||
|
||||
@@ -9,11 +9,14 @@ import path from "node:path";
|
||||
import { pathToFileURL } from "node:url";
|
||||
import type { OpenClawConfig } from "../config/config.js";
|
||||
import type { InternalHookHandler } from "./internal-hooks.js";
|
||||
import { createSubsystemLogger } from "../logging/subsystem.js";
|
||||
import { resolveHookConfig } from "./config.js";
|
||||
import { shouldIncludeHook } from "./config.js";
|
||||
import { registerInternalHook } from "./internal-hooks.js";
|
||||
import { loadWorkspaceHookEntries } from "./workspace.js";
|
||||
|
||||
const log = createSubsystemLogger("hooks:loader");
|
||||
|
||||
/**
|
||||
* Load and register all hook handlers
|
||||
*
|
||||
@@ -78,16 +81,14 @@ export async function loadInternalHooks(
|
||||
const handler = mod[exportName];
|
||||
|
||||
if (typeof handler !== "function") {
|
||||
console.error(
|
||||
`Hook error: Handler '${exportName}' from ${entry.hook.name} is not a function`,
|
||||
);
|
||||
log.error(`Handler '${exportName}' from ${entry.hook.name} is not a function`);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Register for all events listed in metadata
|
||||
const events = entry.metadata?.events ?? [];
|
||||
if (events.length === 0) {
|
||||
console.warn(`Hook warning: Hook '${entry.hook.name}' has no events defined in metadata`);
|
||||
log.warn(`Hook '${entry.hook.name}' has no events defined in metadata`);
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -95,21 +96,19 @@ export async function loadInternalHooks(
|
||||
registerInternalHook(event, handler as InternalHookHandler);
|
||||
}
|
||||
|
||||
console.log(
|
||||
log.info(
|
||||
`Registered hook: ${entry.hook.name} -> ${events.join(", ")}${exportName !== "default" ? ` (export: ${exportName})` : ""}`,
|
||||
);
|
||||
loadedCount++;
|
||||
} catch (err) {
|
||||
console.error(
|
||||
`Failed to load hook ${entry.hook.name}:`,
|
||||
err instanceof Error ? err.message : String(err),
|
||||
log.error(
|
||||
`Failed to load hook ${entry.hook.name}: ${err instanceof Error ? err.message : String(err)}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(
|
||||
"Failed to load directory-based hooks:",
|
||||
err instanceof Error ? err.message : String(err),
|
||||
log.error(
|
||||
`Failed to load directory-based hooks: ${err instanceof Error ? err.message : String(err)}`,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -132,20 +131,18 @@ export async function loadInternalHooks(
|
||||
const handler = mod[exportName];
|
||||
|
||||
if (typeof handler !== "function") {
|
||||
console.error(`Hook error: Handler '${exportName}' from ${modulePath} is not a function`);
|
||||
log.error(`Handler '${exportName}' from ${modulePath} is not a function`);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Register the handler
|
||||
registerInternalHook(handlerConfig.event, handler as InternalHookHandler);
|
||||
console.log(
|
||||
log.info(
|
||||
`Registered hook (legacy): ${handlerConfig.event} -> ${modulePath}${exportName !== "default" ? `#${exportName}` : ""}`,
|
||||
);
|
||||
loadedCount++;
|
||||
} catch (err) {
|
||||
console.error(
|
||||
`Failed to load hook handler from ${handlerConfig.module}:`,
|
||||
err instanceof Error ? err.message : String(err),
|
||||
log.error(
|
||||
`Failed to load hook handler from ${handlerConfig.module}: ${err instanceof Error ? err.message : String(err)}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user