fix(hooks): replace debug console.log with proper subsystem logging in session-memory (#10730)
* fix: replace debug console.log with proper subsystem logging in session-memory * fix(hooks): normalize session-memory subsystem logging --------- Co-authored-by: Tak Hoffman <781889+Takhoffman@users.noreply.github.com>main
parent
48b0fd8d88
commit
2c8af78d20
|
|
@ -12,9 +12,12 @@ import { fileURLToPath } from "node:url";
|
||||||
import type { OpenClawConfig } from "../../../config/config.js";
|
import type { OpenClawConfig } from "../../../config/config.js";
|
||||||
import type { HookHandler } from "../../hooks.js";
|
import type { HookHandler } from "../../hooks.js";
|
||||||
import { resolveAgentWorkspaceDir } from "../../../agents/agent-scope.js";
|
import { resolveAgentWorkspaceDir } from "../../../agents/agent-scope.js";
|
||||||
|
import { createSubsystemLogger } from "../../../logging/subsystem.js";
|
||||||
import { resolveAgentIdFromSessionKey } from "../../../routing/session-key.js";
|
import { resolveAgentIdFromSessionKey } from "../../../routing/session-key.js";
|
||||||
import { resolveHookConfig } from "../../config.js";
|
import { resolveHookConfig } from "../../config.js";
|
||||||
|
|
||||||
|
const log = createSubsystemLogger("hooks/session-memory");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read recent messages from session file for slug generation
|
* Read recent messages from session file for slug generation
|
||||||
*/
|
*/
|
||||||
|
|
@ -69,7 +72,7 @@ const saveSessionToMemory: HookHandler = async (event) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
console.log("[session-memory] Hook triggered for /new command");
|
log.debug("Hook triggered for /new command");
|
||||||
|
|
||||||
const context = event.context || {};
|
const context = event.context || {};
|
||||||
const cfg = context.cfg as OpenClawConfig | undefined;
|
const cfg = context.cfg as OpenClawConfig | undefined;
|
||||||
|
|
@ -92,9 +95,11 @@ const saveSessionToMemory: HookHandler = async (event) => {
|
||||||
const currentSessionId = sessionEntry.sessionId as string;
|
const currentSessionId = sessionEntry.sessionId as string;
|
||||||
const currentSessionFile = sessionEntry.sessionFile as string;
|
const currentSessionFile = sessionEntry.sessionFile as string;
|
||||||
|
|
||||||
console.log("[session-memory] Current sessionId:", currentSessionId);
|
log.debug("Session context resolved", {
|
||||||
console.log("[session-memory] Current sessionFile:", currentSessionFile);
|
sessionId: currentSessionId,
|
||||||
console.log("[session-memory] cfg present:", !!cfg);
|
sessionFile: currentSessionFile,
|
||||||
|
hasCfg: Boolean(cfg),
|
||||||
|
});
|
||||||
|
|
||||||
const sessionFile = currentSessionFile || undefined;
|
const sessionFile = currentSessionFile || undefined;
|
||||||
|
|
||||||
|
|
@ -111,10 +116,13 @@ const saveSessionToMemory: HookHandler = async (event) => {
|
||||||
if (sessionFile) {
|
if (sessionFile) {
|
||||||
// Get recent conversation content
|
// Get recent conversation content
|
||||||
sessionContent = await getRecentSessionContent(sessionFile, messageCount);
|
sessionContent = await getRecentSessionContent(sessionFile, messageCount);
|
||||||
console.log("[session-memory] sessionContent length:", sessionContent?.length || 0);
|
log.debug("Session content loaded", {
|
||||||
|
length: sessionContent?.length ?? 0,
|
||||||
|
messageCount,
|
||||||
|
});
|
||||||
|
|
||||||
if (sessionContent && cfg) {
|
if (sessionContent && cfg) {
|
||||||
console.log("[session-memory] Calling generateSlugViaLLM...");
|
log.debug("Calling generateSlugViaLLM...");
|
||||||
// Dynamically import the LLM slug generator (avoids module caching issues)
|
// Dynamically import the LLM slug generator (avoids module caching issues)
|
||||||
// When compiled, handler is at dist/hooks/bundled/session-memory/handler.js
|
// When compiled, handler is at dist/hooks/bundled/session-memory/handler.js
|
||||||
// Going up ../.. puts us at dist/hooks/, so just add llm-slug-generator.js
|
// Going up ../.. puts us at dist/hooks/, so just add llm-slug-generator.js
|
||||||
|
|
@ -124,7 +132,7 @@ const saveSessionToMemory: HookHandler = async (event) => {
|
||||||
|
|
||||||
// Use LLM to generate a descriptive slug
|
// Use LLM to generate a descriptive slug
|
||||||
slug = await generateSlugViaLLM({ sessionContent, cfg });
|
slug = await generateSlugViaLLM({ sessionContent, cfg });
|
||||||
console.log("[session-memory] Generated slug:", slug);
|
log.debug("Generated slug", { slug });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -132,14 +140,16 @@ const saveSessionToMemory: HookHandler = async (event) => {
|
||||||
if (!slug) {
|
if (!slug) {
|
||||||
const timeSlug = now.toISOString().split("T")[1].split(".")[0].replace(/:/g, "");
|
const timeSlug = now.toISOString().split("T")[1].split(".")[0].replace(/:/g, "");
|
||||||
slug = timeSlug.slice(0, 4); // HHMM
|
slug = timeSlug.slice(0, 4); // HHMM
|
||||||
console.log("[session-memory] Using fallback timestamp slug:", slug);
|
log.debug("Using fallback timestamp slug", { slug });
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create filename with date and slug
|
// Create filename with date and slug
|
||||||
const filename = `${dateStr}-${slug}.md`;
|
const filename = `${dateStr}-${slug}.md`;
|
||||||
const memoryFilePath = path.join(memoryDir, filename);
|
const memoryFilePath = path.join(memoryDir, filename);
|
||||||
console.log("[session-memory] Generated filename:", filename);
|
log.debug("Memory file path resolved", {
|
||||||
console.log("[session-memory] Full path:", memoryFilePath);
|
filename,
|
||||||
|
path: memoryFilePath.replace(os.homedir(), "~"),
|
||||||
|
});
|
||||||
|
|
||||||
// Format time as HH:MM:SS UTC
|
// Format time as HH:MM:SS UTC
|
||||||
const timeStr = now.toISOString().split("T")[1].split(".")[0];
|
const timeStr = now.toISOString().split("T")[1].split(".")[0];
|
||||||
|
|
@ -167,16 +177,21 @@ const saveSessionToMemory: HookHandler = async (event) => {
|
||||||
|
|
||||||
// Write to new memory file
|
// Write to new memory file
|
||||||
await fs.writeFile(memoryFilePath, entry, "utf-8");
|
await fs.writeFile(memoryFilePath, entry, "utf-8");
|
||||||
console.log("[session-memory] Memory file written successfully");
|
log.debug("Memory file written successfully");
|
||||||
|
|
||||||
// Log completion (but don't send user-visible confirmation - it's internal housekeeping)
|
// Log completion (but don't send user-visible confirmation - it's internal housekeeping)
|
||||||
const relPath = memoryFilePath.replace(os.homedir(), "~");
|
const relPath = memoryFilePath.replace(os.homedir(), "~");
|
||||||
console.log(`[session-memory] Session context saved to ${relPath}`);
|
log.info(`Session context saved to ${relPath}`);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(
|
if (err instanceof Error) {
|
||||||
"[session-memory] Failed to save session memory:",
|
log.error("Failed to save session memory", {
|
||||||
err instanceof Error ? err.message : String(err),
|
errorName: err.name,
|
||||||
);
|
errorMessage: err.message,
|
||||||
|
stack: err.stack,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
log.error("Failed to save session memory", { error: String(err) });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue