Pick any line to see why it is the way it is: the commit, the pull request and issue it came from, and what the agent was thinking.
| Chat and workspace agents: channels, DMs and named agents you talk to | 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 { readAgentForm } from "../../../lib/agent-form"; | |
| 8 | import { workspaceAgents } from "../../../lib/services.server"; | |
| 9 | import { assertSameOrigin, requireUser, roleIn } from "../../../lib/session.server"; | |
| 10 | ||
| 11 | /** Saving makes a new version; every run records which one it ran. */ | |
| 12 | export async function action({ params, context, request }: Route.ActionArgs) { | |
| 13 | assertSameOrigin(request); | |
| 14 | const viewer = requireUser(context, request); | |
| 15 | const slug = params.owner.toLowerCase(); | |
| 16 | if (!roleIn(viewer, slug)) throw data(null, { status: 404 }); | |
| 17 | const read = readAgentForm(await request.formData()); | |
| 18 | if (!read.ok) return { errors: read.errors, saved: false }; | |
| 19 | const saved = await workspaceAgents.update(slug, params.handle.toLowerCase(), viewer, read.input).catch(() => null); | |
| 20 | if (!saved) return { errors: { form: "The agents service didn't answer. Try again in a moment." }, saved: false }; | |
| 21 | if (!saved.ok) return { errors: { [saved.error.code === "conflict" ? "handle" : "form"]: saved.error.message }, saved: false }; | |
| 22 | // A new handle is a new address. | |
| 23 | if (saved.value.handle !== params.handle.toLowerCase()) throw redirect(`/${slug}/-/agents/${saved.value.handle}/profile`); | |
| 24 | return { errors: undefined, saved: true }; | |
| 25 | } | |
| 26 | ||
| 27 | export default function Profile({ actionData }: Route.ComponentProps) { | |
| 28 | const agent = useOutletContext<WorkspaceAgent>(); | |
| 29 | const errors = actionData?.errors as Record<string, string> | undefined; | |
| 30 | return ( | |
| 31 | <> | |
| 32 | {actionData?.saved && ( | |
| 33 | <p role="status" className="mb-6 rounded-lg border border-success/30 bg-success/10 px-4 py-2.5 text-sm text-success"> | |
| 34 | Saved as version {agent.version}. | |
| 35 | </p> | |
| 36 | )} | |
| 37 | {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>} | |
| 38 | <AgentForm draft={agent} errors={errors} submit="Save changes" intent="update" formKey={`${agent.id}:${agent.version}`} /> | |
| 39 | </> | |
| 40 | ); | |
| 41 | } |