mirror of
https://github.com/moltbot/moltbot.git
synced 2026-03-30 01:06:11 +00:00
fix(hooks): align session-memory session key display
This commit is contained in:
@@ -270,9 +270,7 @@ describe("session-memory hook", () => {
|
||||
cfg: {
|
||||
agents: {
|
||||
defaults: { workspace: mainWorkspace },
|
||||
list: {
|
||||
navi: { workspace: naviWorkspace },
|
||||
},
|
||||
list: [{ id: "navi", workspace: naviWorkspace }],
|
||||
},
|
||||
} satisfies OpenClawConfig,
|
||||
sessionKey: "agent:main:main",
|
||||
@@ -286,6 +284,7 @@ describe("session-memory hook", () => {
|
||||
expect(files.length).toBe(1);
|
||||
expect(memoryContent).toContain("user: Remember this under Navi");
|
||||
expect(memoryContent).toContain("assistant: Stored in the bound workspace");
|
||||
expect(memoryContent).toContain("- **Session Key**: agent:navi:main");
|
||||
await expect(fs.access(path.join(mainWorkspace, "memory"))).rejects.toThrow();
|
||||
});
|
||||
|
||||
|
||||
@@ -8,12 +8,19 @@
|
||||
import fs from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { resolveAgentWorkspaceDir } from "../../../agents/agent-scope.js";
|
||||
import {
|
||||
resolveAgentIdByWorkspacePath,
|
||||
resolveAgentWorkspaceDir,
|
||||
} from "../../../agents/agent-scope.js";
|
||||
import type { OpenClawConfig } from "../../../config/config.js";
|
||||
import { resolveStateDir } from "../../../config/paths.js";
|
||||
import { writeFileWithinRoot } from "../../../infra/fs-safe.js";
|
||||
import { createSubsystemLogger } from "../../../logging/subsystem.js";
|
||||
import { resolveAgentIdFromSessionKey } from "../../../routing/session-key.js";
|
||||
import {
|
||||
parseAgentSessionKey,
|
||||
resolveAgentIdFromSessionKey,
|
||||
toAgentStoreSessionKey,
|
||||
} from "../../../routing/session-key.js";
|
||||
import { hasInterSessionUserProvenance } from "../../../sessions/input-provenance.js";
|
||||
import { resolveHookConfig } from "../../config.js";
|
||||
import type { HookHandler } from "../../hooks.js";
|
||||
@@ -21,6 +28,25 @@ import { generateSlugViaLLM } from "../../llm-slug-generator.js";
|
||||
|
||||
const log = createSubsystemLogger("hooks/session-memory");
|
||||
|
||||
function resolveDisplaySessionKey(params: {
|
||||
cfg?: OpenClawConfig;
|
||||
workspaceDir?: string;
|
||||
sessionKey: string;
|
||||
}): string {
|
||||
if (!params.cfg || !params.workspaceDir) {
|
||||
return params.sessionKey;
|
||||
}
|
||||
const workspaceAgentId = resolveAgentIdByWorkspacePath(params.cfg, params.workspaceDir);
|
||||
const parsed = parseAgentSessionKey(params.sessionKey);
|
||||
if (!workspaceAgentId || !parsed || workspaceAgentId === parsed.agentId) {
|
||||
return params.sessionKey;
|
||||
}
|
||||
return toAgentStoreSessionKey({
|
||||
agentId: workspaceAgentId,
|
||||
requestKey: parsed.rest,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Read recent messages from session file for slug generation
|
||||
*/
|
||||
@@ -192,6 +218,11 @@ const saveSessionToMemory: HookHandler = async (event) => {
|
||||
(cfg
|
||||
? resolveAgentWorkspaceDir(cfg, agentId)
|
||||
: path.join(resolveStateDir(process.env, os.homedir), "workspace"));
|
||||
const displaySessionKey = resolveDisplaySessionKey({
|
||||
cfg,
|
||||
workspaceDir: contextWorkspaceDir,
|
||||
sessionKey: event.sessionKey,
|
||||
});
|
||||
const memoryDir = path.join(workspaceDir, "memory");
|
||||
await fs.mkdir(memoryDir, { recursive: true });
|
||||
|
||||
@@ -299,7 +330,7 @@ const saveSessionToMemory: HookHandler = async (event) => {
|
||||
const entryParts = [
|
||||
`# Session: ${dateStr} ${timeStr} UTC`,
|
||||
"",
|
||||
`- **Session Key**: ${event.sessionKey}`,
|
||||
`- **Session Key**: ${displaySessionKey}`,
|
||||
`- **Session ID**: ${sessionId}`,
|
||||
`- **Source**: ${source}`,
|
||||
"",
|
||||
|
||||
Reference in New Issue
Block a user