@devries-cameron20 / t3code-1 / commits / 00c6adbdce
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

feat(fx): choose a backend per thread (#29388)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: maria-rcks <maria@kuuro.net> Co-authored-by: Julius Marminge <51714798+juliusmarminge@users.noreply.github.com>

Jake Leventhal committed Nov 18, 2026 at 19:53 UTC 00c6adbdce26eda05ccc3306d616fea4311aeb7a
30 files changed +1280 -213
apps/mobile/src/features/threads/ThreadSettingsSheet.tsx
+115 -10
@@ -72,6 +72,7 @@ import {
72 type ProviderGroup,
73 withStoredThreadModelOptions,
74 } from "../../lib/modelOptions";
75 +import { groupModelsBySubProvider } from "@t3tools/client-runtime/providerSubProviders";
76 import { applyProviderOptionSelection } from "../../lib/providerOptions";
77 import { resolveProviderOptionDescriptors } from "../../lib/providerOptions";
78 import {
@@ -153,6 +154,10 @@ function ProviderHeader(props: {
154 readonly driver: string | undefined;
155 readonly iconUrl: string | undefined;
156 readonly label: string;
157 + /** A backend inside a provider's section, such as one of fx's: no icon. */
158 + readonly nested?: boolean;
159 + /** Why the section has no usable models, such as a signed-out backend. */
160 + readonly detail?: string | undefined;
161 readonly collapsible: boolean;
162 readonly collapsed: boolean;
163 readonly modelCount: number;
@@ -161,8 +166,17 @@ function ProviderHeader(props: {
166 const gt = useGT();
167 const content = (
168 <>
164 - <ProviderIcon iconUrl={props.iconUrl} provider={props.driver} size={15} />
165 - <Text className="text-sm font-t3-medium text-foreground-muted">{props.label}</Text>
169 + {props.nested ? null : (
170 + <ProviderIcon iconUrl={props.iconUrl} provider={props.driver} size={15} />
171 + )}
172 + {props.detail ? (
173 + <View className="min-w-0 flex-1 py-1">
174 + <Text className="text-sm font-t3-medium text-foreground-muted">{props.label}</Text>
175 + <Text className="text-xs text-foreground-muted">{props.detail}</Text>
176 + </View>
177 + ) : (
178 + <Text className="text-sm font-t3-medium text-foreground-muted">{props.label}</Text>
179 + )}
180 {props.collapsible ? (
181 <>
182 <View className="flex-1" />
@@ -204,7 +218,10 @@ function ProviderHeader(props: {
218 }
219
220 return (
207 - <View accessibilityRole="header" className="mx-4 min-h-9 flex-row items-center gap-2 px-1 pt-1">
221 + <View
222 + accessibilityRole="header"
223 + className={cn("mx-4 min-h-9 flex-row items-center gap-2 px-1 pt-1", props.nested && "mt-1")}
224 + >
225 {content}
226 </View>
227 );
@@ -706,6 +723,15 @@ type ThreadSettingsCatalogItem =
723 readonly key: string;
724 readonly provider: ThreadSettingsProviderCatalog;
725 }
726 + | {
727 + readonly kind: "subProvider";
728 + readonly key: string;
729 + readonly expansionKey: string;
730 + readonly label: string;
731 + readonly detail: string | undefined;
732 + readonly collapsed: boolean;
733 + readonly modelCount: number;
734 + }
735 | {
736 readonly kind: "model";
737 readonly key: string;
@@ -774,6 +800,88 @@ function ThreadSettingsProviderListHeader(props: {
800 );
801 }
802
803 +function ThreadSettingsSubProviderListHeader(props: {
804 + readonly item: Extract<ThreadSettingsCatalogItem, { kind: "subProvider" }>;
805 +}) {
806 + const session = useThreadSettingsSession();
807 + const onToggle = () => session.toggleProvider(props.item.expansionKey);
808 +
809 + return (
810 + <ProviderHeader
811 + nested
812 + collapsible={props.item.detail === undefined}
813 + collapsed={props.item.collapsed}
814 + detail={props.item.detail}
815 + driver={undefined}
816 + iconUrl={undefined}
817 + label={props.item.label}
818 + modelCount={props.item.modelCount}
819 + onToggle={onToggle}
820 + />
821 + );
822 +}
823 +
824 +const modelItems = (models: ReadonlyArray<ModelOption>) =>
825 + models.map((option, index) => ({
826 + kind: "model" as const,
827 + key: `model:${option.key}`,
828 + option,
829 + isFirst: index === 0,
830 + isLast: index === models.length - 1,
831 + }));
832 +
833 +/**
834 + * A provider whose models run on several backends, such as fx, lists each
835 + * backend under its own collapsible heading; the thread's backend opens.
836 + * Search and Favorites stay flat.
837 + */
838 +function subProviderCatalogItems(
839 + session: ThreadSettingsSessionValue,
840 + group: ProviderGroup,
841 + models: ReadonlyArray<ModelOption>,
842 +): ReadonlyArray<ThreadSettingsCatalogItem> | null {
843 + if (
844 + !group.subProviders?.length ||
845 + session.searchQuery.trim().length > 0 ||
846 + session.providerFilter === FAVORITES_PROVIDER_FILTER
847 + ) {
848 + return null;
849 + }
850 + const { ungrouped, groups } = groupModelsBySubProvider(
851 + models,
852 + group.subProviders,
853 + (option) => option.subtitle,
854 + );
855 + return [
856 + ...modelItems(ungrouped),
857 + ...groups.flatMap(({ subProvider, models: backendModels }) => {
858 + const expansionKey = `${group.providerKey}:sub-provider:${subProvider.name}`;
859 + const collapsed =
860 + subProvider.unavailableReason !== undefined ||
861 + providerSectionIsCollapsed({
862 + defaultExpanded: backendModels.some(session.isApplied),
863 + hasExpansionOverride: session.providerExpansionOverrides.has(expansionKey),
864 + isNarrowed: false,
865 + });
866 + return [
867 + {
868 + kind: "subProvider" as const,
869 + key: `sub-provider:${expansionKey}`,
870 + expansionKey,
871 + label: subProvider.name,
872 + detail: subProvider.unavailableReason,
873 + collapsed,
874 + modelCount: backendModels.length,
875 + },
876 + // The heading names the backend, so rows drop it.
877 + ...(collapsed
878 + ? []
879 + : modelItems(backendModels.map((option) => ({ ...option, subtitle: "" })))),
880 + ];
881 + }),
882 + ];
883 +}
884 +
885 function buildThreadSettingsCatalogItems(
886 session: ThreadSettingsSessionValue,
887 resolve: MessageResolver,
@@ -847,13 +955,8 @@ function buildThreadSettingsCatalogItems(
955 key: `provider:${group.providerKey}`,
956 provider,
957 },
850 - ...provider.models.map((option, index) => ({
851 - kind: "model" as const,
852 - key: `model:${option.key}`,
853 - option,
854 - isFirst: index === 0,
855 - isLast: index === provider.models.length - 1,
856 - })),
958 + ...((collapsed ? null : subProviderCatalogItems(session, group, provider.models)) ??
959 + modelItems(provider.models)),
960 ...(!collapsed && updateRequiredNotice
961 ? [
962 {
@@ -1033,6 +1136,8 @@ function ThreadSettingsMainContent(props: {
1136
1137 if (item.kind === "provider") {
1138 content = <ThreadSettingsProviderListHeader provider={item.provider} />;
1139 + } else if (item.kind === "subProvider") {
1140 + content = <ThreadSettingsSubProviderListHeader item={item} />;
1141 } else if (item.kind === "model") {
1142 content = (
1143 <ThreadSettingsModelListRow
apps/mobile/src/lib/modelOptions.test.ts
+44
@@ -21,6 +21,7 @@ import {
21 withStoredThreadModelOptions,
22 type ModelOption,
23 } from "./modelOptions";
24 +import { groupModelsBySubProvider } from "@t3tools/client-runtime/providerSubProviders";
25
26 describe("mobile model options", () => {
27 it("distinguishes same-name OpenCode models without changing their routing", () => {
@@ -72,6 +73,49 @@ describe("mobile model options", () => {
73 ]);
74 });
75
76 + it("groups fx's models under its backends, keeping a signed-out backend", () => {
77 + const subProviders = [
78 + { name: "Codex subscription" },
79 + { name: "Grok subscription", unavailableReason: "Signed out." },
80 + ];
81 + const config = {
82 + providers: [
83 + {
84 + instanceId: "fx",
85 + driver: "fx",
86 + displayName: "fx",
87 + enabled: true,
88 + installed: true,
89 + auth: { status: "authenticated" },
90 + subProviders,
91 + models: [
92 + { slug: "default", name: "Default (gpt-5.5)", isCustom: false, capabilities: null },
93 + {
94 + slug: "codex:gpt-5.5",
95 + name: "gpt-5.5",
96 + subProvider: "Codex subscription",
97 + isCustom: false,
98 + capabilities: null,
99 + },
100 + ],
101 + },
102 + ],
103 + } as unknown as ServerConfig;
104 +
105 + const [group] = groupByProvider(buildModelOptions(config, null, undefined, null));
106 + expect(group?.subProviders).toEqual(subProviders);
107 + const { ungrouped, groups } = groupModelsBySubProvider(
108 + group?.models ?? [],
109 + group?.subProviders ?? [],
110 + (option) => option.subtitle,
111 + );
112 + expect(ungrouped.map((option) => option.key)).toEqual(["fx:default"]);
113 + expect(groups.map(({ subProvider, models }) => [subProvider.name, models.length])).toEqual([
114 + ["Codex subscription", 1],
115 + ["Grok subscription", 0],
116 + ]);
117 + });
118 +
119 it("does not materialize catalog defaults for missing stored options", () => {
120 const config = {
121 providers: [
apps/mobile/src/lib/modelOptions.ts
+7
@@ -59,6 +59,7 @@ export type ModelOption = {
59 readonly capabilities: ModelCapabilities | null;
60 readonly selection: ModelSelection;
61 readonly providerUpdateRequired?: ProviderUpdateRequired;
62 + readonly providerSubProviders?: T3ServerConfig["providers"][number]["subProviders"];
63 };
64
65 type ProviderUpdateRequired = Pick<
@@ -71,6 +72,7 @@ export type ProviderGroup = {
72 readonly providerLabel: string;
73 readonly models: ReadonlyArray<ModelOption>;
74 readonly updateRequired?: ProviderUpdateRequired;
75 + readonly subProviders?: ModelOption["providerSubProviders"];
76 };
77
78 function providerDisplayLabel(provider: {
@@ -429,6 +431,7 @@ export function buildModelOptions(
431 isDefault: model.isDefault === true,
432 isLegacy: model.isLegacy === true,
433 ...(updateRequired ? { providerUpdateRequired: updateRequired } : {}),
434 + ...(provider.subProviders?.length ? { providerSubProviders: provider.subProviders } : {}),
435 capabilities: model.capabilities,
436 selection: normalizeSelectionOptions(
437 {
@@ -512,6 +515,7 @@ export function groupByProvider(options: ReadonlyArray<ModelOption>): ReadonlyAr
515 providerLabel: string;
516 models: ModelOption[];
517 updateRequired: ProviderUpdateRequired | undefined;
518 + subProviders: ModelOption["providerSubProviders"];
519 }
520 >();
521 for (const option of options) {
@@ -519,11 +523,13 @@ export function groupByProvider(options: ReadonlyArray<ModelOption>): ReadonlyAr
523 if (existing) {
524 existing.models.push(option);
525 existing.updateRequired ??= option.providerUpdateRequired;
526 + existing.subProviders ??= option.providerSubProviders;
527 } else {
528 groups.set(option.providerKey, {
529 providerLabel: option.providerLabel,
530 models: [option],
531 updateRequired: option.providerUpdateRequired,
532 + subProviders: option.providerSubProviders,
533 });
534 }
535 }
@@ -533,5 +539,6 @@ export function groupByProvider(options: ReadonlyArray<ModelOption>): ReadonlyAr
539 providerLabel: group.providerLabel,
540 models: group.models,
541 ...(group.updateRequired ? { updateRequired: group.updateRequired } : {}),
542 + ...(group.subProviders ? { subProviders: group.subProviders } : {}),
543 }));
544 }
apps/server/src/orchestration-v2/Adapters/FxAdapterV2.test.ts
+164 -39
@@ -1,6 +1,6 @@
1 import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
2 import * as NodeServices from "@effect/platform-node/NodeServices";
3 -import { describe, it } from "@effect/vitest";
3 +import { assert, describe, it } from "@effect/vitest";
4 import {
5 MessageId,
6 NodeId,
@@ -21,7 +21,9 @@ import { ProviderAdapterV2RuntimePolicy } from "@t3tools/provider-core/server/Pr
21 import * as TestProviderHost from "@t3tools/provider-testing/TestProviderHost";
22 import { resolveSelfInvocation } from "@t3tools/shared/nodeRuntime";
23 import * as DateTime from "effect/DateTime";
24 +import * as Cause from "effect/Cause";
25 import * as Effect from "effect/Effect";
26 +import * as Exit from "effect/Exit";
27 import * as FileSystem from "effect/FileSystem";
28 import * as Layer from "effect/Layer";
29 import * as Path from "effect/Path";
@@ -47,7 +49,7 @@ const testLayer = Layer.mergeAll(
49
50 const SESSION_ID = "fx-session-01";
51 const ENABLED_FX_SETTINGS = Schema.decodeSync(FxSettings)({ enabled: true });
50 -/** The model `fx status --json` reports. */
52 +/** The model `fx status --json` reports in the thread's folder, on the gateway. */
53 const FX_DEFAULT_MODEL = "anthropic/claude-opus-5.5";
54 const NAMED_MODEL = "anthropic/claude-sonnet-5.5";
55 const CUSTOM_MODEL = "openai/gpt-6.1-sol";
@@ -91,10 +93,11 @@ const fxConfig = (input: {
93 readonly model: string;
94 readonly modes: ReadonlyArray<string>;
95 readonly mode?: string;
96 + readonly backend?: string;
97 /** The session's own model list, on a backend other than the gateway. */
98 readonly models?: ReadonlyArray<string>;
99 }) => [
97 - select("provider", "model", "gateway", ["gateway", "codex", "grok"]),
100 + select("provider", "model", input.backend ?? "gateway", ["gateway", "codex", "grok"]),
101 select(
102 "model",
103 "model",
@@ -112,6 +115,7 @@ const fxConfig = (input: {
115 const fxSession = (input: {
116 readonly model: string;
117 readonly modes: ReadonlyArray<string>;
118 + readonly backend?: string;
119 readonly models?: ReadonlyArray<string>;
120 }) => ({
121 sessionId: SESSION_ID,
@@ -151,6 +155,7 @@ const openFxSession = Effect.fn("openFxSession")(function* (input: {
155 readonly frames: ReadonlyArray<Frame>;
156 readonly runtimeMode: RuntimeMode;
157 readonly model?: string;
158 + readonly defaultBackend?: string;
159 readonly modeOptions?: ReadonlyArray<{ readonly id: string; readonly value: string }>;
160 /** Loads this fx session instead of creating one. */
161 readonly loadSessionId?: string;
@@ -180,7 +185,13 @@ const openFxSession = Effect.fn("openFxSession")(function* (input: {
185 settings: ENABLED_FX_SETTINGS,
186 environment: {},
187 selfInvocation: yield* resolveSelfInvocation(),
183 - defaultModel: Effect.succeed(FX_DEFAULT_MODEL),
188 + defaultChoice: (cwd) =>
189 + // "Default" resolves in the thread's own folder.
190 + Effect.succeed(
191 + cwd === replayDir
192 + ? { backend: input.defaultBackend ?? "gateway", model: FX_DEFAULT_MODEL }
193 + : undefined,
194 + ),
195 makeRuntime: makeAcpReplayRuntime({
196 transcript,
197 statusPath,
@@ -316,54 +327,168 @@ describe("FxAdapterV2", () => {
327 }).pipe(Effect.provide(testLayer)),
328 );
329
319 - it.effect("puts a loaded session that ran a named model back on fx's default for Default", () =>
320 - openFxSession({
321 - scenario: "default-model",
322 - runtimeMode: "approval-required",
323 - model: "default",
324 - loadSessionId: SESSION_ID,
325 - frames: [
326 - ...initialize,
327 - outbound("session/load", {
328 - sessionId: SESSION_ID,
329 - cwd: "<workspace>",
330 - mcpServers: "<any>",
331 - _meta: { fx: { profileMcpServers: true } },
332 - }),
333 - answer("session/load", fxSession({ model: NAMED_MODEL, modes: FX_0_0_13_MODES })),
334 - // Config id `model`, never fx's `provider`, and never T3's "default".
335 - ...setConfig(
336 - "model",
337 - FX_DEFAULT_MODEL,
338 - fxConfig({ model: FX_DEFAULT_MODEL, modes: FX_0_0_13_MODES }),
339 - ),
340 - // Written on load even though fx reports ask.
341 - ...setConfig("mode", "ask", fxConfig({ model: FX_DEFAULT_MODEL, modes: FX_0_0_13_MODES })),
330 + it.effect("restores Default only when its backend is selectable", () =>
331 + Effect.forEach(
332 + [
333 + { backend: "gateway", hasBackendOption: true, restore: true },
334 + { backend: "unavailable", hasBackendOption: true, restore: false },
335 + { backend: "gateway", hasBackendOption: false, restore: false },
336 ],
337 + ({ backend, hasBackendOption, restore }) =>
338 + openFxSession({
339 + scenario: `default-model-${backend}-${hasBackendOption}`,
340 + runtimeMode: "approval-required",
341 + model: "default",
342 + defaultBackend: backend,
343 + loadSessionId: SESSION_ID,
344 + frames: [
345 + ...initialize,
346 + outbound("session/load", {
347 + sessionId: SESSION_ID,
348 + cwd: "<workspace>",
349 + mcpServers: "<any>",
350 + _meta: { fx: { profileMcpServers: true } },
351 + }),
352 + answer("session/load", {
353 + ...fxSession({ model: NAMED_MODEL, modes: FX_0_0_13_MODES }),
354 + configOptions: fxConfig({ model: NAMED_MODEL, modes: FX_0_0_13_MODES }).filter(
355 + (option) => hasBackendOption || option.id !== "provider",
356 + ),
357 + }),
358 + ...(restore
359 + ? setConfig(
360 + "model",
361 + FX_DEFAULT_MODEL,
362 + fxConfig({ model: FX_DEFAULT_MODEL, modes: FX_0_0_13_MODES }),
363 + )
364 + : []),
365 + ...setConfig(
366 + "mode",
367 + "ask",
368 + fxConfig({ model: restore ? FX_DEFAULT_MODEL : NAMED_MODEL, modes: FX_0_0_13_MODES }),
369 + ),
370 + ],
371 + }),
372 + { discard: true },
373 + ).pipe(Effect.provide(testLayer)),
374 + );
375 +
376 + // `fx status` names a gateway model, but this loaded session runs on Codex,
377 + // whose catalog lacks it: the backend goes back first.
378 + it.effect("moves a loaded session on another backend back to fx's default backend", () =>
379 + Effect.gen(function* () {
380 + for (const modelAvailable of [true, false]) {
381 + const result = yield* openFxSession({
382 + scenario: `default-other-backend-${modelAvailable}`,
383 + runtimeMode: "approval-required",
384 + model: "default",
385 + loadSessionId: SESSION_ID,
386 + frames: [
387 + ...initialize,
388 + outbound("session/load"),
389 + answer(
390 + "session/load",
391 + fxSession({
392 + model: CODEX_MODEL,
393 + backend: "codex",
394 + models: [CODEX_MODEL],
395 + modes: FX_0_0_13_MODES,
396 + }),
397 + ),
398 + // fx then runs the gateway model it saved for that backend.
399 + ...setConfig(
400 + "provider",
401 + "gateway",
402 + fxConfig({
403 + model: NAMED_MODEL,
404 + ...(modelAvailable ? {} : { models: [NAMED_MODEL] }),
405 + modes: FX_0_0_13_MODES,
406 + }),
407 + ),
408 + ...(modelAvailable
409 + ? [
410 + ...setConfig(
411 + "model",
412 + FX_DEFAULT_MODEL,
413 + fxConfig({ model: FX_DEFAULT_MODEL, modes: FX_0_0_13_MODES }),
414 + ),
415 + ...setConfig(
416 + "mode",
417 + "ask",
418 + fxConfig({ model: FX_DEFAULT_MODEL, modes: FX_0_0_13_MODES }),
419 + ),
420 + ]
421 + : setConfig(
422 + "provider",
423 + "codex",
424 + fxConfig({
425 + backend: "codex",
426 + model: CODEX_MODEL,
427 + models: [CODEX_MODEL],
428 + modes: FX_0_0_13_MODES,
429 + }),
430 + )),
431 + ],
432 + }).pipe(Effect.exit);
433 + if (modelAvailable) {
434 + assert.isTrue(Exit.isSuccess(result));
435 + } else if (Exit.isFailure(result)) {
436 + assert.include(
437 + Cause.pretty(result.cause),
438 + "The default fx model is unavailable on its configured backend.",
439 + );
440 + } else {
441 + assert.fail("Expected Default selection to reject the unavailable model");
442 + }
443 + }
444 }).pipe(Effect.provide(testLayer)),
445 );
446
346 - // `fx status` names the gateway default, but this loaded session runs on
347 - // Codex, whose catalog lacks it; fx would refuse the write and the thread
348 - // could not start.
349 - it.effect("keeps the session's model when its backend lacks fx's default", () =>
447 + it.effect("switches the session's backend, then its model, for a backend's model", () =>
448 openFxSession({
351 - scenario: "default-other-backend",
449 + scenario: "backend-model",
450 runtimeMode: "approval-required",
353 - model: "default",
354 - loadSessionId: SESSION_ID,
451 + model: `codex:${CODEX_MODEL}`,
452 + turnModel: "gateway:openai/gpt-5.6-sol",
453 frames: [
454 ...initialize,
357 - outbound("session/load"),
358 - answer(
359 - "session/load",
360 - fxSession({ model: CODEX_MODEL, models: [CODEX_MODEL], modes: FX_0_0_13_MODES }),
455 + outbound("session/new"),
456 + answer("session/new", fxSession({ model: FX_DEFAULT_MODEL, modes: FX_0_0_13_MODES })),
457 + // fx already picks the thread's model on Codex, so only the backend is written.
458 + ...setConfig(
459 + "provider",
460 + "codex",
461 + fxConfig({
462 + model: CODEX_MODEL,
463 + backend: "codex",
464 + models: [CODEX_MODEL, "gpt-5.4-mini"],
465 + modes: FX_0_0_13_MODES,
466 + }),
467 ),
468 ...setConfig(
469 "mode",
470 "ask",
365 - fxConfig({ model: CODEX_MODEL, models: [CODEX_MODEL], modes: FX_0_0_13_MODES }),
471 + fxConfig({
472 + model: CODEX_MODEL,
473 + backend: "codex",
474 + models: [CODEX_MODEL, "gpt-5.4-mini"],
475 + modes: FX_0_0_13_MODES,
476 + }),
477 + ),
478 + // The next turn moves the same session to a gateway model; the id
479 + // after the backend goes to fx whole, slash included.
480 + ...setConfig(
481 + "provider",
482 + "gateway",
483 + fxConfig({ model: NAMED_MODEL, modes: FX_0_0_13_MODES }),
484 + ),
485 + ...setConfig(
486 + "model",
487 + "openai/gpt-5.6-sol",
488 + fxConfig({ model: "openai/gpt-5.6-sol", modes: FX_0_0_13_MODES }),
489 ),
490 + outbound("session/prompt"),
491 + answer("session/prompt", { stopReason: "end_turn" }),
492 ],
493 }).pipe(Effect.provide(testLayer)),
494 );
apps/server/src/provider/ProviderRegistry.test.ts
+35
@@ -1322,6 +1322,41 @@ it.layer(
1322 );
1323 });
1324
1325 + it("does not bring back models on a sub-provider that is now unavailable", () => {
1326 + const subProviderModel = (slug: string, subProvider: string) => ({
1327 + slug,
1328 + name: slug,
1329 + subProvider,
1330 + isCustom: false,
1331 + capabilities: null,
1332 + });
1333 + const previousProvider = {
1334 + instanceId: ProviderInstanceId.make("fx"),
1335 + driver: ProviderDriverKind.make("fx"),
1336 + status: "ready",
1337 + enabled: true,
1338 + installed: true,
1339 + auth: { status: "authenticated" },
1340 + checkedAt: "2026-04-14T00:00:00.000Z",
1341 + version: "0.0.17",
1342 + models: [subProviderModel("gateway:a", "Gateway"), subProviderModel("codex:b", "Codex")],
1343 + subProviders: [{ name: "Gateway" }, { name: "Codex" }],
1344 + slashCommands: [],
1345 + skills: [],
1346 + } as const satisfies ServerProvider;
1347 + const refreshedProvider = {
1348 + ...previousProvider,
1349 + status: "warning",
1350 + models: [],
1351 + subProviders: [{ name: "Gateway" }, { name: "Codex", unavailableReason: "Signed out." }],
1352 + } satisfies ServerProvider;
1353 +
1354 + assert.deepStrictEqual(
1355 + ProviderRegistry.mergeProviderSnapshot(previousProvider, refreshedProvider).models,
1356 + [previousProvider.models[0]],
1357 + );
1358 + });
1359 +
1360 it("retains ACP Registry models while discovery has not completed", () => {
1361 const previousProvider = {
1362 instanceId: ProviderInstanceId.make("acpRegistry_codex"),
apps/server/src/provider/ProviderRegistry.ts
+9 -1
@@ -264,8 +264,16 @@ const mergeProviderModels = (
264 ): ReadonlyArray<ServerProvider["models"][number]> => {
265 const shouldRetainMissingModels = shouldRetainMissingProviderModels(provider);
266 const updateRequiredSlugs = new Set(provider.updateRequiredModels?.map((model) => model.slug));
267 + const unavailableSubProviders = new Set(
268 + provider.subProviders?.flatMap((subProvider) =>
269 + subProvider.unavailableReason === undefined ? [] : [subProvider.name],
270 + ),
271 + );
272 const retainablePreviousModels = previousModels.filter(
268 - (model) => !model.isCustom && !updateRequiredSlugs.has(model.slug),
273 + (model) =>
274 + !model.isCustom &&
275 + !updateRequiredSlugs.has(model.slug) &&
276 + !unavailableSubProviders.has(model.subProvider ?? ""),
277 );
278
279 if (shouldRetainMissingModels && nextModels.length === 0 && retainablePreviousModels.length > 0) {
apps/web/src/components/chat/ModelPickerContent.tsx
+135 -3
@@ -11,6 +11,7 @@ import {
11 import { resolveSelectableModel } from "@t3tools/shared/model";
12 import { useAtomValue } from "@effect/atom-react";
13 import { formatProviderUpdateRequiredNotice } from "@t3tools/client-runtime/providerUpdateRequiredModels";
14 +import { groupModelsBySubProvider } from "@t3tools/client-runtime/providerSubProviders";
15 import { useNavigate } from "@tanstack/react-router";
16 import { LegendList, type LegendListRef } from "@legendapp/list/react";
17 import {
@@ -23,6 +24,7 @@ import {
24 type ReactNode,
25 type RefObject,
26 } from "react";
27 +import { ChevronRightIcon } from "lucide-react";
28 import { ModelListRow } from "./ModelListRow";
29 import { ModelPickerSidebar } from "./ModelPickerSidebar";
30 import { useProviderWorkspaceSnapshotRefresh } from "./useProviderWorkspaceSnapshotRefresh";
@@ -30,6 +32,7 @@ import { getProviderStatusMessage, hasProviderSetup } from "./ProviderStatusBann
32 import {
33 modelPickerLegacySectionKey,
34 modelPickerModelKey,
35 + modelPickerSubProviderSectionKey,
36 parseModelPickerLegacySectionKey,
37 parseModelPickerModelKey,
38 } from "./modelPickerKeys";
@@ -60,6 +63,7 @@ import { cn } from "~/lib/utils";
63 import { getVirtualizedScrollFadeClassName } from "../ui/scroll-area";
64 import { TooltipProvider } from "../ui/tooltip";
65 import { InlineButton } from "../ui/button";
66 +import { Badge } from "../ui/badge";
67 import {
68 isProviderInstancePickerReady,
69 isProviderInstancePickerVisible,
@@ -462,6 +466,15 @@ export const ModelPickerContent = memo(function ModelPickerContent(props: {
466 : [],
467 ),
468 );
469 + // The thread's backend opens expanded, so its models show without a click.
470 + const [expandedSubProviderKeys, setExpandedSubProviderKeys] = useState(
471 + () =>
472 + new Set<string>(
473 + activeModel?.subProvider
474 + ? [modelPickerSubProviderSectionKey(props.activeInstanceId, activeModel.subProvider)]
475 + : [],
476 + ),
477 + );
478 const serverKeybindings = useAtomValue(primaryServerKeybindingsAtom);
479 const keybindings = providedKeybindings ?? serverKeybindings;
480 const [searchHeight, setSearchHeight] = useState(0);
@@ -492,6 +505,14 @@ export const ModelPickerContent = memo(function ModelPickerContent(props: {
505 });
506 };
507
508 + const toggleSubProviderSection = (key: string) => {
509 + setExpandedSubProviderKeys((expanded) => {
510 + const next = new Set(expanded);
511 + if (!next.delete(key)) next.add(key);
512 + return next;
513 + });
514 + };
515 +
516 const handleModelSelect = (
517 modelSlug: string,
518 instanceId: ProviderInstanceId,
@@ -599,6 +620,13 @@ export const ModelPickerContent = memo(function ModelPickerContent(props: {
620 return out;
621 })();
622
623 + const subProvidersByInstance = new Map(
624 + instanceEntries.flatMap((entry) =>
625 + entry.snapshot.subProviders?.length
626 + ? [[entry.instanceId, entry.snapshot.subProviders] as const]
627 + : [],
628 + ),
629 + );
630 const isLocked = lockedProvider !== null;
631 const isSearching = searchQuery.trim().length > 0;
632 // Fit the tallest tab so switching tabs never resizes the popup and moves the rail under the cursor.
@@ -623,6 +651,9 @@ export const ModelPickerContent = memo(function ModelPickerContent(props: {
651 }
652 }
653 for (const instanceId of tabsWithLegacyRow) addRow(instanceId);
654 + for (const [instanceId, subProviders] of subProvidersByInstance) {
655 + for (const _subProvider of subProviders) addRow(instanceId);
656 + }
657 return Math.max(0, ...unitsByTab.values());
658 })();
659 const lockedDisabledInstanceIds = (() => {
@@ -714,7 +745,46 @@ export const ModelPickerContent = memo(function ModelPickerContent(props: {
745 };
746 })();
747
748 + // A provider whose models run on several backends, such as fx, lists each
749 + // backend as a collapsible heading. Search and Favorites stay flat.
750 + const subProviderSections = (() => {
751 + const subProviders =
752 + isSearching || selectedInstanceId === "favorites"
753 + ? undefined
754 + : subProvidersByInstance.get(selectedInstanceId);
755 + if (!subProviders || selectedInstanceId === "favorites") return null;
756 + const { ungrouped, groups } = groupModelsBySubProvider(
757 + filteredModels,
758 + subProviders,
759 + (model) => model.subProvider,
760 + );
761 + return {
762 + ungrouped,
763 + sections: groups.map(({ subProvider, models }) => {
764 + const key = modelPickerSubProviderSectionKey(selectedInstanceId, subProvider.name);
765 + return {
766 + key,
767 + name: subProvider.name,
768 + unavailableReason: subProvider.unavailableReason,
769 + models,
770 + isExpanded: expandedSubProviderKeys.has(key),
771 + };
772 + }),
773 + };
774 + })();
775 + const subProviderSectionByKey = new Map(
776 + subProviderSections?.sections.map((section) => [section.key, section] as const),
777 + );
778 +
779 const visibleModels = (() => {
780 + if (subProviderSections) {
781 + return [
782 + ...subProviderSections.ungrouped,
783 + ...subProviderSections.sections.flatMap((section) =>
784 + section.isExpanded ? section.models : [],
785 + ),
786 + ];
787 + }
788 if (!legacySection) {
789 return filteredModels;
790 }
@@ -762,11 +832,24 @@ export const ModelPickerContent = memo(function ModelPickerContent(props: {
832 .filter((model) => model.isLegacy)
833 .map((model) => modelPickerLegacySectionKey(model.instanceId)),
834 ),
835 + ...[...subProvidersByInstance].flatMap(([instanceId, subProviders]) =>
836 + subProviders.map((subProvider) =>
837 + modelPickerSubProviderSectionKey(instanceId, subProvider.name),
838 + ),
839 + ),
840 ])();
841 const filteredItemKeys = ((): string[] => {
767 - const modelKeys = visibleModels.map((model) =>
768 - modelPickerModelKey(model.instanceId, model.slug),
769 - );
842 + const keyOf = (model: ModelPickerItem) => modelPickerModelKey(model.instanceId, model.slug);
843 + if (subProviderSections) {
844 + return [
845 + ...subProviderSections.ungrouped.map(keyOf),
846 + ...subProviderSections.sections.flatMap((section) => [
847 + section.key,
848 + ...(section.isExpanded ? section.models.map(keyOf) : []),
849 + ]),
850 + ];
851 + }
852 + const modelKeys = visibleModels.map(keyOf);
853 // The toggle trails the list, so expanding it reveals older models above it.
854 return legacySection ? [...modelKeys, legacySection.key] : modelKeys;
855 })();
@@ -925,6 +1008,10 @@ export const ModelPickerContent = memo(function ModelPickerContent(props: {
1008 toggleLegacySection(legacyInstanceId);
1009 return;
1010 }
1011 + if (subProviderSectionByKey.has(modelKey)) {
1012 + toggleSubProviderSection(modelKey);
1013 + return;
1014 + }
1015 const model = parseModelPickerModelKey(modelKey);
1016 if (model) {
1017 handleModelSelect(
@@ -1017,6 +1104,10 @@ export const ModelPickerContent = memo(function ModelPickerContent(props: {
1104 toggleLegacySection(legacyInstanceId);
1105 return;
1106 }
1107 + if (subProviderSectionByKey.has(highlightedModelKeyRef.current)) {
1108 + toggleSubProviderSection(highlightedModelKeyRef.current);
1109 + return;
1110 + }
1111 const model = parseModelPickerModelKey(highlightedModelKeyRef.current);
1112 if (model) {
1113 handleModelSelect(model.slug, model.instanceId, e.shiftKey);
@@ -1051,6 +1142,47 @@ export const ModelPickerContent = memo(function ModelPickerContent(props: {
1142 </ComboboxItem>
1143 );
1144 }
1145 + const subProviderSection = subProviderSectionByKey.get(modelKey);
1146 + if (subProviderSection) {
1147 + const unavailable = subProviderSection.unavailableReason !== undefined;
1148 + return (
1149 + <ComboboxItem
1150 + hideIndicator
1151 + index={index}
1152 + value={modelKey}
1153 + disabled={unavailable}
1154 + aria-expanded={unavailable ? undefined : subProviderSection.isExpanded}
1155 + className="group w-full cursor-pointer"
1156 + >
1157 + <div className="min-w-0 flex-1 text-left">
1158 + <div className="flex min-w-0 items-center gap-2">
1159 + <div className="min-w-0 truncate text-sm font-normal leading-snug">
1160 + {subProviderSection.name}
1161 + </div>
1162 + {unavailable ? (
1163 + <Badge variant="outline" size="sm">
1164 + {gt("Unavailable", { $context: "backend cannot be used right now" })}
1165 + </Badge>
1166 + ) : null}
1167 + </div>
1168 + <div className="mt-1 text-xs font-normal leading-snug text-muted-foreground">
1169 + {subProviderSection.unavailableReason ??
1170 + gt("{count, plural, one {{count} model} other {{count} models}}", {
1171 + count: subProviderSection.models.length,
1172 + })}
1173 + </div>
1174 + </div>
1175 + {unavailable ? null : (
1176 + <ChevronRightIcon
1177 + className={cn(
1178 + "size-4 shrink-0 transition-transform",
1179 + subProviderSection.isExpanded && "rotate-90",
1180 + )}
1181 + />
1182 + )}
1183 + </ComboboxItem>
1184 + );
1185 + }
1186 const model = filteredModelByKey.get(modelKey);
1187 if (!model) {
1188 return null;
apps/web/src/components/chat/modelPickerKeys.ts
+8
@@ -2,6 +2,7 @@ import type { ProviderInstanceId } from "@t3tools/contracts";
2
3 const MODEL_KEY_PREFIX = "model:";
4 const LEGACY_SECTION_KEY_PREFIX = "legacy-models:";
5 +const SUB_PROVIDER_SECTION_KEY_PREFIX = "sub-provider:";
6
7 export function modelPickerModelKey(instanceId: ProviderInstanceId, slug: string): string {
8 return `${MODEL_KEY_PREFIX}${instanceId.length}:${instanceId}${slug}`;
@@ -45,3 +46,10 @@ export function parseModelPickerLegacySectionKey(key: string): ProviderInstanceI
46 ? (key.slice(LEGACY_SECTION_KEY_PREFIX.length) as ProviderInstanceId)
47 : null;
48 }
49 +
50 +export function modelPickerSubProviderSectionKey(
51 + instanceId: ProviderInstanceId,
52 + subProvider: string,
53 +): string {
54 + return `${SUB_PROVIDER_SECTION_KEY_PREFIX}${instanceId.length}:${instanceId}${subProvider}`;
55 +}
apps/web/src/i18n/translations/de.json
+2 -1
@@ -3694,6 +3694,8 @@
3694 "071b1cfc4d0d10d9": "Modelle durchsuchen...",
3695 "ffea432e08602f9c": "Weniger anzeigen",
3696 "04b2f5e7a86fd8db": "Mehr anzeigen",
3697 + "316d2cea8d8145ea": "Unavailable",
3698 + "84e6a3d4f6d08b5b": "{count, plural, one {{count} Modell} other {{count} Modelle}}",
3699 "aaae02e050a73bc2": "Noch keine Favoriten",
3700 "656e11e9444ed41e": "Projektmodelle werden durchsucht...",
3701 "4a1b2d720142a885": "Keine Modelle gefunden",
@@ -8974,7 +8976,6 @@
8976 "d0a399c2a9ce6524": "Modelle filtern",
8977 "e988564bf678ea40": "Alle aktivieren",
8978 "e9b965739869e86f": "Alle deaktivieren",
8977 - "84e6a3d4f6d08b5b": "{count, plural, one {{count} Modell} other {{count} Modelle}}",
8979 "2439b61eb85cb5be": "{count, plural, one {{count} Favorit} other {{count} Favoriten}}",
8980 "75191b82aba347d7": "{count, plural, one {{count} ausgeblendet} other {{count} ausgeblendet}}",
8981 "d318e899fdd202ff": "Keine passenden Modelle.",
apps/web/src/i18n/translations/en.json
+2 -1
@@ -3693,6 +3693,8 @@
3693 "071b1cfc4d0d10d9": "Search models...",
3694 "ffea432e08602f9c": "Show less",
3695 "04b2f5e7a86fd8db": "Show more",
3696 + "316d2cea8d8145ea": "Unavailable",
3697 + "84e6a3d4f6d08b5b": "{count, plural, one {{count} model} other {{count} models}}",
3698 "aaae02e050a73bc2": "No favorites yet",
3699 "656e11e9444ed41e": "Searching project models...",
3700 "4a1b2d720142a885": "No models found",
@@ -8982,7 +8984,6 @@
8984 "d0a399c2a9ce6524": "Filter models",
8985 "e988564bf678ea40": "Enable all",
8986 "e9b965739869e86f": "Disable all",
8985 - "84e6a3d4f6d08b5b": "{count, plural, one {{count} model} other {{count} models}}",
8987 "2439b61eb85cb5be": "{count, plural, one {{count} favorite} other {{count} favorites}}",
8988 "75191b82aba347d7": "{count, plural, other {{count} hidden}}",
8989 "d318e899fdd202ff": "No models match.",
apps/web/src/i18n/translations/es.json
+2 -1
@@ -3691,6 +3691,8 @@
3691 "071b1cfc4d0d10d9": "Buscar modelos...",
3692 "ffea432e08602f9c": "Mostrar menos",
3693 "04b2f5e7a86fd8db": "Mostrar más",
3694 + "316d2cea8d8145ea": "Unavailable",
3695 + "84e6a3d4f6d08b5b": "{count, plural, one {{count} modelo} other {{count} modelos}}",
3696 "aaae02e050a73bc2": "Aún no hay favoritos",
3697 "656e11e9444ed41e": "Buscando modelos del proyecto...",
3698 "4a1b2d720142a885": "No se encontraron modelos",
@@ -8972,7 +8974,6 @@
8974 "d0a399c2a9ce6524": "Filtrar modelos",
8975 "e988564bf678ea40": "Activar todos",
8976 "e9b965739869e86f": "Desactivar todos",
8975 - "84e6a3d4f6d08b5b": "{count, plural, one {{count} modelo} other {{count} modelos}}",
8977 "2439b61eb85cb5be": "{count, plural, one {{count} favorito} other {{count} favoritos}}",
8978 "75191b82aba347d7": "{count, plural, one {{count} oculto} other {{count} ocultos}}",
8979 "d318e899fdd202ff": "Ningún modelo coincide.",
apps/web/src/i18n/translations/fr.json
+2 -1
@@ -3692,6 +3692,8 @@
3692 "071b1cfc4d0d10d9": "Rechercher des modèles...",
3693 "ffea432e08602f9c": "Afficher moins",
3694 "04b2f5e7a86fd8db": "Afficher plus",
3695 + "316d2cea8d8145ea": "Unavailable",
3696 + "84e6a3d4f6d08b5b": "{count, plural, one {{count} modèle} other {{count} modèles}}",
3697 "aaae02e050a73bc2": "Pas encore de favori",
3698 "656e11e9444ed41e": "Recherche des modèles du projet...",
3699 "4a1b2d720142a885": "Aucun modèle trouvé",
@@ -8974,7 +8976,6 @@
8976 "d0a399c2a9ce6524": "Filtrer les modèles",
8977 "e988564bf678ea40": "Tout activer",
8978 "e9b965739869e86f": "Tout désactiver",
8977 - "84e6a3d4f6d08b5b": "{count, plural, one {{count} modèle} other {{count} modèles}}",
8979 "2439b61eb85cb5be": "{count, plural, one {{count} favori} other {{count} favoris}}",
8980 "75191b82aba347d7": "{count, plural, one {{count} masqué} other {{count} masqués}}",
8981 "d318e899fdd202ff": "Aucun modèle ne correspond.",
apps/web/src/i18n/translations/ja.json
+2 -1
@@ -3685,6 +3685,8 @@
3685 "071b1cfc4d0d10d9": "モデルを検索...",
3686 "ffea432e08602f9c": "表示を減らす",
3687 "04b2f5e7a86fd8db": "さらに表示",
3688 + "316d2cea8d8145ea": "Unavailable",
3689 + "84e6a3d4f6d08b5b": "{count, plural, other {{count} 個のモデル}}",
3690 "aaae02e050a73bc2": "お気に入りはまだありません",
3691 "656e11e9444ed41e": "プロジェクトのモデルを検索しています...",
3692 "4a1b2d720142a885": "モデルが見つかりません",
@@ -8930,7 +8932,6 @@
8932 "d0a399c2a9ce6524": "モデルを絞り込む",
8933 "e988564bf678ea40": "すべて有効にする",
8934 "e9b965739869e86f": "すべて無効にする",
8933 - "84e6a3d4f6d08b5b": "{count, plural, other {{count} 個のモデル}}",
8935 "2439b61eb85cb5be": "{count, plural, other {{count} 件のお気に入り}}",
8936 "75191b82aba347d7": "{count, plural, other {{count} 件を非表示}}",
8937 "d318e899fdd202ff": "一致するモデルはありません。",
apps/web/src/i18n/translations/ko.json
+2 -1
@@ -3683,6 +3683,8 @@
3683 "071b1cfc4d0d10d9": "모델 검색...",
3684 "ffea432e08602f9c": "간략히 보기",
3685 "04b2f5e7a86fd8db": "더 보기",
3686 + "316d2cea8d8145ea": "Unavailable",
3687 + "84e6a3d4f6d08b5b": "{count, plural, other {모델 {count}개}}",
3688 "aaae02e050a73bc2": "아직 즐겨찾기가 없습니다",
3689 "656e11e9444ed41e": "프로젝트 모델을 검색하는 중...",
3690 "4a1b2d720142a885": "모델을 찾을 수 없음",
@@ -8932,7 +8934,6 @@
8934 "d0a399c2a9ce6524": "모델 필터",
8935 "e988564bf678ea40": "모두 사용",
8936 "e9b965739869e86f": "모두 사용 안 함",
8935 - "84e6a3d4f6d08b5b": "{count, plural, other {모델 {count}개}}",
8937 "2439b61eb85cb5be": "{count, plural, other {즐겨찾기 {count}개}}",
8938 "75191b82aba347d7": "{count, plural, other {{count}개 숨김}}",
8939 "d318e899fdd202ff": "일치하는 모델이 없습니다.",
apps/web/src/i18n/translations/pt-BR.json
+2 -1
@@ -3692,6 +3692,8 @@
3692 "071b1cfc4d0d10d9": "Pesquisar modelos...",
3693 "ffea432e08602f9c": "Mostrar menos",
3694 "04b2f5e7a86fd8db": "Mostrar mais",
3695 + "316d2cea8d8145ea": "Unavailable",
3696 + "84e6a3d4f6d08b5b": "{count, plural, one {{count} modelo} other {{count} modelos}}",
3697 "aaae02e050a73bc2": "Nenhum favorito ainda",
3698 "656e11e9444ed41e": "Pesquisando modelos do projeto...",
3699 "4a1b2d720142a885": "Nenhum modelo encontrado",
@@ -8973,7 +8975,6 @@
8975 "d0a399c2a9ce6524": "Filtrar modelos",
8976 "e988564bf678ea40": "Ativar todos",
8977 "e9b965739869e86f": "Desativar todos",
8976 - "84e6a3d4f6d08b5b": "{count, plural, one {{count} modelo} other {{count} modelos}}",
8978 "2439b61eb85cb5be": "{count, plural, one {{count} favorito} other {{count} favoritos}}",
8979 "75191b82aba347d7": "{count, plural, one {{count} oculto} other {{count} ocultos}}",
8980 "d318e899fdd202ff": "Nenhum modelo corresponde.",
apps/web/src/i18n/translations/ru.json
+2 -1
@@ -3692,6 +3692,8 @@
3692 "071b1cfc4d0d10d9": "Поиск моделей...",
3693 "ffea432e08602f9c": "Свернуть",
3694 "04b2f5e7a86fd8db": "Показать ещё",
3695 + "316d2cea8d8145ea": "Unavailable",
3696 + "84e6a3d4f6d08b5b": "{count, plural, one {{count} модель} few {{count} модели} many {{count} моделей} other {{count} модели}}",
3697 "aaae02e050a73bc2": "В избранном пока пусто",
3698 "656e11e9444ed41e": "Поиск моделей проекта...",
3699 "4a1b2d720142a885": "Модели не найдены",
@@ -8974,7 +8976,6 @@
8976 "d0a399c2a9ce6524": "Фильтр моделей",
8977 "e988564bf678ea40": "Включить все",
8978 "e9b965739869e86f": "Отключить все",
8977 - "84e6a3d4f6d08b5b": "{count, plural, one {{count} модель} few {{count} модели} many {{count} моделей} other {{count} модели}}",
8979 "2439b61eb85cb5be": "{count, plural, one {{count} избранная} few {{count} избранные} many {{count} избранных} other {{count} избранной}}",
8980 "75191b82aba347d7": "{count, plural, one {{count} скрыта} few {{count} скрыто} many {{count} скрыто} other {{count} скрыто}}",
8981 "d318e899fdd202ff": "Нет подходящих моделей.",
apps/web/src/i18n/translations/sv.json
+2 -1
@@ -3692,6 +3692,8 @@
3692 "071b1cfc4d0d10d9": "Sök modeller...",
3693 "ffea432e08602f9c": "Visa färre",
3694 "04b2f5e7a86fd8db": "Visa fler",
3695 + "316d2cea8d8145ea": "Unavailable",
3696 + "84e6a3d4f6d08b5b": "{count, plural, one {{count} modell} other {{count} modeller}}",
3697 "aaae02e050a73bc2": "Inga favoriter än",
3698 "656e11e9444ed41e": "Söker projektmodeller...",
3699 "4a1b2d720142a885": "Inga modeller hittades",
@@ -8980,7 +8982,6 @@
8982 "d0a399c2a9ce6524": "Filtrera modeller",
8983 "e988564bf678ea40": "Aktivera alla",
8984 "e9b965739869e86f": "Inaktivera alla",
8983 - "84e6a3d4f6d08b5b": "{count, plural, one {{count} modell} other {{count} modeller}}",
8985 "2439b61eb85cb5be": "{count, plural, one {{count} favorit} other {{count} favoriter}}",
8986 "75191b82aba347d7": "{count, plural, one {{count} dold} other {{count} dolda}}",
8987 "d318e899fdd202ff": "Inga modeller matchar.",
apps/web/src/i18n/translations/zh.json
+2 -1
@@ -3696,6 +3696,8 @@
3696 "071b1cfc4d0d10d9": "搜索模型…",
3697 "ffea432e08602f9c": "收起",
3698 "04b2f5e7a86fd8db": "显示更多",
3699 + "316d2cea8d8145ea": "Unavailable",
3700 + "84e6a3d4f6d08b5b": "{count, plural, other {{count} 个模型}}",
3701 "aaae02e050a73bc2": "还没有收藏",
3702 "656e11e9444ed41e": "正在搜索项目模型…",
3703 "4a1b2d720142a885": "未找到模型",
@@ -8990,7 +8992,6 @@
8992 "d0a399c2a9ce6524": "筛选模型",
8993 "e988564bf678ea40": "全部启用",
8994 "e9b965739869e86f": "全部停用",
8993 - "84e6a3d4f6d08b5b": "{count, plural, other {{count} 个模型}}",
8995 "2439b61eb85cb5be": "{count, plural, other {{count} 个收藏}}",
8996 "75191b82aba347d7": "{count, plural, other {{count} 个已隐藏}}",
8997 "d318e899fdd202ff": "没有匹配的模型。",
apps/web/src/providerInstances.test.ts
+25
@@ -71,6 +71,31 @@ describe("isProviderInstancePickerReady", () => {
71 expect(entry?.status).toBe("ready");
72 expect(entry && isProviderInstancePickerReady(entry)).toBe(false);
73 });
74 +
75 + it("offers a warning provider's models on its usable sub-providers only", () => {
76 + const backendModel = (slug: string, subProvider: string) => ({ ...model(slug), subProvider });
77 + const fx = (models: ServerProvider["models"]) => ({
78 + ...provider({
79 + provider: ProviderDriverKind.make("fx"),
80 + instanceId: "fx",
81 + status: "warning",
82 + models,
83 + }),
84 + subProviders: [
85 + { name: "Gateway" },
86 + { name: "Codex", unavailableReason: "Signed out. Run `fx login codex` on this machine." },
87 + ],
88 + });
89 + const ready = (snapshot: ServerProvider) =>
90 + deriveProviderInstanceEntries([snapshot]).map(isProviderInstancePickerReady)[0];
91 +
92 + expect(
93 + ready(fx([model("default", false, true), backendModel("openai/gpt-5.5", "Gateway")])),
94 + ).toBe(true);
95 + expect(ready(fx([model("default", false, true), backendModel("gpt-5.5", "Codex")]))).toBe(
96 + false,
97 + );
98 + });
99 });
100
101 describe("applyProviderInstanceSettings", () => {
apps/web/src/providerInstances.ts
+15 -2
@@ -48,9 +48,22 @@ export function hasInstanceBadge(entries: ReadonlyArray<ProviderInstanceEntry>):
48 return entries.some((entry) => shouldShowInstanceBadge(entry, entries));
49 }
50
51 -/** Disabled instances may retain ready status until reconciliation. */
51 +/**
52 + * Disabled instances may retain ready status until reconciliation. A warning still offers
53 + * the models of sub-providers that report no problem of their own.
54 + */
55 export function isProviderInstancePickerReady(entry: ProviderInstanceEntry): boolean {
53 - return entry.enabled && entry.isAvailable && entry.status === "ready";
56 + if (!entry.enabled || !entry.isAvailable) return false;
57 + if (entry.status === "ready") return true;
58 + if (entry.status !== "warning") return false;
59 + const usable = new Set(
60 + entry.snapshot.subProviders?.flatMap((subProvider) =>
61 + subProvider.unavailableReason === undefined ? [subProvider.name] : [],
62 + ),
63 + );
64 + return entry.models.some(
65 + (model) => model.subProvider !== undefined && usable.has(model.subProvider),
66 + );
67 }
68
69 export function isProviderInstancePickerVisible(entry: ProviderInstanceEntry): boolean {
docs/user/providers-fx.md
+8 -4
@@ -25,13 +25,17 @@ When a newer fx release is out, the fx card in **Settings → Providers** shows
25
26 To update a standalone install manually, run `fx upgrade` on the server's machine. If **Binary path** is set, run that exact executable instead, for example `"/path/to/fx" upgrade`. Standalone version checks cover stable releases only; use the same manual command to get newer dev builds on fx's dev channel. On macOS 14 or earlier, which newer fx releases do not support, T3 Code does not offer fx updates.
27
28 -## Models and effort
28 +## Backends, models, and effort
29
30 -The model picker lists **Default** first. That option runs the model `fx status` reports on the server's machine. A model set only for one project in fx's settings is not picked up, so choose it from the list instead. If the thread's fx backend does not offer that model, the thread keeps its current one. The other entries come from `fx models`. A custom model you add in the provider settings goes to fx as is, and fx reports an error if its backend does not offer it.
30 +fx can run models through several backends: Vercel AI Gateway, a Codex subscription, a Grok subscription, and any OpenAI-compatible endpoint you configure in fx's settings. In the model picker, the fx tab lists each backend under its own heading. Open a backend to see the models it offers, then pick one. Search lists every backend's models together.
31
32 -fx also has an **Effort** control. A new thread starts at Medium.
32 +The backend is part of each thread's model choice, so two threads can run on different backends at the same time. Switching it in T3 Code changes only that thread's fx session. It never changes the default `fx provider` sets for your terminal.
33 +
34 +Sign-ins are different: every fx instance on a machine shares that machine's fx login. A backend you are signed out of shows as unavailable, with the command to run on that machine, such as `fx login grok`. Refresh the provider after signing in.
35
34 -T3 Code does not show fx's backend selector. To use Codex, Grok, or Vercel AI Gateway, run `fx provider` on the environment's machine.
36 +**Default** runs whatever fx would start a new session on in the thread's project folder, including a model fx's settings give only that folder. Its label shows the machine-wide default. A custom model you add in the provider settings goes to fx as is, on the thread's current backend, and fx reports an error if that backend does not offer it.
37 +
38 +fx also has an **Effort** control. A new thread starts at Medium.
39
40 Type `$` in the composer to list the skills fx finds in its skill folders, in the order fx's own skill menu shows them. When two different skills share a name, the menu lists the one fx loads for that name: the first copy in fx's folder order, starting with the workspace. The menu shows one skill per name regardless of case; to run a skill whose name differs only in case, such as `Review` next to `review`, type its exact name.
41
packages/client-runtime/package.json
+4
@@ -167,6 +167,10 @@
167 "types": "./src/providerUpdateRequiredModels.ts",
168 "default": "./src/providerUpdateRequiredModels.ts"
169 },
170 + "./providerSubProviders": {
171 + "types": "./src/providerSubProviders.ts",
172 + "default": "./src/providerSubProviders.ts"
173 + },
174 "./voice-input": {
175 "types": "./src/voice-input/index.ts",
176 "default": "./src/voice-input/index.ts"
packages/client-runtime/src/providerSubProviders.ts new
+29
@@ -0,0 +1,29 @@
1 +import type { ServerProviderSubProvider } from "@t3tools/contracts";
2 +
3 +export interface SubProviderGroup<Model> {
4 + readonly subProvider: ServerProviderSubProvider;
5 + readonly models: ReadonlyArray<Model>;
6 +}
7 +
8 +/**
9 + * Splits a provider's models into the ones outside any sub-provider, such as
10 + * "Default", and one group per sub-provider in the provider's order. A
11 + * signed-out sub-provider keeps its group, with no models.
12 + */
13 +export function groupModelsBySubProvider<Model>(
14 + models: ReadonlyArray<Model>,
15 + subProviders: ReadonlyArray<ServerProviderSubProvider>,
16 + subProviderOf: (model: Model) => string | undefined,
17 +): {
18 + readonly ungrouped: ReadonlyArray<Model>;
19 + readonly groups: ReadonlyArray<SubProviderGroup<Model>>;
20 +} {
21 + const names = new Set(subProviders.map((subProvider) => subProvider.name));
22 + return {
23 + ungrouped: models.filter((model) => !names.has(subProviderOf(model) ?? "")),
24 + groups: subProviders.map((subProvider) => ({
25 + subProvider,
26 + models: models.filter((model) => subProviderOf(model) === subProvider.name),
27 + })),
28 + };
29 +}
packages/contracts/src/server.ts
+10
@@ -92,6 +92,14 @@ export const ServerProviderModel = Schema.Struct({
92 });
93 export type ServerProviderModel = typeof ServerProviderModel.Type;
94
95 +/** A backend a provider's models run on, such as one of fx's. */
96 +export const ServerProviderSubProvider = Schema.Struct({
97 + name: TrimmedNonEmptyString,
98 + /** Why none of its models can run now, such as a signed-out backend. */
99 + unavailableReason: Schema.optional(TrimmedNonEmptyString),
100 +});
101 +export type ServerProviderSubProvider = typeof ServerProviderSubProvider.Type;
102 +
103 export const ServerProviderUpdateRequiredModel = Schema.Struct({
104 slug: TrimmedNonEmptyString,
105 name: TrimmedNonEmptyString,
@@ -269,6 +277,8 @@ export const ServerProvider = Schema.Struct({
277 availability: Schema.optional(ServerProviderAvailability),
278 unavailableReason: Schema.optional(TrimmedNonEmptyString),
279 models: Schema.Array(ServerProviderModel),
280 + // Pickers group `models` under these, in order, by `subProvider`.
281 + subProviders: Schema.optional(Schema.Array(ServerProviderSubProvider)),
282 // Kept apart from `models` so clients that predate it never offer them.
283 updateRequiredModels: Schema.optionalKey(Schema.Array(ServerProviderUpdateRequiredModel)),
284 slashCommands: Schema.Array(ServerProviderSlashCommand).pipe(
packages/provider-fx/src/server/acpSupport.test.ts
+75 -1
@@ -1,9 +1,18 @@
1 import { assert, describe, it } from "@effect/vitest";
2 +import { ProviderInstanceId } from "@t3tools/contracts";
3 +import * as Cause from "effect/Cause";
4 +import * as Effect from "effect/Effect";
5 +import * as Exit from "effect/Exit";
6 +import * as EffectAcpErrors from "effect-acp/errors";
7 import type * as EffectAcpSchema from "effect-acp/compat";
8
9 import { ProviderAdapterV2RuntimePolicy } from "@t3tools/provider-core/server/ProviderAdapter";
10
6 -import { fxPermissionDisposition, fxToolCallWasCancelled } from "./acpSupport.ts";
11 +import {
12 + applyFxModelSelection,
13 + fxPermissionDisposition,
14 + fxToolCallWasCancelled,
15 +} from "./acpSupport.ts";
16
17 const editRequest = {
18 sessionId: "fx-session-01",
@@ -32,6 +41,71 @@ describe("fx permission prompts", () => {
41 });
42 });
43
44 +describe("fx model selection", () => {
45 + it.effect.each(["unavailable", "rejected"] as const)(
46 + "restores the original backend and model when Default is %s",
47 + (failure) =>
48 + Effect.gen(function* () {
49 + let backend = "codex";
50 + let model = "codex-custom";
51 + const configOptions = (): ReadonlyArray<EffectAcpSchema.SessionConfigOption> => [
52 + {
53 + id: "provider",
54 + name: "provider",
55 + type: "select",
56 + currentValue: backend,
57 + options: ["codex", "gateway"].map((value) => ({ value, name: value })),
58 + },
59 + {
60 + id: "model",
61 + name: "model",
62 + type: "select",
63 + currentValue: model,
64 + options: (backend === "codex"
65 + ? ["codex-default", "codex-custom"]
66 + : ["gateway-fallback", ...(failure === "rejected" ? ["gateway-default"] : [])]
67 + ).map((value) => ({ value, name: value })),
68 + },
69 + ];
70 + const runtime = {
71 + getConfigOptions: Effect.sync(configOptions),
72 + setConfigOption: (id: string, value: string | boolean) =>
73 + Effect.gen(function* () {
74 + if (id === "provider") {
75 + backend = String(value);
76 + model = backend === "codex" ? "codex-default" : "gateway-fallback";
77 + } else if (id === "model") {
78 + if (value === "gateway-default") {
79 + return yield* new EffectAcpErrors.AcpRequestError({
80 + code: -32602,
81 + errorMessage: "Model rejected",
82 + });
83 + }
84 + model = String(value);
85 + }
86 + return { configOptions: configOptions() };
87 + }),
88 + };
89 + const result = yield* applyFxModelSelection(
90 + runtime,
91 + { instanceId: ProviderInstanceId.make("fx"), model: "default" },
92 + Effect.succeed({ backend: "gateway", model: "gateway-default" }),
93 + ).pipe(Effect.exit);
94 + assert.isTrue(Exit.isFailure(result));
95 + if (Exit.isFailure(result)) {
96 + assert.include(
97 + Cause.pretty(result.cause),
98 + failure === "unavailable"
99 + ? "The default fx model is unavailable on its configured backend."
100 + : "Model rejected",
101 + );
102 + }
103 + assert.equal(backend, "codex");
104 + assert.equal(model, "codex-custom");
105 + }),
106 + );
107 +});
108 +
109 describe("fx cancellation failures", () => {
110 const diff = { type: "diff", path: "notes.txt", oldText: "old", newText: "new" };
111 const terminal = { type: "terminal", terminalId: "fx-terminal" };
packages/provider-fx/src/server/acpSupport.ts
+88 -25
@@ -27,6 +27,7 @@ import type * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAda
27 export const FX_DRIVER_KIND = ProviderDriverKind.make("fx");
28 /** T3's slug for "Default": the model `fx status` reports, never an fx model id. */
29 export const FX_DEFAULT_MODEL_SLUG = "default";
30 +const FX_PROVIDER_CONFIG_ID = "provider";
31 const FX_MODEL_CONFIG_ID = "model";
32 export const FX_MODE_CONFIG_ID = "mode";
33 const FX_EFFORT_CONFIG_ID = "effort";
@@ -213,36 +214,98 @@ export function withFxSessionMode(
214 };
215 }
216
216 -/**
217 - * Writes config id `model` (fx's `provider` option shares its category).
218 - * "Default" picks the model `fx status` reports when the session lists it.
219 - * An unlisted model the thread picked is still sent: fx's backends accept or
220 - * refuse it themselves, so a thread never runs on a model it did not pick.
221 - */
217 +/** A model on one fx backend (`gateway`, `codex`, `grok`, or a configured endpoint's id). */
218 +export interface FxModelChoice {
219 + readonly backend: string | undefined;
220 + readonly model: string;
221 +}
222 +
223 +/** fx backend ids never contain `:`, so the first one ends the backend. */
224 +export const fxModelSlug = (backend: string, model: string) => `${backend}:${model}`;
225 +
226 +/** A slug without a known backend is a model id for the session's current backend. */
227 +function parseFxModelSlug(slug: string, backends: ReadonlyArray<string>): FxModelChoice {
228 + const separator = slug.indexOf(":");
229 + const backend = separator > 0 ? slug.slice(0, separator) : undefined;
230 + return backend !== undefined && backends.includes(backend)
231 + ? { backend, model: slug.slice(separator + 1) }
232 + : { backend: undefined, model: slug };
233 +}
234 +
235 +const selectOption = (
236 + options: ReadonlyArray<EffectAcpSchema.SessionConfigOption>,
237 + id: string,
238 +): Extract<EffectAcpSchema.SessionConfigOption, { type: "select" }> | undefined => {
239 + const option = options.find((entry) => entry.id === id);
240 + return option?.type === "select" ? option : undefined;
241 +};
242 +
243 +/** fx's model catalog changes with its backend, so it must be read after the provider write. */
244 export const applyFxModelSelection = Effect.fn("applyFxModelSelection")(function* (
223 - runtime: AcpSessionRuntime.AcpSessionRuntime["Service"],
245 + runtime: Pick<
246 + AcpSessionRuntime.AcpSessionRuntime["Service"],
247 + "getConfigOptions" | "setConfigOption"
248 + >,
249 modelSelection: ModelSelection,
225 - defaultModel: Effect.Effect<string | undefined> | undefined,
250 + defaultChoice: Effect.Effect<FxModelChoice | undefined> | undefined,
251 ): Effect.fn.Return<string | undefined, EffectAcpErrors.AcpError> {
227 - const option = (yield* runtime.getConfigOptions).find(
228 - (entry) => entry.id === FX_MODEL_CONFIG_ID && entry.type === "select",
229 - );
230 - const current = option?.type === "select" ? option.currentValue : undefined;
252 + const originalOptions = yield* runtime.getConfigOptions;
253 + const backendOption = selectOption(originalOptions, FX_PROVIDER_CONFIG_ID);
254 + const originalBackend = backendOption?.currentValue;
255 + const originalModel = selectOption(originalOptions, FX_MODEL_CONFIG_ID)?.currentValue;
256 const requested = modelSelection.model.trim();
257 const isDefault = requested.length === 0 || requested === FX_DEFAULT_MODEL_SLUG;
233 - const target = isDefault ? defaultModel && (yield* defaultModel) : requested;
234 - if (target === undefined || target === current) return current;
235 - // The status default can belong to another fx backend than the session's
236 - // (after `fx provider`, or a loaded older session); then the session's
237 - // own model stands.
238 - if (
239 - isDefault &&
240 - (option === undefined || !collectSessionConfigOptionValues(option).includes(target))
241 - ) {
242 - return current;
243 - }
244 - yield* runtime.setConfigOption(FX_MODEL_CONFIG_ID, target, { allowUnlistedValue: true });
245 - return target;
258 + const target = isDefault
259 + ? defaultChoice && (yield* defaultChoice)
260 + : parseFxModelSlug(
261 + requested,
262 + backendOption ? collectSessionConfigOptionValues(backendOption) : [],
263 + );
264 + const canSelectBackend =
265 + target?.backend === undefined ||
266 + (backendOption !== undefined &&
267 + (target.backend === backendOption.currentValue ||
268 + collectSessionConfigOptionValues(backendOption).includes(target.backend)));
269 + const changingBackend =
270 + target?.backend !== undefined &&
271 + backendOption !== undefined &&
272 + canSelectBackend &&
273 + target.backend !== backendOption.currentValue;
274 + return yield* Effect.gen(function* () {
275 + if (changingBackend && target?.backend !== undefined) {
276 + yield* runtime.setConfigOption(FX_PROVIDER_CONFIG_ID, target.backend);
277 + }
278 + const modelOption = selectOption(yield* runtime.getConfigOptions, FX_MODEL_CONFIG_ID);
279 + const current = modelOption?.currentValue;
280 + if (target === undefined || target.model === current) return current;
281 + if (isDefault && !canSelectBackend) return current;
282 + if (
283 + isDefault &&
284 + (modelOption === undefined ||
285 + !collectSessionConfigOptionValues(modelOption).includes(target.model))
286 + ) {
287 + return yield* new EffectAcpErrors.AcpRequestError({
288 + code: -32602,
289 + errorMessage: "The default fx model is unavailable on its configured backend.",
290 + data: { configId: FX_MODEL_CONFIG_ID },
291 + });
292 + }
293 + yield* runtime.setConfigOption(FX_MODEL_CONFIG_ID, target.model, { allowUnlistedValue: true });
294 + return target.model;
295 + }).pipe(
296 + Effect.onError(() => {
297 + if (!changingBackend || originalBackend === undefined) return Effect.void;
298 + return Effect.gen(function* () {
299 + yield* runtime.setConfigOption(FX_PROVIDER_CONFIG_ID, originalBackend);
300 + const restoredModel = selectOption(yield* runtime.getConfigOptions, FX_MODEL_CONFIG_ID);
301 + if (originalModel !== undefined && restoredModel?.currentValue !== originalModel) {
302 + yield* runtime.setConfigOption(FX_MODEL_CONFIG_ID, originalModel, {
303 + allowUnlistedValue: true,
304 + });
305 + }
306 + }).pipe(Effect.ignoreCause({ log: true }));
307 + }),
308 + );
309 });
310
311 /** The shared option loop writes a stored effort; a thread without one starts at medium. */
packages/provider-fx/src/server/adapter.ts
+16 -9
@@ -21,6 +21,7 @@ import {
21 FX_DRIVER_KIND,
22 FX_SESSION_META,
23 fxPermissionDisposition,
24 + type FxModelChoice,
25 FX_MODE_CONFIG_ID,
26 fxToolCallWasCancelled,
27 fxSessionModeForPolicy,
@@ -178,8 +179,8 @@ export interface FxAdapterV2Options {
179 readonly settings: FxSettings;
180 readonly environment: NodeJS.ProcessEnv;
181 readonly selfInvocation: SelfInvocation;
181 - /** fx's id for "Default", the model the provider snapshot read from `fx status`. */
182 - readonly defaultModel?: Effect.Effect<string | undefined>;
182 + /** The backend and model "Default" runs in a folder, which `fx status` reports there. */
183 + readonly defaultChoice?: (cwd: string) => Effect.Effect<FxModelChoice | undefined>;
184 readonly nativeLogging?: Parameters<typeof makeAcpAdapterV2>[0]["nativeLogging"];
185 readonly testHooks?: Parameters<typeof makeAcpAdapterV2>[0]["testHooks"];
186 /** Replaces the `fx acp` launch (replay tests). fx's session setup still applies. */
@@ -202,6 +203,8 @@ function makeFxAcpAdapterFlavor(options: FxAdapterV2Options): AcpAdapterV2Flavor
203 fxSettings: options.settings,
204 environment: { ...options.environment, ...processEnvironment },
205 }));
206 + // A runtime serves one folder; "Default" resolves there.
207 + const runtimeCwds = new WeakMap<AcpSessionRuntime.AcpSessionRuntime["Service"], string>();
208 return {
209 driver: FX_DRIVER_KIND,
210 runtimeHarness: "fx",
@@ -209,11 +212,16 @@ function makeFxAcpAdapterFlavor(options: FxAdapterV2Options): AcpAdapterV2Flavor
212 makeRuntime: (input) =>
213 makeRuntime({ ...input, sessionMeta: () => FX_SESSION_META }).pipe(
214 Effect.map(withFxSessionMode),
215 + Effect.tap((runtime) => Effect.sync(() => runtimeCwds.set(runtime, input.cwd))),
216 ),
213 - applyModelSelection: ({ runtime, modelSelection }) =>
214 - applyFxModelSelection(runtime, modelSelection, options.defaultModel).pipe(
215 - Effect.tap(() => applyFxEffortDefault(runtime, modelSelection)),
216 - ),
217 + applyModelSelection: ({ runtime, modelSelection }) => {
218 + const cwd = runtimeCwds.get(runtime);
219 + return applyFxModelSelection(
220 + runtime,
221 + modelSelection,
222 + cwd === undefined ? undefined : options.defaultChoice?.(cwd),
223 + ).pipe(Effect.tap(() => applyFxEffortDefault(runtime, modelSelection)));
224 + },
225 ownedModelOptionIds: [FX_MODE_CONFIG_ID],
226 lockSessionModeToPolicy: true,
227 sessionModeForPolicy: fxSessionModeForPolicy,
@@ -246,9 +254,8 @@ export type FxAdapterV2DriverEnv =
254 | ProviderEventLoggers.ProviderEventLoggers
255 | ProviderHost.ProviderHost;
256
249 -/** `defaultModel` reads fx's id for "Default" from the provider snapshot. */
257 export const makeFxAdapterV2Driver = (
251 - defaultModel: Effect.Effect<string | undefined>,
258 + defaultChoice: (cwd: string) => Effect.Effect<FxModelChoice | undefined>,
259 ): ProviderAdapterDriver<FxSettings, FxAdapterV2DriverEnv> => ({
260 driverKind: FX_DRIVER_KIND,
261 configSchema: FxSettings,
@@ -264,7 +271,7 @@ export const makeFxAdapterV2Driver = (
271 settings: { ...input.config, enabled: input.enabled },
272 environment: yield* mergeProviderInstanceEnvironment(input.environment, hostEnvironment),
273 selfInvocation,
267 - defaultModel,
274 + defaultChoice,
275 nativeLogging: (threadId) =>
276 makeNativeLogger({
277 nativeEventLogger: providerEventLoggers.native,
packages/provider-fx/src/server/driver.ts
+7 -4
@@ -16,7 +16,7 @@ import { ProviderDriverError } from "@t3tools/provider-core/server/errors";
16 import {
17 buildInitialFxProviderSnapshot,
18 checkFxProviderStatus,
19 - fxDefaultModelId,
19 + readFxDefaultChoice,
20 } from "./status.ts";
21 import { makeManagedServerProvider } from "@t3tools/provider-core/server/managedProvider";
22 import {
@@ -137,6 +137,8 @@ export const FxDriver: ProviderDriver<FxSettings, FxDriverEnv> = {
137 checkProvider: checkFxProviderStatus(effectiveConfig, processEnv).pipe(
138 Effect.map(stampIdentity),
139 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
140 + Effect.provideService(FileSystem.FileSystem, fileSystem),
141 + Effect.provideService(Path.Path, path),
142 ),
143 enrichSnapshot: ({ settings, snapshot: currentSnapshot, publishSnapshot }) =>
144 resolveMaintenance().pipe(
@@ -161,9 +163,10 @@ export const FxDriver: ProviderDriver<FxSettings, FxDriverEnv> = {
163 ),
164 );
165
164 - // "Default" selects the model the snapshot read from `fx status`.
165 - const orchestrationAdapter = yield* makeFxAdapterV2Driver(
166 - snapshot.getSnapshot.pipe(Effect.map((current) => fxDefaultModelId(current.models))),
166 + const orchestrationAdapter = yield* makeFxAdapterV2Driver((cwd) =>
167 + readFxDefaultChoice(effectiveConfig, processEnv, cwd).pipe(
168 + Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
169 + ),
170 )
171 .create({
172 instanceId,
packages/provider-fx/src/server/status.test.ts
+255 -66
@@ -1,8 +1,12 @@
1 import * as NodeServices from "@effect/platform-node/NodeServices";
2 import { assert, describe, it } from "@effect/vitest";
3 +import * as Deferred from "effect/Deferred";
4 import type * as Duration from "effect/Duration";
5 import * as Effect from "effect/Effect";
6 import * as Fiber from "effect/Fiber";
7 +import * as FileSystem from "effect/FileSystem";
8 +import * as Path from "effect/Path";
9 +import * as Redacted from "effect/Redacted";
10 import * as Schema from "effect/Schema";
11 import * as Sink from "effect/Sink";
12 import * as Stream from "effect/Stream";
@@ -11,7 +15,7 @@ import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
15 import * as TestClock from "effect/testing/TestClock";
16
17 import { FxSettings } from "../settings.ts";
14 -import { checkFxProviderStatus } from "./status.ts";
18 +import { checkFxProviderStatus, readFxDefaultChoice } from "./status.ts";
19
20 const encoder = new TextEncoder();
21 const settings = Schema.decodeSync(FxSettings)({ enabled: true });
@@ -38,46 +42,77 @@ function processHandle(stdout: string, code: number, answered: Effect.Effect<voi
42 });
43 }
44
41 -// `fx models --json` from fx 0.0.13, ids trimmed.
42 -const FX_MODELS_JSON = JSON.stringify({
45 +// `fx status --json` and `fx models --json` from fx 0.0.15, trimmed to the
46 +// fields T3 reads plus a few it ignores.
47 +const status = (auth: string, fields: Record<string, string> = {}) =>
48 + JSON.stringify({ kind: "status", model: "default", auth, auth_refreshable: false, ...fields });
49 +const models = (ids: ReadonlyArray<string>) =>
50 + JSON.stringify({ kind: "models", count: ids.length, private_models_hidden: false, ids });
51 +const GATEWAY_MODELS = models(["anthropic/claude-sonnet-5.5", "anthropic/claude-opus-5.5"]);
52 +const SIGNED_OUT_LISTING = JSON.stringify({
53 kind: "models",
44 - count: 2,
45 - shown_count: 2,
46 - more_count: 0,
47 - private_models_hidden: false,
48 - ids: ["anthropic/claude-sonnet-5.5", "anthropic/claude-opus-5.5"],
54 + error: "could not list models: AuthenticationRejected",
55 + code: "AuthenticationRejected",
56 });
57
51 -/** Answers `fx` invocations by their arguments and records which ran. */
58 +interface Invocation {
59 + readonly args: string;
60 + readonly backend: string | undefined;
61 + readonly cwd: string | undefined;
62 +}
63 +
64 +/**
65 + * Answers `fx` invocations keyed by `FX_PROVIDER` and arguments, such as
66 + * `codex status --json`; the unscoped status is ` status --json`. HOME holds
67 + * an fx profile whose settings define `profileProviders`, or `FX_HOME` does
68 + * when `profileInFxHome` is set.
69 + */
70 const check = (
53 - statusJson: string,
71 + outputs: Record<string, string>,
72 + profileProviders: ReadonlyArray<string> = [],
73 versionDelay: Duration.Input = 0,
55 - modelsJson: string = FX_MODELS_JSON,
74 + versionSpawned?: Deferred.Deferred<void>,
75 + profileInFxHome = false,
76 ) =>
77 Effect.gen(function* () {
58 - const outputs: Record<string, string> = {
59 - "--version": "fx 0.0.13\n",
60 - "status --json": statusJson,
61 - "models --json": modelsJson,
62 - };
63 - const invoked: Array<string> = [];
78 + const fileSystem = yield* FileSystem.FileSystem;
79 + const path = yield* Path.Path;
80 + const home = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-fx-status-" });
81 + const profile = path.join(home, profileInFxHome ? "fx-home" : ".fx");
82 + yield* fileSystem.makeDirectory(profile);
83 + yield* fileSystem.writeFileString(
84 + path.join(profile, "settings.json"),
85 + JSON.stringify({
86 + providers: Object.fromEntries(profileProviders.map((id) => [id, { protocol: "x" }])),
87 + }),
88 + );
89 + const invoked: Array<Invocation> = [];
90 const spawner = ChildProcessSpawner.make((command) => {
65 - const args = ChildProcess.isStandardCommand(command) ? command.args.join(" ") : "";
66 - invoked.push(args);
67 - const stdout = outputs[args];
68 - return Effect.succeed(
69 - processHandle(
91 + const standard = ChildProcess.isStandardCommand(command) ? command : undefined;
92 + const args = standard?.args.join(" ") ?? "";
93 + const backendValue = standard?.options.env?.FX_PROVIDER;
94 + const backend =
95 + typeof backendValue === "string" || backendValue === undefined
96 + ? backendValue
97 + : Redacted.value(backendValue);
98 + invoked.push({ args, backend, cwd: standard?.options.cwd });
99 + const stdout = args === "--version" ? "fx 0.0.15\n" : outputs[`${backend ?? ""} ${args}`];
100 + return Effect.gen(function* () {
101 + if (args === "--version" && versionSpawned)
102 + yield* Deferred.succeed(versionSpawned, undefined);
103 + return processHandle(
104 stdout ?? "",
105 stdout === undefined ? 2 : 0,
106 args === "--version" ? Effect.sleep(versionDelay) : Effect.void,
73 - ),
74 - );
107 + );
108 + });
109 });
76 - const draft = yield* checkFxProviderStatus(settings, {}).pipe(
77 - Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
78 - );
110 + const draft = yield* checkFxProviderStatus(
111 + settings,
112 + profileInFxHome ? { HOME: home, FX_HOME: profile } : { HOME: home },
113 + ).pipe(Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner));
114 return { draft, invoked };
80 - }).pipe(Effect.provide(NodeServices.layer));
115 + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer));
116
117 describe("checkFxProviderStatus", () => {
118 it.effect.each([
@@ -85,9 +120,16 @@ describe("checkFxProviderStatus", () => {
120 label: "answers after 6 s",
121 versionDelay: "6 seconds",
122 status: "ready",
88 - version: "0.0.13",
123 + version: "0.0.15",
124 message: undefined,
90 - invoked: ["--version", "status --json", "models --json"],
125 + invoked: [
126 + "--version",
127 + "status --json",
128 + "status --json",
129 + "status --json",
130 + "status --json",
131 + "models --json",
132 + ],
133 },
134 {
135 label: "never answers",
@@ -99,59 +141,164 @@ describe("checkFxProviderStatus", () => {
141 },
142 ] as const)("gives `fx --version` 10 s when it $label", (row) =>
143 Effect.gen(function* () {
144 + const versionSpawned = yield* Deferred.make<void>();
145 const fiber = yield* check(
103 - JSON.stringify({ kind: "status", model: "anthropic/claude-opus-5.5", auth: "fx login" }),
146 + {
147 + " status --json": status("fx login"),
148 + "gateway status --json": status("fx login"),
149 + "gateway models --json": GATEWAY_MODELS,
150 + "codex status --json": status("missing"),
151 + "grok status --json": status("missing"),
152 + },
153 + [],
154 row.versionDelay,
155 + versionSpawned,
156 ).pipe(Effect.forkChild);
157 + yield* Deferred.await(versionSpawned);
158 yield* TestClock.adjust("10 seconds");
159 const { draft, invoked } = yield* Fiber.join(fiber);
160 assert.equal(draft.status, row.status);
161 assert.equal(draft.version, row.version);
162 assert.equal(draft.message, row.message);
111 - assert.deepEqual(invoked, [...row.invoked]);
163 + assert.deepEqual(invoked.map(({ args }) => args).toSorted(), [...row.invoked].toSorted());
164 }),
165 );
166
115 - // `fx status --json` from fx 0.0.13, trimmed to the fields T3 reads plus a few it ignores.
116 - it.effect("reports sign-in from `fx status` without opening an ACP session", () =>
167 + it.effect("lists each backend's models and marks signed-out backends", () =>
168 Effect.gen(function* () {
118 - const signedIn = yield* check(
119 - JSON.stringify({
120 - kind: "status",
121 - model: "anthropic/claude-opus-5.5",
122 - auth: "fx login",
123 - auth_refreshable: true,
124 - permission_mode: "ask",
125 - }),
169 + const { draft, invoked } = yield* check(
170 + {
171 + " status --json": status("chatgpt subscription", {
172 + model: "gpt-5.5",
173 + model_source: "Codex subscription",
174 + }),
175 + "gateway status --json": status("fx login"),
176 + "gateway models --json": GATEWAY_MODELS,
177 + "codex status --json": status("chatgpt subscription"),
178 + "codex models --json": models(["gpt-5.5", "gpt-5.4-mini"]),
179 + "grok status --json": status("missing"),
180 + "local status --json": status("missing", {
181 + auth_help: "The configured provider credential is unavailable.",
182 + }),
183 + },
184 + ["local"],
185 );
127 - assert.deepEqual(signedIn.invoked, ["--version", "status --json", "models --json"]);
128 - assert.equal(signedIn.draft.status, "ready");
129 - assert.equal(signedIn.draft.version, "0.0.13");
130 - assert.deepEqual(signedIn.draft.auth, { status: "authenticated", label: "fx login" });
186 + // Never `fx acp`, which saves a session; signed-out backends list nothing.
187 + assert.deepEqual(invoked.map(({ backend, args }) => `${backend ?? ""} ${args}`).toSorted(), [
188 + " --version",
189 + " status --json",
190 + "codex models --json",
191 + "codex status --json",
192 + "gateway models --json",
193 + "gateway status --json",
194 + "grok status --json",
195 + "local status --json",
196 + ]);
197 + assert.equal(draft.status, "ready");
198 + assert.deepEqual(draft.auth, { status: "authenticated", label: "chatgpt subscription" });
199 assert.deepEqual(
132 - signedIn.draft.models.map((model) => model.slug),
133 - ["default", "anthropic/claude-sonnet-5.5", "anthropic/claude-opus-5.5"],
200 + draft.models.map((model) => [model.slug, model.name, model.subProvider]),
201 + [
202 + ["default", "Default (gpt-5.5)", undefined],
203 + [
204 + "gateway:anthropic/claude-sonnet-5.5",
205 + "anthropic/claude-sonnet-5.5",
206 + "Vercel AI Gateway",
207 + ],
208 + ["gateway:anthropic/claude-opus-5.5", "anthropic/claude-opus-5.5", "Vercel AI Gateway"],
209 + ["codex:gpt-5.5", "gpt-5.5", "Codex subscription"],
210 + ["codex:gpt-5.4-mini", "gpt-5.4-mini", "Codex subscription"],
211 + ],
212 );
135 - // "Default" carries the model `fx status` reports, which the adapter selects for it.
136 - assert.deepEqual(signedIn.draft.models[0]?.aliases, ["anthropic/claude-opus-5.5"]);
137 - assert.isDefined(signedIn.draft.versionAdvisory);
138 -
139 - const signedOut = yield* check(
140 - JSON.stringify({
141 - kind: "status",
142 - model: "spacexai/grok-4.7",
143 - auth: "missing",
144 - auth_refreshable: false,
145 - auth_help:
146 - "fx needs access to Vercel AI Gateway. Run fx login to sign in, fx setup to use an API key, or set AI_GATEWAY_API_KEY.",
147 - }),
213 + assert.deepEqual(draft.subProviders, [
214 + { name: "Vercel AI Gateway" },
215 + { name: "Codex subscription" },
216 + {
217 + name: "Grok subscription",
218 + unavailableReason: "Signed out. Run `fx login grok` on this machine.",
219 + },
220 + { name: "local", unavailableReason: "The configured provider credential is unavailable." },
221 + ]);
222 + assert.isDefined(draft.versionAdvisory);
223 + }),
224 + );
225 +
226 + it.effect("reads configured backends from the profile `FX_HOME` names", () =>
227 + Effect.gen(function* () {
228 + const { draft } = yield* check(
229 + {
230 + " status --json": status("fx login"),
231 + "gateway status --json": status("fx login"),
232 + "gateway models --json": GATEWAY_MODELS,
233 + "codex status --json": status("missing"),
234 + "grok status --json": status("missing"),
235 + "local status --json": status("fx login"),
236 + "local models --json": models([]),
237 + },
238 + ["local"],
239 + 0,
240 + undefined,
241 + true,
242 );
243 + assert.deepEqual(
244 + draft.subProviders?.map((subProvider) => subProvider.name),
245 + ["Vercel AI Gateway", "Codex subscription", "Grok subscription", "local"],
246 + );
247 + }),
248 + );
249 +
250 + it.effect("stays usable while fx's default backend is signed out", () =>
251 + Effect.gen(function* () {
252 + const { draft } = yield* check({
253 + " status --json": status("missing", { model: "spacexai/grok-4.7" }),
254 + "gateway status --json": status("missing"),
255 + "gateway models --json": GATEWAY_MODELS,
256 + "codex status --json": status("chatgpt subscription"),
257 + "codex models --json": models(["gpt-5.5"]),
258 + "grok status --json": status("missing"),
259 + });
260 + assert.equal(draft.status, "warning");
261 + assert.deepEqual(draft.auth, { status: "authenticated", label: "Codex subscription" });
262 + assert.equal(
263 + draft.message,
264 + "fx's default backend, Vercel AI Gateway, is signed out. Pick a model on another backend or sign in.",
265 + );
266 + // The gateway lists its public catalog signed out, but none of it can run.
267 + assert.deepEqual(
268 + draft.models.map((model) => model.slug),
269 + ["default", "codex:gpt-5.5"],
270 + );
271 +
272 + // An earlier backend whose sign-in fx could not report does not hide Codex's.
273 + const unreadable = yield* check({
274 + " status --json": status("missing", { model: "spacexai/grok-4.7" }),
275 + "gateway models --json": GATEWAY_MODELS,
276 + "codex status --json": status("chatgpt subscription"),
277 + "codex models --json": models(["gpt-5.5"]),
278 + "grok status --json": status("missing"),
279 + });
280 + assert.deepEqual(unreadable.draft.auth, {
281 + status: "authenticated",
282 + label: "Codex subscription",
283 + });
284 + }),
285 + );
286 +
287 + it.effect("reports a machine signed out of every backend", () =>
288 + Effect.gen(function* () {
289 + const signedOut = yield* check({
290 + " status --json": status("missing", { model: "spacexai/grok-4.7" }),
291 + "gateway status --json": status("missing"),
292 + "codex status --json": status("missing"),
293 + "codex models --json": SIGNED_OUT_LISTING,
294 + "grok status --json": status("missing"),
295 + });
296 assert.equal(signedOut.draft.status, "error");
297 assert.equal(signedOut.draft.auth.status, "unauthenticated");
298 assert.equal(signedOut.draft.message, "Run `fx login` to sign in.");
299
300 // An fx whose status output T3 cannot read is not reported as signed out.
154 - const unknown = yield* check("not json");
301 + const unknown = yield* check({ " status --json": "not json" });
302 assert.equal(unknown.draft.status, "warning");
303 assert.equal(unknown.draft.auth.status, "unknown");
304 }),
@@ -160,16 +307,58 @@ describe("checkFxProviderStatus", () => {
307 // A ready snapshot drops models it does not list, so only a real listing may report ready.
308 it.effect("treats `fx models` output without ids as a failed listing", () =>
309 Effect.gen(function* () {
163 - const signedIn = JSON.stringify({ kind: "status", auth: "fx login" });
164 - const reshaped = yield* check(signedIn, 0, JSON.stringify({ kind: "models", models: [] }));
310 + const outputs = (gatewayModels: string) => ({
311 + " status --json": status("fx login"),
312 + "gateway status --json": status("fx login"),
313 + "gateway models --json": gatewayModels,
314 + "codex status --json": status("chatgpt subscription"),
315 + "codex models --json": models(["gpt-5.5"]),
316 + "grok status --json": status("missing"),
317 + });
318 + const reshaped = yield* check(outputs(JSON.stringify({ kind: "models", models: [] })));
319 assert.equal(reshaped.draft.status, "warning");
320 assert.deepEqual(
321 reshaped.draft.models.map((model) => model.slug),
168 - ["default"],
322 + ["default", "codex:gpt-5.5"],
323 );
324 + // The server keeps the gateway's earlier models, which can still run.
325 + assert.deepEqual(reshaped.draft.subProviders?.[0], { name: "Vercel AI Gateway" });
326
171 - const empty = yield* check(signedIn, 0, JSON.stringify({ kind: "models", ids: [] }));
327 + const empty = yield* check(outputs(models([])));
328 assert.equal(empty.draft.status, "ready");
329 }),
330 );
331 });
332 +
333 +describe("readFxDefaultChoice", () => {
334 + // fx's settings can give one folder its own model, which `fx status` reports only there.
335 + it.effect("reads fx's default backend and model in the thread's folder", () =>
336 + Effect.forEach(
337 + [
338 + [{ model: "openai/gpt-5.5" }, { backend: "gateway", model: "openai/gpt-5.5" }],
339 + [
340 + { model: "gpt-5.5", model_source: "Codex subscription" },
341 + { backend: "codex", model: "gpt-5.5" },
342 + ],
343 + [
344 + { model: "llama3", model_source: "local" },
345 + { backend: "local", model: "llama3" },
346 + ],
347 + ] as const,
348 + ([fields, expected]) =>
349 + Effect.gen(function* () {
350 + const cwds: Array<string | undefined> = [];
351 + const spawner = ChildProcessSpawner.make((command) => {
352 + if (ChildProcess.isStandardCommand(command)) cwds.push(command.options.cwd);
353 + return Effect.succeed(processHandle(status("fx login", fields), 0));
354 + });
355 + const choice = yield* readFxDefaultChoice(settings, {}, "/work/project").pipe(
356 + Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
357 + );
358 + assert.deepEqual(choice, expected);
359 + assert.deepEqual(cwds, ["/work/project"]);
360 + }),
361 + { discard: true },
362 + ).pipe(Effect.provide(NodeServices.layer)),
363 + );
364 +});
packages/provider-fx/src/server/status.ts
+211 -39
@@ -1,11 +1,16 @@
1 -import { resolveProviderProbeTimeoutMs } from "@t3tools/provider-core/server/snapshotProbe";
2 -import { type ServerProviderAuth, type ServerProviderModel } from "@t3tools/contracts";
1 +import {
2 + type ServerProviderAuth,
3 + type ServerProviderModel,
4 + type ServerProviderSubProvider,
5 +} from "@t3tools/contracts";
6 import type { FxSettings } from "../settings.ts";
7 import { createModelCapabilities } from "@t3tools/shared/model";
8 import { resolveSpawnCommand } from "@t3tools/shared/shell";
9 import * as DateTime from "effect/DateTime";
10 import * as Effect from "effect/Effect";
11 +import * as FileSystem from "effect/FileSystem";
12 import * as Option from "effect/Option";
13 +import * as Path from "effect/Path";
14 import * as Result from "effect/Result";
15 import * as Schema from "effect/Schema";
16 import * as ChildProcess from "effect/process/ChildProcess";
@@ -16,6 +21,8 @@ import {
21 FX_DRIVER_KIND,
22 FX_EFFORT_OPTION_DESCRIPTOR,
23 FX_SUPPORTED_RUNTIME_MODES,
24 + type FxModelChoice,
25 + fxModelSlug,
26 } from "./acpSupport.ts";
27 import {
28 AUTH_PROBE_TIMEOUT_MS,
@@ -27,6 +34,7 @@ import {
34 providerModelsFromSettings,
35 type ServerProviderDraft,
36 providerProbeEnvironment,
37 + resolveProviderProbeTimeoutMs,
38 spawnAndCollect,
39 } from "@t3tools/provider-core/server/snapshotProbe";
40
@@ -39,53 +47,84 @@ const FX_MODEL_CAPABILITIES = createModelCapabilities({
47 optionDescriptors: [FX_EFFORT_OPTION_DESCRIPTOR],
48 });
49
50 +/** fx's built-in backends, in the order its `provider` option lists them. */
51 +const FX_BUILT_IN_BACKENDS = [
52 + { id: "gateway", name: "Vercel AI Gateway", login: "fx login" },
53 + { id: "codex", name: "Codex subscription", login: "fx login codex" },
54 + { id: "grok", name: "Grok subscription", login: "fx login grok" },
55 +] as const;
56 +
57 +/** What `fx status` and `fx models` report for one backend. */
58 +interface FxBackendProbe {
59 + readonly id: string;
60 + readonly name: string;
61 + /** Undefined when its status could not be read. */
62 + readonly signedIn: boolean | undefined;
63 + readonly signInHint: string;
64 + readonly models: ReadonlyArray<string> | undefined;
65 +}
66 +
67 /**
43 - * "Default" keeps the model `fx status` reports as its alias, which the
44 - * adapter selects for it. `fx models` lists the rest.
68 + * "Default" runs what fx would start a new session on in the thread's
69 + * folder; the label shows the machine-wide choice. Other models carry their
70 + * backend in the slug, and only signed-in backends list any.
71 */
72 const fxModels = (
73 settings: FxSettings,
74 defaultModel?: string,
49 - listed: ReadonlyArray<string> = [],
75 + backends: ReadonlyArray<FxBackendProbe> = [],
76 ): ReadonlyArray<ServerProviderModel> =>
77 providerModelsFromSettings(
78 [
79 {
80 slug: FX_DEFAULT_MODEL_SLUG,
81 name: defaultModel === undefined ? "Default" : `Default (${defaultModel})`,
56 - ...(defaultModel === undefined ? {} : { aliases: [defaultModel] }),
82 isCustom: false,
83 isDefault: true,
84 capabilities: FX_MODEL_CAPABILITIES,
85 },
61 - ...listed
62 - .filter((id, index) => id !== FX_DEFAULT_MODEL_SLUG && listed.indexOf(id) === index)
63 - .map((id) => ({
64 - slug: id,
65 - name: id,
66 - isCustom: false,
67 - capabilities: FX_MODEL_CAPABILITIES,
68 - })),
86 + ...backends.flatMap((backend) =>
87 + backend.signedIn === false
88 + ? []
89 + : [...new Set(backend.models)].map((id) => ({
90 + slug: fxModelSlug(backend.id, id),
91 + name: id,
92 + subProvider: backend.name,
93 + isCustom: false,
94 + capabilities: FX_MODEL_CAPABILITIES,
95 + })),
96 + ),
97 ],
98 settings.customModels,
99 FX_MODEL_CAPABILITIES,
100 );
101
102 +const fxSubProviders = (
103 + backends: ReadonlyArray<FxBackendProbe>,
104 +): ReadonlyArray<ServerProviderSubProvider> =>
105 + // A failed listing does not stop a backend's models from running; the server keeps its earlier ones.
106 + backends.map((backend) =>
107 + backend.signedIn === false
108 + ? { name: backend.name, unavailableReason: backend.signInHint }
109 + : { name: backend.name },
110 + );
111 +
112 const snapshot = (
113 settings: FxSettings,
114 checkedAt: string,
115 probe: ProviderProbeResult,
116 defaultModel?: string,
79 - listed?: ReadonlyArray<string>,
117 + backends: ReadonlyArray<FxBackendProbe> = [],
118 ): ServerProviderDraft => ({
119 ...buildServerProvider({
120 driver: FX_DRIVER_KIND,
121 presentation: FX_PRESENTATION,
122 enabled: settings.enabled,
123 checkedAt,
86 - models: fxModels(settings, defaultModel, listed),
124 + models: fxModels(settings, defaultModel, backends),
125 probe,
126 }),
127 + ...(backends.length > 0 ? { subProviders: fxSubProviders(backends) } : {}),
128 // Every `fx acp` session is saved under `~/.fx` and ACP has no session
129 // delete, so a throwaway text-generation session would stay on disk.
130 supportsTextGeneration: false,
@@ -110,6 +149,7 @@ const runFxCli = Effect.fn("runFxCli")(function* (
149 settings: FxSettings,
150 args: ReadonlyArray<string>,
151 environment: NodeJS.ProcessEnv,
152 + cwd?: string,
153 ) {
154 const command = settings.binaryPath || "fx";
155 const spawnCommand = yield* resolveSpawnCommand(command, args, { env: environment });
@@ -118,20 +158,47 @@ const runFxCli = Effect.fn("runFxCli")(function* (
158 ChildProcess.make(spawnCommand.command, spawnCommand.args, {
159 env: environment,
160 shell: spawnCommand.shell,
161 + ...(cwd === undefined ? {} : { cwd }),
162 }),
163 );
164 });
165
125 -/** `fx status --json` on fx 0.0.13: `auth` names the login method, `missing` when signed out. */
166 +/** Stdout of a successful run, or none. Never opens an ACP session, which fx would save. */
167 +const readFxCli = (
168 + settings: FxSettings,
169 + args: ReadonlyArray<string>,
170 + environment: NodeJS.ProcessEnv,
171 + cwd?: string,
172 +) =>
173 + runFxCli(settings, args, environment, cwd).pipe(
174 + Effect.timeoutOption(resolveProviderProbeTimeoutMs("fx", AUTH_PROBE_TIMEOUT_MS, environment)),
175 + Effect.map(Option.filter((output) => output.code === 0)),
176 + Effect.map(Option.map((output) => output.stdout)),
177 + Effect.orElseSucceed(() => Option.none<string>()),
178 + );
179 +
180 +/**
181 + * `fx status --json`: `auth` names the login method, `missing` when signed
182 + * out. `model_source` names a backend other than the gateway, by its label
183 + * for a built-in one and its id for a configured endpoint.
184 + */
185 const FxStatus = Schema.fromJsonString(
186 Schema.Struct({
187 auth: Schema.optional(Schema.String),
188 + auth_help: Schema.optional(Schema.String),
189 model: Schema.optional(Schema.String),
190 + model_source: Schema.optional(Schema.String),
191 }),
192 );
193 const decodeFxStatus = Schema.decodeUnknownOption(FxStatus);
194 const FxModels = Schema.fromJsonString(Schema.Struct({ ids: Schema.Array(Schema.String) }));
195 const decodeFxModels = Schema.decodeUnknownOption(FxModels);
196 +const FxProfileSettings = Schema.fromJsonString(
197 + Schema.Struct({
198 + providers: Schema.optional(Schema.Record(Schema.String, Schema.Unknown)),
199 + }),
200 +);
201 +const decodeFxProfileSettings = Schema.decodeUnknownOption(FxProfileSettings);
202
203 const VERSION_PROBE_TIMEOUT_MS = 10_000;
204
@@ -143,14 +210,81 @@ function fxAuth(auth: string | undefined): ServerProviderAuth {
210 : { status: "authenticated", label: method };
211 }
212
213 +const fxStatusBackend = (modelSource: string | undefined) => {
214 + const source = nonEmptyTrimmed(modelSource);
215 + if (source === undefined) return "gateway";
216 + return FX_BUILT_IN_BACKENDS.find((backend) => backend.name === source)?.id ?? source;
217 +};
218 +
219 +/** Configured endpoints are the `providers` keys of fx's profile settings. */
220 +const readFxConfiguredBackends = Effect.fn("readFxConfiguredBackends")(function* (
221 + environment: NodeJS.ProcessEnv,
222 +) {
223 + const path = yield* Path.Path;
224 + // fx keeps its profile in `FX_HOME` when set, else `~/.fx`.
225 + const fxHome = environment.FX_HOME?.trim();
226 + const home = environment.HOME?.trim();
227 + const profile = fxHome ? fxHome : home ? path.join(home, ".fx") : undefined;
228 + if (profile === undefined) return [];
229 + const fileSystem = yield* FileSystem.FileSystem;
230 + const text = yield* fileSystem
231 + .readFileString(path.join(profile, "settings.json"))
232 + .pipe(Effect.orElseSucceed(() => ""));
233 + const providers = Option.getOrUndefined(decodeFxProfileSettings(text))?.providers ?? {};
234 + // fx's own id rule; the built-in names are reserved.
235 + return Object.keys(providers).filter(
236 + (id) =>
237 + /^[A-Za-z][\w-]*$/.test(id) &&
238 + !FX_BUILT_IN_BACKENDS.some((backend) => backend.id === id.toLowerCase()),
239 + );
240 +});
241 +
242 /**
147 - * Reads `fx --version`, `fx status --json` (login and default model) and
148 - * `fx models --json`. None of them opens an ACP session, which fx would save.
243 + * `FX_PROVIDER` points `fx status` and `fx models` at one backend. fx
244 + * refuses a backend without a saved model unless `FX_MODEL` names one; any
245 + * value does, since neither command uses it.
246 + */
247 +const probeFxBackend = Effect.fn("probeFxBackend")(function* (
248 + settings: FxSettings,
249 + environment: NodeJS.ProcessEnv,
250 + backend: { readonly id: string; readonly name: string; readonly login?: string },
251 +) {
252 + const env = { ...environment, FX_PROVIDER: backend.id, FX_MODEL: FX_DEFAULT_MODEL_SLUG };
253 + const status = Option.getOrUndefined(
254 + Option.flatMap(yield* readFxCli(settings, ["status", "--json"], env), decodeFxStatus),
255 + );
256 + const auth = fxAuth(status?.auth).status;
257 + const signedIn = auth === "unknown" ? undefined : auth === "authenticated";
258 + const models =
259 + signedIn === false
260 + ? undefined
261 + : Option.getOrUndefined(
262 + Option.flatMap(yield* readFxCli(settings, ["models", "--json"], env), decodeFxModels),
263 + )?.ids.flatMap((id) => nonEmptyTrimmed(id) ?? []);
264 + return {
265 + id: backend.id,
266 + name: backend.name,
267 + signedIn,
268 + signInHint:
269 + backend.login === undefined
270 + ? (nonEmptyTrimmed(status?.auth_help) ?? "fx has no credential for this backend.")
271 + : `Signed out. Run \`${backend.login}\` on this machine.`,
272 + models,
273 + } satisfies FxBackendProbe;
274 +});
275 +
276 +/**
277 + * Reads `fx --version`, `fx status --json` (the default backend and model)
278 + * and, for every backend, its sign-in and `fx models --json`.
279 */
280 export const checkFxProviderStatus = Effect.fn("checkFxProviderStatus")(function* (
281 settings: FxSettings,
282 environment: NodeJS.ProcessEnv,
153 -): Effect.fn.Return<ServerProviderDraft, never, ChildProcessSpawner.ChildProcessSpawner> {
283 +): Effect.fn.Return<
284 + ServerProviderDraft,
285 + never,
286 + ChildProcessSpawner.ChildProcessSpawner | FileSystem.FileSystem | Path.Path
287 +> {
288 const checkedAt = DateTime.formatIso(yield* DateTime.now);
289 if (!settings.enabled) {
290 return snapshot(settings, checkedAt, {
@@ -191,19 +325,34 @@ export const checkFxProviderStatus = Effect.fn("checkFxProviderStatus")(function
325 });
326 }
327
194 - const read = (args: ReadonlyArray<string>) =>
195 - runFxCli(settings, args, probeEnvironment).pipe(
196 - Effect.timeoutOption(resolveProviderProbeTimeoutMs("fx", AUTH_PROBE_TIMEOUT_MS, environment)),
197 - Effect.map(Option.filter((output) => output.code === 0)),
198 - Effect.map(Option.map((output) => output.stdout)),
199 - Effect.orElseSucceed(() => Option.none<string>()),
200 - );
201 - const status = Option.flatMap(yield* read(["status", "--json"]), decodeFxStatus);
202 - const listed = Option.flatMap(yield* read(["models", "--json"]), decodeFxModels).pipe(
203 - Option.map((models) => models.ids.flatMap((id) => nonEmptyTrimmed(id) ?? [])),
328 + const status = Option.getOrUndefined(
329 + Option.flatMap(
330 + yield* readFxCli(settings, ["status", "--json"], probeEnvironment),
331 + decodeFxStatus,
332 + ),
333 );
205 - const auth = fxAuth(Option.getOrUndefined(status)?.auth);
206 - const defaultModel = nonEmptyTrimmed(Option.getOrUndefined(status)?.model);
334 + const backends = yield* Effect.forEach(
335 + [
336 + ...FX_BUILT_IN_BACKENDS,
337 + ...(yield* readFxConfiguredBackends(environment)).map((id) => ({ id, name: id })),
338 + ],
339 + (backend) => probeFxBackend(settings, probeEnvironment, backend),
340 + { concurrency: "unbounded" },
341 + );
342 + const defaultModel = nonEmptyTrimmed(status?.model);
343 + const defaultBackend = backends.find(
344 + (backend) => backend.id === fxStatusBackend(status?.model_source),
345 + );
346 + // A ready snapshot replaces the model list, so every backend that may be signed in must have listed.
347 + const listed = backends.filter((backend) => backend.signedIn !== false);
348 + const listingFailed =
349 + listed.length === 0 || listed.some((backend) => backend.models === undefined);
350 + const signedIn = backends.find((backend) => backend.signedIn === true);
351 + const statusAuth = fxAuth(status?.auth);
352 + const auth: ServerProviderAuth =
353 + statusAuth.status !== "authenticated" && signedIn !== undefined
354 + ? { status: "authenticated", label: signedIn.name }
355 + : statusAuth;
356 const probe =
357 auth.status === "unauthenticated"
358 ? { status: "error" as const, message: "Run `fx login` to sign in." }
@@ -212,21 +361,44 @@ export const checkFxProviderStatus = Effect.fn("checkFxProviderStatus")(function
361 status: "warning" as const,
362 message: "fx is installed but login state could not be determined.",
363 }
215 - : Option.isNone(listed)
364 + : defaultBackend?.signedIn === false
365 ? {
366 status: "warning" as const,
218 - message: "fx is installed but model listing failed. Model options may be incomplete.",
367 + message: `fx's default backend, ${defaultBackend.name}, is signed out. Pick a model on another backend or sign in.`,
368 }
220 - : { status: "ready" as const };
369 + : listingFailed
370 + ? {
371 + status: "warning" as const,
372 + message:
373 + "fx is installed but model listing failed. Model options may be incomplete.",
374 + }
375 + : { status: "ready" as const };
376 return snapshot(
377 settings,
378 checkedAt,
379 { installed: true, version, auth, ...probe },
380 defaultModel,
226 - Option.getOrUndefined(listed),
381 + backends,
382 );
383 });
384
230 -/** fx's id for "Default": the alias the snapshot gave it, if `fx status` reported one. */
231 -export const fxDefaultModelId = (models: ReadonlyArray<ServerProviderModel>) =>
232 - models.find((model) => model.slug === FX_DEFAULT_MODEL_SLUG)?.aliases?.[0];
385 +/**
386 + * The backend and model fx starts a new session on in `cwd`, which can come
387 + * from fx's settings for that folder. Undefined when `fx status` fails.
388 + */
389 +export const readFxDefaultChoice = Effect.fn("readFxDefaultChoice")(function* (
390 + settings: FxSettings,
391 + environment: NodeJS.ProcessEnv,
392 + cwd: string,
393 +): Effect.fn.Return<FxModelChoice | undefined, never, ChildProcessSpawner.ChildProcessSpawner> {
394 + const status = Option.getOrUndefined(
395 + Option.flatMap(
396 + yield* readFxCli(settings, ["status", "--json"], environment, cwd),
397 + decodeFxStatus,
398 + ),
399 + );
400 + const model = nonEmptyTrimmed(status?.model);
401 + return model === undefined
402 + ? undefined
403 + : { backend: fxStatusBackend(status?.model_source), model };
404 +});