Skip to content
210 linesCodeBlameRaw
1import { ChevronRight, History } from "lucide-react";
2import { useState } from "react";
3import { Form, data, redirect, useNavigation, useOutletContext } from "react-router";
4
5import type { AgentVersion, WorkspaceAgent } from "@g1t/contracts";
6
7import type { Route } from "./+types/profile";
8import { AgentForm } from "../../../components/agents-mode";
9import { readOrNull } from "../../../components/agents/actions.server";
10import { versionChanges } from "../../../components/agents/format";
11import { TimeAgo } from "../../../components/ui";
12import { isOrchestrator } from "../../../components/orchestrator";
13import { readAgentForm } from "../../../lib/agent-form";
14import { docs, identity, workspaceAgents } from "../../../lib/services.server";
15import { assertSameOrigin, requireUser, roleIn } from "../../../lib/session.server";
16
17/** The workspace's teams, to put the agent on one, and its saved versions. */
18export async function loader({ params, context, request }: Route.LoaderArgs) {
19 const viewer = requireUser(context, request);
20 const [teams, versions, spaces] = await Promise.all([
21 identity.listTeams(viewer, params.owner).catch(() => null),
22 readOrNull(workspaceAgents.versions(params.owner.toLowerCase(), params.handle.toLowerCase(), viewer)),
23 readingSpaces(params.owner.toLowerCase(), viewer),
24 ]);
25 return { teams: teams?.ok ? teams.value.map((team) => ({ slug: team.slug, name: team.name })) : [], versions, spaces };
26}
27
28/** The Docs spaces the viewer can read, for an agent's required reading; none when Docs can't say. */
29async function readingSpaces(slug: string, viewer: Parameters<typeof docs.sidebar>[1]): Promise<{ id: string; name: string; kind: string }[]> {
30 const sidebar = await docs.sidebar(slug, viewer).catch(() => null);
31 return sidebar?.ok ? sidebar.value.spaces.filter((space) => !space.archived_at).map((space) => ({ id: space.id, name: space.name, kind: space.kind })) : [];
32}
33
34
35/** Saving makes a new version; every run records which one it ran. */
36export async function action({ params, context, request }: Route.ActionArgs) {
37 assertSameOrigin(request);
38 const viewer = requireUser(context, request);
39 const slug = params.owner.toLowerCase();
40 if (!roleIn(viewer, slug)) throw data(null, { status: 404 });
41 const form = await request.formData();
42 // Archiving keeps the agent's history (runs, replies, spend) but takes it
43 // out of chat and frees its handle. g1t itself can't be archived.
44 if (form.get("intent") === "archive") {
45 const archived = await workspaceAgents.archive(slug, params.handle.toLowerCase(), viewer).catch(() => null);
46 if (!archived) return { errors: { form: "The agents service didn't answer. Try again in a moment." }, saved: false };
47 if (!archived.ok) return { errors: { form: archived.error.message }, saved: false };
48 throw redirect(`/${slug}/-/agents`);
49 }
50 const read = readAgentForm(form, { orchestrator: params.handle.toLowerCase() === "g1t" });
51 if (!read.ok) return { errors: read.errors, saved: false };
52 const saved = await workspaceAgents.update(slug, params.handle.toLowerCase(), viewer, read.input).catch(() => null);
53 if (!saved) return { errors: { form: "The agents service didn't answer. Try again in a moment." }, saved: false };
54 if (!saved.ok) return { errors: { [saved.error.code === "conflict" ? "handle" : "form"]: saved.error.message }, saved: false };
55 // A new handle is a new address.
56 if (saved.value.handle !== params.handle.toLowerCase()) throw redirect(`/${slug}/-/agents/${saved.value.handle}/profile`);
57 return { errors: undefined, saved: true };
58}
59
60export default function Profile({ loaderData, actionData }: Route.ComponentProps) {
61 const agent = useOutletContext<WorkspaceAgent>();
62 const errors = actionData?.errors as Record<string, string> | undefined;
63 return (
64 <>
65 {actionData?.saved && (
66 <p role="status" className="mb-6 rounded-lg border border-success/30 bg-success/10 px-4 py-2.5 text-sm text-success">
67 Saved as version {agent.version}.
68 </p>
69 )}
70 {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>}
71 <AgentForm
72 draft={agent}
73 errors={errors}
74 submit="Save changes"
75 intent="update"
76 formKey={`${agent.id}:${agent.version}`}
77 locked={isOrchestrator(agent)}
78 teams={loaderData.teams} spaces={loaderData.spaces}
79 seed={agent.avatar_seed || agent.id}
80 />
81 {loaderData.versions && loaderData.versions.length > 0 && <Versions versions={loaderData.versions} current={agent.version} />}
82 {!isOrchestrator(agent) && <Archive name={agent.display_name} />}
83 </>
84 );
85}
86
87/** Retiring an agent: asked twice, since it leaves every channel at once. */
88function Archive({ name }: { name: string }) {
89 const [asking, setAsking] = useState(false);
90 const busy = useNavigation().formData?.get("intent") === "archive";
91 return (
92 <section className="mt-12 border-t border-line pt-8 md:grid md:grid-cols-[14rem_1fr] md:gap-10">
93 <div className="mb-4 md:mb-0">
94 <h2 className="text-sm font-semibold text-fg">Archive</h2>
95 <p className="mt-1 text-sm text-muted">Retire this agent. Only owners can.</p>
96 </div>
97 <div>
98 <p className="text-sm text-muted">
99 {name} stops answering, leaves every channel and DM, and its handle is free to use again. Its runs, replies and spend stay in
100 the audit log and on Usage.
101 </p>
102 {asking ? (
103 <Form method="post" className="mt-4 flex flex-wrap items-center gap-3">
104 <input type="hidden" name="intent" value="archive" />
105 <button
106 type="submit"
107 disabled={busy}
108 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"
109 >
110 {busy ? "Archiving…" : `Archive ${name}`}
111 </button>
112 <button type="button" onClick={() => setAsking(false)} className="px-2 py-2 text-sm text-muted hover:text-fg">
113 Keep {name}
114 </button>
115 </Form>
116 ) : (
117 <button
118 type="button"
119 onClick={() => setAsking(true)}
120 className="mt-4 rounded-lg border border-line px-4 py-2 text-sm text-muted hover:border-danger/50 hover:text-danger"
121 >
122 Archive agent…
123 </button>
124 )}
125 </div>
126 </section>
127 );
128}
129
130/**
131 * Every saved version of the agent, newest first: who saved it, when, and
132 * what changed from the one before. Each run and reply records the version
133 * it ran with.
134 */
135function Versions({ versions, current }: { versions: AgentVersion[]; current: number }) {
136 const [open, setOpen] = useState(false);
137 const sorted = [...versions].sort((a, b) => b.version - a.version);
138 const shown = open ? sorted : sorted.slice(0, 3);
139 return (
140 <section className="mt-12 border-t border-line pt-8 md:grid md:grid-cols-[14rem_1fr] md:gap-10">
141 <div className="mb-4 md:mb-0">
142 <h2 className="flex items-center gap-2 text-sm font-semibold text-fg">
143 <History size={14} className="text-faint" />
144 Version history
145 </h2>
146 <p className="mt-1 text-sm text-muted">Every save is a new version. Sessions and replies record the one they ran with.</p>
147 </div>
148 <div>
149 <ol className="divide-y divide-line/60 overflow-hidden rounded-xl border border-line bg-surface">
150 {shown.map((version) => {
151 const before = sorted.find((v) => v.version < version.version) ?? null;
152 const definition = version.definition as Record<string, unknown>;
153 return (
154 <li key={version.version} className="px-4 py-3">
155 <details className="group">
156 <summary className="flex cursor-pointer list-none items-center gap-3 text-sm [&::-webkit-details-marker]:hidden">
157 <ChevronRight size={14} className="shrink-0 text-faint transition-transform group-open:rotate-90" />
158 <span className="font-medium tabular-nums">Version {version.version}</span>
159 {version.version === current && <span className="rounded-full bg-accent/15 px-1.5 py-px text-[0.6875rem] font-medium text-accent">Current</span>}
160 <span className="min-w-0 grow truncate text-muted">
161 {before ? versionChanges(before.definition as Record<string, unknown>, definition) : version.version === 1 ? "Created" : "Saved"}
162 </span>
163 <span className="shrink-0 text-xs text-faint">
164 @{version.changed_by} · <TimeAgo at={version.created_at} />
165 </span>
166 </summary>
167 <dl className="mt-3 grid gap-x-6 gap-y-2 pl-7 text-xs sm:grid-cols-2">
168 {summaryOf(definition).map(([label, value]) => (
169 <div key={label} className="min-w-0">
170 <dt className="text-faint">{label}</dt>
171 <dd className="mt-0.5 line-clamp-3 text-fg-soft">{value}</dd>
172 </div>
173 ))}
174 </dl>
175 </details>
176 </li>
177 );
178 })}
179 </ol>
180 {sorted.length > 3 && (
181 <button type="button" onClick={() => setOpen(!open)} className="mt-3 text-sm text-muted hover:text-fg">
182 {open ? "Show fewer" : `Show all ${sorted.length} versions`}
183 </button>
184 )}
185 </div>
186 </section>
187 );
188}
189
190/** A saved definition, in a few lines. */
191function summaryOf(definition: Record<string, unknown>): [string, string][] {
192 const out: [string, string][] = [];
193 const text = (value: unknown) => (typeof value === "string" && value.trim() ? value.trim() : null);
194 const name = text(definition.display_name);
195 if (name) out.push(["Name", `${name}${text(definition.handle) ? ` (@${definition.handle})` : ""}`]);
196 const title = text(definition.title) ?? text(definition.role);
197 if (title) out.push(["Role", title]);
198 const job = text(definition.instructions);
199 if (job) out.push(["Job", job]);
200 const voice = text(definition.personality_preset);
201 if (voice) out.push(["Personality", [voice, text(definition.personality)].filter(Boolean).join(" · ")]);
202 const budget = definition.budget as { monthly_micros?: number | null; task_micros?: number | null } | undefined;
203 if (budget && (budget.monthly_micros != null || budget.task_micros != null)) {
204 const dollars = (m: number) => `$${(m / 1_000_000).toLocaleString("en-US", { maximumFractionDigits: 2 })}`;
205 out.push(["Budget", [budget.monthly_micros != null ? `${dollars(budget.monthly_micros)} a month` : null, budget.task_micros != null ? `${dollars(budget.task_micros)} a session` : null].filter(Boolean).join(", ")]);
206 }
207 const duties = Array.isArray(definition.responsibilities) ? (definition.responsibilities as string[]) : [];
208 if (duties.length > 0) out.push(["Responsibilities", duties.join("; ")]);
209 return out;
210}