Initial commit: deerflow-server (frontend-web + offline-backend-20260512)

This commit is contained in:
吉鑫升 committed 2026-05-13 09:36:15 +08:00
commit e7a455f22e
560 files changed
+101331

No files matched your search

+107
View File
@@ -0,0 +1,107 @@
/**
* API functions for file uploads
*/
import { getBackendBaseURL } from "../config";
import { apiFetch } from "../api";
export interface UploadedFileInfo {
filename: string;
size: number;
path: string;
virtual_path: string;
artifact_url: string;
extension?: string;
modified?: number;
markdown_file?: string;
markdown_path?: string;
markdown_virtual_path?: string;
markdown_artifact_url?: string;
}
export interface UploadResponse {
success: boolean;
files: UploadedFileInfo[];
message: string;
}
export interface ListFilesResponse {
files: UploadedFileInfo[];
count: number;
}
async function readErrorDetail(
response: Response,
fallback: string,
): Promise<string> {
const error = await response.json().catch(() => ({ detail: fallback }));
return error.detail ?? fallback;
}
/**
* Upload files to a thread
*/
export async function uploadFiles(
threadId: string,
files: File[],
): Promise<UploadResponse> {
const formData = new FormData();
files.forEach((file) => {
formData.append("files", file);
});
const response = await apiFetch(
`${getBackendBaseURL()}/api/threads/${threadId}/uploads`,
{
method: "POST",
body: formData,
},
);
if (!response.ok) {
throw new Error(await readErrorDetail(response, "Upload failed"));
}
return response.json();
}
/**
* List all uploaded files for a thread
*/
export async function listUploadedFiles(
threadId: string,
): Promise<ListFilesResponse> {
const response = await apiFetch(
`${getBackendBaseURL()}/api/threads/${threadId}/uploads/list`,
);
if (!response.ok) {
throw new Error(
await readErrorDetail(response, "Failed to list uploaded files"),
);
}
return response.json();
}
/**
* Delete an uploaded file
*/
export async function deleteUploadedFile(
threadId: string,
filename: string,
): Promise<{ success: boolean; message: string }> {
const response = await apiFetch(
`${getBackendBaseURL()}/api/threads/${threadId}/uploads/${filename}`,
{
method: "DELETE",
},
);
if (!response.ok) {
throw new Error(await readErrorDetail(response, "Failed to delete file"));
}
return response.json();
}
@@ -0,0 +1,34 @@
const MACOS_APP_BUNDLE_CONTENT_TYPES = new Set([
"",
"application/octet-stream",
]);
export const MACOS_APP_BUNDLE_UPLOAD_MESSAGE =
"macOS .app bundles can't be uploaded directly from the browser. Compress the app as a .zip or upload the .dmg instead.";
export function isLikelyMacOSAppBundle(file: Pick<File, "name" | "type">) {
return (
file.name.toLowerCase().endsWith(".app") &&
MACOS_APP_BUNDLE_CONTENT_TYPES.has(file.type)
);
}
export function splitUnsupportedUploadFiles(fileList: File[] | FileList) {
const incoming = Array.from(fileList);
const accepted: File[] = [];
const rejected: File[] = [];
for (const file of incoming) {
if (isLikelyMacOSAppBundle(file)) {
rejected.push(file);
continue;
}
accepted.push(file);
}
return {
accepted,
rejected,
message: rejected.length > 0 ? MACOS_APP_BUNDLE_UPLOAD_MESSAGE : undefined,
};
}
+79
View File
@@ -0,0 +1,79 @@
/**
* React hooks for file uploads
*/
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { useCallback } from "react";
import {
deleteUploadedFile,
listUploadedFiles,
uploadFiles,
type UploadedFileInfo,
type UploadResponse,
} from "./api";
/**
* Hook to upload files
*/
export function useUploadFiles(threadId: string) {
const queryClient = useQueryClient();
return useMutation<UploadResponse, Error, File[]>({
mutationFn: (files: File[]) => uploadFiles(threadId, files),
onSuccess: () => {
// Invalidate the uploaded files list
void queryClient.invalidateQueries({
queryKey: ["uploads", "list", threadId],
});
},
});
}
/**
* Hook to list uploaded files
*/
export function useUploadedFiles(threadId: string) {
return useQuery({
queryKey: ["uploads", "list", threadId],
queryFn: () => listUploadedFiles(threadId),
enabled: !!threadId,
});
}
/**
* Hook to delete an uploaded file
*/
export function useDeleteUploadedFile(threadId: string) {
const queryClient = useQueryClient();
return useMutation({
mutationFn: (filename: string) => deleteUploadedFile(threadId, filename),
onSuccess: () => {
// Invalidate the uploaded files list
void queryClient.invalidateQueries({
queryKey: ["uploads", "list", threadId],
});
},
});
}
/**
* Hook to handle file uploads in submit flow
* Returns a function that uploads files and returns their info
*/
export function useUploadFilesOnSubmit(threadId: string) {
const uploadMutation = useUploadFiles(threadId);
return useCallback(
async (files: File[]): Promise<UploadedFileInfo[]> => {
if (files.length === 0) {
return [];
}
const result = await uploadMutation.mutateAsync(files);
return result.files;
},
[uploadMutation],
);
}
+8
View File
@@ -0,0 +1,8 @@
/**
* File uploads module
*/
export * from "./api";
export * from "./file-validation";
export * from "./hooks";
export * from "./prompt-input-files";
@@ -0,0 +1,52 @@
import type { FileUIPart } from "ai";
export type PromptInputFilePart = FileUIPart & {
// Transient submit-time handle to the original browser File; not serializable.
file?: File;
};
export async function promptInputFilePartToFile(
filePart: PromptInputFilePart,
): Promise<File | null> {
if (filePart.file instanceof File) {
const filename =
typeof filePart.filename === "string" && filePart.filename.length > 0
? filePart.filename
: filePart.file.name;
const mediaType =
typeof filePart.mediaType === "string" && filePart.mediaType.length > 0
? filePart.mediaType
: filePart.file.type;
if (filePart.file.name === filename && filePart.file.type === mediaType) {
return filePart.file;
}
return new File([filePart.file], filename, { type: mediaType });
}
if (!filePart.url || !filePart.filename) {
return null;
}
try {
const response = await fetch(filePart.url);
if (!response.ok) {
throw new Error(
`HTTP ${response.status} while fetching fallback file URL`,
);
}
const blob = await response.blob();
return new File([blob], filePart.filename, {
type: filePart.mediaType || blob.type,
});
} catch (error) {
console.warn("promptInputFilePartToFile: fetch fallback failed", {
error,
url: filePart.url,
filename: filePart.filename,
});
return null;
}
}