feat(knowledge): choose which KB folder an upload batch lands in

The last piece of #866. #899 stopped a linked folder's subdirectories from
collapsing, and same-named documents no longer overwrite or vanish — but the
reporter's actual workflow still could not rebuild his tree. He added one
subtree at a time, and a browser folder pick reports each file's path
relative to the chosen directory: `应用开发/钉钉CLI/` arrives as
`钉钉CLI/...`, with no way to say it belongs under `应用开发/`. Every batch
piled up at the KB root, and the ancestor was not missing data — it was
never in the payload.

Uploads now take an optional destination folder, composed ahead of each
file's own relative path. The picker offers the KB's existing folders (and
its root, the default) once files are staged, so batching a large tree is a
matter of picking where each part goes. It runs through the same
_sanitize_rel_subdir guard as a directory upload's own path, so a crafted
value cannot escape raw/.

Listing folders is best-effort: if that call fails the picker simply does
not appear and the batch goes to the root exactly as before.

Linked-folder sync keeps its own root-relative staging and does not take a
destination prefix yet — that needs the prefix persisted per linked folder
in metadata, which is a separate change.
This commit is contained in:
Bingxi Zhao (Frank)
2026-08-20 00:54:35 +08:00
parent 7cc213f24d
commit ea8e271fdc
9 changed files with 172 additions and 15 deletions

View File

@@ -1,9 +1,12 @@
"use client";
import { useState } from "react";
import { useEffect, useState } from "react";
import { useTranslation } from "react-i18next";
import { Loader2, RefreshCw, Upload } from "lucide-react";
import type { KnowledgeUploadPolicy } from "@/lib/knowledge-api";
import { FolderInput, Loader2, RefreshCw, Upload } from "lucide-react";
import {
listKnowledgeBaseFiles,
type KnowledgeUploadPolicy,
} from "@/lib/knowledge-api";
import {
kbIsUploadable,
kbNeedsReindex,
@@ -28,7 +31,7 @@ interface KbDocumentsSectionProps {
history: HistoryEntry[];
onClearHistory: () => void;
onRetry?: () => Promise<void>;
onUpload: (files: File[]) => Promise<void>;
onUpload: (files: File[], destSubdir?: string) => Promise<void>;
}
/**
@@ -50,6 +53,31 @@ export default function KbDocumentsSection({
const [files, setFiles] = useState<File[]>([]);
const [submitting, setSubmitting] = useState(false);
const [retrySubmitting, setRetrySubmitting] = useState(false);
// Existing folders in this KB, offered as a destination for the batch.
const [folders, setFolders] = useState<string[]>([]);
const [destSubdir, setDestSubdir] = useState("");
useEffect(() => {
let cancelled = false;
void listKnowledgeBaseFiles(kb.name)
.then((entries) => {
if (cancelled) return;
setFolders(
entries
.filter((entry) => entry.type === "folder")
.map((entry) => entry.name)
.sort((a, b) => a.localeCompare(b)),
);
})
.catch(() => {
// A destination picker is an optional convenience; failing to list
// folders just means the batch goes to the root as it always did.
if (!cancelled) setFolders([]);
});
return () => {
cancelled = true;
};
}, [kb.name]);
const uploadable = kbIsUploadable(kb);
const needsReindex = kbNeedsReindex(kb);
@@ -97,7 +125,7 @@ export default function KbDocumentsSection({
if (!canSubmit) return;
setSubmitting(true);
try {
await onUpload(selection.validFiles);
await onUpload(selection.validFiles, destSubdir || undefined);
setFiles([]);
} finally {
setSubmitting(false);
@@ -182,6 +210,26 @@ export default function KbDocumentsSection({
disabled={!canUpload || isUploadingHere}
/>
{folders.length > 0 && files.length > 0 && (
<label className="flex items-center gap-2 text-[12px] text-[var(--muted-foreground)]">
<FolderInput size={13} strokeWidth={1.7} />
<span>{t("Add to folder")}</span>
<select
value={destSubdir}
onChange={(event) => setDestSubdir(event.target.value)}
disabled={!canUpload || isUploadingHere}
className="min-w-0 flex-1 rounded-md border border-[var(--border)] bg-[var(--background)] px-2 py-1 text-[12px] text-[var(--foreground)] disabled:opacity-40"
>
<option value="">{t("Knowledge base root")}</option>
{folders.map((folder) => (
<option key={folder} value={folder}>
{folder}
</option>
))}
</select>
</label>
)}
<div className="flex items-center justify-end">
<button
type="button"

View File

@@ -36,7 +36,11 @@ interface KnowledgeBaseDetailProps {
task?: TaskState;
history: HistoryEntry[];
onCreate: () => void;
onUpload: (kbName: string, files: File[]) => Promise<void>;
onUpload: (
kbName: string,
files: File[],
destSubdir?: string,
) => Promise<void>;
onReindex: (kbName: string) => Promise<void>;
onRetry: (kbName: string) => Promise<void>;
onSetDefault: (kbName: string) => Promise<void>;
@@ -238,8 +242,10 @@ export default function KnowledgeBaseDetail({
history={history}
onClearHistory={() => onClearHistory(kb.name)}
onRetry={handleRetry}
onUpload={(files) =>
kb.read_only ? Promise.resolve() : onUpload(kb.name, files)
onUpload={(files, destSubdir) =>
kb.read_only
? Promise.resolve()
: onUpload(kb.name, files, destSubdir)
}
/>
)}

View File

@@ -174,9 +174,9 @@ export default function KnowledgePage() {
);
const handleUpload = useCallback(
async (kbName: string, files: File[]) => {
async (kbName: string, files: File[], destSubdir?: string) => {
try {
await uploadFiles(kbName, files);
await uploadFiles(kbName, files, undefined, destSubdir);
} catch (err) {
setError(err instanceof Error ? err.message : String(err));
throw err;