mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-07 01:21:36 +00:00
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.
|
- 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/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/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: 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.
|
- 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.
|
- 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 { ensureAuthProfileStore } from "../../agents/auth-profiles.js";
|
||||||
import { resolveForwardCompatModel } from "../../agents/model-forward-compat.js";
|
import { resolveForwardCompatModel } from "../../agents/model-forward-compat.js";
|
||||||
import { parseModelRef } from "../../agents/model-selection.js";
|
import { parseModelRef } from "../../agents/model-selection.js";
|
||||||
|
import { resolveModel } from "../../agents/pi-embedded-runner/model.js";
|
||||||
import { loadConfig } from "../../config/config.js";
|
import { loadConfig } from "../../config/config.js";
|
||||||
import { resolveConfiguredEntries } from "./list.configured.js";
|
import { resolveConfiguredEntries } from "./list.configured.js";
|
||||||
import { formatErrorWithStack } from "./list.errors.js";
|
import { formatErrorWithStack } from "./list.errors.js";
|
||||||
@@ -109,6 +110,9 @@ export async function modelsListCommand(
|
|||||||
modelByKey.set(entry.key, forwardCompat);
|
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)) {
|
if (opts.local && model && !isLocalBaseUrl(model.baseUrl)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import fs from "node:fs/promises";
|
import fs from "node:fs/promises";
|
||||||
import os from "node:os";
|
import os from "node:os";
|
||||||
import path from "node:path";
|
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 type { OpenClawConfig } from "../config/config.js";
|
||||||
import {
|
import {
|
||||||
clearInternalHooks,
|
clearInternalHooks,
|
||||||
@@ -151,8 +151,6 @@ describe("loader", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("should handle module loading errors gracefully", async () => {
|
it("should handle module loading errors gracefully", async () => {
|
||||||
const consoleError = vi.spyOn(console, "error").mockImplementation(() => {});
|
|
||||||
|
|
||||||
const cfg: OpenClawConfig = {
|
const cfg: OpenClawConfig = {
|
||||||
hooks: {
|
hooks: {
|
||||||
internal: {
|
internal: {
|
||||||
@@ -167,19 +165,12 @@ describe("loader", () => {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Should not throw and should return 0 (handler failed to load)
|
||||||
const count = await loadInternalHooks(cfg, tmpDir);
|
const count = await loadInternalHooks(cfg, tmpDir);
|
||||||
expect(count).toBe(0);
|
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 () => {
|
it("should handle non-function exports", async () => {
|
||||||
const consoleError = vi.spyOn(console, "error").mockImplementation(() => {});
|
|
||||||
|
|
||||||
// Create a module with a non-function export
|
// Create a module with a non-function export
|
||||||
const handlerPath = path.join(tmpDir, "bad-export.js");
|
const handlerPath = path.join(tmpDir, "bad-export.js");
|
||||||
await fs.writeFile(handlerPath, 'export default "not a function";', "utf-8");
|
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);
|
const count = await loadInternalHooks(cfg, tmpDir);
|
||||||
expect(count).toBe(0);
|
expect(count).toBe(0);
|
||||||
expect(consoleError).toHaveBeenCalledWith(expect.stringContaining("is not a function"));
|
|
||||||
|
|
||||||
consoleError.mockRestore();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should handle relative paths", async () => {
|
it("should handle relative paths", async () => {
|
||||||
|
|||||||
@@ -9,11 +9,14 @@ import path from "node:path";
|
|||||||
import { pathToFileURL } from "node:url";
|
import { pathToFileURL } from "node:url";
|
||||||
import type { OpenClawConfig } from "../config/config.js";
|
import type { OpenClawConfig } from "../config/config.js";
|
||||||
import type { InternalHookHandler } from "./internal-hooks.js";
|
import type { InternalHookHandler } from "./internal-hooks.js";
|
||||||
|
import { createSubsystemLogger } from "../logging/subsystem.js";
|
||||||
import { resolveHookConfig } from "./config.js";
|
import { resolveHookConfig } from "./config.js";
|
||||||
import { shouldIncludeHook } from "./config.js";
|
import { shouldIncludeHook } from "./config.js";
|
||||||
import { registerInternalHook } from "./internal-hooks.js";
|
import { registerInternalHook } from "./internal-hooks.js";
|
||||||
import { loadWorkspaceHookEntries } from "./workspace.js";
|
import { loadWorkspaceHookEntries } from "./workspace.js";
|
||||||
|
|
||||||
|
const log = createSubsystemLogger("hooks:loader");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Load and register all hook handlers
|
* Load and register all hook handlers
|
||||||
*
|
*
|
||||||
@@ -78,16 +81,14 @@ export async function loadInternalHooks(
|
|||||||
const handler = mod[exportName];
|
const handler = mod[exportName];
|
||||||
|
|
||||||
if (typeof handler !== "function") {
|
if (typeof handler !== "function") {
|
||||||
console.error(
|
log.error(`Handler '${exportName}' from ${entry.hook.name} is not a function`);
|
||||||
`Hook error: Handler '${exportName}' from ${entry.hook.name} is not a function`,
|
|
||||||
);
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Register for all events listed in metadata
|
// Register for all events listed in metadata
|
||||||
const events = entry.metadata?.events ?? [];
|
const events = entry.metadata?.events ?? [];
|
||||||
if (events.length === 0) {
|
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;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -95,21 +96,19 @@ export async function loadInternalHooks(
|
|||||||
registerInternalHook(event, handler as InternalHookHandler);
|
registerInternalHook(event, handler as InternalHookHandler);
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(
|
log.info(
|
||||||
`Registered hook: ${entry.hook.name} -> ${events.join(", ")}${exportName !== "default" ? ` (export: ${exportName})` : ""}`,
|
`Registered hook: ${entry.hook.name} -> ${events.join(", ")}${exportName !== "default" ? ` (export: ${exportName})` : ""}`,
|
||||||
);
|
);
|
||||||
loadedCount++;
|
loadedCount++;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(
|
log.error(
|
||||||
`Failed to load hook ${entry.hook.name}:`,
|
`Failed to load hook ${entry.hook.name}: ${err instanceof Error ? err.message : String(err)}`,
|
||||||
err instanceof Error ? err.message : String(err),
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(
|
log.error(
|
||||||
"Failed to load directory-based hooks:",
|
`Failed to load directory-based hooks: ${err instanceof Error ? err.message : String(err)}`,
|
||||||
err instanceof Error ? err.message : String(err),
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -132,20 +131,18 @@ export async function loadInternalHooks(
|
|||||||
const handler = mod[exportName];
|
const handler = mod[exportName];
|
||||||
|
|
||||||
if (typeof handler !== "function") {
|
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;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Register the handler
|
|
||||||
registerInternalHook(handlerConfig.event, handler as InternalHookHandler);
|
registerInternalHook(handlerConfig.event, handler as InternalHookHandler);
|
||||||
console.log(
|
log.info(
|
||||||
`Registered hook (legacy): ${handlerConfig.event} -> ${modulePath}${exportName !== "default" ? `#${exportName}` : ""}`,
|
`Registered hook (legacy): ${handlerConfig.event} -> ${modulePath}${exportName !== "default" ? `#${exportName}` : ""}`,
|
||||||
);
|
);
|
||||||
loadedCount++;
|
loadedCount++;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(
|
log.error(
|
||||||
`Failed to load hook handler from ${handlerConfig.module}:`,
|
`Failed to load hook handler from ${handlerConfig.module}: ${err instanceof Error ? err.message : String(err)}`,
|
||||||
err instanceof Error ? err.message : String(err),
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user