Skip to content

The open-source agent harness - the runtime layer that turns an LLM into a working agent.

chore: add custom model provider edit flow (#226)

sayan-truefoundryf6ba61e459b6
diff --git a/packages/trueforge-ui-sdk/src/containers/SettingsBuilder/CustomModelProviderForm.tsx b/packages/trueforge-ui-sdk/src/containers/SettingsBuilder/CustomModelProviderForm.tsx
index a49cc3676..2282bba97 100644
--- a/packages/trueforge-ui-sdk/src/containers/SettingsBuilder/CustomModelProviderForm.tsx
+++ b/packages/trueforge-ui-sdk/src/containers/SettingsBuilder/CustomModelProviderForm.tsx
@@ -23,10 +23,14 @@ export type CustomProviderDraft = {
}>;
};
+export type CustomProviderInitialValues = Omit<CustomProviderDraft, 'apiKey'>;
+
type CustomModelProviderFormProps = {
open: boolean;
onOpenChange: (open: boolean) => void;
- onAdd: (draft: CustomProviderDraft) => void | Promise<void>;
+ onSubmit: (draft: CustomProviderDraft) => void | Promise<void>;
+ isEditMode?: boolean;
+ initialValues?: CustomProviderInitialValues;
reasoningEffortOptions?: readonly string[];
busy?: boolean;
error?: string | null;
@@ -62,6 +66,17 @@ const createEmptyModelRow = (): ModelRow => ({
maxOutputTokens: '',
});
+const createModelRows = (initialValues?: CustomProviderInitialValues): ModelRow[] =>
+ initialValues?.models.map(model => ({
+ id: model.id,
+ name: model.name,
+ advancedExpanded: true,
+ reasoningEfforts: model.properties?.reasoningEfforts,
+ contextLength: model.properties?.contextLength?.toString() ?? '',
+ maxOutputTokens: model.properties?.maxOutputTokens?.toString() ?? '',
+ nameDirty: true,
+ })) ?? [createEmptyModelRow()];
+
// Flat, de-boxed input with enough contrast to read as editable: a slightly
// deeper fill, a subtle hairline, and a clear focus ring.
const inputClassName =
@@ -107,23 +122,25 @@ const FieldHelp = ({ children }: { children: ReactNode }) => (
const CustomModelProviderForm = ({
open,
onOpenChange,
- onAdd,
+ onSubmit,
reasoningEffortOptions,
busy = false,
error,
+ isEditMode = false,
+ initialValues,
}: CustomModelProviderFormProps) => {
- const [name, setName] = useState('');
- const [baseUrl, setBaseUrl] = useState('');
+ const [name, setName] = useState(initialValues?.name ?? '');
+ const [baseUrl, setBaseUrl] = useState(initialValues?.baseUrl ?? '');
const [apiKey, setApiKey] = useState('');
- const [models, setModels] = useState<ModelRow[]>([createEmptyModelRow()]);
+ const [models, setModels] = useState<ModelRow[]>(() => createModelRows(initialValues));
const [nameTouched, setNameTouched] = useState(false);
const [baseUrlTouched, setBaseUrlTouched] = useState(false);
const resetForm = () => {
- setName('');
- setBaseUrl('');
+ setName(initialValues?.name ?? '');
+ setBaseUrl(initialValues?.baseUrl ?? '');
setApiKey('');
- setModels([createEmptyModelRow()]);
+ setModels(createModelRows(initialValues));
setNameTouched(false);
setBaseUrlTouched(false);
};
@@ -186,7 +203,7 @@ const CustomModelProviderForm = ({
// Both limits are required: the harness budgets a run as input + reserved output ≤ context window.
const modelContextError = (model: ModelRow): string | null =>
- parsePositiveInt(model.contextLength) == null ? 'Set the model’s context window.' : null;
+ parsePositiveInt(model.contextLength) == null ? "Set the model's context window." : null;
const modelMaxOutputError = (model: ModelRow): string | null =>
parsePositiveInt(model.maxOutputTokens) == null ? 'Set the max output tokens.' : null;
@@ -219,7 +236,7 @@ const CustomModelProviderForm = ({
}
try {
- await onAdd({
+ await onSubmit({
name: trimmedName,
baseUrl: trimmedBaseUrl,
apiKey: apiKey.trim(),
@@ -237,8 +254,7 @@ const CustomModelProviderForm = ({
};
}),
});
- resetForm();
- onOpenChange(false);
+ handleOpenChange(false);
} catch {
// Parent surfaces error; keep form open.
}
@@ -248,8 +264,12 @@ const CustomModelProviderForm = ({
<CenteredModal
open={open}
onOpenChange={handleOpenChange}
- title="Add custom provider"
- description="Connect any OpenAI-compatible endpoint, whether it's a local model, a proxy, or your own hosted service."
+ title={isEditMode ? `Edit ${initialValues?.name ?? name}` : 'Add custom provider'}
+ description={
+ isEditMode
+ ? 'Update the endpoint, credentials, and models for this provider.'
+ : "Connect any OpenAI-compatible endpoint, whether it's a local model, a proxy, or your own hosted service."
+ }
contentSized
>
<form
@@ -273,10 +293,17 @@ const CustomModelProviderForm = ({
onChange={event => setName(event.target.value)}
onBlur={() => setNameTouched(true)}
placeholder="local-llama"
- autoFocus
+ autoFocus={!isEditMode}
+ readOnly={isEditMode}
+ aria-readonly={isEditMode}
aria-invalid={showNameError ? true : undefined}
- className={cn(inputClassName, showNameError && inputErrorClassName)}
+ className={cn(
+ inputClassName,
+ isEditMode && 'cursor-not-allowed bg-secondary-bg/60 text-text-secondary',
+ showNameError && inputErrorClassName,
+ )}
/>
+ {isEditMode ? <FieldHelp>Provider names cannot be changed after creation.</FieldHelp> : null}
{showNameError ? <FieldError>{nameError}</FieldError> : null}
</div>
@@ -292,6 +319,7 @@ const CustomModelProviderForm = ({
onChange={event => setBaseUrl(event.target.value)}
onBlur={() => setBaseUrlTouched(true)}
placeholder="http://localhost:11434/v1"
+ autoFocus={isEditMode}
aria-invalid={showBaseUrlError ? true : undefined}
className={cn(inputClassName, showBaseUrlError && inputErrorClassName)}
/>
@@ -315,7 +343,11 @@ const CustomModelProviderForm = ({
placeholder="sk-…"
className={inputClassName}
/>
- <FieldHelp>Leave empty if your endpoint needs no key (e.g. a local model).</FieldHelp>
+ <FieldHelp>
+ {isEditMode
+ ? 'Leave empty to keep the saved key, or enter a new key to replace it.'
+ : 'Leave empty if your endpoint needs no key (e.g. a local model).'}
+ </FieldHelp>
</div>
<fieldset className="m-0 min-w-0 border-0 p-0">
@@ -551,7 +583,7 @@ const CustomModelProviderForm = ({
<div className="shrink-0 space-y-3 border-t border-border px-5 py-4">
{error ? <p className="text-failure-bg text-sm">{error}</p> : null}
<Button type="submit" size="lg" disabled={!visibleValid || busy} className="w-full">
- Add provider
+ {isEditMode ? 'Save changes' : 'Add provider'}
</Button>
</div>
</form>
diff --git a/packages/trueforge-ui-sdk/src/containers/SettingsBuilder/ModelSettings.tsx b/packages/trueforge-ui-sdk/src/containers/SettingsBuilder/ModelSettings.tsx
index a5d0712f0..0c5bb6c08 100644
--- a/packages/trueforge-ui-sdk/src/containers/SettingsBuilder/ModelSettings.tsx
+++ b/packages/trueforge-ui-sdk/src/containers/SettingsBuilder/ModelSettings.tsx
@@ -41,6 +41,7 @@ const ModelSettings = () => {
const [baseUrl, setBaseUrl] = useState('');
const [advancedOpen, setAdvancedOpen] = useState(false);
const [customProviderOpen, setCustomProviderOpen] = useState(false);
+ const [customProviderToEdit, setCustomProviderToEdit] = useState<ModelProviderBase | null>(null);
const modelProviderIconMap = useMemo(() => {
return (catalog ?? []).reduce(
@@ -219,6 +220,40 @@ const ModelSettings = () => {
}, 0);
};
+ const handleUpdateCustomProvider = async (draft: CustomProviderDraft) => {
+ if (!customProviderToEdit) return;
+
+ const provider = customProviderToEdit;
+ setFormError(null);
+ await runMutation(
+ async () => {
+ await modelCatalog.updateModelProvider({
+ id: provider.id,
+ type: provider.type,
+ name: provider.name,
+ baseUrl: draft.baseUrl,
+ apiKey: draft.apiKey,
+ models: draft.models,
+ });
+ },
+ err => setFormError(getErrorMessage(err, 'Request failed')),
+ );
+ setTimeout(() => {
+ toaster?.showSuccess({
+ title: 'Model provider updated',
+ description: `${provider.name} was updated successfully.`,
+ });
+ }, 0);
+ };
+
+ const customProviderInitialValues = customProviderToEdit
+ ? {
+ name: customProviderToEdit.name,
+ baseUrl: customProviderToEdit.baseUrl ?? '',
+ models: customProviderToEdit.models,
+ }
+ : undefined;
+
const renderKeyEditor = (opts: { id: string; submitLabel: string; onSave: () => void; isReplacingKey?: boolean }) => (
<form
className="mt-4 rounded-lg bg-secondary-bg/40 p-4"
@@ -315,6 +350,7 @@ const ModelSettings = () => {
type="button"
onClick={() => {
setFormError(null);
+ setCustomProviderToEdit(null);
setCustomProviderOpen(true);
}}
>
@@ -364,25 +400,32 @@ const ModelSettings = () => {
<span className="h-1.5 w-1.5 rounded-full bg-success-bg"></span>
Connected
</span>
- {editingProviderId === provider.id ? null : (
- <Button
- variant="secondary"
- size="sm"
- className="text-[0.8125rem]"
- type="button"
- disabled={busy}
- onClick={() => {
+ <Button
+ variant="secondary"
+ size="sm"
+ className="text-[0.8125rem]"
+ type="button"
+ aria-label={`Edit ${provider.name}`}
+ title={`Edit ${provider.name}`}
+ disabled={busy}
+ onClick={() => {
+ if (provider.type === 'custom') {
+ closeKeyEditor();
+ setFormError(null);
+ setCustomProviderToEdit(provider);
+ setCustomProviderOpen(true);
+ } else {
setEditingCatalogType(null);
setEditingProviderId(provider.id);
setApiKey('');
setBaseUrl(provider.baseUrl ?? '');
setAdvancedOpen(false);
- }}
- >
- <Icon name="wrench" className="size-3.5" />
- Edit
- </Button>
- )}
+ }
+ }}
+ >
+ <Icon name="wrench" className="size-3.5" />
+ Edit
+ </Button>
{modelCatalog.deleteModelProvider ? (
<Button
variant="outline"
@@ -542,12 +585,18 @@ const ModelSettings = () => {
</div>
<CustomModelProviderForm
+ key={customProviderToEdit?.id ?? 'add-custom-provider'}
open={customProviderOpen}
onOpenChange={open => {
setCustomProviderOpen(open);
- if (!open) setFormError(null);
+ if (!open) {
+ setFormError(null);
+ setCustomProviderToEdit(null);
+ }
}}
- onAdd={handleAddCustomProvider}
+ isEditMode={customProviderToEdit !== null}
+ initialValues={customProviderInitialValues}
+ onSubmit={customProviderToEdit ? handleUpdateCustomProvider : handleAddCustomProvider}
reasoningEffortOptions={supportedReasoningEfforts}
busy={busy}
error={formError}
diff --git a/packages/trueforge-ui-sdk/test/containers/SettingsBuilder/CustomModelProviderForm.test.tsx b/packages/trueforge-ui-sdk/test/containers/SettingsBuilder/CustomModelProviderForm.test.tsx
index 9d3fec4b9..928b87c1a 100644
--- a/packages/trueforge-ui-sdk/test/containers/SettingsBuilder/CustomModelProviderForm.test.tsx
+++ b/packages/trueforge-ui-sdk/test/containers/SettingsBuilder/CustomModelProviderForm.test.tsx
@@ -21,7 +21,7 @@ function renderForm(onAdd: (draft: CustomProviderDraft) => void | Promise<void>
<CustomModelProviderForm
open
onOpenChange={() => undefined}
- onAdd={onAdd}
+ onSubmit={onAdd}
reasoningEffortOptions={['low', 'high']}
/>,
);
@@ -221,4 +221,67 @@ describe('CustomModelProviderForm', () => {
}),
);
});
+
+ it('prefills edit values, keeps the provider name immutable, and preserves model properties', async () => {
+ const onSubmit = vi.fn(async () => undefined);
+ render(
+ <CustomModelProviderForm
+ isEditMode
+ open
+ onOpenChange={() => undefined}
+ onSubmit={onSubmit}
+ reasoningEffortOptions={['low', 'high']}
+ initialValues={{
+ name: 'local-llama',
+ baseUrl: 'http://localhost:11434/v1',
+ models: [
+ {
+ id: 'llama3.1:70b',
+ name: 'llama-3-1-70b',
+ properties: {
+ contextLength: 64000,
+ maxOutputTokens: 2048,
+ reasoningEfforts: ['high'],
+ },
+ },
+ ],
+ }}
+ />,
+ );
+
+ expect(screen.getByRole('heading', { name: 'Edit local-llama' })).toBeInTheDocument();
+ expect(screen.getByPlaceholderText('local-llama')).toHaveValue('local-llama');
+ expect(screen.getByPlaceholderText('local-llama')).toHaveAttribute('readonly');
+ expect(screen.getByPlaceholderText('http://localhost:11434/v1')).toHaveValue('http://localhost:11434/v1');
+ expect(screen.getByPlaceholderText('llama3.1:70b')).toHaveValue('llama3.1:70b');
+ expect(screen.getByPlaceholderText('llama-3-1-70b')).toHaveValue('llama-3-1-70b');
+ expect(screen.getByPlaceholderText('128000')).toHaveValue(64000);
+ expect(screen.getByPlaceholderText('4096')).toHaveValue(2048);
+ expect(screen.getByRole('button', { name: 'high' })).toHaveAttribute('aria-pressed', 'true');
+
+ fireEvent.change(screen.getByPlaceholderText('http://localhost:11434/v1'), {
+ target: { value: 'http://localhost:11434/v2' },
+ });
+ fireEvent.click(screen.getByRole('button', { name: 'Save changes' }));
+
+ await waitFor(() => expect(onSubmit).toHaveBeenCalledTimes(1));
+ expect(onSubmit).toHaveBeenCalledWith(
+ expect.objectContaining({
+ name: 'local-llama',
+ baseUrl: 'http://localhost:11434/v2',
+ apiKey: '',
+ models: [
+ expect.objectContaining({
+ id: 'llama3.1:70b',
+ name: 'llama-3-1-70b',
+ properties: {
+ contextLength: 64000,
+ maxOutputTokens: 2048,
+ reasoningEfforts: ['high'],
+ },
+ }),
+ ],
+ }),
+ );
+ });
});
diff --git a/packages/trueforge-ui-sdk/test/containers/SettingsBuilder/ModelSettings.test.tsx b/packages/trueforge-ui-sdk/test/containers/SettingsBuilder/ModelSettings.test.tsx
new file mode 100644
index 000000000..a7086ad6e
--- /dev/null
+++ b/packages/trueforge-ui-sdk/test/containers/SettingsBuilder/ModelSettings.test.tsx
@@ -0,0 +1,103 @@
+// @vitest-environment jsdom
+import { fireEvent, render, screen, waitFor } from '@testing-library/react';
+import { beforeAll, describe, expect, it, vi } from 'vitest';
+
+import type { CustomProviderDraft } from '@/containers/SettingsBuilder/CustomModelProviderForm.js';
+import ModelSettings from '@/containers/SettingsBuilder/ModelSettings.js';
+import { ServerProvider } from '@/server/ServerContext.js';
+import type { ModelProviderBase, UpdateModelProviderRequest } from '@/server/types.js';
+import { createMockAgentUIServer, createMockCatalog } from '../../server/mockServer.js';
+
+beforeAll(() => {
+ HTMLDialogElement.prototype.showModal = function showModal() {
+ this.setAttribute('open', '');
+ };
+ HTMLDialogElement.prototype.close = function close() {
+ this.removeAttribute('open');
+ };
+});
+
+const customProvider: ModelProviderBase<CustomProviderDraft['models'][number]> = {
+ id: 'local-llama',
+ type: 'custom',
+ name: 'local-llama',
+ baseUrl: 'http://localhost:11434/v1',
+ models: [
+ {
+ id: 'llama3.1:70b',
+ name: 'llama-3-1-70b',
+ properties: {
+ contextLength: 64000,
+ maxOutputTokens: 2048,
+ reasoningEfforts: ['high'],
+ },
+ },
+ ],
+};
+
+const builtInProvider: ModelProviderBase = {
+ id: 'openai',
+ type: 'openai',
+ name: 'OpenAI',
+ models: [{ id: 'gpt-4.1', name: 'gpt-4-1' }],
+};
+
+describe('ModelSettings custom provider editing', () => {
+ it('opens the custom provider modal from Edit and updates the provider from the prefilled form', async () => {
+ const updateModelProvider = vi.fn(async (request: UpdateModelProviderRequest) => ({
+ ...customProvider,
+ baseUrl: request.baseUrl,
+ models: request.models,
+ }));
+ const server = createMockAgentUIServer({
+ catalog: createMockCatalog({
+ modelCatalog: {
+ getModelProviderCatalog: async () => [
+ {
+ type: 'custom',
+ name: 'custom',
+ models: [],
+ supportedReasoningEfforts: ['low', 'high'],
+ },
+ ],
+ listModelProviders: async () => [customProvider, builtInProvider],
+ createModelProvider: vi.fn(),
+ updateModelProvider,
+ },
+ }),
+ });
+
+ render(
+ <ServerProvider server={server}>
+ <ModelSettings />
+ </ServerProvider>,
+ );
+
+ const editButton = await screen.findByRole('button', { name: 'Edit local-llama' });
+ expect(screen.getByRole('button', { name: 'Edit OpenAI' })).toBeInTheDocument();
+ fireEvent.click(editButton);
+
+ expect(screen.getByRole('heading', { name: 'Edit local-llama' })).toBeInTheDocument();
+ expect(screen.getByPlaceholderText('local-llama')).toHaveValue('local-llama');
+ expect(screen.getByPlaceholderText('local-llama')).toHaveAttribute('readonly');
+ expect(screen.getByPlaceholderText('http://localhost:11434/v1')).toHaveValue('http://localhost:11434/v1');
+ expect(screen.getByPlaceholderText('llama3.1:70b')).toHaveValue('llama3.1:70b');
+ expect(screen.getByPlaceholderText('128000')).toHaveValue(64000);
+ expect(screen.getByPlaceholderText('4096')).toHaveValue(2048);
+
+ fireEvent.change(screen.getByPlaceholderText('http://localhost:11434/v1'), {
+ target: { value: 'http://localhost:11434/v2' },
+ });
+ fireEvent.click(screen.getByRole('button', { name: 'Save changes' }));
+
+ await waitFor(() => expect(updateModelProvider).toHaveBeenCalledTimes(1));
+ expect(updateModelProvider).toHaveBeenCalledWith({
+ id: 'local-llama',
+ type: 'custom',
+ name: 'local-llama',
+ baseUrl: 'http://localhost:11434/v2',
+ apiKey: '',
+ models: customProvider.models,
+ });
+ });
+});