feat(cron): allow users to associate cron jobs with specific agents (#835)

This commit is contained in:
Tao Yiping
2026-04-12 11:51:29 +08:00
committed by GitHub
Unverified
parent 49518300dc
commit 87ab12849c
9 changed files with 429 additions and 10 deletions

View File

@@ -347,6 +347,14 @@ function buildCronUpdatePatch(input: Record<string, unknown>): Record<string, un
patch.delivery = normalizeCronDeliveryPatch(patch.delivery);
}
if ('agentId' in patch) {
const agentId = typeof patch.agentId === 'string' && patch.agentId.trim()
? patch.agentId.trim()
: 'main';
patch.agentId = agentId;
// Keep sessionTarget as isolated when agentId changes
}
return patch;
}
@@ -377,6 +385,9 @@ function transformCronJob(job: GatewayCronJob) {
? new Date(job.state.nextRunAtMs).toISOString()
: undefined;
// Parse agentId from the job's agentId field
const agentId = (job as unknown as { agentId?: string }).agentId || 'main';
return {
id: job.id,
name: job.name,
@@ -389,6 +400,7 @@ function transformCronJob(job: GatewayCronJob) {
updatedAt: new Date(job.updatedAtMs).toISOString(),
lastRun,
nextRun,
agentId,
};
}
@@ -515,7 +527,11 @@ export async function handleCronRoutes(
schedule: string;
delivery?: GatewayCronDelivery;
enabled?: boolean;
agentId?: string;
}>(req);
const agentId = typeof input.agentId === 'string' && input.agentId.trim()
? input.agentId.trim()
: 'main';
const delivery = normalizeCronDelivery(input.delivery);
const unsupportedDeliveryError = getUnsupportedCronDeliveryError(delivery.channel);
if (delivery.mode === 'announce' && unsupportedDeliveryError) {
@@ -529,6 +545,7 @@ export async function handleCronRoutes(
enabled: input.enabled ?? true,
wakeMode: 'next-heartbeat',
sessionTarget: 'isolated',
agentId,
delivery,
});
sendJson(res, 200, result && typeof result === 'object' ? transformCronJob(result as GatewayCronJob) : result);