An open-source, faster Devin alternative
TypeScript
feat(web): optimistic credential ops + drop 'Use org default' picker option
Igor Zalutskic63016195e13
diff --git a/web/src/pages/Agents.tsx b/web/src/pages/Agents.tsxindex a662f83e..65b16495 100644--- a/web/src/pages/Agents.tsx+++ b/web/src/pages/Agents.tsx@@ -34,11 +34,10 @@ const MODELS = []const DEFAULT_MODEL = MODELS[0].value-// Agents run on the "claude" runtime → anthropic credentials. Sentinels for the-// credential picker's two non-credential choices.+// Agents run on the "claude" runtime → anthropic credentials. Sentinel for the+// "create one inline" choice in the credential picker.const CRED_PROVIDER = 'anthropic'const NEW_CRED = '__new__'-const ORG_DEFAULT = '__default__'export default function Agents() {const queryClient = useQueryClient()@@ -60,8 +59,8 @@ export default function Agents() {const [name, setName] = useState('')const [prompt, setPrompt] = useState('')const [model, setModel] = useState(DEFAULT_MODEL)- // Credential selection: a credential id, NEW_CRED (create inline), or ORG_DEFAULT- // (rely on the org default). '' is resolved to a sensible default on dialog open.+ // Credential selection: a credential id or NEW_CRED (create inline). '' is+ // resolved to a sensible default on dialog open.const [credChoice, setCredChoice] = useState('')const [newCredName, setNewCredName] = useState('')const [newCredKey, setNewCredKey] = useState('')@@ -87,7 +86,7 @@ export default function Agents() {setShowCreate(true)}- // Build options: existing creds + "New credential…" + (when one exists) "Use org default".+ // Build options: existing creds + "New credential…".const credOptions = [...anthropicCreds.map((c) => ({value: c.id,@@ -96,15 +95,13 @@ export default function Agents() {}`,})),{ value: NEW_CRED, label: '+ New credential…' },- ...(hasDefault ? [{ value: ORG_DEFAULT, label: 'Use org default' }] : []),]const createMutation = useMutation({mutationFn: async () => {// Resolve the agent's credential without leaving the dialog:- // - NEW_CRED → create the credential first, pin the new id.- // - ORG_DEFAULT → pin nothing (sessions fall back to the org default).- // - else → pin the chosen existing credential.+ // - NEW_CRED → create the credential first, pin the new id.+ // - else → pin the chosen existing credential.let credentialId: string | undefinedif (credChoice === NEW_CRED) {const cred = await createCredential({@@ -114,7 +111,7 @@ export default function Agents() {is_default: !hasDefault, // first key becomes the org default})credentialId = cred.id- } else if (credChoice && credChoice !== ORG_DEFAULT) {+ } else if (credChoice) {credentialId = credChoice}return createAgent({@@ -322,11 +319,7 @@ export default function Agents() {<Fieldlabel="Credential"htmlFor="agent-cred"- description={- credChoice === ORG_DEFAULT- ? 'Sessions use your org default Anthropic credential.'- : 'The Anthropic key this agent runs on. Reuse one from Credentials, or add a new one here.'- }+ description="The Anthropic key this agent runs on. Reuse one from Credentials, or add a new one here."><Selectid="agent-cred"diff --git a/web/src/pages/Credentials.tsx b/web/src/pages/Credentials.tsxindex 60ced88b..f74029e7 100644--- a/web/src/pages/Credentials.tsx+++ b/web/src/pages/Credentials.tsx@@ -59,6 +59,19 @@ export default function Credentials() {setMakeDefault(true)}+ // These operations chain through the edge → sessions-api → Infisical, so each+ // can take a second or two. We apply the change to the cache immediately and+ // reconcile on settle; on failure we roll the cache back and surface the error,+ // so the UI feels instant without ever showing a state the server rejected.+ const CREDS_KEY = ['credentials'] as const+ const snapshot = async () => {+ await queryClient.cancelQueries({ queryKey: CREDS_KEY })+ return queryClient.getQueryData<Credential[]>(CREDS_KEY)+ }+ const rollback = (previous?: Credential[]) => {+ if (previous) queryClient.setQueryData(CREDS_KEY, previous)+ }+const createMutation = useMutation({mutationFn: () =>createCredential({@@ -67,26 +80,83 @@ export default function Credentials() {name: name.trim() || undefined,is_default: makeDefault,}),- onSuccess: () => {- void queryClient.invalidateQueries({ queryKey: ['credentials'] })+ onMutate: async () => {+ const previous = await snapshot()+ // Optimistic row: real last4 (client-derived), temp id replaced on settle.+ const optimistic: Credential = {+ id: `temp_${Date.now()}`,+ provider,+ name: name.trim() || undefined,+ last4: key.trim().slice(-4),+ is_default: makeDefault,+ created_at: new Date().toISOString(),+ }+ queryClient.setQueryData<Credential[]>(CREDS_KEY, (old = []) => [+ optimistic,+ // a new default clears the previous one for the same provider+ ...(makeDefault+ ? old.map((c) =>+ c.provider === provider ? { ...c, is_default: false } : c,+ )+ : old),+ ])+ // Close instantly, but keep the form values so a failure can reopen with the+ // typed key intact (it's never recoverable once the row truly exists).+ const form = { name, provider, key, makeDefault }setShowCreate(false)- resetForm()+ return { previous, form }},- onError: (e) => notifyError("Couldn't add the credential.", e),+ onError: (e, _vars, ctx) => {+ rollback(ctx?.previous)+ notifyError("Couldn't add the credential.", e)+ if (ctx?.form) {+ setName(ctx.form.name)+ setProvider(ctx.form.provider)+ setKey(ctx.form.key)+ setMakeDefault(ctx.form.makeDefault)+ setShowCreate(true) // reopen so the user can retry without re-typing+ }+ },+ onSuccess: () => resetForm(),+ onSettled: () => queryClient.invalidateQueries({ queryKey: CREDS_KEY }),})const defaultMutation = useMutation({mutationFn: (id: string) => setDefaultCredential(id),- onError: (e) => notifyError("Couldn't set the default.", e),- onSettled: () =>- queryClient.invalidateQueries({ queryKey: ['credentials'] }),+ onMutate: async (id) => {+ const previous = await snapshot()+ const target = previous?.find((c) => c.id === id)+ queryClient.setQueryData<Credential[]>(CREDS_KEY, (old = []) =>+ old.map((c) =>+ c.provider === target?.provider+ ? { ...c, is_default: c.id === id }+ : c,+ ),+ )+ return { previous }+ },+ onError: (e, _id, ctx) => {+ rollback(ctx?.previous)+ notifyError("Couldn't set the default.", e)+ },+ onSettled: () => queryClient.invalidateQueries({ queryKey: CREDS_KEY }),})const deleteMutation = useMutation({mutationFn: (id: string) => deleteCredential(id),- onError: (e) => notifyError("Couldn't delete the credential.", e),- onSettled: () =>- queryClient.invalidateQueries({ queryKey: ['credentials'] }),+ onMutate: async (id) => {+ const previous = await snapshot()+ queryClient.setQueryData<Credential[]>(CREDS_KEY, (old = []) =>+ old.filter((c) => c.id !== id),+ )+ setToDelete(null) // dismiss the confirm immediately+ return { previous }+ },+ onError: (e, _id, ctx) => {+ rollback(ctx?.previous)+ notifyError("Couldn't delete the credential.", e)+ },+ onSettled: () => queryClient.invalidateQueries({ queryKey: CREDS_KEY }),})const columns: Column<Credential>[] = [@@ -296,12 +366,8 @@ export default function Credentials() {description="Agents pinned to it, and sessions that fall back to it, will stop running until you point them at another credential. This can't be undone."confirmLabel="Delete credential"destructive- pending={deleteMutation.isPending}onConfirm={() => {- if (!toDelete) return- deleteMutation.mutate(toDelete.id, {- onSuccess: () => setToDelete(null),- })+ if (toDelete) deleteMutation.mutate(toDelete.id)}}/></div>