Skip to content
112 linesCodeBlameRaw
1import { useState } from "react";
2import { Form, data, redirect, useNavigation, useOutletContext } from "react-router";
3
4import type { WorkspaceAgent } from "@g1t/contracts";
5
6import type { Route } from "./+types/profile";
7import { AgentForm } from "../../../components/agents-mode";
8import { isOrchestrator } from "../../../components/orchestrator";
9import { readAgentForm } from "../../../lib/agent-form";
10import { identity, workspaceAgents } from "../../../lib/services.server";
11import { assertSameOrigin, requireUser, roleIn } from "../../../lib/session.server";
12
13/** The workspace's teams, to put the agent on one. */
14export async function loader({ params, context, request }: Route.LoaderArgs) {
15 const viewer = requireUser(context, request);
16 const teams = await identity.listTeams(viewer, params.owner).catch(() => null);
17 return { teams: teams?.ok ? teams.value.map((team) => ({ slug: team.slug, name: team.name })) : [] };
18}
19
20/** Saving makes a new version; every run records which one it ran. */
21export async function action({ params, context, request }: Route.ActionArgs) {
22 assertSameOrigin(request);
23 const viewer = requireUser(context, request);
24 const slug = params.owner.toLowerCase();
25 if (!roleIn(viewer, slug)) throw data(null, { status: 404 });
26 const form = await request.formData();
27 // Archiving keeps the agent's history (runs, replies, spend) but takes it
28 // out of chat and frees its handle. g1t itself can't be archived.
29 if (form.get("intent") === "archive") {
30 const archived = await workspaceAgents.archive(slug, params.handle.toLowerCase(), viewer).catch(() => null);
31 if (!archived) return { errors: { form: "The agents service didn't answer. Try again in a moment." }, saved: false };
32 if (!archived.ok) return { errors: { form: archived.error.message }, saved: false };
33 throw redirect(`/${slug}/-/agents`);
34 }
35 const read = readAgentForm(form, { orchestrator: params.handle.toLowerCase() === "g1t" });
36 if (!read.ok) return { errors: read.errors, saved: false };
37 const saved = await workspaceAgents.update(slug, params.handle.toLowerCase(), viewer, read.input).catch(() => null);
38 if (!saved) return { errors: { form: "The agents service didn't answer. Try again in a moment." }, saved: false };
39 if (!saved.ok) return { errors: { [saved.error.code === "conflict" ? "handle" : "form"]: saved.error.message }, saved: false };
40 // A new handle is a new address.
41 if (saved.value.handle !== params.handle.toLowerCase()) throw redirect(`/${slug}/-/agents/${saved.value.handle}/profile`);
42 return { errors: undefined, saved: true };
43}
44
45export default function Profile({ loaderData, actionData }: Route.ComponentProps) {
46 const agent = useOutletContext<WorkspaceAgent>();
47 const errors = actionData?.errors as Record<string, string> | undefined;
48 return (
49 <>
50 {actionData?.saved && (
51 <p role="status" className="mb-6 rounded-lg border border-success/30 bg-success/10 px-4 py-2.5 text-sm text-success">
52 Saved as version {agent.version}.
53 </p>
54 )}
55 {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>}
56 <AgentForm
57 draft={agent}
58 errors={errors}
59 submit="Save changes"
60 intent="update"
61 formKey={`${agent.id}:${agent.version}`}
62 locked={isOrchestrator(agent)}
63 teams={loaderData.teams}
64 seed={agent.avatar_seed || agent.id}
65 />
66 {!isOrchestrator(agent) && <Archive name={agent.display_name} />}
67 </>
68 );
69}
70
71/** Retiring an agent: asked twice, since it leaves every channel at once. */
72function Archive({ name }: { name: string }) {
73 const [asking, setAsking] = useState(false);
74 const busy = useNavigation().formData?.get("intent") === "archive";
75 return (
76 <section className="mt-12 border-t border-line pt-8 md:grid md:grid-cols-[14rem_1fr] md:gap-10">
77 <div className="mb-4 md:mb-0">
78 <h2 className="text-sm font-semibold text-fg">Archive</h2>
79 <p className="mt-1 text-sm text-muted">Retire this agent. Only owners can.</p>
80 </div>
81 <div>
82 <p className="text-sm text-muted">
83 {name} stops answering, leaves every channel and DM, and its handle is free to use again. Its runs, replies and spend stay in
84 the audit log and on Usage.
85 </p>
86 {asking ? (
87 <Form method="post" className="mt-4 flex flex-wrap items-center gap-3">
88 <input type="hidden" name="intent" value="archive" />
89 <button
90 type="submit"
91 disabled={busy}
92 className="rounded-lg border border-danger/50 bg-danger/15 px-4 py-2 text-sm font-medium text-danger hover:bg-danger/25 disabled:opacity-60"
93 >
94 {busy ? "Archiving…" : `Archive ${name}`}
95 </button>
96 <button type="button" onClick={() => setAsking(false)} className="px-2 py-2 text-sm text-muted hover:text-fg">
97 Keep {name}
98 </button>
99 </Form>
100 ) : (
101 <button
102 type="button"
103 onClick={() => setAsking(true)}
104 className="mt-4 rounded-lg border border-line px-4 py-2 text-sm text-muted hover:border-danger/50 hover:text-danger"
105 >
106 Archive agent…
107 </button>
108 )}
109 </div>
110 </section>
111 );
112}