fix: use event.prompt instead of ctx.sessionKey for prompt storage in OpenClaw plugin
The before_agent_start handler was passing ctx.sessionKey (e.g. "agent:main:main") as the prompt to the worker API, causing the viewer to display the session key instead of actual user prompt text. Now correctly reads event.prompt from OpenClaw's BeforeAgentStartEvent. Also adds message_received hook to capture inbound user prompts from messaging channels (Telegram, Discord, etc.) and stores them via the worker API. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
+31
-2
@@ -67,13 +67,27 @@ interface SessionEndEvent {
|
|||||||
durationMs?: number;
|
durationMs?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface MessageReceivedEvent {
|
||||||
|
from: string;
|
||||||
|
content: string;
|
||||||
|
timestamp?: number;
|
||||||
|
metadata?: Record<string, unknown>;
|
||||||
|
}
|
||||||
|
|
||||||
interface EventContext {
|
interface EventContext {
|
||||||
sessionKey?: string;
|
sessionKey?: string;
|
||||||
workspaceDir?: string;
|
workspaceDir?: string;
|
||||||
agentId?: string;
|
agentId?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface MessageContext {
|
||||||
|
channelId: string;
|
||||||
|
accountId?: string;
|
||||||
|
conversationId?: string;
|
||||||
|
}
|
||||||
|
|
||||||
type EventCallback<T> = (event: T, ctx: EventContext) => void | Promise<void>;
|
type EventCallback<T> = (event: T, ctx: EventContext) => void | Promise<void>;
|
||||||
|
type MessageEventCallback<T> = (event: T, ctx: MessageContext) => void | Promise<void>;
|
||||||
|
|
||||||
interface OpenClawPluginApi {
|
interface OpenClawPluginApi {
|
||||||
id: string;
|
id: string;
|
||||||
@@ -100,6 +114,7 @@ interface OpenClawPluginApi {
|
|||||||
((event: "agent_end", callback: EventCallback<AgentEndEvent>) => void) &
|
((event: "agent_end", callback: EventCallback<AgentEndEvent>) => void) &
|
||||||
((event: "session_start", callback: EventCallback<SessionStartEvent>) => void) &
|
((event: "session_start", callback: EventCallback<SessionStartEvent>) => void) &
|
||||||
((event: "session_end", callback: EventCallback<SessionEndEvent>) => void) &
|
((event: "session_end", callback: EventCallback<SessionEndEvent>) => void) &
|
||||||
|
((event: "message_received", callback: MessageEventCallback<MessageReceivedEvent>) => void) &
|
||||||
((event: "after_compaction", callback: EventCallback<AfterCompactionEvent>) => void) &
|
((event: "after_compaction", callback: EventCallback<AfterCompactionEvent>) => void) &
|
||||||
((event: "gateway_start", callback: EventCallback<Record<string, never>>) => void);
|
((event: "gateway_start", callback: EventCallback<Record<string, never>>) => void);
|
||||||
runtime: {
|
runtime: {
|
||||||
@@ -482,6 +497,20 @@ export default function claudeMemPlugin(api: OpenClawPluginApi): void {
|
|||||||
api.logger.info(`[claude-mem] Session initialized: ${contentSessionId}`);
|
api.logger.info(`[claude-mem] Session initialized: ${contentSessionId}`);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// ------------------------------------------------------------------
|
||||||
|
// Event: message_received — capture inbound user prompts from channels
|
||||||
|
// ------------------------------------------------------------------
|
||||||
|
api.on("message_received", async (event, ctx) => {
|
||||||
|
const sessionKey = ctx.conversationId || ctx.channelId || "default";
|
||||||
|
const contentSessionId = getContentSessionId(sessionKey);
|
||||||
|
|
||||||
|
await workerPost(workerPort, "/api/sessions/init", {
|
||||||
|
contentSessionId,
|
||||||
|
project: baseProjectName,
|
||||||
|
prompt: event.content || "[media prompt]",
|
||||||
|
}, api.logger);
|
||||||
|
});
|
||||||
|
|
||||||
// ------------------------------------------------------------------
|
// ------------------------------------------------------------------
|
||||||
// Event: after_compaction — re-init session after context compaction
|
// Event: after_compaction — re-init session after context compaction
|
||||||
// ------------------------------------------------------------------
|
// ------------------------------------------------------------------
|
||||||
@@ -500,7 +529,7 @@ export default function claudeMemPlugin(api: OpenClawPluginApi): void {
|
|||||||
// ------------------------------------------------------------------
|
// ------------------------------------------------------------------
|
||||||
// Event: before_agent_start — init session + sync MEMORY.md + track workspace
|
// Event: before_agent_start — init session + sync MEMORY.md + track workspace
|
||||||
// ------------------------------------------------------------------
|
// ------------------------------------------------------------------
|
||||||
api.on("before_agent_start", async (_event, ctx) => {
|
api.on("before_agent_start", async (event, ctx) => {
|
||||||
// Track workspace dir so tool_result_persist can sync MEMORY.md later
|
// Track workspace dir so tool_result_persist can sync MEMORY.md later
|
||||||
if (ctx.workspaceDir) {
|
if (ctx.workspaceDir) {
|
||||||
workspaceDirsBySessionKey.set(ctx.sessionKey || "default", ctx.workspaceDir);
|
workspaceDirsBySessionKey.set(ctx.sessionKey || "default", ctx.workspaceDir);
|
||||||
@@ -512,7 +541,7 @@ export default function claudeMemPlugin(api: OpenClawPluginApi): void {
|
|||||||
await workerPost(workerPort, "/api/sessions/init", {
|
await workerPost(workerPort, "/api/sessions/init", {
|
||||||
contentSessionId,
|
contentSessionId,
|
||||||
project: getProjectName(ctx),
|
project: getProjectName(ctx),
|
||||||
prompt: ctx.sessionKey || "agent run",
|
prompt: event.prompt || "agent run",
|
||||||
}, api.logger);
|
}, api.logger);
|
||||||
|
|
||||||
// Sync MEMORY.md before agent runs (provides context to agent)
|
// Sync MEMORY.md before agent runs (provides context to agent)
|
||||||
|
|||||||
Reference in New Issue
Block a user