Initial commit: deerflow-server (frontend-web + offline-backend-20260512)
This commit is contained in:
commit
e7a455f22e
560 files changed
+101331
No files matched your search
@@ -0,0 +1,216 @@
|
||||
import { apiFetch } from "@/core/api";
|
||||
import { getBackendBaseURL } from "@/core/config";
|
||||
|
||||
import type {
|
||||
ScheduledTask,
|
||||
ScheduledTaskDeliveryProfile,
|
||||
ScheduledTaskResultFileContent,
|
||||
ScheduledTaskRun,
|
||||
SchedulerThread,
|
||||
} from "./types";
|
||||
|
||||
const baseUrl = () => `${getBackendBaseURL()}/api/scheduled-tasks`;
|
||||
|
||||
async function parseResponse<T>(response: Response): Promise<T> {
|
||||
if (!response.ok) {
|
||||
const error = await response
|
||||
.json()
|
||||
.catch(() => ({ detail: "Request failed" }));
|
||||
throw new Error(error.detail ?? "Request failed");
|
||||
}
|
||||
return response.json() as Promise<T>;
|
||||
}
|
||||
|
||||
export async function getSchedulerThread() {
|
||||
return parseResponse<SchedulerThread>(await apiFetch(`${baseUrl()}/thread`));
|
||||
}
|
||||
|
||||
export async function listScheduledTasks() {
|
||||
return parseResponse<ScheduledTask[]>(await apiFetch(baseUrl()));
|
||||
}
|
||||
|
||||
export async function createScheduledTask(data: {
|
||||
name: string;
|
||||
prompt: string;
|
||||
schedule: string;
|
||||
timezone?: string | null;
|
||||
execution_agent_name?: string | null;
|
||||
}) {
|
||||
return parseResponse<ScheduledTask>(
|
||||
await apiFetch(baseUrl(), {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(data),
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
export async function listScheduledTaskRuns(taskId: string) {
|
||||
return parseResponse<ScheduledTaskRun[]>(
|
||||
await apiFetch(`${baseUrl()}/${encodeURIComponent(taskId)}/runs`),
|
||||
);
|
||||
}
|
||||
|
||||
export async function getScheduledTaskRun(runId: string) {
|
||||
return parseResponse<ScheduledTaskRun>(
|
||||
await apiFetch(`${baseUrl()}/runs/${encodeURIComponent(runId)}`),
|
||||
);
|
||||
}
|
||||
|
||||
export async function deleteScheduledTaskRun(runId: string) {
|
||||
return parseResponse<{ success: boolean }>(
|
||||
await apiFetch(`${baseUrl()}/runs/${encodeURIComponent(runId)}`, {
|
||||
method: "DELETE",
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
export function scheduledTaskRunDownloadUrl(runId: string) {
|
||||
return `${baseUrl()}/runs/${encodeURIComponent(runId)}/download`;
|
||||
}
|
||||
|
||||
export function scheduledTaskRunFileDownloadUrl(runId: string, fileIndex: number) {
|
||||
return `${baseUrl()}/runs/${encodeURIComponent(runId)}/files/${fileIndex}/download`;
|
||||
}
|
||||
|
||||
export async function getScheduledTaskRunFileContent(runId: string, fileIndex: number) {
|
||||
return parseResponse<ScheduledTaskResultFileContent>(
|
||||
await apiFetch(`${baseUrl()}/runs/${encodeURIComponent(runId)}/files/${fileIndex}`),
|
||||
);
|
||||
}
|
||||
|
||||
function filenameFromContentDisposition(header: string | null): string | null {
|
||||
if (!header) return null;
|
||||
const star = /filename\*=(?:UTF-8'')?([^;]+)/i.exec(header);
|
||||
if (star) {
|
||||
const raw = star[1].replace(/["']/g, "").trim();
|
||||
try {
|
||||
return decodeURIComponent(raw);
|
||||
} catch {
|
||||
return raw;
|
||||
}
|
||||
}
|
||||
const plain = /filename="?([^";\n]+)"?/i.exec(header);
|
||||
return plain ? plain[1].trim() : null;
|
||||
}
|
||||
|
||||
async function downloadResponse(response: Response, fallbackFilename: string) {
|
||||
if (!response.ok) {
|
||||
const error = await response
|
||||
.json()
|
||||
.catch(() => ({ detail: "Download failed" }));
|
||||
const detail = error.detail;
|
||||
if (typeof detail === "string") throw new Error(detail);
|
||||
if (
|
||||
detail &&
|
||||
typeof detail === "object" &&
|
||||
"message" in detail &&
|
||||
typeof detail.message === "string"
|
||||
) {
|
||||
throw new Error(detail.message);
|
||||
}
|
||||
throw new Error("Download failed");
|
||||
}
|
||||
const blob = await response.blob();
|
||||
const contentType = response.headers.get("Content-Type") ?? blob.type;
|
||||
const fallbackByType =
|
||||
contentType.includes("application/zip") || contentType.includes("application/x-zip-compressed")
|
||||
? fallbackFilename.replace(/\.[^.]+$/, ".zip")
|
||||
: fallbackFilename;
|
||||
const filename =
|
||||
filenameFromContentDisposition(response.headers.get("Content-Disposition")) ??
|
||||
fallbackByType;
|
||||
const url = URL.createObjectURL(blob);
|
||||
const anchor = document.createElement("a");
|
||||
anchor.href = url;
|
||||
anchor.download = filename;
|
||||
anchor.rel = "noopener";
|
||||
document.body.appendChild(anchor);
|
||||
anchor.click();
|
||||
anchor.remove();
|
||||
window.setTimeout(() => URL.revokeObjectURL(url), 30_000);
|
||||
}
|
||||
|
||||
export async function downloadScheduledTaskRun(runId: string) {
|
||||
await downloadResponse(
|
||||
await apiFetch(scheduledTaskRunDownloadUrl(runId)),
|
||||
"scheduled-task-result.zip",
|
||||
);
|
||||
}
|
||||
|
||||
export async function downloadScheduledTaskRunFile(runId: string, fileIndex: number) {
|
||||
await downloadResponse(
|
||||
await apiFetch(scheduledTaskRunFileDownloadUrl(runId, fileIndex)),
|
||||
"scheduled-task-file",
|
||||
);
|
||||
}
|
||||
|
||||
export async function getScheduledTaskDeliveryProfile() {
|
||||
return parseResponse<ScheduledTaskDeliveryProfile>(
|
||||
await apiFetch(`${baseUrl()}/delivery-profile`),
|
||||
);
|
||||
}
|
||||
|
||||
export async function updateScheduledTaskDeliveryProfile(data: {
|
||||
element_user_id?: string | null;
|
||||
}) {
|
||||
return parseResponse<ScheduledTaskDeliveryProfile>(
|
||||
await apiFetch(`${baseUrl()}/delivery-profile`, {
|
||||
method: "PUT",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(data),
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
export async function updateScheduledTask(
|
||||
taskId: string,
|
||||
data: Partial<{
|
||||
name: string;
|
||||
prompt: string;
|
||||
schedule: string;
|
||||
timezone: string;
|
||||
enabled: boolean;
|
||||
execution_agent_name: string | null;
|
||||
}>,
|
||||
) {
|
||||
return parseResponse<ScheduledTask>(
|
||||
await apiFetch(`${baseUrl()}/${encodeURIComponent(taskId)}`, {
|
||||
method: "PATCH",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(data),
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
export async function deleteScheduledTask(taskId: string) {
|
||||
return parseResponse<{ success: boolean }>(
|
||||
await apiFetch(`${baseUrl()}/${encodeURIComponent(taskId)}`, {
|
||||
method: "DELETE",
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
export async function pauseScheduledTask(taskId: string) {
|
||||
return parseResponse<ScheduledTask>(
|
||||
await apiFetch(`${baseUrl()}/${encodeURIComponent(taskId)}/pause`, {
|
||||
method: "POST",
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
export async function resumeScheduledTask(taskId: string) {
|
||||
return parseResponse<ScheduledTask>(
|
||||
await apiFetch(`${baseUrl()}/${encodeURIComponent(taskId)}/resume`, {
|
||||
method: "POST",
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
export async function triggerScheduledTask(taskId: string) {
|
||||
return parseResponse<{ success: boolean; task: ScheduledTask }>(
|
||||
await apiFetch(`${baseUrl()}/${encodeURIComponent(taskId)}/trigger`, {
|
||||
method: "POST",
|
||||
}),
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
|
||||
import {
|
||||
createScheduledTask,
|
||||
deleteScheduledTask,
|
||||
deleteScheduledTaskRun,
|
||||
getSchedulerThread,
|
||||
getScheduledTaskDeliveryProfile,
|
||||
getScheduledTaskRun,
|
||||
listScheduledTaskRuns,
|
||||
listScheduledTasks,
|
||||
pauseScheduledTask,
|
||||
resumeScheduledTask,
|
||||
triggerScheduledTask,
|
||||
updateScheduledTaskDeliveryProfile,
|
||||
updateScheduledTask,
|
||||
} from "./api";
|
||||
|
||||
export function useSchedulerThread() {
|
||||
return useQuery({
|
||||
queryKey: ["scheduled-tasks", "thread"],
|
||||
queryFn: getSchedulerThread,
|
||||
refetchOnWindowFocus: false,
|
||||
});
|
||||
}
|
||||
|
||||
export function useScheduledTasks() {
|
||||
return useQuery({
|
||||
queryKey: ["scheduled-tasks"],
|
||||
queryFn: listScheduledTasks,
|
||||
refetchOnWindowFocus: false,
|
||||
});
|
||||
}
|
||||
|
||||
export function useScheduledTaskRuns(taskId?: string | null) {
|
||||
return useQuery({
|
||||
queryKey: ["scheduled-tasks", taskId, "runs"],
|
||||
queryFn: () => listScheduledTaskRuns(taskId!),
|
||||
enabled: !!taskId,
|
||||
refetchOnWindowFocus: false,
|
||||
});
|
||||
}
|
||||
|
||||
export function useScheduledTaskRun(runId?: string | null) {
|
||||
return useQuery({
|
||||
queryKey: ["scheduled-tasks", "runs", runId],
|
||||
queryFn: () => getScheduledTaskRun(runId!),
|
||||
enabled: !!runId,
|
||||
refetchOnWindowFocus: false,
|
||||
});
|
||||
}
|
||||
|
||||
export function useScheduledTaskDeliveryProfile() {
|
||||
return useQuery({
|
||||
queryKey: ["scheduled-tasks", "delivery-profile"],
|
||||
queryFn: getScheduledTaskDeliveryProfile,
|
||||
refetchOnWindowFocus: false,
|
||||
});
|
||||
}
|
||||
|
||||
export function useScheduledTaskActions() {
|
||||
const queryClient = useQueryClient();
|
||||
const invalidate = () =>
|
||||
queryClient.invalidateQueries({ queryKey: ["scheduled-tasks"] });
|
||||
const invalidateRuns = () =>
|
||||
queryClient.invalidateQueries({ queryKey: ["scheduled-tasks"] });
|
||||
|
||||
return {
|
||||
update: useMutation({
|
||||
mutationFn: ({
|
||||
taskId,
|
||||
data,
|
||||
}: {
|
||||
taskId: string;
|
||||
data: Parameters<typeof updateScheduledTask>[1];
|
||||
}) => updateScheduledTask(taskId, data),
|
||||
onSuccess: invalidate,
|
||||
}),
|
||||
create: useMutation({
|
||||
mutationFn: createScheduledTask,
|
||||
onSuccess: invalidate,
|
||||
}),
|
||||
remove: useMutation({
|
||||
mutationFn: deleteScheduledTask,
|
||||
onSuccess: invalidate,
|
||||
}),
|
||||
removeRun: useMutation({
|
||||
mutationFn: deleteScheduledTaskRun,
|
||||
onSuccess: invalidate,
|
||||
}),
|
||||
pause: useMutation({
|
||||
mutationFn: pauseScheduledTask,
|
||||
onSuccess: invalidate,
|
||||
}),
|
||||
resume: useMutation({
|
||||
mutationFn: resumeScheduledTask,
|
||||
onSuccess: invalidate,
|
||||
}),
|
||||
trigger: useMutation({
|
||||
mutationFn: triggerScheduledTask,
|
||||
onSuccess: invalidateRuns,
|
||||
}),
|
||||
updateDeliveryProfile: useMutation({
|
||||
mutationFn: updateScheduledTaskDeliveryProfile,
|
||||
onSuccess: () =>
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ["scheduled-tasks", "delivery-profile"],
|
||||
}),
|
||||
}),
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
export * from "./api";
|
||||
export * from "./hooks";
|
||||
export * from "./types";
|
||||
@@ -0,0 +1,67 @@
|
||||
export type ScheduledTask = {
|
||||
task_id: string;
|
||||
name: string;
|
||||
prompt: string;
|
||||
schedule_text: string;
|
||||
cron_expr: string;
|
||||
timezone: string;
|
||||
enabled: boolean;
|
||||
next_run_at?: string | null;
|
||||
last_run_at?: string | null;
|
||||
last_status?: string | null;
|
||||
source_agent_name?: string | null;
|
||||
execution_agent_name?: string | null;
|
||||
scheduler_thread_id: string;
|
||||
};
|
||||
|
||||
export type SchedulerThread = {
|
||||
user_id: string;
|
||||
thread_id: string;
|
||||
};
|
||||
|
||||
export type ScheduledTaskDeliveryProfile = {
|
||||
user_id: string;
|
||||
element_user_id?: string | null;
|
||||
};
|
||||
|
||||
export type ScheduledTaskResultFile = {
|
||||
name: string;
|
||||
path?: string | null;
|
||||
mime_type?: string | null;
|
||||
size?: number | null;
|
||||
kind?: "markdown" | "attachment" | "artifact" | null;
|
||||
};
|
||||
|
||||
export type ScheduledTaskResultFileContent = {
|
||||
index: number;
|
||||
name: string;
|
||||
mime_type?: string | null;
|
||||
kind?: string | null;
|
||||
size?: number | null;
|
||||
previewable: boolean;
|
||||
content?: string | null;
|
||||
};
|
||||
|
||||
export type ScheduledTaskRunResult = {
|
||||
title: string;
|
||||
content: string;
|
||||
format: "markdown" | "text" | "json" | "mixed";
|
||||
files: ScheduledTaskResultFile[];
|
||||
created_at: string;
|
||||
};
|
||||
|
||||
export type ScheduledTaskRun = {
|
||||
id: string;
|
||||
task_id: string;
|
||||
user_id: string;
|
||||
scheduler_thread_id: string;
|
||||
agent_run_id?: string | null;
|
||||
scheduled_for: string;
|
||||
started_at?: string | null;
|
||||
finished_at?: string | null;
|
||||
status: "pending" | "running" | "succeeded" | "failed" | string;
|
||||
error?: string | null;
|
||||
result?: ScheduledTaskRunResult | null;
|
||||
created_at?: string | null;
|
||||
updated_at?: string | null;
|
||||
};
|
||||
Reference in new issue
Block a user