智能体分权

This commit is contained in:
吉鑫升 committed 2026-05-13 11:36:17 +08:00
1 parent e7a455f22e
commit cf2e4bdd9c
31 files changed
+1237 -436

No files matched your search

+7 -7
View File
@@ -22,9 +22,9 @@ export async function listAgents(): Promise<Agent[]> {
return data.agents;
}
export async function getAgent(name: string): Promise<Agent> {
const res = await apiFetch(`${getBackendBaseURL()}/api/agents/${name}`);
if (!res.ok) throw new Error(`Agent '${name}' not found`);
export async function getAgent(id: string): Promise<Agent> {
const res = await apiFetch(`${getBackendBaseURL()}/api/agents/${id}`);
if (!res.ok) throw new Error(`Agent '${id}' not found`);
return res.json() as Promise<Agent>;
}
@@ -42,10 +42,10 @@ export async function createAgent(request: CreateAgentRequest): Promise<Agent> {
}
export async function updateAgent(
name: string,
id: string,
request: UpdateAgentRequest,
): Promise<Agent> {
const res = await apiFetch(`${getBackendBaseURL()}/api/agents/${name}`, {
const res = await apiFetch(`${getBackendBaseURL()}/api/agents/${id}`, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(request),
@@ -57,8 +57,8 @@ export async function updateAgent(
return res.json() as Promise<Agent>;
}
export async function deleteAgent(name: string): Promise<void> {
const res = await apiFetch(`${getBackendBaseURL()}/api/agents/${name}`, {
export async function deleteAgent(id: string): Promise<void> {
const res = await apiFetch(`${getBackendBaseURL()}/api/agents/${id}`, {
method: "DELETE",
});
if (!res.ok) throw new Error(`Failed to delete agent: ${res.statusText}`);
+10 -10
View File
@@ -17,11 +17,11 @@ export function useAgents() {
return { agents: data ?? [], isLoading, error };
}
export function useAgent(name: string | null | undefined) {
export function useAgent(id: string | null | undefined) {
const { data, isLoading, error } = useQuery({
queryKey: ["agents", name],
queryFn: () => getAgent(name!),
enabled: !!name,
queryKey: ["agents", id],
queryFn: () => getAgent(id!),
enabled: !!id,
});
return { agent: data ?? null, isLoading, error };
}
@@ -40,15 +40,15 @@ export function useUpdateAgent() {
const queryClient = useQueryClient();
return useMutation({
mutationFn: ({
name,
id,
request,
}: {
name: string;
id: string;
request: UpdateAgentRequest;
}) => updateAgent(name, request),
onSuccess: (_data, { name }) => {
}) => updateAgent(id, request),
onSuccess: (_data, { id }) => {
void queryClient.invalidateQueries({ queryKey: ["agents"] });
void queryClient.invalidateQueries({ queryKey: ["agents", name] });
void queryClient.invalidateQueries({ queryKey: ["agents", id] });
},
});
}
@@ -56,7 +56,7 @@ export function useUpdateAgent() {
export function useDeleteAgent() {
const queryClient = useQueryClient();
return useMutation({
mutationFn: (name: string) => deleteAgent(name),
mutationFn: (id: string) => deleteAgent(id),
onSuccess: () => {
void queryClient.invalidateQueries({ queryKey: ["agents"] });
},
+16 -7
View File
@@ -1,25 +1,34 @@
export interface Agent {
id: string;
name: string;
description: string;
model: string | null;
tool_groups: string[] | null;
skills: string[] | null;
user_id: string | null;
published: boolean;
builtin: boolean;
created_at?: string | null;
updated_at?: string | null;
soul?: string | null;
model?: string | null;
tool_groups?: string[] | null;
skills?: string[] | null;
}
export interface CreateAgentRequest {
name: string;
description?: string;
soul?: string;
published?: boolean;
skills?: string[] | null;
model?: string | null;
tool_groups?: string[] | null;
skills?: string[] | null;
soul?: string;
}
export interface UpdateAgentRequest {
name?: string | null;
description?: string | null;
soul?: string | null;
published?: boolean | null;
skills?: string[] | null;
model?: string | null;
tool_groups?: string[] | null;
skills?: string[] | null;
soul?: string | null;
}
+6 -3
View File
@@ -28,6 +28,7 @@ export type ThreadStreamOptions = {
threadId?: string | null | undefined;
context: LocalSettings["context"];
isMock?: boolean;
assistantId?: string;
onStart?: (threadId: string) => void;
onFinish?: (state: AgentThreadState) => void;
onToolEnd?: (event: ToolEndEvent) => void;
@@ -139,6 +140,7 @@ export function useThreadStream({
threadId,
context,
isMock,
assistantId,
onStart,
onFinish,
onToolEnd,
@@ -204,7 +206,7 @@ export function useThreadStream({
const thread = useStream<AgentThreadState>({
client: getAPIClient(isMock),
assistantId: "lead_agent",
assistantId: assistantId ?? "lead_agent",
threadId: onStreamThreadId,
reconnectOnMount: runMetadataStorageRef.current
? () => runMetadataStorageRef.current!
@@ -213,10 +215,11 @@ export function useThreadStream({
onCreated(meta) {
handleStreamStart(meta.thread_id);
setOnStreamThreadId(meta.thread_id);
if (context.agent_name && !isMock) {
const metadataAgentId = context.agent_id ?? context.agent_name;
if (metadataAgentId && !isMock) {
void getAPIClient()
.threads.update(meta.thread_id, {
metadata: { agent_name: context.agent_name },
metadata: { agent_id: metadataAgentId },
})
.catch(() => ({}));
}
+2
View File
@@ -16,6 +16,8 @@ export interface AgentThreadContext extends Record<string, unknown> {
is_plan_mode: boolean;
subagent_enabled: boolean;
reasoning_effort?: "minimal" | "low" | "medium" | "high";
agent_id?: string;
/** @deprecated kept for reading legacy thread metadata; prefer agent_id */
agent_name?: string;
}
+23 -16
View File
@@ -2,34 +2,41 @@ import type { Message } from "@langchain/langgraph-sdk";
import type { AgentThread, AgentThreadContext } from "./types";
type ThreadAgentRef = Pick<AgentThreadContext, "agent_id" | "agent_name">;
type ThreadRouteTarget =
| string
| {
thread_id: string;
context?: Pick<AgentThreadContext, "agent_name"> | null;
context?: ThreadAgentRef | null;
metadata?: Record<string, unknown> | null;
};
function pickAgentRef(
context: ThreadAgentRef | null | undefined,
metadata: Record<string, unknown> | null | undefined,
): string | undefined {
if (context?.agent_id) return context.agent_id;
if (context?.agent_name) return context.agent_name;
const metaId = metadata?.agent_id;
if (typeof metaId === "string" && metaId) return metaId;
const metaName = metadata?.agent_name;
if (typeof metaName === "string" && metaName) return metaName;
return undefined;
}
export function pathOfThread(
thread: ThreadRouteTarget,
context?: Pick<AgentThreadContext, "agent_name"> | null,
context?: ThreadAgentRef | null,
) {
const threadId = typeof thread === "string" ? thread : thread.thread_id;
let agentName: string | undefined;
if (typeof thread === "string") {
agentName = context?.agent_name;
} else {
agentName = thread.context?.agent_name;
if (!agentName) {
const metaAgent = thread.metadata?.agent_name;
if (typeof metaAgent === "string") {
agentName = metaAgent;
}
}
}
const agentRef =
typeof thread === "string"
? pickAgentRef(context, null)
: pickAgentRef(thread.context, thread.metadata);
return agentName
? `/workspace/agents/${encodeURIComponent(agentName)}/chats/${threadId}`
return agentRef
? `/workspace/agents/${encodeURIComponent(agentRef)}/chats/${threadId}`
: `/workspace/chats/${threadId}`;
}