mirror of
https://github.com/bytedance/deer-flow.git
synced 2026-11-08 03:30:34 +00:00
* feat(deer-flow): configure agent tool access Signed-off-by: PeaceMaker-best <221849497+PeaceMaker-best@users.noreply.github.com> * fix(frontend): reconcile knowledge defaults with agent tool access * docs: reduce frontend agent guidance below size limit * fix: address agent tool-group catalog review findings --------- Signed-off-by: PeaceMaker-best <221849497+PeaceMaker-best@users.noreply.github.com> Co-authored-by: PeaceMaker-best <221849497+PeaceMaker-best@users.noreply.github.com> Co-authored-by: Willem Jiang <willem.jiang@gmail.com>
137 lines
3.8 KiB
TypeScript
137 lines
3.8 KiB
TypeScript
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
|
import { useEffect, useState } from "react";
|
|
|
|
import {
|
|
createAgent,
|
|
deleteAgent,
|
|
fetchAgentsApiEnabled,
|
|
getAgent,
|
|
importAgentPackage,
|
|
listAgentToolGroups,
|
|
listAgents,
|
|
updateAgent,
|
|
} from "./api";
|
|
import {
|
|
readCachedAgentsApiEnabled,
|
|
resolveAgentsApiEnabled,
|
|
writeCachedAgentsApiEnabled,
|
|
} from "./feature-cache";
|
|
import type { CreateAgentRequest, UpdateAgentRequest } from "./types";
|
|
|
|
export function useAgentsApiEnabled() {
|
|
const { data, isPending } = useQuery({
|
|
queryKey: ["features", "agents_api"],
|
|
queryFn: () => fetchAgentsApiEnabled(),
|
|
// Re-check on every mount so flipping config.yaml + revisiting the
|
|
// agents section auto-enables the feature without a rebuild.
|
|
staleTime: 0,
|
|
refetchOnMount: true,
|
|
retry: false,
|
|
});
|
|
|
|
// localStorage only exists in the browser, so read the last-known value
|
|
// after mount (not during render). This keeps the first client render equal
|
|
// to the server's (cache unknown → fail open), avoiding a hydration mismatch
|
|
// on the non-loading-gated sidebar; the sticky value is applied on the next
|
|
// render.
|
|
const [cached, setCached] = useState<boolean | undefined>(undefined);
|
|
useEffect(() => {
|
|
setCached(readCachedAgentsApiEnabled());
|
|
}, []);
|
|
|
|
// Persist every definitive answer so a cold start during an /api/features
|
|
// outage can fall back to it instead of failing open and re-introducing the
|
|
// 403 storm (#3757).
|
|
useEffect(() => {
|
|
if (data !== undefined) {
|
|
writeCachedAgentsApiEnabled(data);
|
|
setCached(data);
|
|
}
|
|
}, [data]);
|
|
|
|
// A live answer wins; otherwise stay on the last-known value (sticky) and
|
|
// only fail open when nothing has ever been observed.
|
|
return {
|
|
enabled: resolveAgentsApiEnabled(data, cached),
|
|
isLoading: isPending,
|
|
};
|
|
}
|
|
|
|
export function useAgents() {
|
|
const { data, isLoading, error } = useQuery({
|
|
queryKey: ["agents"],
|
|
queryFn: () => listAgents(),
|
|
});
|
|
return { agents: data ?? [], isLoading, error };
|
|
}
|
|
|
|
export function useAgentToolGroups() {
|
|
const { data, isLoading, error } = useQuery({
|
|
queryKey: ["agent-tool-groups"],
|
|
queryFn: () => listAgentToolGroups(),
|
|
});
|
|
return { toolGroups: data ?? [], isLoading, error };
|
|
}
|
|
|
|
export function useAgent(name: string | null | undefined) {
|
|
const { data, isLoading, error } = useQuery({
|
|
queryKey: ["agents", name],
|
|
queryFn: () => getAgent(name!),
|
|
enabled: !!name,
|
|
});
|
|
return { agent: data ?? null, isLoading, error };
|
|
}
|
|
|
|
export function useCreateAgent() {
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: (request: CreateAgentRequest) => createAgent(request),
|
|
onSuccess: () => {
|
|
void queryClient.invalidateQueries({ queryKey: ["agents"] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useImportAgentPackage() {
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: ({
|
|
agentPackage,
|
|
name,
|
|
}: {
|
|
agentPackage: unknown;
|
|
name?: string;
|
|
}) => importAgentPackage(agentPackage, name),
|
|
onSuccess: () => {
|
|
void queryClient.invalidateQueries({ queryKey: ["agents"] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useUpdateAgent() {
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: ({
|
|
name,
|
|
request,
|
|
}: {
|
|
name: string;
|
|
request: UpdateAgentRequest;
|
|
}) => updateAgent(name, request),
|
|
onSuccess: (_data, { name }) => {
|
|
void queryClient.invalidateQueries({ queryKey: ["agents"] });
|
|
void queryClient.invalidateQueries({ queryKey: ["agents", name] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useDeleteAgent() {
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: (name: string) => deleteAgent(name),
|
|
onSuccess: () => {
|
|
void queryClient.invalidateQueries({ queryKey: ["agents"] });
|
|
},
|
|
});
|
|
}
|