Merge 505357542a into 6af205a13a
This commit is contained in:
commit
4b1823fc79
@ -14,6 +14,9 @@ import { resolveAgentWorkspaceDir } from "../../../agents/agent-scope.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";
|
||||||
import type { HookHandler } from "../../hooks.js";
|
import type { HookHandler } from "../../hooks.js";
|
||||||
|
import { createSubsystemLogger } from "../../../logging/subsystem.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
|
||||||
@ -68,8 +71,6 @@ const saveSessionToMemory: HookHandler = async (event) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
console.log("[session-memory] 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;
|
||||||
const agentId = resolveAgentIdFromSessionKey(event.sessionKey);
|
const agentId = resolveAgentIdFromSessionKey(event.sessionKey);
|
||||||
@ -88,13 +89,8 @@ const saveSessionToMemory: HookHandler = async (event) => {
|
|||||||
string,
|
string,
|
||||||
unknown
|
unknown
|
||||||
>;
|
>;
|
||||||
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);
|
|
||||||
console.log("[session-memory] Current sessionFile:", currentSessionFile);
|
|
||||||
console.log("[session-memory] cfg present:", !!cfg);
|
|
||||||
|
|
||||||
const sessionFile = currentSessionFile || undefined;
|
const sessionFile = currentSessionFile || undefined;
|
||||||
|
|
||||||
// Read message count from hook config (default: 15)
|
// Read message count from hook config (default: 15)
|
||||||
@ -110,10 +106,8 @@ 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);
|
|
||||||
|
|
||||||
if (sessionContent && cfg) {
|
if (sessionContent && cfg) {
|
||||||
console.log("[session-memory] 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
|
||||||
@ -123,7 +117,6 @@ 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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -131,14 +124,11 @@ 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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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);
|
|
||||||
console.log("[session-memory] Full path:", memoryFilePath);
|
|
||||||
|
|
||||||
// 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];
|
||||||
@ -166,11 +156,10 @@ 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 completion (but don't send user-visible confirmation - it's internal housekeeping)
|
// Log completion using subsystem logger (visible when log level is trace/debug)
|
||||||
const relPath = memoryFilePath.replace(os.homedir(), "~");
|
const relPath = memoryFilePath.replace(os.homedir(), "~");
|
||||||
console.log(`[session-memory] Session context saved to ${relPath}`);
|
log.trace(`Session context saved to ${relPath}`);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(
|
console.error(
|
||||||
"[session-memory] Failed to save session memory:",
|
"[session-memory] Failed to save session memory:",
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user