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

@@ -7,20 +7,10 @@ export function registerGatewayStartHook(api: any, deps: {
pushMetaToMonitor: () => Promise<void>; pushMetaToMonitor: () => Promise<void>;
startCalendarScheduler: () => void; startCalendarScheduler: () => void;
setMetaPushInterval: (handle: ReturnType<typeof setInterval>) => void; setMetaPushInterval: (handle: ReturnType<typeof setInterval>) => void;
onCronServiceAvailable?: (cron: any) => void;
}) { }) {
const { logger, pushMetaToMonitor, startCalendarScheduler, setMetaPushInterval } = deps; const { logger, pushMetaToMonitor, startCalendarScheduler, setMetaPushInterval } = deps;
api.on('gateway_start', (event?: any) => { api.on('gateway_start', () => {
// Extract cron service from gateway startup event (same as memory-core dreaming)
if (event && deps.onCronServiceAvailable) {
const context = event?.context;
const cron = context?.cron ?? context?.deps?.cron;
if (cron && typeof cron.add === 'function') {
deps.onCronServiceAvailable(cron);
logger.info('HarborForge: cron service acquired from gateway_start');
}
}
logger.info('HarborForge plugin active'); logger.info('HarborForge plugin active');
const live = getPluginConfig(api); const live = getPluginConfig(api);

View File

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