103 lines
2.4 KiB
TypeScript
103 lines
2.4 KiB
TypeScript
import { apiFetch } from "@/core/api";
|
|
import { getBackendBaseURL } from "@/core/config";
|
|
|
|
export type ArtifactLibraryKind =
|
|
| "all"
|
|
| "document"
|
|
| "spreadsheet"
|
|
| "text_data"
|
|
| "archive"
|
|
| "other";
|
|
|
|
export interface ArtifactLibraryItem {
|
|
threadId: string;
|
|
threadTitle: string;
|
|
agentId: string | null;
|
|
threadType: string | null;
|
|
path: string;
|
|
name: string;
|
|
sizeBytes: number;
|
|
mimeType: string | null;
|
|
modifiedAt: string;
|
|
fileKind: Exclude<ArtifactLibraryKind, "all">;
|
|
}
|
|
|
|
export interface ArtifactLibraryResponse {
|
|
total: number;
|
|
offset: number;
|
|
limit: number;
|
|
items: ArtifactLibraryItem[];
|
|
}
|
|
|
|
export async function fetchArtifactLibrary({
|
|
query,
|
|
fileKind,
|
|
offset,
|
|
limit,
|
|
refresh = false,
|
|
signal,
|
|
}: {
|
|
query?: string;
|
|
fileKind: ArtifactLibraryKind;
|
|
offset: number;
|
|
limit: number;
|
|
refresh?: boolean;
|
|
signal?: AbortSignal;
|
|
}): Promise<ArtifactLibraryResponse> {
|
|
const search = new URLSearchParams({
|
|
file_kind: fileKind,
|
|
offset: String(offset),
|
|
limit: String(limit),
|
|
});
|
|
const normalizedQuery = query?.trim();
|
|
if (normalizedQuery) search.set("query", normalizedQuery);
|
|
if (refresh) search.set("refresh", "true");
|
|
|
|
const response = await apiFetch(
|
|
`${getBackendBaseURL()}/api/artifact-library?${search.toString()}`,
|
|
{ method: "GET", signal },
|
|
);
|
|
if (!response.ok) {
|
|
const detail = await response.text().catch(() => "");
|
|
throw new Error(
|
|
`Failed to load artifact library (${response.status})${detail ? `: ${detail}` : ""}`,
|
|
);
|
|
}
|
|
|
|
const data = (await response.json()) as {
|
|
total?: number;
|
|
offset?: number;
|
|
limit?: number;
|
|
items?: Array<{
|
|
thread_id: string;
|
|
thread_title: string;
|
|
agent_id?: string | null;
|
|
thread_type?: string | null;
|
|
path: string;
|
|
name: string;
|
|
size_bytes: number;
|
|
mime_type?: string | null;
|
|
modified_at: string;
|
|
file_kind: Exclude<ArtifactLibraryKind, "all">;
|
|
}>;
|
|
};
|
|
|
|
return {
|
|
total: data.total ?? 0,
|
|
offset: data.offset ?? offset,
|
|
limit: data.limit ?? limit,
|
|
items: (data.items ?? []).map((item) => ({
|
|
threadId: item.thread_id,
|
|
threadTitle: item.thread_title,
|
|
agentId: item.agent_id ?? null,
|
|
threadType: item.thread_type ?? null,
|
|
path: item.path,
|
|
name: item.name,
|
|
sizeBytes: item.size_bytes,
|
|
mimeType: item.mime_type ?? null,
|
|
modifiedAt: item.modified_at,
|
|
fileKind: item.file_kind,
|
|
})),
|
|
};
|
|
}
|