Skip to content

Commit f6731f0

Browse files
author
Julius Marminge
committed
Move provider settings metadata into schema annotations
- Derive provider settings UI from schema annotations - Remove separate provider UI metadata and hide internal fields in contracts
1 parent cc102a0 commit f6731f0

3 files changed

Lines changed: 124 additions & 136 deletions

File tree

‎apps/web/src/components/settings/ProviderSettingsForm.tsx‎

Lines changed: 47 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22

33
import { useMemo, type ReactNode } from "react";
44
import { Schema } from "effect";
5+
import type {
6+
ProviderSettingsFormAnnotation,
7+
ProviderSettingsFormControl,
8+
} from "@t3tools/contracts";
59

610
import { cn } from "../../lib/utils";
711
import { DraftInput } from "../ui/draft-input";
812
import { Input } from "../ui/input";
913
import { Switch } from "../ui/switch";
1014
import { Textarea } from "../ui/textarea";
11-
import type {
12-
ProviderClientDefinition,
13-
ProviderSettingsControl,
14-
ProviderSettingsFieldUi,
15-
} from "./providerDriverMeta";
15+
import type { ProviderClientDefinition } from "./providerDriverMeta";
1616

1717
export interface ProviderSettingsFieldModel {
1818
readonly key: string;
19-
readonly control: ProviderSettingsControl;
19+
readonly control: ProviderSettingsFormControl;
2020
readonly label: string;
2121
readonly description?: string | undefined;
2222
readonly placeholder?: string | undefined;
@@ -30,51 +30,62 @@ function titleizeFieldKey(key: string): string {
3030
.replace(/^./, (char) => char.toUpperCase());
3131
}
3232

33-
function fieldControlFromUi(ui: ProviderSettingsFieldUi | undefined): ProviderSettingsControl {
34-
return ui?.control ?? "text";
33+
function readFieldAnnotations(
34+
fieldSchema: ProviderClientDefinition["settingsSchema"]["fields"][string],
35+
) {
36+
return Schema.resolveAnnotationsKey(fieldSchema) ?? Schema.resolveAnnotations(fieldSchema);
3537
}
3638

3739
function readFieldAnnotationString(
3840
fieldSchema: ProviderClientDefinition["settingsSchema"]["fields"][string],
3941
key: "title" | "description",
4042
): string | undefined {
41-
const annotations =
42-
Schema.resolveAnnotationsKey(fieldSchema) ?? Schema.resolveAnnotations(fieldSchema);
43+
const annotations = readFieldAnnotations(fieldSchema);
4344
const value = annotations?.[key];
4445
return typeof value === "string" ? value : undefined;
4546
}
4647

48+
function readProviderSettingsFormAnnotation(
49+
fieldSchema: ProviderClientDefinition["settingsSchema"]["fields"][string],
50+
): ProviderSettingsFormAnnotation {
51+
const annotation = readFieldAnnotations(fieldSchema)?.providerSettingsForm;
52+
return annotation ?? {};
53+
}
54+
4755
export function deriveProviderSettingsFields(
4856
definition: ProviderClientDefinition,
4957
): ReadonlyArray<ProviderSettingsFieldModel> {
50-
const schemaKeys = Object.keys(definition.settingsSchema.fields);
51-
const schemaKeySet = new Set(schemaKeys);
52-
const orderedKeys = [
53-
...(definition.settingsUi.order ?? []).filter((key) => schemaKeySet.has(key)),
54-
...schemaKeys.filter((key) => !(definition.settingsUi.order ?? []).includes(key)),
55-
];
58+
return Object.keys(definition.settingsSchema.fields)
59+
.map((key, index) => ({ key, index }))
60+
.toSorted((left, right) => {
61+
const leftAnnotation = readProviderSettingsFormAnnotation(
62+
definition.settingsSchema.fields[left.key]!,
63+
);
64+
const rightAnnotation = readProviderSettingsFormAnnotation(
65+
definition.settingsSchema.fields[right.key]!,
66+
);
67+
return (leftAnnotation.order ?? left.index) - (rightAnnotation.order ?? right.index);
68+
})
69+
.flatMap(({ key }) => {
70+
const fieldSchema = definition.settingsSchema.fields[key]!;
71+
const formAnnotation = readProviderSettingsFormAnnotation(fieldSchema);
72+
if (formAnnotation.hidden) return [];
5673

57-
return orderedKeys.flatMap((key) => {
58-
const ui = definition.settingsUi.fields?.[key];
59-
if (ui?.hidden) return [];
60-
const fieldSchema = definition.settingsSchema.fields[key]!;
61-
const annotatedTitle = readFieldAnnotationString(fieldSchema, "title");
62-
const annotatedDescription = readFieldAnnotationString(fieldSchema, "description");
63-
return [
64-
{
65-
key,
66-
control: fieldControlFromUi(ui),
67-
label: ui?.label ?? annotatedTitle ?? titleizeFieldKey(key),
68-
...(ui?.description !== undefined
69-
? { description: ui.description }
70-
: annotatedDescription !== undefined
71-
? { description: annotatedDescription }
74+
const annotatedTitle = readFieldAnnotationString(fieldSchema, "title");
75+
const annotatedDescription = readFieldAnnotationString(fieldSchema, "description");
76+
return [
77+
{
78+
key,
79+
control: formAnnotation.control ?? "text",
80+
label: annotatedTitle ?? titleizeFieldKey(key),
81+
...(annotatedDescription !== undefined ? { description: annotatedDescription } : {}),
82+
...(formAnnotation.placeholder !== undefined
83+
? { placeholder: formAnnotation.placeholder }
7284
: {}),
73-
...(ui?.placeholder !== undefined ? { placeholder: ui.placeholder } : {}),
74-
clearWhenEmpty: ui?.clearWhenEmpty ?? "omit",
75-
} satisfies ProviderSettingsFieldModel,
76-
];
77-
});
85+
clearWhenEmpty: formAnnotation.clearWhenEmpty ?? "omit",
86+
} satisfies ProviderSettingsFieldModel,
87+
];
88+
});
7889
}
7990

8091
export function readProviderConfigString(config: unknown, key: string): string {

‎apps/web/src/components/settings/providerDriverMeta.ts‎

Lines changed: 3 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,17 @@ type ProviderSettingsSchema = {
1212
readonly fields: Readonly<Record<string, Schema.Top>>;
1313
};
1414

15-
export type ProviderSettingsControl = "text" | "password" | "textarea" | "switch";
16-
17-
export interface ProviderSettingsFieldUi {
18-
readonly control?: ProviderSettingsControl;
19-
readonly label?: string;
20-
readonly placeholder?: string;
21-
readonly description?: string;
22-
readonly hidden?: boolean;
23-
readonly clearWhenEmpty?: "omit" | "persist";
24-
}
25-
26-
export interface ProviderSettingsUi {
27-
readonly order?: readonly string[];
28-
readonly fields?: Readonly<Record<string, ProviderSettingsFieldUi>>;
29-
}
30-
3115
/**
3216
* Browser-safe provider definition. This is deliberately shaped like the
33-
* future provider package client export: the core web app gets a schema,
34-
* presentation metadata, and a small UI hint layer, then renders generically.
17+
* future provider package client export: the core web app gets a schema with
18+
* field annotations plus provider-level presentation metadata, then renders
19+
* settings generically.
3520
*/
3621
export interface ProviderClientDefinition {
3722
readonly value: ProviderDriverKind;
3823
readonly label: string;
3924
readonly icon: Icon;
4025
readonly settingsSchema: ProviderSettingsSchema;
41-
readonly settingsUi: ProviderSettingsUi;
4226
/**
4327
* Optional short label rendered as a `variant="warning"` badge next to
4428
* the instance title. Used to flag drivers that still ship under an
@@ -55,98 +39,25 @@ export const PROVIDER_CLIENT_DEFINITIONS: readonly ProviderClientDefinition[] =
5539
label: "Codex",
5640
icon: OpenAI,
5741
settingsSchema: CodexSettings,
58-
settingsUi: {
59-
order: ["binaryPath", "homePath", "shadowHomePath"],
60-
fields: {
61-
enabled: { hidden: true },
62-
customModels: { hidden: true },
63-
binaryPath: {
64-
placeholder: "codex",
65-
clearWhenEmpty: "omit",
66-
},
67-
homePath: {
68-
placeholder: "~/.codex",
69-
clearWhenEmpty: "omit",
70-
},
71-
shadowHomePath: {
72-
placeholder: "~/.codex-t3/personal",
73-
clearWhenEmpty: "omit",
74-
},
75-
},
76-
},
7742
},
7843
{
7944
value: ProviderDriverKind.make("claudeAgent"),
8045
label: "Claude",
8146
icon: ClaudeAI,
8247
settingsSchema: ClaudeSettings,
83-
settingsUi: {
84-
order: ["binaryPath", "homePath", "launchArgs"],
85-
fields: {
86-
enabled: { hidden: true },
87-
customModels: { hidden: true },
88-
binaryPath: {
89-
placeholder: "claude",
90-
clearWhenEmpty: "omit",
91-
},
92-
homePath: {
93-
placeholder: "~",
94-
clearWhenEmpty: "omit",
95-
},
96-
launchArgs: {
97-
placeholder: "e.g. --chrome",
98-
clearWhenEmpty: "omit",
99-
},
100-
},
101-
},
10248
},
10349
{
10450
value: ProviderDriverKind.make("cursor"),
10551
label: "Cursor",
10652
icon: CursorIcon,
10753
badgeLabel: "Early Access",
10854
settingsSchema: CursorSettings,
109-
settingsUi: {
110-
order: ["binaryPath", "apiEndpoint"],
111-
fields: {
112-
enabled: { hidden: true },
113-
customModels: { hidden: true },
114-
binaryPath: {
115-
placeholder: "agent",
116-
clearWhenEmpty: "omit",
117-
},
118-
apiEndpoint: {
119-
placeholder: "https://...",
120-
clearWhenEmpty: "omit",
121-
},
122-
},
123-
},
12455
},
12556
{
12657
value: ProviderDriverKind.make("opencode"),
12758
label: "OpenCode",
12859
icon: OpenCodeIcon,
12960
settingsSchema: OpenCodeSettings,
130-
settingsUi: {
131-
order: ["binaryPath", "serverUrl", "serverPassword"],
132-
fields: {
133-
enabled: { hidden: true },
134-
customModels: { hidden: true },
135-
binaryPath: {
136-
placeholder: "opencode",
137-
clearWhenEmpty: "omit",
138-
},
139-
serverUrl: {
140-
placeholder: "http://127.0.0.1:4096",
141-
clearWhenEmpty: "omit",
142-
},
143-
serverPassword: {
144-
control: "password",
145-
placeholder: "Optional",
146-
clearWhenEmpty: "omit",
147-
},
148-
},
149-
},
15061
},
15162
];
15263

0 commit comments

Comments
 (0)