AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
import { useCreateRaisedExpert } from "@/app/api/__generated__/endpoints/experts/experts";
import type { RaiseResult } from "@/app/api/__generated__/models/raiseResult";
import { toast } from "@/components/molecules/Toast/use-toast";
import { ApiError } from "@/lib/autogpt-server-api/helpers";
import { invalidateExpertRosterQueries } from "@/services/experts/invalidate-experts";
import { useQueryClient } from "@tanstack/react-query";
import { useRouter } from "next/navigation";
import { useRef, useState } from "react";
import { roleFor } from "./components/CategoryStep/helpers";
import {
failedAttachmentMessage,
toRaiseAttachments,
} from "./components/KitStep/helpers";
import {
clearDraft,
getExpertLimitCode,
type RaiseDraft,
type RaiseKit,
} from "./helpers";
export function useRaiseSubmission() {
const queryClient = useQueryClient();
const router = useRouter();
const { mutateAsync: createRaisedExpert, isPending } =
useCreateRaisedExpert();
// The latch survives re-renders so a double click cannot fire two POSTs
// before `isPending` has flipped; it is released again on failure so the
// user can retry.
const submitLatch = useRef(false);
const [isLocked, setIsLocked] = useState(false);
async function finish(draft: RaiseDraft, kit: RaiseKit) {
if (submitLatch.current) return;
submitLatch.current = true;
setIsLocked(true);
try {
const response = await createRaisedExpert({
data: {
name: draft.name,
role: draft.legacyRole ?? roleFor(draft.category),
job_title: draft.jobTitle || null,
color: draft.color,
avatar_url: draft.avatarUrl || null,
about: draft.about || null,
voice_preferences: draft.voicePreferences || null,
weekly_budget: kit.weeklyBudget,
attachments: toRaiseAttachments(kit.attachments),
},
});
const result = response.data as RaiseResult;
if (result.failed_attachments?.length) {
toast({
title: `Created ${draft.name || "your expert"}, but some tools didn't attach`,
description: failedAttachmentMessage(
result.failed_attachments,
kit.attachments,
),
});
}
clearDraft();
// The copilot page only fires the kickoff for an expert it finds in the
// roster, and the sidebar keeps that roster cached, so refresh it before
// handing over or the new expert is treated as unknown and the kickoff
// param is dropped.
await invalidateExpertRosterQueries(queryClient);
// kickoff=1 has the expert open the thread itself: introduce who it is,
// say what it can take on, and start or ask for its first job.
router.push(
`/copilot?expertId=${encodeURIComponent(result.expert.id)}&kickoff=1`,
);
} catch (error) {
submitLatch.current = false;
setIsLocked(false);
reportFailure(error, draft.name);
}
}
return { finish, isSubmitting: isPending || isLocked };
}
function reportFailure(error: unknown, name: string) {
if (error instanceof ApiError && error.status === 409) {
if (getExpertLimitCode(error.response) === "raised_expert_lifetime_limit") {
toast({
title: "Expert creation limit reached",
description:
"This account has reached its lifetime limit for created Experts. Contact support if you need more capacity.",
variant: "destructive",
});
return;
}
toast({
title: "Your team is full",
description:
"You've reached the limit of active experts. Archive one from your team page to create another.",
variant: "destructive",
});
return;
}
toast({
title: `Couldn't create ${name || "your expert"}`,
description:
error instanceof ApiError && [400, 422, 503].includes(error.status)
? error.message
: "Something went wrong. Please try again.",
variant: "destructive",
});
}