refactor: split ipc task processor handlers
This commit is contained in:
@@ -22,6 +22,11 @@ import {
|
|||||||
} from './task-watch-status.js';
|
} from './task-watch-status.js';
|
||||||
import type { IpcDeps, TaskIpcPayload } from './ipc-types.js';
|
import type { IpcDeps, TaskIpcPayload } from './ipc-types.js';
|
||||||
|
|
||||||
|
type RoomBindings = ReturnType<IpcDeps['roomBindings']>;
|
||||||
|
type ScheduleType = 'cron' | 'interval' | 'once';
|
||||||
|
type TaskUpdates = Parameters<typeof updateTask>[1];
|
||||||
|
type MemorySourceKind = Parameters<typeof rememberMemory>[0]['sourceKind'];
|
||||||
|
|
||||||
export async function processTaskIpc(
|
export async function processTaskIpc(
|
||||||
data: TaskIpcPayload,
|
data: TaskIpcPayload,
|
||||||
sourceGroup: string,
|
sourceGroup: string,
|
||||||
@@ -32,25 +37,85 @@ export async function processTaskIpc(
|
|||||||
|
|
||||||
switch (data.type) {
|
switch (data.type) {
|
||||||
case 'schedule_task':
|
case 'schedule_task':
|
||||||
|
handleScheduleTask(data, sourceGroup, isMain, deps, roomBindings);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'pause_task':
|
||||||
|
handleTaskStateMutation(data, sourceGroup, isMain, {
|
||||||
|
action: () => updateTask(data.taskId!, { status: 'paused' }),
|
||||||
|
successMessage: 'Task paused via IPC',
|
||||||
|
unauthorizedMessage: 'Unauthorized task pause attempt',
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'resume_task':
|
||||||
|
handleTaskStateMutation(data, sourceGroup, isMain, {
|
||||||
|
action: () => updateTask(data.taskId!, { status: 'active' }),
|
||||||
|
successMessage: 'Task resumed via IPC',
|
||||||
|
unauthorizedMessage: 'Unauthorized task resume attempt',
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'cancel_task':
|
||||||
|
handleTaskStateMutation(data, sourceGroup, isMain, {
|
||||||
|
action: () => deleteTask(data.taskId!),
|
||||||
|
successMessage: 'Task cancelled via IPC',
|
||||||
|
unauthorizedMessage: 'Unauthorized task cancel attempt',
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'host_evidence_request':
|
||||||
|
await handleHostEvidenceRequest(data, sourceGroup, isMain, deps);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'update_task':
|
||||||
|
handleUpdateTask(data, sourceGroup, isMain);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'refresh_groups':
|
||||||
|
await handleRefreshGroups(sourceGroup, isMain, deps);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'assign_room':
|
||||||
|
handleAssignRoom(data, sourceGroup, isMain, deps);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'persist_memory':
|
||||||
|
handlePersistMemory(data, sourceGroup);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
logger.warn({ type: data.type }, 'Unknown IPC task type');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleScheduleTask(
|
||||||
|
data: TaskIpcPayload,
|
||||||
|
sourceGroup: string,
|
||||||
|
isMain: boolean,
|
||||||
|
deps: IpcDeps,
|
||||||
|
roomBindings: RoomBindings,
|
||||||
|
): void {
|
||||||
if (
|
if (
|
||||||
data.prompt &&
|
!data.prompt ||
|
||||||
data.schedule_type &&
|
!data.schedule_type ||
|
||||||
data.schedule_value &&
|
!data.schedule_value ||
|
||||||
data.targetJid
|
!data.targetJid
|
||||||
) {
|
) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const targetJid = data.targetJid;
|
const targetJid = data.targetJid;
|
||||||
const targetGroupEntry =
|
const targetGroupEntry =
|
||||||
roomBindings[targetJid] ||
|
roomBindings[targetJid] ||
|
||||||
Object.values(roomBindings).find(
|
Object.values(roomBindings).find((group) => group.folder === targetJid);
|
||||||
(group) => group.folder === targetJid,
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!targetGroupEntry) {
|
if (!targetGroupEntry) {
|
||||||
logger.warn(
|
logger.warn(
|
||||||
{ targetJid },
|
{ targetJid },
|
||||||
'Cannot schedule task: target group not registered',
|
'Cannot schedule task: target group not registered',
|
||||||
);
|
);
|
||||||
break;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const targetFolder = targetGroupEntry.folder;
|
const targetFolder = targetGroupEntry.folder;
|
||||||
@@ -66,7 +131,7 @@ export async function processTaskIpc(
|
|||||||
{ targetJid, targetFolder },
|
{ targetJid, targetFolder },
|
||||||
'Cannot resolve scheduled task target JID from folder',
|
'Cannot resolve scheduled task target JID from folder',
|
||||||
);
|
);
|
||||||
break;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isMain && targetFolder !== sourceGroup) {
|
if (!isMain && targetFolder !== sourceGroup) {
|
||||||
@@ -74,48 +139,12 @@ export async function processTaskIpc(
|
|||||||
{ sourceGroup, targetFolder },
|
{ sourceGroup, targetFolder },
|
||||||
'Unauthorized schedule_task attempt blocked',
|
'Unauthorized schedule_task attempt blocked',
|
||||||
);
|
);
|
||||||
break;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const scheduleType = data.schedule_type as 'cron' | 'interval' | 'once';
|
const scheduleType = data.schedule_type as ScheduleType;
|
||||||
|
const nextRunResult = resolveNextRun(data, scheduleType);
|
||||||
let nextRun: string | null = null;
|
if (!nextRunResult.ok) return;
|
||||||
if (scheduleType === 'cron') {
|
|
||||||
try {
|
|
||||||
const interval = CronExpressionParser.parse(data.schedule_value, {
|
|
||||||
tz: TIMEZONE,
|
|
||||||
});
|
|
||||||
nextRun = interval.next().toISOString();
|
|
||||||
} catch {
|
|
||||||
logger.warn(
|
|
||||||
{ scheduleValue: data.schedule_value },
|
|
||||||
'Invalid cron expression',
|
|
||||||
);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
} else if (scheduleType === 'interval') {
|
|
||||||
const ms = parseInt(data.schedule_value, 10);
|
|
||||||
if (isNaN(ms) || ms <= 0) {
|
|
||||||
logger.warn(
|
|
||||||
{ scheduleValue: data.schedule_value },
|
|
||||||
'Invalid interval',
|
|
||||||
);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
nextRun = isWatchCiTask({ prompt: data.prompt })
|
|
||||||
? new Date().toISOString()
|
|
||||||
: new Date(Date.now() + ms).toISOString();
|
|
||||||
} else if (scheduleType === 'once') {
|
|
||||||
const date = new Date(data.schedule_value);
|
|
||||||
if (isNaN(date.getTime())) {
|
|
||||||
logger.warn(
|
|
||||||
{ scheduleValue: data.schedule_value },
|
|
||||||
'Invalid timestamp',
|
|
||||||
);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
nextRun = date.toISOString();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (data.ci_provider && data.ci_metadata) {
|
if (data.ci_provider && data.ci_metadata) {
|
||||||
const existing = findDuplicateCiWatcher(
|
const existing = findDuplicateCiWatcher(
|
||||||
@@ -133,7 +162,7 @@ export async function processTaskIpc(
|
|||||||
},
|
},
|
||||||
'Duplicate CI watcher skipped — another agent already watches this run',
|
'Duplicate CI watcher skipped — another agent already watches this run',
|
||||||
);
|
);
|
||||||
break;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -158,7 +187,7 @@ export async function processTaskIpc(
|
|||||||
schedule_type: scheduleType,
|
schedule_type: scheduleType,
|
||||||
schedule_value: data.schedule_value,
|
schedule_value: data.schedule_value,
|
||||||
context_mode: contextMode,
|
context_mode: contextMode,
|
||||||
next_run: nextRun,
|
next_run: nextRunResult.nextRun,
|
||||||
status: 'active',
|
status: 'active',
|
||||||
created_at: new Date().toISOString(),
|
created_at: new Date().toISOString(),
|
||||||
});
|
});
|
||||||
@@ -172,136 +201,100 @@ export async function processTaskIpc(
|
|||||||
},
|
},
|
||||||
'Task created via IPC',
|
'Task created via IPC',
|
||||||
);
|
);
|
||||||
if (nextRun && new Date(nextRun).getTime() <= Date.now()) {
|
if (
|
||||||
|
nextRunResult.nextRun &&
|
||||||
|
new Date(nextRunResult.nextRun).getTime() <= Date.now()
|
||||||
|
) {
|
||||||
deps.nudgeScheduler?.();
|
deps.nudgeScheduler?.();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
|
||||||
|
|
||||||
case 'pause_task':
|
function resolveNextRun(
|
||||||
if (data.taskId) {
|
data: TaskIpcPayload,
|
||||||
|
scheduleType: ScheduleType,
|
||||||
|
): { ok: true; nextRun: string | null } | { ok: false } {
|
||||||
|
if (scheduleType === 'cron') {
|
||||||
|
try {
|
||||||
|
const interval = CronExpressionParser.parse(data.schedule_value!, {
|
||||||
|
tz: TIMEZONE,
|
||||||
|
});
|
||||||
|
return { ok: true, nextRun: interval.next().toISOString() };
|
||||||
|
} catch {
|
||||||
|
logger.warn(
|
||||||
|
{ scheduleValue: data.schedule_value },
|
||||||
|
'Invalid cron expression',
|
||||||
|
);
|
||||||
|
return { ok: false };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (scheduleType === 'interval') {
|
||||||
|
const ms = parseInt(data.schedule_value!, 10);
|
||||||
|
if (isNaN(ms) || ms <= 0) {
|
||||||
|
logger.warn({ scheduleValue: data.schedule_value }, 'Invalid interval');
|
||||||
|
return { ok: false };
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
ok: true,
|
||||||
|
nextRun: isWatchCiTask({ prompt: data.prompt! })
|
||||||
|
? new Date().toISOString()
|
||||||
|
: new Date(Date.now() + ms).toISOString(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (scheduleType === 'once') {
|
||||||
|
const date = new Date(data.schedule_value!);
|
||||||
|
if (isNaN(date.getTime())) {
|
||||||
|
logger.warn({ scheduleValue: data.schedule_value }, 'Invalid timestamp');
|
||||||
|
return { ok: false };
|
||||||
|
}
|
||||||
|
return { ok: true, nextRun: date.toISOString() };
|
||||||
|
}
|
||||||
|
|
||||||
|
return { ok: true, nextRun: null };
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleTaskStateMutation(
|
||||||
|
data: TaskIpcPayload,
|
||||||
|
sourceGroup: string,
|
||||||
|
isMain: boolean,
|
||||||
|
config: {
|
||||||
|
action: () => void;
|
||||||
|
successMessage: string;
|
||||||
|
unauthorizedMessage: string;
|
||||||
|
},
|
||||||
|
): void {
|
||||||
|
if (!data.taskId) return;
|
||||||
|
|
||||||
const task = getTaskById(data.taskId);
|
const task = getTaskById(data.taskId);
|
||||||
if (task && (isMain || task.group_folder === sourceGroup)) {
|
if (task && (isMain || task.group_folder === sourceGroup)) {
|
||||||
updateTask(data.taskId, { status: 'paused' });
|
config.action();
|
||||||
logger.info(
|
logger.info({ taskId: data.taskId, sourceGroup }, config.successMessage);
|
||||||
{ taskId: data.taskId, sourceGroup },
|
|
||||||
'Task paused via IPC',
|
|
||||||
);
|
|
||||||
} else {
|
} else {
|
||||||
logger.warn(
|
logger.warn(
|
||||||
{ taskId: data.taskId, sourceGroup },
|
{ taskId: data.taskId, sourceGroup },
|
||||||
'Unauthorized task pause attempt',
|
config.unauthorizedMessage,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
|
||||||
|
|
||||||
case 'resume_task':
|
async function handleHostEvidenceRequest(
|
||||||
if (data.taskId) {
|
data: TaskIpcPayload,
|
||||||
const task = getTaskById(data.taskId);
|
sourceGroup: string,
|
||||||
if (task && (isMain || task.group_folder === sourceGroup)) {
|
isMain: boolean,
|
||||||
updateTask(data.taskId, { status: 'active' });
|
deps: IpcDeps,
|
||||||
logger.info(
|
): Promise<void> {
|
||||||
{ taskId: data.taskId, sourceGroup },
|
|
||||||
'Task resumed via IPC',
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
logger.warn(
|
|
||||||
{ taskId: data.taskId, sourceGroup },
|
|
||||||
'Unauthorized task resume attempt',
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'cancel_task':
|
|
||||||
if (data.taskId) {
|
|
||||||
const task = getTaskById(data.taskId);
|
|
||||||
if (task && (isMain || task.group_folder === sourceGroup)) {
|
|
||||||
deleteTask(data.taskId);
|
|
||||||
logger.info(
|
|
||||||
{ taskId: data.taskId, sourceGroup },
|
|
||||||
'Task cancelled via IPC',
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
logger.warn(
|
|
||||||
{ taskId: data.taskId, sourceGroup },
|
|
||||||
'Unauthorized task cancel attempt',
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'host_evidence_request':
|
|
||||||
if (!data.requestId) {
|
if (!data.requestId) {
|
||||||
logger.warn(
|
logger.warn(
|
||||||
{ sourceGroup },
|
{ sourceGroup },
|
||||||
'Ignoring host_evidence_request without requestId',
|
'Ignoring host_evidence_request without requestId',
|
||||||
);
|
);
|
||||||
break;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (data.action === 'ejclaw_room_runtime') {
|
if (data.action === 'ejclaw_room_runtime') {
|
||||||
if (!data.chatJid) {
|
handleRoomRuntimeReportRequest(data, sourceGroup, isMain, deps);
|
||||||
writeHostEvidenceResponse(sourceGroup, {
|
return;
|
||||||
requestId: data.requestId,
|
|
||||||
ok: false,
|
|
||||||
action: 'ejclaw_room_runtime',
|
|
||||||
command: 'internal:ejclaw_room_runtime',
|
|
||||||
stdout: '',
|
|
||||||
stderr: '',
|
|
||||||
exitCode: 1,
|
|
||||||
error: 'Missing chatJid for ejclaw_room_runtime request',
|
|
||||||
});
|
|
||||||
logger.warn(
|
|
||||||
{ sourceGroup, requestId: data.requestId },
|
|
||||||
'Rejected ejclaw_room_runtime request without chatJid',
|
|
||||||
);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!deps.getRoomRuntimeReport) {
|
|
||||||
writeHostEvidenceResponse(sourceGroup, {
|
|
||||||
requestId: data.requestId,
|
|
||||||
ok: false,
|
|
||||||
action: 'ejclaw_room_runtime',
|
|
||||||
command: 'internal:ejclaw_room_runtime',
|
|
||||||
stdout: '',
|
|
||||||
stderr: '',
|
|
||||||
exitCode: 1,
|
|
||||||
error: 'Room runtime reporting is not configured',
|
|
||||||
});
|
|
||||||
logger.warn(
|
|
||||||
{ sourceGroup, requestId: data.requestId, chatJid: data.chatJid },
|
|
||||||
'Rejected ejclaw_room_runtime request because runtime reporter is unavailable',
|
|
||||||
);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
const report = deps.getRoomRuntimeReport({
|
|
||||||
chatJid: data.chatJid,
|
|
||||||
sourceGroup,
|
|
||||||
isMain,
|
|
||||||
});
|
|
||||||
writeHostEvidenceResponse(sourceGroup, {
|
|
||||||
requestId: data.requestId,
|
|
||||||
ok: true,
|
|
||||||
action: 'ejclaw_room_runtime',
|
|
||||||
command: 'internal:ejclaw_room_runtime',
|
|
||||||
stdout: JSON.stringify(report, null, 2),
|
|
||||||
stderr: '',
|
|
||||||
exitCode: 0,
|
|
||||||
});
|
|
||||||
logger.info(
|
|
||||||
{
|
|
||||||
sourceGroup,
|
|
||||||
requestId: data.requestId,
|
|
||||||
action: data.action,
|
|
||||||
chatJid: data.chatJid,
|
|
||||||
},
|
|
||||||
'Processed room runtime report request via IPC',
|
|
||||||
);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isHostEvidenceAction(data.action)) {
|
if (!isHostEvidenceAction(data.action)) {
|
||||||
@@ -319,10 +312,9 @@ export async function processTaskIpc(
|
|||||||
{ sourceGroup, requestId: data.requestId, action: data.action },
|
{ sourceGroup, requestId: data.requestId, action: data.action },
|
||||||
'Rejected unsupported host evidence action',
|
'Rejected unsupported host evidence action',
|
||||||
);
|
);
|
||||||
break;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
|
||||||
const result = await runHostEvidenceRequest({
|
const result = await runHostEvidenceRequest({
|
||||||
requestId: data.requestId,
|
requestId: data.requestId,
|
||||||
action: data.action,
|
action: data.action,
|
||||||
@@ -346,48 +338,115 @@ export async function processTaskIpc(
|
|||||||
'Processed host evidence request via IPC',
|
'Processed host evidence request via IPC',
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
break;
|
|
||||||
|
|
||||||
case 'update_task':
|
function handleRoomRuntimeReportRequest(
|
||||||
if (data.taskId) {
|
data: TaskIpcPayload,
|
||||||
|
sourceGroup: string,
|
||||||
|
isMain: boolean,
|
||||||
|
deps: IpcDeps,
|
||||||
|
): void {
|
||||||
|
if (!data.chatJid) {
|
||||||
|
writeHostEvidenceResponse(sourceGroup, {
|
||||||
|
requestId: data.requestId!,
|
||||||
|
ok: false,
|
||||||
|
action: 'ejclaw_room_runtime',
|
||||||
|
command: 'internal:ejclaw_room_runtime',
|
||||||
|
stdout: '',
|
||||||
|
stderr: '',
|
||||||
|
exitCode: 1,
|
||||||
|
error: 'Missing chatJid for ejclaw_room_runtime request',
|
||||||
|
});
|
||||||
|
logger.warn(
|
||||||
|
{ sourceGroup, requestId: data.requestId },
|
||||||
|
'Rejected ejclaw_room_runtime request without chatJid',
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!deps.getRoomRuntimeReport) {
|
||||||
|
writeHostEvidenceResponse(sourceGroup, {
|
||||||
|
requestId: data.requestId!,
|
||||||
|
ok: false,
|
||||||
|
action: 'ejclaw_room_runtime',
|
||||||
|
command: 'internal:ejclaw_room_runtime',
|
||||||
|
stdout: '',
|
||||||
|
stderr: '',
|
||||||
|
exitCode: 1,
|
||||||
|
error: 'Room runtime reporting is not configured',
|
||||||
|
});
|
||||||
|
logger.warn(
|
||||||
|
{ sourceGroup, requestId: data.requestId, chatJid: data.chatJid },
|
||||||
|
'Rejected ejclaw_room_runtime request because runtime reporter is unavailable',
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const report = deps.getRoomRuntimeReport({
|
||||||
|
chatJid: data.chatJid,
|
||||||
|
sourceGroup,
|
||||||
|
isMain,
|
||||||
|
});
|
||||||
|
writeHostEvidenceResponse(sourceGroup, {
|
||||||
|
requestId: data.requestId!,
|
||||||
|
ok: true,
|
||||||
|
action: 'ejclaw_room_runtime',
|
||||||
|
command: 'internal:ejclaw_room_runtime',
|
||||||
|
stdout: JSON.stringify(report, null, 2),
|
||||||
|
stderr: '',
|
||||||
|
exitCode: 0,
|
||||||
|
});
|
||||||
|
logger.info(
|
||||||
|
{
|
||||||
|
sourceGroup,
|
||||||
|
requestId: data.requestId,
|
||||||
|
action: data.action,
|
||||||
|
chatJid: data.chatJid,
|
||||||
|
},
|
||||||
|
'Processed room runtime report request via IPC',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleUpdateTask(
|
||||||
|
data: TaskIpcPayload,
|
||||||
|
sourceGroup: string,
|
||||||
|
isMain: boolean,
|
||||||
|
): void {
|
||||||
|
if (!data.taskId) return;
|
||||||
|
|
||||||
const task = getTaskById(data.taskId);
|
const task = getTaskById(data.taskId);
|
||||||
if (!task) {
|
if (!task) {
|
||||||
logger.warn(
|
logger.warn(
|
||||||
{ taskId: data.taskId, sourceGroup },
|
{ taskId: data.taskId, sourceGroup },
|
||||||
'Task not found for update',
|
'Task not found for update',
|
||||||
);
|
);
|
||||||
break;
|
return;
|
||||||
}
|
}
|
||||||
if (!isMain && task.group_folder !== sourceGroup) {
|
if (!isMain && task.group_folder !== sourceGroup) {
|
||||||
logger.warn(
|
logger.warn(
|
||||||
{ taskId: data.taskId, sourceGroup },
|
{ taskId: data.taskId, sourceGroup },
|
||||||
'Unauthorized task update attempt',
|
'Unauthorized task update attempt',
|
||||||
);
|
);
|
||||||
break;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const updates: Parameters<typeof updateTask>[1] = {};
|
const updates: TaskUpdates = {};
|
||||||
if (data.prompt !== undefined) updates.prompt = data.prompt;
|
if (data.prompt !== undefined) updates.prompt = data.prompt;
|
||||||
if (data.schedule_type !== undefined) {
|
if (data.schedule_type !== undefined) {
|
||||||
updates.schedule_type = data.schedule_type as
|
updates.schedule_type = data.schedule_type as ScheduleType;
|
||||||
| 'cron'
|
|
||||||
| 'interval'
|
|
||||||
| 'once';
|
|
||||||
}
|
}
|
||||||
if (data.schedule_value !== undefined) {
|
if (data.schedule_value !== undefined) {
|
||||||
updates.schedule_value = data.schedule_value;
|
updates.schedule_value = data.schedule_value;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (data.schedule_type || data.schedule_value) {
|
if (data.schedule_type || data.schedule_value) {
|
||||||
const updatedTask = {
|
const updatedTask = { ...task, ...updates };
|
||||||
...task,
|
|
||||||
...updates,
|
|
||||||
};
|
|
||||||
if (updatedTask.schedule_type === 'cron') {
|
if (updatedTask.schedule_type === 'cron') {
|
||||||
try {
|
try {
|
||||||
const interval = CronExpressionParser.parse(
|
const interval = CronExpressionParser.parse(
|
||||||
updatedTask.schedule_value,
|
updatedTask.schedule_value,
|
||||||
{ tz: TIMEZONE },
|
{
|
||||||
|
tz: TIMEZONE,
|
||||||
|
},
|
||||||
);
|
);
|
||||||
updates.next_run = interval.next().toISOString();
|
updates.next_run = interval.next().toISOString();
|
||||||
} catch {
|
} catch {
|
||||||
@@ -395,7 +454,7 @@ export async function processTaskIpc(
|
|||||||
{ taskId: data.taskId, value: updatedTask.schedule_value },
|
{ taskId: data.taskId, value: updatedTask.schedule_value },
|
||||||
'Invalid cron in task update',
|
'Invalid cron in task update',
|
||||||
);
|
);
|
||||||
break;
|
return;
|
||||||
}
|
}
|
||||||
} else if (updatedTask.schedule_type === 'interval') {
|
} else if (updatedTask.schedule_type === 'interval') {
|
||||||
const ms = parseInt(updatedTask.schedule_value, 10);
|
const ms = parseInt(updatedTask.schedule_value, 10);
|
||||||
@@ -411,40 +470,49 @@ export async function processTaskIpc(
|
|||||||
'Task updated via IPC',
|
'Task updated via IPC',
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
break;
|
|
||||||
|
|
||||||
case 'refresh_groups':
|
async function handleRefreshGroups(
|
||||||
if (isMain) {
|
sourceGroup: string,
|
||||||
logger.info(
|
isMain: boolean,
|
||||||
{ sourceGroup },
|
deps: IpcDeps,
|
||||||
'Group metadata refresh requested via IPC',
|
): Promise<void> {
|
||||||
);
|
if (!isMain) {
|
||||||
|
logger.warn({ sourceGroup }, 'Unauthorized refresh_groups attempt blocked');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.info({ sourceGroup }, 'Group metadata refresh requested via IPC');
|
||||||
await deps.syncGroups(true);
|
await deps.syncGroups(true);
|
||||||
const availableGroups = deps.getAvailableGroups();
|
const availableGroups = deps.getAvailableGroups();
|
||||||
deps.writeGroupsSnapshot(sourceGroup, true, availableGroups);
|
deps.writeGroupsSnapshot(sourceGroup, true, availableGroups);
|
||||||
} else {
|
|
||||||
logger.warn(
|
|
||||||
{ sourceGroup },
|
|
||||||
'Unauthorized refresh_groups attempt blocked',
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
break;
|
|
||||||
|
|
||||||
case 'assign_room':
|
function handleAssignRoom(
|
||||||
|
data: TaskIpcPayload,
|
||||||
|
sourceGroup: string,
|
||||||
|
isMain: boolean,
|
||||||
|
deps: IpcDeps,
|
||||||
|
): void {
|
||||||
if (!isMain) {
|
if (!isMain) {
|
||||||
logger.warn(
|
logger.warn(
|
||||||
{ sourceGroup, type: data.type },
|
{ sourceGroup, type: data.type },
|
||||||
`Unauthorized ${data.type} attempt blocked`,
|
`Unauthorized ${data.type} attempt blocked`,
|
||||||
);
|
);
|
||||||
break;
|
return;
|
||||||
|
}
|
||||||
|
if (!data.jid || !data.name) {
|
||||||
|
logger.warn(
|
||||||
|
{ data },
|
||||||
|
`Invalid ${data.type} request - missing required fields`,
|
||||||
|
);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
if (data.jid && data.name) {
|
|
||||||
if (data.folder && !isValidGroupFolder(data.folder)) {
|
if (data.folder && !isValidGroupFolder(data.folder)) {
|
||||||
logger.warn(
|
logger.warn(
|
||||||
{ sourceGroup, folder: data.folder },
|
{ sourceGroup, folder: data.folder },
|
||||||
`Invalid ${data.type} request - unsafe folder name`,
|
`Invalid ${data.type} request - unsafe folder name`,
|
||||||
);
|
);
|
||||||
break;
|
return;
|
||||||
}
|
}
|
||||||
if (
|
if (
|
||||||
data.room_mode !== undefined &&
|
data.room_mode !== undefined &&
|
||||||
@@ -455,7 +523,7 @@ export async function processTaskIpc(
|
|||||||
{ sourceGroup, roomMode: data.room_mode },
|
{ sourceGroup, roomMode: data.room_mode },
|
||||||
'Invalid assign_room request - unknown room_mode',
|
'Invalid assign_room request - unknown room_mode',
|
||||||
);
|
);
|
||||||
break;
|
return;
|
||||||
}
|
}
|
||||||
if (
|
if (
|
||||||
data.owner_agent_type !== undefined &&
|
data.owner_agent_type !== undefined &&
|
||||||
@@ -466,7 +534,7 @@ export async function processTaskIpc(
|
|||||||
{ sourceGroup, ownerAgentType: data.owner_agent_type },
|
{ sourceGroup, ownerAgentType: data.owner_agent_type },
|
||||||
'Invalid assign_room request - unknown owner_agent_type',
|
'Invalid assign_room request - unknown owner_agent_type',
|
||||||
);
|
);
|
||||||
break;
|
return;
|
||||||
}
|
}
|
||||||
if (
|
if (
|
||||||
data.reviewer_agent_type !== undefined &&
|
data.reviewer_agent_type !== undefined &&
|
||||||
@@ -477,7 +545,7 @@ export async function processTaskIpc(
|
|||||||
{ sourceGroup, reviewerAgentType: data.reviewer_agent_type },
|
{ sourceGroup, reviewerAgentType: data.reviewer_agent_type },
|
||||||
'Invalid assign_room request - unknown reviewer_agent_type',
|
'Invalid assign_room request - unknown reviewer_agent_type',
|
||||||
);
|
);
|
||||||
break;
|
return;
|
||||||
}
|
}
|
||||||
if (
|
if (
|
||||||
data.arbiter_agent_type !== undefined &&
|
data.arbiter_agent_type !== undefined &&
|
||||||
@@ -489,7 +557,7 @@ export async function processTaskIpc(
|
|||||||
{ sourceGroup, arbiterAgentType: data.arbiter_agent_type },
|
{ sourceGroup, arbiterAgentType: data.arbiter_agent_type },
|
||||||
'Invalid assign_room request - unknown arbiter_agent_type',
|
'Invalid assign_room request - unknown arbiter_agent_type',
|
||||||
);
|
);
|
||||||
break;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
deps.assignRoom(data.jid, {
|
deps.assignRoom(data.jid, {
|
||||||
@@ -502,15 +570,9 @@ export async function processTaskIpc(
|
|||||||
isMain: data.isMain,
|
isMain: data.isMain,
|
||||||
workDir: data.workDir,
|
workDir: data.workDir,
|
||||||
});
|
});
|
||||||
} else {
|
|
||||||
logger.warn(
|
|
||||||
{ data },
|
|
||||||
`Invalid ${data.type} request - missing required fields`,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
break;
|
|
||||||
|
|
||||||
case 'persist_memory': {
|
function handlePersistMemory(data: TaskIpcPayload, sourceGroup: string): void {
|
||||||
if (
|
if (
|
||||||
data.scopeKind !== 'room' ||
|
data.scopeKind !== 'room' ||
|
||||||
typeof data.scopeKey !== 'string' ||
|
typeof data.scopeKey !== 'string' ||
|
||||||
@@ -520,7 +582,7 @@ export async function processTaskIpc(
|
|||||||
{ sourceGroup, data },
|
{ sourceGroup, data },
|
||||||
'Invalid persist_memory request - missing required fields',
|
'Invalid persist_memory request - missing required fields',
|
||||||
);
|
);
|
||||||
break;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const expectedScopeKey = `room:${sourceGroup}`;
|
const expectedScopeKey = `room:${sourceGroup}`;
|
||||||
@@ -529,7 +591,7 @@ export async function processTaskIpc(
|
|||||||
{ sourceGroup, scopeKey: data.scopeKey, expectedScopeKey },
|
{ sourceGroup, scopeKey: data.scopeKey, expectedScopeKey },
|
||||||
'Unauthorized persist_memory attempt blocked',
|
'Unauthorized persist_memory attempt blocked',
|
||||||
);
|
);
|
||||||
break;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (
|
if (
|
||||||
@@ -543,7 +605,7 @@ export async function processTaskIpc(
|
|||||||
{ sourceGroup, sourceKind: data.source_kind },
|
{ sourceGroup, sourceKind: data.source_kind },
|
||||||
'Invalid persist_memory request - unknown source_kind',
|
'Invalid persist_memory request - unknown source_kind',
|
||||||
);
|
);
|
||||||
break;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (
|
if (
|
||||||
@@ -554,7 +616,7 @@ export async function processTaskIpc(
|
|||||||
{ sourceGroup, keywords: data.keywords },
|
{ sourceGroup, keywords: data.keywords },
|
||||||
'Invalid persist_memory request - keywords must be strings',
|
'Invalid persist_memory request - keywords must be strings',
|
||||||
);
|
);
|
||||||
break;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
rememberMemory({
|
rememberMemory({
|
||||||
@@ -562,15 +624,8 @@ export async function processTaskIpc(
|
|||||||
scopeKey: data.scopeKey,
|
scopeKey: data.scopeKey,
|
||||||
content: data.content,
|
content: data.content,
|
||||||
keywords: data.keywords,
|
keywords: data.keywords,
|
||||||
memoryKind:
|
memoryKind: typeof data.memory_kind === 'string' ? data.memory_kind : null,
|
||||||
typeof data.memory_kind === 'string' ? data.memory_kind : null,
|
sourceKind: (data.source_kind as MemorySourceKind | undefined) ?? 'compact',
|
||||||
sourceKind:
|
|
||||||
(data.source_kind as
|
|
||||||
| 'compact'
|
|
||||||
| 'explicit'
|
|
||||||
| 'import'
|
|
||||||
| 'system'
|
|
||||||
| undefined) ?? 'compact',
|
|
||||||
sourceRef: typeof data.source_ref === 'string' ? data.source_ref : null,
|
sourceRef: typeof data.source_ref === 'string' ? data.source_ref : null,
|
||||||
});
|
});
|
||||||
logger.info(
|
logger.info(
|
||||||
@@ -581,10 +636,4 @@ export async function processTaskIpc(
|
|||||||
},
|
},
|
||||||
'Memory persisted via IPC',
|
'Memory persisted via IPC',
|
||||||
);
|
);
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
default:
|
|
||||||
logger.warn({ type: data.type }, 'Unknown IPC task type');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user