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:
@@ -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);
|
||||||
|
|
||||||
|
|||||||
@@ -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) {
|
|
||||||
logger.error('Cron service not available — cannot wake agent');
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
const agentId = process.env.AGENT_ID || 'unknown';
|
const agentId = process.env.AGENT_ID || 'unknown';
|
||||||
const slotId = context.slot.id ?? context.slot.virtual_id ?? 'unknown';
|
const slotId = context.slot.id ?? context.slot.virtual_id ?? 'unknown';
|
||||||
|
const sessionKey = `agent:${agentId}:hf-calendar:slot-${slotId}`;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await cronService.add({
|
const { dispatchInboundMessageWithDispatcher } = await import(
|
||||||
name: `hf-calendar-slot-${slotId}`,
|
'openclaw/plugin-sdk/reply-dispatch-runtime'
|
||||||
description: `[managed-by=harbor-forge.calendar] Slot ${slotId}: ${context.taskDescription}`,
|
);
|
||||||
deleteAfterRun: true,
|
|
||||||
wakeMode: 'now',
|
const cfg = api.runtime?.config?.loadConfig?.();
|
||||||
agentId,
|
if (!cfg) {
|
||||||
payload: {
|
logger.error('Cannot load OpenClaw config for dispatch');
|
||||||
kind: 'agentTurn',
|
return false;
|
||||||
message: context.prompt,
|
}
|
||||||
timeoutSeconds: context.slot.estimated_duration * 60,
|
|
||||||
|
const result = await dispatchInboundMessageWithDispatcher({
|
||||||
|
ctx: {
|
||||||
|
Body: context.prompt,
|
||||||
|
SessionKey: sessionKey,
|
||||||
|
From: 'harborforge-calendar',
|
||||||
|
Provider: 'harborforge',
|
||||||
},
|
},
|
||||||
delivery: {
|
cfg,
|
||||||
mode: 'none',
|
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;
|
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, {
|
||||||
|
|||||||
Reference in New Issue
Block a user