| 1 | import { data, redirect, useOutletContext } from "react-router"; |
| 2 | |
| 3 | import type { WorkspaceAgent } from "@g1t/contracts"; |
| 4 | |
| 5 | import type { Route } from "./+types/profile"; |
| 6 | import { AgentForm } from "../../../components/agents-mode"; |
| 7 | import { isOrchestrator } from "../../../components/orchestrator"; |
| 8 | import { readAgentForm } from "../../../lib/agent-form"; |
| 9 | import { identity, workspaceAgents } from "../../../lib/services.server"; |
| 10 | import { assertSameOrigin, requireUser, roleIn } from "../../../lib/session.server"; |
| 11 | |
| 12 | /** The workspace's teams, to put the agent on one. */ |
| 13 | export async function loader({ params, context, request }: Route.LoaderArgs) { |
| 14 | const viewer = requireUser(context, request); |
| 15 | const teams = await identity.listTeams(viewer, params.owner).catch(() => null); |
| 16 | return { teams: teams?.ok ? teams.value.map((team) => ({ slug: team.slug, name: team.name })) : [] }; |
| 17 | } |
| 18 | |
| 19 | /** Saving makes a new version; every run records which one it ran. */ |
| 20 | export async function action({ params, context, request }: Route.ActionArgs) { |
| 21 | assertSameOrigin(request); |
| 22 | const viewer = requireUser(context, request); |
| 23 | const slug = params.owner.toLowerCase(); |
| 24 | if (!roleIn(viewer, slug)) throw data(null, { status: 404 }); |
| 25 | const read = readAgentForm(await request.formData(), { orchestrator: params.handle.toLowerCase() === "g1t" }); |
| 26 | if (!read.ok) return { errors: read.errors, saved: false }; |
| 27 | const saved = await workspaceAgents.update(slug, params.handle.toLowerCase(), viewer, read.input).catch(() => null); |
| 28 | if (!saved) return { errors: { form: "The agents service didn't answer. Try again in a moment." }, saved: false }; |
| 29 | if (!saved.ok) return { errors: { [saved.error.code === "conflict" ? "handle" : "form"]: saved.error.message }, saved: false }; |
| 30 | // A new handle is a new address. |
| 31 | if (saved.value.handle !== params.handle.toLowerCase()) throw redirect(`/${slug}/-/agents/${saved.value.handle}/profile`); |
| 32 | return { errors: undefined, saved: true }; |
| 33 | } |
| 34 | |
| 35 | export default function Profile({ loaderData, actionData }: Route.ComponentProps) { |
| 36 | const agent = useOutletContext<WorkspaceAgent>(); |
| 37 | const errors = actionData?.errors as Record<string, string> | undefined; |
| 38 | return ( |
| 39 | <> |
| 40 | {actionData?.saved && ( |
| 41 | <p role="status" className="mb-6 rounded-lg border border-success/30 bg-success/10 px-4 py-2.5 text-sm text-success"> |
| 42 | Saved as version {agent.version}. |
| 43 | </p> |
| 44 | )} |
| 45 | {errors?.form && <p className="mb-6 rounded-lg border border-danger/40 bg-danger/10 px-4 py-3 text-sm text-danger">{errors.form}</p>} |
| 46 | <AgentForm |
| 47 | draft={agent} |
| 48 | errors={errors} |
| 49 | submit="Save changes" |
| 50 | intent="update" |
| 51 | formKey={`${agent.id}:${agent.version}`} |
| 52 | locked={isOrchestrator(agent)} |
| 53 | teams={loaderData.teams} |
| 54 | seed={agent.avatar_seed || agent.id} |
| 55 | /> |
| 56 | </> |
| 57 | ); |
| 58 | } |