feat: add runtime event handling for providers in ProvidersSection feat: update routing to include Channels and Agents pages feat: extend route types and navigation items for Channels and Agents feat: implement agents store for managing agent data and interactions fix: update chat store to utilize agents store for agent-related functionality chore: export agents store from index fix: enhance runtime types for better event handling fix: update Vite config to handle dev server URL correctly
87 lines
2.4 KiB
TypeScript
87 lines
2.4 KiB
TypeScript
import type { HostApiContext } from '../context';
|
|
import type { NormalizedHostApiRequest } from '../route-utils';
|
|
import { fail, ok, parseJsonBody } from '../route-utils';
|
|
import {
|
|
createCronJob,
|
|
deleteCronJob,
|
|
listCronJobs,
|
|
toggleCronJob,
|
|
triggerCronJob,
|
|
updateCronJob,
|
|
} from '../../utils/cron-store';
|
|
import type { CronJobCreateInput, CronJobUpdateInput } from '@src/lib/cron-types';
|
|
|
|
export async function handleCronRoutes(
|
|
request: NormalizedHostApiRequest,
|
|
_ctx: HostApiContext,
|
|
) {
|
|
const { pathname, method } = request;
|
|
|
|
if (pathname === '/api/cron/jobs' && method === 'GET') {
|
|
return ok(listCronJobs());
|
|
}
|
|
|
|
if (pathname === '/api/cron/jobs' && method === 'POST') {
|
|
try {
|
|
const body = parseJsonBody<CronJobCreateInput & { agentId?: string | null }>(request.body);
|
|
return ok(createCronJob(body), 201);
|
|
} catch (error) {
|
|
return fail(400, error instanceof Error ? error.message : String(error));
|
|
}
|
|
}
|
|
|
|
if (pathname === '/api/cron/toggle' && method === 'POST') {
|
|
try {
|
|
const body = parseJsonBody<{ id?: string; enabled?: boolean }>(request.body);
|
|
if (!body?.id) {
|
|
return fail(400, 'id is required');
|
|
}
|
|
|
|
return ok(toggleCronJob(body.id, body.enabled !== false));
|
|
} catch (error) {
|
|
return fail(400, error instanceof Error ? error.message : String(error));
|
|
}
|
|
}
|
|
|
|
if (pathname === '/api/cron/trigger' && method === 'POST') {
|
|
try {
|
|
const body = parseJsonBody<{ id?: string }>(request.body);
|
|
if (!body?.id) {
|
|
return fail(400, 'id is required');
|
|
}
|
|
|
|
return ok(triggerCronJob(body.id));
|
|
} catch (error) {
|
|
return fail(400, error instanceof Error ? error.message : String(error));
|
|
}
|
|
}
|
|
|
|
if (!pathname.startsWith('/api/cron/jobs/')) {
|
|
return null;
|
|
}
|
|
|
|
const jobId = decodeURIComponent(pathname.slice('/api/cron/jobs/'.length)).trim();
|
|
if (!jobId) {
|
|
return fail(400, 'id is required');
|
|
}
|
|
|
|
if (method === 'PUT') {
|
|
try {
|
|
const body = parseJsonBody<CronJobUpdateInput & { agentId?: string | null }>(request.body);
|
|
return ok(updateCronJob(jobId, body));
|
|
} catch (error) {
|
|
return fail(400, error instanceof Error ? error.message : String(error));
|
|
}
|
|
}
|
|
|
|
if (method === 'DELETE') {
|
|
try {
|
|
return ok(deleteCronJob(jobId));
|
|
} catch (error) {
|
|
return fail(400, error instanceof Error ? error.message : String(error));
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|