mirror of
https://github.com/farcasclaudiu/openclaw.git
synced 2026-06-28 11:01:39 +03:00
ebe5730401
* fix: use STATE_DIR instead of hardcoded ~/.openclaw for identity and canvas device-identity.ts and canvas-host/server.ts used hardcoded path.join(os.homedir(), '.openclaw', ...) ignoring OPENCLAW_STATE_DIR env var and the resolveStateDir() logic from config/paths.ts. This caused ~/.openclaw/identity and ~/.openclaw/canvas directories to be created even when state dir was overridden or resided elsewhere. * fix: format and remove duplicate imports * fix: scope state-dir patch + add regression tests (#4824) (thanks @kossoy) * fix: align state-dir fallbacks in hooks and agent paths (#4824) (thanks @kossoy) --------- Co-authored-by: Gustavo Madeira Santana <gumadeiras@gmail.com>
68 lines
1.7 KiB
TypeScript
68 lines
1.7 KiB
TypeScript
/**
|
|
* Example hook handler: Log all commands to a file
|
|
*
|
|
* This handler demonstrates how to create a hook that logs all command events
|
|
* to a centralized log file for audit/debugging purposes.
|
|
*
|
|
* To enable this handler, add it to your config:
|
|
*
|
|
* ```json
|
|
* {
|
|
* "hooks": {
|
|
* "internal": {
|
|
* "enabled": true,
|
|
* "handlers": [
|
|
* {
|
|
* "event": "command",
|
|
* "module": "./hooks/handlers/command-logger.ts"
|
|
* }
|
|
* ]
|
|
* }
|
|
* }
|
|
* }
|
|
* ```
|
|
*/
|
|
|
|
import fs from "node:fs/promises";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
import type { HookHandler } from "../../hooks.js";
|
|
import { resolveStateDir } from "../../../config/paths.js";
|
|
|
|
/**
|
|
* Log all command events to a file
|
|
*/
|
|
const logCommand: HookHandler = async (event) => {
|
|
// Only trigger on command events
|
|
if (event.type !== "command") {
|
|
return;
|
|
}
|
|
|
|
try {
|
|
// Create log directory
|
|
const stateDir = resolveStateDir(process.env, os.homedir);
|
|
const logDir = path.join(stateDir, "logs");
|
|
await fs.mkdir(logDir, { recursive: true });
|
|
|
|
// Append to command log file
|
|
const logFile = path.join(logDir, "commands.log");
|
|
const logLine =
|
|
JSON.stringify({
|
|
timestamp: event.timestamp.toISOString(),
|
|
action: event.action,
|
|
sessionKey: event.sessionKey,
|
|
senderId: event.context.senderId ?? "unknown",
|
|
source: event.context.commandSource ?? "unknown",
|
|
}) + "\n";
|
|
|
|
await fs.appendFile(logFile, logLine, "utf-8");
|
|
} catch (err) {
|
|
console.error(
|
|
"[command-logger] Failed to log command:",
|
|
err instanceof Error ? err.message : String(err),
|
|
);
|
|
}
|
|
};
|
|
|
|
export default logCommand;
|