feat: wake agent via dispatchInboundMessage (in-process)

Use the same mechanism as Discord plugin — direct in-process call
to dispatchInboundMessageWithDispatcher from plugin-sdk. No WebSocket,
no CLI, no cron service dependency.

Creates unique session key per slot: agent:{agentId}:hf-calendar:slot-{slotId}

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
operator
2026-04-19 07:56:36 +00:00
parent ae2d035a83
commit 6a5d135ef6
2 changed files with 28 additions and 38 deletions

View File

@@ -187,46 +187,49 @@ export default {
return null;
}
// Cron service reference, acquired from gateway_start event
let cronService: any = null;
/**
* Wake agent via cron service — same mechanism used by dreaming/memory-core.
* Creates a one-shot cron job with wakeMode: "now" and deleteAfterRun: true.
* Wake agent via dispatchInboundMessage — same mechanism used by Discord plugin.
* Direct in-process call, no WebSocket or CLI needed.
*/
async function wakeAgent(context: AgentWakeContext): Promise<boolean> {
logger.info(`Waking agent for slot: ${context.taskDescription}`);
if (!cronService) {
logger.error('Cron service not available — cannot wake agent');
return false;
}
const agentId = process.env.AGENT_ID || 'unknown';
const slotId = context.slot.id ?? context.slot.virtual_id ?? 'unknown';
const sessionKey = `agent:${agentId}:hf-calendar:slot-${slotId}`;
try {
await cronService.add({
name: `hf-calendar-slot-${slotId}`,
description: `[managed-by=harbor-forge.calendar] Slot ${slotId}: ${context.taskDescription}`,
deleteAfterRun: true,
wakeMode: 'now',
agentId,
payload: {
kind: 'agentTurn',
message: context.prompt,
timeoutSeconds: context.slot.estimated_duration * 60,
const { dispatchInboundMessageWithDispatcher } = await import(
'openclaw/plugin-sdk/reply-dispatch-runtime'
);
const cfg = api.runtime?.config?.loadConfig?.();
if (!cfg) {
logger.error('Cannot load OpenClaw config for dispatch');
return false;
}
const result = await dispatchInboundMessageWithDispatcher({
ctx: {
Body: context.prompt,
SessionKey: sessionKey,
From: 'harborforge-calendar',
Provider: 'harborforge',
},
delivery: {
mode: 'none',
cfg,
dispatcherOptions: {
deliver: async (payload: any) => {
// No delivery — agent works silently
logger.info(`Agent reply for slot ${slotId}: ${(payload.text || '').slice(0, 100)}`);
},
},
});
logger.info(`Agent wake cron job created for slot ${slotId}`);
logger.info(`Agent dispatched for slot ${slotId}: ${result?.status || 'ok'}`);
return true;
} catch (err) {
logger.error('Failed to create wake cron job:', err);
logger.error('Failed to dispatch agent for slot:', err);
return false;
}
}
@@ -309,9 +312,6 @@ export default {
setMetaPushInterval(handle) {
metaPushInterval = handle;
},
onCronServiceAvailable(cron) {
cronService = cron;
},
});
registerGatewayStopHook(api, {