Compare commits

...
Author SHA1 Message Date
Cursor Agent 42afbb2266 Fix child process env: spread process.env before CODEX_HOME override
The env option on ChildProcess.make was set to only { CODEX_HOME: ... }
without spreading process.env, causing the spawned codex exec process to
lose all inherited environment variables (PATH, HOME, API keys, etc.).
This matches the pattern used in codexAppServerManager.ts and
CodexProvider.ts.
2026-03-26 05:06:53 +00:00
Julius Marminge 775794743d Move provider settings authority into server settings
- Read Codex and Claude CLI paths from server settings
- Add provider registry and status layers for health checks
- Update session startup, model selection, and UI status handling
2026-03-25 21:51:52 -07:00
Julius Marminge b4b3b2b636 rm reexport 2026-03-25 17:35:49 -07:00
Julius Marminge 53726680e4 pass configured binary to health checks 2026-03-25 17:33:55 -07:00
Julius Marminge 19f09b7ba1 nit 2026-03-25 17:22:43 -07:00
Julius Marminge 57ff0ae390 fix 2026-03-25 17:19:04 -07:00
Julius Marminge 6715932142 mv 2026-03-25 17:14:24 -07:00
Julius Marminge 619fd2027c mv to contract package 2026-03-25 17:12:19 -07:00
Julius Marminge c91bd26b3c fix 2026-03-25 17:00:57 -07:00
Julius Marminge da0eaff9d4 rm reexport shi 2026-03-25 16:17:49 -07:00
Julius Marminge 7b0c9c0a9d kewl 2026-03-25 16:14:09 -07:00
Julius Marminge 007bd1755e kewl 2026-03-25 16:13:39 -07:00
Julius Marminge 5b750e2b1e use more lenient json parse 2026-03-25 16:05:30 -07:00
Julius Marminge d6099ed9ca fix expectation 2026-03-25 14:45:57 -07:00
Julius Marminge 69e5d81ef6 Default server settings providers object
- Allow server settings to decode without an explicit providers block
2026-03-25 14:39:36 -07:00
Julius Marminge 14e81b90b6 lint 2026-03-25 14:36:04 -07:00
Julius Marminge f942c3484a mv 2026-03-25 14:33:20 -07:00
Julius Marminge a9aa5c4a05 Centralize provider settings under server authority
- Move Codex and Claude config lookups to `settings.providers`
- Update web composer, settings UI, and model selection to use unified settings
- Refresh browser fixtures and contracts for the new settings shape
2026-03-25 14:26:17 -07:00
Julius Marminge 3f089a454b layers 2026-03-25 13:57:01 -07:00
Julius Marminge b225dfaf77 Make server settings authoritative
- Move settings persistence and validation into the server
- Route runtime/provider behavior through server settings
- Update client and tests to use the unified settings flow
2026-03-25 13:26:48 -07:00
96 changed files with 5085 additions and 3138 deletions

No files matched your search

@@ -38,6 +38,7 @@ import { ProjectionPendingApprovalRepository } from "../src/persistence/Services
import { ProviderUnsupportedError } from "../src/provider/Errors.ts";
import { ProviderAdapterRegistry } from "../src/provider/Services/ProviderAdapterRegistry.ts";
import { ProviderSessionDirectoryLive } from "../src/provider/Layers/ProviderSessionDirectory.ts";
import { ServerSettingsService } from "../src/serverSettings.ts";
import { makeProviderServiceLive } from "../src/provider/Layers/ProviderService.ts";
import { makeCodexAdapterLive } from "../src/provider/Layers/CodexAdapter.ts";
import { CodexAdapter } from "../src/provider/Services/CodexAdapter.ts";
@@ -295,8 +296,10 @@ export const makeOrchestrationIntegrationHarness = (
providerLayer,
RuntimeReceiptBusLive,
);
const serverSettingsLayer = ServerSettingsService.layerTest();
const runtimeIngestionLayer = ProviderRuntimeIngestionLive.pipe(
Layer.provideMerge(runtimeServicesLayer),
Layer.provideMerge(serverSettingsLayer),
);
const gitCoreLayer = Layer.succeed(GitCore, {
renameBranch: (input: Parameters<GitCoreShape["renameBranch"]>[0]) =>
@@ -309,6 +312,7 @@ export const makeOrchestrationIntegrationHarness = (
Layer.provideMerge(runtimeServicesLayer),
Layer.provideMerge(gitCoreLayer),
Layer.provideMerge(textGenerationLayer),
Layer.provideMerge(serverSettingsLayer),
);
const checkpointReactorLayer = CheckpointReactorLive.pipe(
Layer.provideMerge(runtimeServicesLayer),
@@ -320,6 +324,7 @@ export const makeOrchestrationIntegrationHarness = (
);
const layer = orchestrationReactorLayer.pipe(
Layer.provide(persistenceLayer),
Layer.provideMerge(ServerSettingsService.layerTest()),
Layer.provideMerge(ServerConfig.layerTest(workspaceDir, rootDir)),
Layer.provideMerge(NodeServices.layer),
);
@@ -1,5 +1,6 @@
import type { ProviderRuntimeEvent } from "@t3tools/contracts";
import { ThreadId } from "@t3tools/contracts";
import { DEFAULT_SERVER_SETTINGS } from "@t3tools/contracts/settings";
import * as NodeServices from "@effect/platform-node/NodeServices";
import { it, assert } from "@effect/vitest";
import { Effect, FileSystem, Layer, Path, Queue, Stream } from "effect";
@@ -12,6 +13,7 @@ import {
ProviderService,
type ProviderServiceShape,
} from "../src/provider/Services/ProviderService.ts";
import { ServerSettingsService } from "../src/serverSettings.ts";
import { AnalyticsService } from "../src/telemetry/Services/AnalyticsService.ts";
import { SqlitePersistenceMemory } from "../src/persistence/Layers/Sqlite.ts";
import { ProviderSessionRuntimeRepositoryLive } from "../src/persistence/Layers/ProviderSessionRuntime.ts";
@@ -60,6 +62,7 @@ const makeIntegrationFixture = Effect.gen(function* () {
const shared = Layer.mergeAll(
directoryLayer,
Layer.succeed(ProviderAdapterRegistry, registry),
ServerSettingsService.layerTest(DEFAULT_SERVER_SETTINGS),
AnalyticsService.layerTest,
).pipe(Layer.provide(SqlitePersistenceMemory));
+6 -12
View File
@@ -342,6 +342,7 @@ describe("startSession", () => {
manager.startSession({
threadId: asThreadId("thread-1"),
provider: "codex",
binaryPath: "codex",
runtimeMode: "full-access",
}),
).rejects.toThrow("cwd missing");
@@ -390,6 +391,7 @@ describe("startSession", () => {
manager.startSession({
threadId: asThreadId("thread-1"),
provider: "codex",
binaryPath: "codex",
runtimeMode: "full-access",
}),
).rejects.toThrow(
@@ -948,12 +950,8 @@ describe.skipIf(!process.env.CODEX_BINARY_PATH)("startSession live Codex resume"
provider: "codex",
cwd: workspaceDir,
runtimeMode: "full-access",
providerOptions: {
codex: {
...(process.env.CODEX_BINARY_PATH ? { binaryPath: process.env.CODEX_BINARY_PATH } : {}),
...(process.env.CODEX_HOME_PATH ? { homePath: process.env.CODEX_HOME_PATH } : {}),
},
},
binaryPath: process.env.CODEX_BINARY_PATH!,
...(process.env.CODEX_HOME_PATH ? { homePath: process.env.CODEX_HOME_PATH } : {}),
});
const firstTurn = await manager.sendTurn({
@@ -983,12 +981,8 @@ describe.skipIf(!process.env.CODEX_BINARY_PATH)("startSession live Codex resume"
cwd: workspaceDir,
runtimeMode: "approval-required",
resumeCursor: firstSession.resumeCursor,
providerOptions: {
codex: {
...(process.env.CODEX_BINARY_PATH ? { binaryPath: process.env.CODEX_BINARY_PATH } : {}),
...(process.env.CODEX_HOME_PATH ? { homePath: process.env.CODEX_HOME_PATH } : {}),
},
},
binaryPath: process.env.CODEX_BINARY_PATH!,
...(process.env.CODEX_HOME_PATH ? { homePath: process.env.CODEX_HOME_PATH } : {}),
});
expect(resumedSession.threadId).toBe(originalThreadId);
+9 -20
View File
@@ -14,7 +14,6 @@ import {
type ProviderApprovalDecision,
type ProviderEvent,
type ProviderSession,
type ProviderSessionStartInput,
type ProviderTurnStartResult,
RuntimeMode,
ProviderInteractionMode,
@@ -131,7 +130,8 @@ export interface CodexAppServerStartSessionInput {
readonly model?: string;
readonly serviceTier?: string;
readonly resumeCursor?: unknown;
readonly providerOptions?: ProviderSessionStartInput["providerOptions"];
readonly binaryPath: string;
readonly homePath?: string;
readonly runtimeMode: RuntimeMode;
}
@@ -541,9 +541,8 @@ export class CodexAppServerManager extends EventEmitter<CodexAppServerManagerEve
updatedAt: now,
};
const codexOptions = readCodexProviderOptions(input);
const codexBinaryPath = codexOptions.binaryPath ?? "codex";
const codexHomePath = codexOptions.homePath;
const codexBinaryPath = input.binaryPath;
const codexHomePath = input.homePath;
this.assertSupportedCodexCliVersion({
binaryPath: codexBinaryPath,
cwd: resolvedCwd,
@@ -1591,20 +1590,6 @@ function normalizeProviderThreadId(value: string | undefined): string | undefine
return brandIfNonEmpty(value, (normalized) => normalized);
}
function readCodexProviderOptions(input: CodexAppServerStartSessionInput): {
readonly binaryPath?: string;
readonly homePath?: string;
} {
const options = input.providerOptions?.codex;
if (!options) {
return {};
}
return {
...(options.binaryPath ? { binaryPath: options.binaryPath } : {}),
...(options.homePath ? { homePath: options.homePath } : {}),
};
}
function assertSupportedCodexCliVersion(input: {
readonly binaryPath: string;
readonly cwd: string;
@@ -1658,7 +1643,11 @@ function readResumeCursorThreadId(resumeCursor: unknown): string | undefined {
return typeof rawThreadId === "string" ? normalizeProviderThreadId(rawThreadId) : undefined;
}
function readResumeThreadId(input: CodexAppServerStartSessionInput): string | undefined {
function readResumeThreadId(input: {
readonly resumeCursor?: unknown;
readonly threadId?: ThreadId;
readonly runtimeMode?: RuntimeMode;
}): string | undefined {
return readResumeCursorThreadId(input.resumeCursor);
}
+2
View File
@@ -19,6 +19,7 @@ export interface ServerDerivedPaths {
readonly stateDir: string;
readonly dbPath: string;
readonly keybindingsConfigPath: string;
readonly settingsPath: string;
readonly worktreesDir: string;
readonly attachmentsDir: string;
readonly logsDir: string;
@@ -60,6 +61,7 @@ export const deriveServerPaths = Effect.fn(function* (
stateDir,
dbPath,
keybindingsConfigPath: join(stateDir, "keybindings.json"),
settingsPath: join(stateDir, "settings.json"),
worktreesDir: join(baseDir, "worktrees"),
attachmentsDir,
logsDir,
@@ -6,8 +6,10 @@ import { expect } from "vitest";
import { ServerConfig } from "../../config.ts";
import { TextGeneration } from "../Services/TextGeneration.ts";
import { ClaudeTextGenerationLive } from "./ClaudeTextGeneration.ts";
import { ServerSettingsService } from "../../serverSettings.ts";
const ClaudeTextGenerationTestLayer = ClaudeTextGenerationLive.pipe(
Layer.provideMerge(ServerSettingsService.layerTest()),
Layer.provideMerge(
ServerConfig.layerTest(process.cwd(), {
prefix: "t3code-claude-text-generation-test-",
@@ -11,7 +11,6 @@ import { Effect, Layer, Option, Schema, Stream } from "effect";
import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process";
import { ClaudeModelSelection } from "@t3tools/contracts";
import { normalizeClaudeModelOptions } from "@t3tools/shared/model";
import { sanitizeBranchFragment, sanitizeFeatureBranchName } from "@t3tools/shared/git";
import { TextGenerationError } from "../Errors.ts";
@@ -27,6 +26,8 @@ import {
sanitizePrTitle,
toJsonSchemaObject,
} from "../Utils.ts";
import { normalizeClaudeModelOptions } from "../../provider/Layers/ClaudeProvider.ts";
import { ServerSettingsService } from "../../serverSettings.ts";
const CLAUDE_TIMEOUT_MS = 180_000;
@@ -40,6 +41,7 @@ const ClaudeOutputEnvelope = Schema.Struct({
const makeClaudeTextGeneration = Effect.gen(function* () {
const commandSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
const serverSettingsService = yield* Effect.service(ServerSettingsService);
const readStreamAsString = <E>(
operation: string,
@@ -86,9 +88,14 @@ const makeClaudeTextGeneration = Effect.gen(function* () {
...(normalizedOptions?.fastMode ? { fastMode: true } : {}),
};
const claudeSettings = yield* Effect.map(
serverSettingsService.getSettings,
(settings) => settings.providers.claudeAgent,
).pipe(Effect.catch(() => Effect.undefined));
const runClaudeCommand = Effect.gen(function* () {
const command = ChildProcess.make(
"claude",
claudeSettings?.binaryPath || "claude",
[
"-p",
"--output-format",
@@ -7,6 +7,7 @@ import { ServerConfig } from "../../config.ts";
import { CodexTextGenerationLive } from "./CodexTextGeneration.ts";
import { TextGenerationError } from "../Errors.ts";
import { TextGeneration } from "../Services/TextGeneration.ts";
import { ServerSettingsService } from "../../serverSettings.ts";
const DEFAULT_TEST_MODEL_SELECTION = {
provider: "codex" as const,
@@ -14,6 +15,7 @@ const DEFAULT_TEST_MODEL_SELECTION = {
};
const CodexTextGenerationTestLayer = CodexTextGenerationLive.pipe(
Layer.provideMerge(ServerSettingsService.layerTest()),
Layer.provideMerge(
ServerConfig.layerTest(process.cwd(), {
prefix: "t3code-codex-text-generation-test-",
@@ -4,7 +4,6 @@ import { Effect, FileSystem, Layer, Option, Path, Schema, Scope, Stream } from "
import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process";
import { CodexModelSelection } from "@t3tools/contracts";
import { normalizeCodexModelOptions } from "@t3tools/shared/model";
import { sanitizeBranchFragment, sanitizeFeatureBranchName } from "@t3tools/shared/git";
import { resolveAttachmentPath } from "../../attachmentStore.ts";
@@ -26,6 +25,8 @@ import {
sanitizePrTitle,
toJsonSchemaObject,
} from "../Utils.ts";
import { normalizeCodexModelOptions } from "../../provider/Layers/CodexProvider.ts";
import { ServerSettingsService } from "../../serverSettings.ts";
const CODEX_GIT_TEXT_GENERATION_REASONING_EFFORT = "low";
const CODEX_TIMEOUT_MS = 180_000;
@@ -35,6 +36,7 @@ const makeCodexTextGeneration = Effect.gen(function* () {
const path = yield* Path.Path;
const commandSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
const serverConfig = yield* Effect.service(ServerConfig);
const serverSettingsService = yield* Effect.service(ServerSettingsService);
type MaterializedImageAttachments = {
readonly imagePaths: ReadonlyArray<string>;
@@ -138,6 +140,11 @@ const makeCodexTextGeneration = Effect.gen(function* () {
);
const outputPath = yield* writeTempFile(operation, "codex-output", "");
const codexSettings = yield* Effect.map(
serverSettingsService.getSettings,
(settings) => settings.providers.codex,
).pipe(Effect.catch(() => Effect.undefined));
const runCodexCommand = Effect.gen(function* () {
const normalizedOptions = normalizeCodexModelOptions(
modelSelection.model,
@@ -146,7 +153,7 @@ const makeCodexTextGeneration = Effect.gen(function* () {
const reasoningEffort =
modelSelection.options?.reasoningEffort ?? CODEX_GIT_TEXT_GENERATION_REASONING_EFFORT;
const command = ChildProcess.make(
"codex",
codexSettings?.binaryPath || "codex",
[
"exec",
"--ephemeral",
@@ -165,6 +172,10 @@ const makeCodexTextGeneration = Effect.gen(function* () {
"-",
],
{
env: {
...process.env,
...(codexSettings?.homePath ? { CODEX_HOME: codexSettings.homePath } : {}),
},
cwd,
shell: process.platform === "win32",
stdin: {
@@ -20,11 +20,7 @@ import { GitCoreLive } from "./GitCore.ts";
import { GitCore } from "../Services/GitCore.ts";
import { makeGitManager } from "./GitManager.ts";
import { ServerConfig } from "../../config.ts";
const DEFAULT_TEST_MODEL_SELECTION = {
provider: "codex",
model: "gpt-5.4-mini",
} as const;
import { ServerSettingsService } from "../../serverSettings.ts";
interface FakeGhScenario {
prListSequence?: string[];
@@ -466,7 +462,6 @@ function runStackedAction(
{
...input,
actionId: input.actionId ?? "test-action-id",
modelSelection: DEFAULT_TEST_MODEL_SELECTION,
},
options,
);
@@ -493,6 +488,8 @@ function makeManager(input?: {
prefix: "t3-git-manager-test-",
});
const serverSettingsLayer = ServerSettingsService.layerTest();
const gitCoreLayer = GitCoreLive.pipe(
Layer.provideMerge(NodeServices.layer),
Layer.provideMerge(ServerConfigLayer),
@@ -502,6 +499,7 @@ function makeManager(input?: {
Layer.succeed(GitHubCli, gitHubCli),
Layer.succeed(TextGeneration, textGeneration),
gitCoreLayer,
serverSettingsLayer,
).pipe(Layer.provideMerge(NodeServices.layer));
return makeGitManager.pipe(
+12 -3
View File
@@ -19,6 +19,7 @@ import {
import { GitCore } from "../Services/GitCore.ts";
import { GitHubCli } from "../Services/GitHubCli.ts";
import { TextGeneration } from "../Services/TextGeneration.ts";
import { ServerSettingsService } from "../../serverSettings.ts";
const COMMIT_TIMEOUT_MS = 10 * 60_000;
const MAX_PROGRESS_TEXT_LENGTH = 500;
@@ -358,6 +359,7 @@ export const makeGitManager = Effect.gen(function* () {
const gitCore = yield* GitCore;
const gitHubCli = yield* GitHubCli;
const textGeneration = yield* TextGeneration;
const serverSettingsService = yield* ServerSettingsService;
const createProgressEmitter = (
input: { cwd: string; action: "commit" | "commit_push" | "commit_push_pr" },
@@ -1173,6 +1175,13 @@ export const makeGitManager = Effect.gen(function* () {
let commitMessageForStep = input.commitMessage;
let preResolvedCommitSuggestion: CommitAndBranchSuggestion | undefined = undefined;
const modelSelection = yield* serverSettingsService.getSettings.pipe(
Effect.map((settings) => settings.textGenerationModelSelection),
Effect.mapError((cause) =>
gitManagerError("runStackedAction", "Failed to get server settings.", cause),
),
);
if (input.featureBranch) {
currentPhase = "branch";
yield* progress.emit({
@@ -1181,7 +1190,7 @@ export const makeGitManager = Effect.gen(function* () {
label: "Preparing feature branch...",
});
const result = yield* runFeatureBranchStep(
input.modelSelection,
modelSelection,
input.cwd,
initialStatus.branch,
input.commitMessage,
@@ -1198,7 +1207,7 @@ export const makeGitManager = Effect.gen(function* () {
currentPhase = "commit";
const commit = yield* runCommitStep(
input.modelSelection,
modelSelection,
input.cwd,
input.action,
currentBranch,
@@ -1237,7 +1246,7 @@ export const makeGitManager = Effect.gen(function* () {
Effect.flatMap(() =>
Effect.gen(function* () {
currentPhase = "pr";
return yield* runPrStep(input.modelSelection, input.cwd, currentBranch);
return yield* runPrStep(modelSelection, input.cwd, currentBranch);
}),
),
)
+22 -11
View File
@@ -23,6 +23,7 @@ import {
Cache,
Cause,
Deferred,
Duration,
Effect,
Exit,
FileSystem,
@@ -42,6 +43,7 @@ import {
} from "effect";
import * as Semaphore from "effect/Semaphore";
import { ServerConfig } from "./config";
import { fromLenientJson } from "@t3tools/shared/schemaJson";
export class KeybindingsConfigError extends Schema.TaggedErrorClass<KeybindingsConfigError>()(
"KeybindingsConfigParseError",
@@ -408,7 +410,7 @@ function encodeWhenAst(node: KeybindingWhenNode): string {
const DEFAULT_RESOLVED_KEYBINDINGS = compileResolvedKeybindingsConfig(DEFAULT_KEYBINDINGS);
const RawKeybindingsEntries = Schema.fromJsonString(Schema.Array(Schema.Unknown));
const RawKeybindingsEntries = fromLenientJson(Schema.Array(Schema.Unknown));
const KeybindingsConfigJson = Schema.fromJsonString(KeybindingsConfig);
const PrettyJsonString = SchemaGetter.parseJson<string>().compose(
SchemaGetter.stringifyJson({ space: 2 }),
@@ -817,16 +819,25 @@ const makeKeybindings = Effect.gen(function* () {
const revalidateAndEmitSafely = revalidateAndEmit.pipe(Effect.ignoreCause({ log: true }));
yield* Stream.runForEach(fs.watch(keybindingsConfigDir), (event) => {
const isTargetConfigEvent =
event.path === keybindingsConfigFile ||
event.path === keybindingsConfigPath ||
path.resolve(keybindingsConfigDir, event.path) === keybindingsConfigPathResolved;
if (!isTargetConfigEvent) {
return Effect.void;
}
return revalidateAndEmitSafely;
}).pipe(Effect.ignoreCause({ log: true }), Effect.forkIn(watcherScope), Effect.asVoid);
// Debounce watch events so the file is fully written before we read it.
// Editors emit multiple events per save (truncate, write, rename) and
// `fs.watch` can fire before the content has been flushed to disk.
const debouncedKeybindingsEvents = fs.watch(keybindingsConfigDir).pipe(
Stream.filter((event) => {
return (
event.path === keybindingsConfigFile ||
event.path === keybindingsConfigPath ||
path.resolve(keybindingsConfigDir, event.path) === keybindingsConfigPathResolved
);
}),
Stream.debounce(Duration.millis(100)),
);
yield* Stream.runForEach(debouncedKeybindingsEvents, () => revalidateAndEmitSafely).pipe(
Effect.ignoreCause({ log: true }),
Effect.forkIn(watcherScope),
Effect.asVoid,
);
});
const start = Effect.gen(function* () {
+10
View File
@@ -6,6 +6,7 @@ import * as ConfigProvider from "effect/ConfigProvider";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Layer from "effect/Layer";
import * as Stream from "effect/Stream";
import * as Command from "effect/unstable/cli/Command";
import { FetchHttpClient } from "effect/unstable/http";
import { beforeEach } from "vitest";
@@ -16,7 +17,9 @@ import { ServerConfig, type ServerConfigShape } from "./config";
import { Open, type OpenShape } from "./open";
import { ProjectionSnapshotQuery } from "./orchestration/Services/ProjectionSnapshotQuery";
import { AnalyticsService } from "./telemetry/Services/AnalyticsService";
import { DEFAULT_SERVER_SETTINGS } from "@t3tools/contracts";
import { Server, type ServerShape } from "./wsServer";
import { ServerSettingsService, type ServerSettingsShape } from "./serverSettings";
const start = vi.fn(() => undefined);
const stop = vi.fn(() => undefined);
@@ -52,6 +55,13 @@ const testLayer = Layer.mergeAll(
openBrowser: (_target: string) => Effect.void,
openInEditor: () => Effect.void,
} satisfies OpenShape),
Layer.succeed(ServerSettingsService, {
start: Effect.void,
ready: Effect.void,
getSettings: Effect.succeed(DEFAULT_SERVER_SETTINGS),
updateSettings: () => Effect.succeed(DEFAULT_SERVER_SETTINGS),
streamChanges: Stream.empty,
} satisfies ServerSettingsShape),
AnalyticsService.layerTest,
FetchHttpClient.layer,
NodeServices.layer,
+12 -5
View File
@@ -22,12 +22,13 @@ import { Open } from "./open";
import * as SqlitePersistence from "./persistence/Layers/Sqlite";
import { makeServerProviderLayer, makeServerRuntimeServicesLayer } from "./serverLayers";
import { ProjectionSnapshotQuery } from "./orchestration/Services/ProjectionSnapshotQuery";
import { ProviderHealthLive } from "./provider/Layers/ProviderHealth";
import { ProviderRegistryLive } from "./provider/Layers/ProviderRegistry";
import { Server } from "./wsServer";
import { ServerLoggerLive } from "./serverLogger";
import { AnalyticsServiceLayerLive } from "./telemetry/Layers/AnalyticsService";
import { AnalyticsService } from "./telemetry/Services/AnalyticsService";
import { readBootstrapEnvelope } from "./bootstrap";
import { ServerSettingsLive } from "./serverSettings";
export class StartupError extends Data.TaggedError("StartupError")<{
readonly message: string;
@@ -293,10 +294,11 @@ const LayerLive = (input: CliInput) =>
Layer.empty.pipe(
Layer.provideMerge(makeServerRuntimeServicesLayer()),
Layer.provideMerge(makeServerProviderLayer()),
Layer.provideMerge(ProviderHealthLive),
Layer.provideMerge(ProviderRegistryLive),
Layer.provideMerge(SqlitePersistence.layerConfig),
Layer.provideMerge(ServerLoggerLive),
Layer.provideMerge(AnalyticsServiceLayerLive),
Layer.provideMerge(ServerSettingsLive),
Layer.provideMerge(ServerConfigLive(input)),
);
@@ -331,12 +333,10 @@ export const recordStartupHeartbeat = Effect.gen(function* () {
});
});
const makeServerProgram = (input: CliInput) =>
const makeServerRuntimeProgram = (input: CliInput) =>
Effect.gen(function* () {
const cliConfig = yield* CliConfig;
const { start, stopSignal } = yield* Server;
const openDeps = yield* Open;
yield* cliConfig.fixPath;
const config = yield* ServerConfig;
@@ -378,6 +378,13 @@ const makeServerProgram = (input: CliInput) =>
return yield* stopSignal;
}).pipe(Effect.provide(LayerLive(input)));
const makeServerProgram = (input: CliInput) =>
Effect.gen(function* () {
const cliConfig = yield* CliConfig;
yield* cliConfig.fixPath;
return yield* makeServerRuntimeProgram(input);
});
/**
* These flags mirrors the environment variables and the config shape.
*/
@@ -34,6 +34,7 @@ import { ProviderCommandReactorLive } from "./ProviderCommandReactor.ts";
import { OrchestrationEngineService } from "../Services/OrchestrationEngine.ts";
import { ProviderCommandReactor } from "../Services/ProviderCommandReactor.ts";
import * as NodeServices from "@effect/platform-node/NodeServices";
import { ServerSettingsService } from "../../serverSettings.ts";
const asProjectId = (value: string): ProjectId => ProjectId.makeUnsafe(value);
const asApprovalRequestId = (value: string): ApprovalRequestId =>
@@ -214,6 +215,7 @@ describe("ProviderCommandReactor", () => {
Layer.provideMerge(
Layer.succeed(TextGeneration, { generateBranchName } as unknown as TextGenerationShape),
),
Layer.provideMerge(ServerSettingsService.layerTest()),
Layer.provideMerge(ServerConfig.layerTest(process.cwd(), baseDir)),
Layer.provideMerge(NodeServices.layer),
);
@@ -1,12 +1,10 @@
import {
type ChatAttachment,
CommandId,
DEFAULT_GIT_TEXT_GENERATION_MODEL_BY_PROVIDER,
EventId,
type ModelSelection,
type OrchestrationEvent,
ProviderKind,
type ProviderStartOptions,
type OrchestrationSession,
ThreadId,
type ProviderSession,
@@ -26,6 +24,7 @@ import {
ProviderCommandReactor,
type ProviderCommandReactorShape,
} from "../Services/ProviderCommandReactor.ts";
import { ServerSettingsService } from "../../serverSettings.ts";
type ProviderIntentEvent = Extract<
OrchestrationEvent,
@@ -138,6 +137,7 @@ const make = Effect.gen(function* () {
const providerService = yield* ProviderService;
const git = yield* GitCore;
const textGeneration = yield* TextGeneration;
const serverSettingsService = yield* ServerSettingsService;
const handledTurnStartKeys = yield* Cache.make<string, true>({
capacity: HANDLED_TURN_START_KEY_MAX,
timeToLive: HANDLED_TURN_START_KEY_TTL,
@@ -151,7 +151,6 @@ const make = Effect.gen(function* () {
),
);
const threadProviderOptions = new Map<string, ProviderStartOptions>();
const threadModelSelections = new Map<string, ModelSelection>();
const appendProviderFailureActivity = (input: {
@@ -210,7 +209,6 @@ const make = Effect.gen(function* () {
createdAt: string,
options?: {
readonly modelSelection?: ModelSelection;
readonly providerOptions?: ProviderStartOptions;
},
) {
const readModel = yield* orchestrationEngine.getReadModel();
@@ -258,9 +256,6 @@ const make = Effect.gen(function* () {
...(preferredProvider ? { provider: preferredProvider } : {}),
...(effectiveCwd ? { cwd: effectiveCwd } : {}),
modelSelection: desiredModelSelection,
...(options?.providerOptions !== undefined
? { providerOptions: options.providerOptions }
: {}),
...(input?.resumeCursor !== undefined ? { resumeCursor: input.resumeCursor } : {}),
runtimeMode: desiredRuntimeMode,
});
@@ -354,7 +349,6 @@ const make = Effect.gen(function* () {
readonly messageText: string;
readonly attachments?: ReadonlyArray<ChatAttachment>;
readonly modelSelection?: ModelSelection;
readonly providerOptions?: ProviderStartOptions;
readonly interactionMode?: "default" | "plan";
readonly createdAt: string;
}) {
@@ -362,13 +356,11 @@ const make = Effect.gen(function* () {
if (!thread) {
return;
}
yield* ensureSessionForThread(input.threadId, input.createdAt, {
...(input.modelSelection !== undefined ? { modelSelection: input.modelSelection } : {}),
...(input.providerOptions !== undefined ? { providerOptions: input.providerOptions } : {}),
});
if (input.providerOptions !== undefined) {
threadProviderOptions.set(input.threadId, input.providerOptions);
}
yield* ensureSessionForThread(
input.threadId,
input.createdAt,
input.modelSelection !== undefined ? { modelSelection: input.modelSelection } : {},
);
if (input.modelSelection !== undefined) {
threadModelSelections.set(input.threadId, input.modelSelection);
}
@@ -437,10 +429,10 @@ const make = Effect.gen(function* () {
cwd,
message: input.messageText,
...(attachments.length > 0 ? { attachments } : {}),
modelSelection: {
provider: "codex",
model: DEFAULT_GIT_TEXT_GENERATION_MODEL_BY_PROVIDER.codex,
},
modelSelection: yield* Effect.map(
serverSettingsService.getSettings,
(settings) => settings.textGenerationModelSelection,
),
})
.pipe(
Effect.catch((error) =>
@@ -518,9 +510,6 @@ const make = Effect.gen(function* () {
...(event.payload.modelSelection !== undefined
? { modelSelection: event.payload.modelSelection }
: {}),
...(event.payload.providerOptions !== undefined
? { providerOptions: event.payload.providerOptions }
: {}),
interactionMode: event.payload.interactionMode,
createdAt: event.payload.createdAt,
}).pipe(
@@ -686,14 +675,12 @@ const make = Effect.gen(function* () {
if (!thread?.session || thread.session.status === "stopped") {
return;
}
const cachedProviderOptions = threadProviderOptions.get(event.payload.threadId);
const cachedModelSelection = threadModelSelections.get(event.payload.threadId);
yield* ensureSessionForThread(event.payload.threadId, event.occurredAt, {
...(cachedProviderOptions !== undefined
? { providerOptions: cachedProviderOptions }
: {}),
...(cachedModelSelection !== undefined ? { modelSelection: cachedModelSelection } : {}),
});
yield* ensureSessionForThread(
event.payload.threadId,
event.occurredAt,
cachedModelSelection !== undefined ? { modelSelection: cachedModelSelection } : {},
);
return;
}
case "thread.turn-start-requested":
@@ -12,10 +12,12 @@ import {
ApprovalRequestId,
CommandId,
DEFAULT_PROVIDER_INTERACTION_MODE,
DEFAULT_SERVER_SETTINGS,
EventId,
MessageId,
ProjectId,
ProviderItemId,
type ServerSettings,
ThreadId,
TurnId,
} from "@t3tools/contracts";
@@ -38,8 +40,20 @@ import {
} from "../Services/OrchestrationEngine.ts";
import { ProviderRuntimeIngestionService } from "../Services/ProviderRuntimeIngestion.ts";
import { ServerConfig } from "../../config.ts";
import { ServerSettingsService, type ServerSettingsShape } from "../../serverSettings.ts";
import * as NodeServices from "@effect/platform-node/NodeServices";
function makeTestServerSettingsLayer(overrides: Partial<ServerSettings> = {}) {
const settings: ServerSettings = { ...DEFAULT_SERVER_SETTINGS, ...overrides };
return Layer.succeed(ServerSettingsService, {
start: Effect.void,
ready: Effect.void,
getSettings: Effect.succeed(settings),
updateSettings: () => Effect.succeed(settings),
streamChanges: Stream.empty,
} satisfies ServerSettingsShape);
}
const asProjectId = (value: string): ProjectId => ProjectId.makeUnsafe(value);
const asItemId = (value: string): ProviderItemId => ProviderItemId.makeUnsafe(value);
const asEventId = (value: string): EventId => EventId.makeUnsafe(value);
@@ -155,7 +169,7 @@ describe("ProviderRuntimeIngestion", () => {
}
});
async function createHarness() {
async function createHarness(options?: { serverSettings?: Partial<ServerSettings> }) {
const workspaceRoot = makeTempDir("t3-provider-project-");
fs.mkdirSync(path.join(workspaceRoot, ".git"));
const provider = createProviderServiceHarness();
@@ -169,6 +183,7 @@ describe("ProviderRuntimeIngestion", () => {
Layer.provideMerge(orchestrationLayer),
Layer.provideMerge(SqlitePersistenceMemory),
Layer.provideMerge(Layer.succeed(ProviderService, provider.service)),
Layer.provideMerge(makeTestServerSettingsLayer(options?.serverSettings)),
Layer.provideMerge(ServerConfig.layerTest(process.cwd(), process.cwd())),
Layer.provideMerge(NodeServices.layer),
);
@@ -1357,7 +1372,7 @@ describe("ProviderRuntimeIngestion", () => {
});
it("streams assistant deltas when thread.turn.start requests streaming mode", async () => {
const harness = await createHarness();
const harness = await createHarness({ serverSettings: { enableAssistantStreaming: true } });
const now = new Date().toISOString();
await Effect.runPromise(
@@ -1371,7 +1386,6 @@ describe("ProviderRuntimeIngestion", () => {
text: "stream please",
attachments: [],
},
assistantDeliveryMode: "streaming",
interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
runtimeMode: "approval-required",
createdAt: now,
@@ -13,7 +13,7 @@ import {
type OrchestrationThreadActivity,
type ProviderRuntimeEvent,
} from "@t3tools/contracts";
import { Cache, Cause, Duration, Effect, Layer, Option, Ref, Stream } from "effect";
import { Cache, Cause, Duration, Effect, Layer, Option, Stream } from "effect";
import { makeDrainableWorker } from "@t3tools/shared/DrainableWorker";
import { ProviderService } from "../../provider/Services/ProviderService.ts";
@@ -26,12 +26,12 @@ import {
ProviderRuntimeIngestionService,
type ProviderRuntimeIngestionShape,
} from "../Services/ProviderRuntimeIngestion.ts";
import { ServerSettingsService } from "../../serverSettings.ts";
const providerTurnKey = (threadId: ThreadId, turnId: TurnId) => `${threadId}:${turnId}`;
const providerCommandId = (event: ProviderRuntimeEvent, tag: string): CommandId =>
CommandId.makeUnsafe(`provider:${event.eventId}:${tag}:${crypto.randomUUID()}`);
const DEFAULT_ASSISTANT_DELIVERY_MODE: AssistantDeliveryMode = "buffered";
const TURN_MESSAGE_IDS_BY_TURN_CACHE_CAPACITY = 10_000;
const TURN_MESSAGE_IDS_BY_TURN_TTL = Duration.minutes(120);
const BUFFERED_MESSAGE_TEXT_BY_MESSAGE_ID_CACHE_CAPACITY = 20_000;
@@ -537,10 +537,7 @@ const make = Effect.gen(function* () {
const orchestrationEngine = yield* OrchestrationEngineService;
const providerService = yield* ProviderService;
const projectionTurnRepository = yield* ProjectionTurnRepository;
const assistantDeliveryModeRef = yield* Ref.make<AssistantDeliveryMode>(
DEFAULT_ASSISTANT_DELIVERY_MODE,
);
const serverSettingsService = yield* ServerSettingsService;
const turnMessageIdsByTurnKey = yield* Cache.make<string, Set<MessageId>>({
capacity: TURN_MESSAGE_IDS_BY_TURN_CACHE_CAPACITY,
@@ -1048,7 +1045,10 @@ const make = Effect.gen(function* () {
yield* rememberAssistantMessageId(thread.id, turnId, assistantMessageId);
}
const assistantDeliveryMode = yield* Ref.get(assistantDeliveryModeRef);
const assistantDeliveryMode: AssistantDeliveryMode = yield* Effect.map(
serverSettingsService.getSettings,
(settings) => (settings.enableAssistantStreaming ? "streaming" : "buffered"),
);
if (assistantDeliveryMode === "buffered") {
const spillChunk = yield* appendBufferedAssistantText(assistantMessageId, assistantDelta);
if (spillChunk.length > 0) {
@@ -1256,11 +1256,7 @@ const make = Effect.gen(function* () {
).pipe(Effect.asVoid);
});
const processDomainEvent = (event: TurnStartRequestedDomainEvent) =>
Ref.set(
assistantDeliveryModeRef,
event.payload.assistantDeliveryMode ?? DEFAULT_ASSISTANT_DELIVERY_MODE,
);
const processDomainEvent = (_event: TurnStartRequestedDomainEvent) => Effect.void;
const processInput = (input: RuntimeIngestionInput) =>
input.source === "runtime" ? processRuntimeEvent(input.event) : processDomainEvent(input.event);
@@ -188,7 +188,6 @@ describe("decider project scripts", () => {
if (turnStartEvent?.type !== "thread.turn-start-requested") {
return;
}
expect(turnStartEvent.payload.assistantDeliveryMode).toBe("buffered");
expect(turnStartEvent.payload).toMatchObject({
threadId: ThreadId.makeUnsafe("thread-1"),
messageId: asMessageId("message-user-1"),
-5
View File
@@ -14,7 +14,6 @@ import {
} from "./commandInvariants.ts";
const nowIso = () => new Date().toISOString();
const DEFAULT_ASSISTANT_DELIVERY_MODE = "buffered" as const;
const defaultMetadata: Omit<OrchestrationEvent, "sequence" | "type" | "payload"> = {
eventId: crypto.randomUUID() as OrchestrationEvent["eventId"],
@@ -330,10 +329,6 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
...(command.modelSelection !== undefined
? { modelSelection: command.modelSelection }
: {}),
...(command.providerOptions !== undefined
? { providerOptions: command.providerOptions }
: {}),
assistantDeliveryMode: command.assistantDeliveryMode ?? DEFAULT_ASSISTANT_DELIVERY_MODE,
runtimeMode: targetThread.runtimeMode,
interactionMode: targetThread.interactionMode,
...(sourceProposedPlan !== undefined ? { sourceProposedPlan } : {}),
@@ -21,6 +21,7 @@ import { Effect, Fiber, Layer, Random, Stream } from "effect";
import { attachmentRelativePath } from "../../attachmentStore.ts";
import { ServerConfig } from "../../config.ts";
import { ServerSettingsService } from "../../serverSettings.ts";
import { ProviderAdapterValidationError } from "../Errors.ts";
import { ClaudeAdapter } from "../Services/ClaudeAdapter.ts";
import { makeClaudeAdapterLive, type ClaudeAdapterLiveOptions } from "./ClaudeAdapter.ts";
@@ -168,6 +169,7 @@ function makeHarness(config?: {
config?.baseDir ?? "/tmp",
),
),
Layer.provideMerge(ServerSettingsService.layerTest()),
Layer.provideMerge(NodeServices.layer),
),
query,
@@ -301,7 +303,7 @@ describe("ClaudeAdapterLive", () => {
);
});
it.effect("keeps explicit claude permission mode over runtime-derived defaults", () => {
it.effect("uses bypass permissions for full-access claude sessions", () => {
const harness = makeHarness();
return Effect.gen(function* () {
const adapter = yield* ClaudeAdapter;
@@ -309,16 +311,11 @@ describe("ClaudeAdapterLive", () => {
threadId: THREAD_ID,
provider: "claudeAgent",
runtimeMode: "full-access",
providerOptions: {
claudeAgent: {
permissionMode: "plan",
},
},
});
const createInput = harness.getLastCreateQueryInput();
assert.equal(createInput?.options.permissionMode, "plan");
assert.equal(createInput?.options.allowDangerouslySkipPermissions, undefined);
assert.equal(createInput?.options.permissionMode, "bypassPermissions");
assert.equal(createInput?.options.allowDangerouslySkipPermissions, true);
}).pipe(
Effect.provideService(Random.Random, makeDeterministicRandomService()),
Effect.provide(harness.layer),
@@ -1195,6 +1192,7 @@ describe("ClaudeAdapterLive", () => {
},
}).pipe(
Layer.provideMerge(ServerConfig.layerTest("/tmp/claude-adapter-test", "/tmp")),
Layer.provideMerge(ServerSettingsService.layerTest()),
Layer.provideMerge(NodeServices.layer),
);
@@ -40,12 +40,7 @@ import {
type UserInputQuestion,
ClaudeCodeEffort,
} from "@t3tools/contracts";
import {
hasEffortLevel,
applyClaudePromptEffortPrefix,
getModelCapabilities,
trimOrNull,
} from "@t3tools/shared/model";
import { hasEffortLevel, applyClaudePromptEffortPrefix, trimOrNull } from "@t3tools/shared/model";
import {
Cause,
DateTime,
@@ -63,6 +58,8 @@ import {
import { resolveAttachmentPath } from "../../attachmentStore.ts";
import { ServerConfig } from "../../config.ts";
import { ServerSettingsService } from "../../serverSettings.ts";
import { getClaudeModelCapabilities } from "./ClaudeProvider.ts";
import {
ProviderAdapterProcessError,
ProviderAdapterRequestError,
@@ -353,19 +350,6 @@ function asRuntimeRequestId(value: ApprovalRequestId): RuntimeRequestId {
return RuntimeRequestId.makeUnsafe(value);
}
function toPermissionMode(value: unknown): PermissionMode | undefined {
switch (value) {
case "default":
case "acceptEdits":
case "bypassPermissions":
case "plan":
case "dontAsk":
return value;
default:
return undefined;
}
}
function readClaudeResumeState(resumeCursor: unknown): ClaudeResumeState | undefined {
if (!resumeCursor || typeof resumeCursor !== "object") {
return undefined;
@@ -525,7 +509,7 @@ function buildPromptText(input: ProviderSendTurnInput): string {
const requestedEffort = trimOrNull(rawEffort);
const claudeModel =
input.modelSelection?.provider === "claudeAgent" ? input.modelSelection.model : undefined;
const caps = getModelCapabilities("claudeAgent", claudeModel);
const caps = getClaudeModelCapabilities(claudeModel);
const promptEffort =
requestedEffort === "ultrathink" && caps.reasoningEffortLevels.length > 0
? "ultrathink"
@@ -943,6 +927,7 @@ function makeClaudeAdapter(options?: ClaudeAdapterLiveOptions) {
const sessions = new Map<ThreadId, ClaudeSessionContext>();
const runtimeEventQueue = yield* Queue.unbounded<ProviderRuntimeEvent>();
const serverSettingsService = yield* ServerSettingsService;
const nowIso = Effect.map(DateTime.now, DateTime.formatIso);
const nextEventId = Effect.map(Random.nextUUIDv4, (id) => EventId.makeUnsafe(id));
@@ -2727,11 +2712,23 @@ function makeClaudeAdapter(options?: ClaudeAdapterLiveOptions) {
}),
);
const providerOptions = input.providerOptions?.claudeAgent;
const claudeSettings = yield* serverSettingsService.getSettings.pipe(
Effect.map((settings) => settings.providers.claudeAgent),
Effect.mapError(
(error) =>
new ProviderAdapterProcessError({
provider: PROVIDER,
threadId: input.threadId,
detail: error.message,
cause: error,
}),
),
);
const claudeBinaryPath = claudeSettings.binaryPath;
const modelSelection =
input.modelSelection?.provider === "claudeAgent" ? input.modelSelection : undefined;
const requestedEffort = trimOrNull(modelSelection?.options?.effort ?? null);
const caps = getModelCapabilities("claudeAgent", modelSelection?.model);
const caps = getClaudeModelCapabilities(modelSelection?.model);
const effort =
requestedEffort && hasEffortLevel(caps, requestedEffort) ? requestedEffort : null;
const fastMode = modelSelection?.options?.fastMode === true && caps.supportsFastMode;
@@ -2741,8 +2738,7 @@ function makeClaudeAdapter(options?: ClaudeAdapterLiveOptions) {
: undefined;
const effectiveEffort = getEffectiveClaudeCodeEffort(effort);
const permissionMode =
toPermissionMode(providerOptions?.permissionMode) ??
(input.runtimeMode === "full-access" ? "bypassPermissions" : undefined);
input.runtimeMode === "full-access" ? "bypassPermissions" : undefined;
const settings = {
...(typeof thinking === "boolean" ? { alwaysThinkingEnabled: thinking } : {}),
...(fastMode ? { fastMode: true } : {}),
@@ -2751,16 +2747,13 @@ function makeClaudeAdapter(options?: ClaudeAdapterLiveOptions) {
const queryOptions: ClaudeQueryOptions = {
...(input.cwd ? { cwd: input.cwd } : {}),
...(modelSelection?.model ? { model: modelSelection.model } : {}),
pathToClaudeCodeExecutable: providerOptions?.binaryPath ?? "claude",
pathToClaudeCodeExecutable: claudeBinaryPath,
settingSources: [...CLAUDE_SETTING_SOURCES],
...(effectiveEffort ? { effort: effectiveEffort } : {}),
...(permissionMode ? { permissionMode } : {}),
...(permissionMode === "bypassPermissions"
? { allowDangerouslySkipPermissions: true }
: {}),
...(providerOptions?.maxThinkingTokens !== undefined
? { maxThinkingTokens: providerOptions.maxThinkingTokens }
: {}),
...(Object.keys(settings).length > 0 ? { settings } : {}),
...(existingResumeSessionId ? { resume: existingResumeSessionId } : {}),
...(newSessionId ? { sessionId: newSessionId } : {}),
@@ -2851,9 +2844,6 @@ function makeClaudeAdapter(options?: ClaudeAdapterLiveOptions) {
...(input.cwd ? { cwd: input.cwd } : {}),
...(effectiveEffort ? { effort: effectiveEffort } : {}),
...(permissionMode ? { permissionMode } : {}),
...(providerOptions?.maxThinkingTokens !== undefined
? { maxThinkingTokens: providerOptions.maxThinkingTokens }
: {}),
...(fastMode ? { fastMode: true } : {}),
},
},
@@ -0,0 +1,389 @@
import type {
ClaudeSettings,
ClaudeModelOptions,
ModelCapabilities,
ServerProvider,
ServerProviderModel,
ServerProviderAuthStatus,
ServerProviderState,
} from "@t3tools/contracts";
import { Effect, Equal, Layer, Option, Result, Stream } from "effect";
import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process";
import { getDefaultEffort, hasEffortLevel, trimOrNull } from "@t3tools/shared/model";
import {
buildServerProvider,
collectStreamAsString,
DEFAULT_TIMEOUT_MS,
detailFromResult,
extractAuthBoolean,
isCommandMissingCause,
parseGenericCliVersion,
providerModelsFromSettings,
type CommandResult,
} from "../providerSnapshot";
import { makeManagedServerProvider } from "../makeManagedServerProvider";
import { ClaudeProvider, type ClaudeProviderShape } from "../Services/ClaudeProvider";
import { ServerSettingsService } from "../../serverSettings";
const PROVIDER = "claudeAgent" as const;
const BUILT_IN_MODELS: ReadonlyArray<ServerProviderModel> = [
{
slug: "claude-opus-4-6",
name: "Claude Opus 4.6",
isCustom: false,
capabilities: {
reasoningEffortLevels: [
{ value: "low", label: "Low" },
{ value: "medium", label: "Medium" },
{ value: "high", label: "High", isDefault: true },
{ value: "max", label: "Max" },
{ value: "ultrathink", label: "Ultrathink" },
],
supportsFastMode: true,
supportsThinkingToggle: false,
promptInjectedEffortLevels: ["ultrathink"],
} satisfies ModelCapabilities,
},
{
slug: "claude-sonnet-4-6",
name: "Claude Sonnet 4.6",
isCustom: false,
capabilities: {
reasoningEffortLevels: [
{ value: "low", label: "Low" },
{ value: "medium", label: "Medium" },
{ value: "high", label: "High", isDefault: true },
{ value: "ultrathink", label: "Ultrathink" },
],
supportsFastMode: false,
supportsThinkingToggle: false,
promptInjectedEffortLevels: ["ultrathink"],
} satisfies ModelCapabilities,
},
{
slug: "claude-haiku-4-5",
name: "Claude Haiku 4.5",
isCustom: false,
capabilities: {
reasoningEffortLevels: [],
supportsFastMode: false,
supportsThinkingToggle: true,
promptInjectedEffortLevels: [],
} satisfies ModelCapabilities,
},
];
export function getClaudeModelCapabilities(model: string | null | undefined): ModelCapabilities {
const slug = model?.trim();
return (
BUILT_IN_MODELS.find((candidate) => candidate.slug === slug)?.capabilities ?? {
reasoningEffortLevels: [],
supportsFastMode: false,
supportsThinkingToggle: false,
promptInjectedEffortLevels: [],
}
);
}
export function normalizeClaudeModelOptions(
model: string | null | undefined,
modelOptions: ClaudeModelOptions | null | undefined,
): ClaudeModelOptions | undefined {
const caps = getClaudeModelCapabilities(model);
const defaultReasoningEffort = getDefaultEffort(caps);
const resolvedEffort = trimOrNull(modelOptions?.effort);
const isPromptInjected = caps.promptInjectedEffortLevels.includes(resolvedEffort ?? "");
const effort =
resolvedEffort &&
!isPromptInjected &&
hasEffortLevel(caps, resolvedEffort) &&
resolvedEffort !== defaultReasoningEffort
? resolvedEffort
: undefined;
const thinking =
caps.supportsThinkingToggle && modelOptions?.thinking === false ? false : undefined;
const fastMode = caps.supportsFastMode && modelOptions?.fastMode === true ? true : undefined;
const nextOptions: ClaudeModelOptions = {
...(thinking === false ? { thinking: false } : {}),
...(effort ? { effort } : {}),
...(fastMode ? { fastMode: true } : {}),
};
return Object.keys(nextOptions).length > 0 ? nextOptions : undefined;
}
export function parseClaudeAuthStatusFromOutput(result: CommandResult): {
readonly status: Exclude<ServerProviderState, "disabled">;
readonly authStatus: ServerProviderAuthStatus;
readonly message?: string;
} {
const lowerOutput = `${result.stdout}\n${result.stderr}`.toLowerCase();
if (
lowerOutput.includes("unknown command") ||
lowerOutput.includes("unrecognized command") ||
lowerOutput.includes("unexpected argument")
) {
return {
status: "warning",
authStatus: "unknown",
message:
"Claude Agent authentication status command is unavailable in this version of Claude.",
};
}
if (
lowerOutput.includes("not logged in") ||
lowerOutput.includes("login required") ||
lowerOutput.includes("authentication required") ||
lowerOutput.includes("run `claude login`") ||
lowerOutput.includes("run claude login")
) {
return {
status: "error",
authStatus: "unauthenticated",
message: "Claude is not authenticated. Run `claude auth login` and try again.",
};
}
const parsedAuth = (() => {
const trimmed = result.stdout.trim();
if (!trimmed || (!trimmed.startsWith("{") && !trimmed.startsWith("["))) {
return { attemptedJsonParse: false as const, auth: undefined as boolean | undefined };
}
try {
return {
attemptedJsonParse: true as const,
auth: extractAuthBoolean(JSON.parse(trimmed)),
};
} catch {
return { attemptedJsonParse: false as const, auth: undefined as boolean | undefined };
}
})();
if (parsedAuth.auth === true) {
return { status: "ready", authStatus: "authenticated" };
}
if (parsedAuth.auth === false) {
return {
status: "error",
authStatus: "unauthenticated",
message: "Claude is not authenticated. Run `claude auth login` and try again.",
};
}
if (parsedAuth.attemptedJsonParse) {
return {
status: "warning",
authStatus: "unknown",
message:
"Could not verify Claude authentication status from JSON output (missing auth marker).",
};
}
if (result.code === 0) {
return { status: "ready", authStatus: "authenticated" };
}
const detail = detailFromResult(result);
return {
status: "warning",
authStatus: "unknown",
message: detail
? `Could not verify Claude authentication status. ${detail}`
: "Could not verify Claude authentication status.",
};
}
const runClaudeCommand = (args: ReadonlyArray<string>) =>
Effect.gen(function* () {
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
const settingsService = yield* ServerSettingsService;
const claudeSettings = yield* settingsService.getSettings.pipe(
Effect.map((settings) => settings.providers.claudeAgent),
);
const command = ChildProcess.make(claudeSettings.binaryPath, [...args], {
shell: process.platform === "win32",
});
const child = yield* spawner.spawn(command);
const [stdout, stderr, exitCode] = yield* Effect.all(
[
collectStreamAsString(child.stdout),
collectStreamAsString(child.stderr),
child.exitCode.pipe(Effect.map(Number)),
],
{ concurrency: "unbounded" },
);
return { stdout, stderr, code: exitCode } satisfies CommandResult;
}).pipe(Effect.scoped);
export const checkClaudeProviderStatus: Effect.Effect<
ServerProvider,
never,
ChildProcessSpawner.ChildProcessSpawner | ServerSettingsService
> = Effect.gen(function* () {
const settingsService = yield* ServerSettingsService;
const settings = yield* settingsService.getSettings.pipe(Effect.orDie);
const claudeSettings = settings.providers.claudeAgent;
const checkedAt = new Date().toISOString();
const models = providerModelsFromSettings(BUILT_IN_MODELS, PROVIDER, claudeSettings.customModels);
if (!claudeSettings.enabled) {
return buildServerProvider({
provider: PROVIDER,
enabled: false,
checkedAt,
models,
probe: {
installed: false,
version: null,
status: "warning",
authStatus: "unknown",
message: "Claude is disabled in T3 Code settings.",
},
});
}
const versionProbe = yield* runClaudeCommand(["--version"]).pipe(
Effect.timeoutOption(DEFAULT_TIMEOUT_MS),
Effect.result,
);
if (Result.isFailure(versionProbe)) {
const error = versionProbe.failure;
return buildServerProvider({
provider: PROVIDER,
enabled: claudeSettings.enabled,
checkedAt,
models,
probe: {
installed: !isCommandMissingCause(error),
version: null,
status: "error",
authStatus: "unknown",
message: isCommandMissingCause(error)
? "Claude Agent CLI (`claude`) is not installed or not on PATH."
: `Failed to execute Claude Agent CLI health check: ${error instanceof Error ? error.message : String(error)}.`,
},
});
}
if (Option.isNone(versionProbe.success)) {
return buildServerProvider({
provider: PROVIDER,
enabled: claudeSettings.enabled,
checkedAt,
models,
probe: {
installed: true,
version: null,
status: "error",
authStatus: "unknown",
message:
"Claude Agent CLI is installed but failed to run. Timed out while running command.",
},
});
}
const version = versionProbe.success.value;
const parsedVersion = parseGenericCliVersion(`${version.stdout}\n${version.stderr}`);
if (version.code !== 0) {
const detail = detailFromResult(version);
return buildServerProvider({
provider: PROVIDER,
enabled: claudeSettings.enabled,
checkedAt,
models,
probe: {
installed: true,
version: parsedVersion,
status: "error",
authStatus: "unknown",
message: detail
? `Claude Agent CLI is installed but failed to run. ${detail}`
: "Claude Agent CLI is installed but failed to run.",
},
});
}
const authProbe = yield* runClaudeCommand(["auth", "status"]).pipe(
Effect.timeoutOption(DEFAULT_TIMEOUT_MS),
Effect.result,
);
if (Result.isFailure(authProbe)) {
const error = authProbe.failure;
return buildServerProvider({
provider: PROVIDER,
enabled: claudeSettings.enabled,
checkedAt,
models,
probe: {
installed: true,
version: parsedVersion,
status: "warning",
authStatus: "unknown",
message:
error instanceof Error
? `Could not verify Claude authentication status: ${error.message}.`
: "Could not verify Claude authentication status.",
},
});
}
if (Option.isNone(authProbe.success)) {
return buildServerProvider({
provider: PROVIDER,
enabled: claudeSettings.enabled,
checkedAt,
models,
probe: {
installed: true,
version: parsedVersion,
status: "warning",
authStatus: "unknown",
message: "Could not verify Claude authentication status. Timed out while running command.",
},
});
}
const parsed = parseClaudeAuthStatusFromOutput(authProbe.success.value);
return buildServerProvider({
provider: PROVIDER,
enabled: claudeSettings.enabled,
checkedAt,
models,
probe: {
installed: true,
version: parsedVersion,
status: parsed.status,
authStatus: parsed.authStatus,
...(parsed.message ? { message: parsed.message } : {}),
},
});
});
export const ClaudeProviderLive = Layer.effect(
ClaudeProvider,
Effect.gen(function* () {
const serverSettings = yield* ServerSettingsService;
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
const checkProvider = checkClaudeProviderStatus.pipe(
Effect.provideService(ServerSettingsService, serverSettings),
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
);
return yield* makeManagedServerProvider<ClaudeSettings>({
getSettings: serverSettings.getSettings.pipe(
Effect.map((settings) => settings.providers.claudeAgent),
Effect.orDie,
),
streamSettings: serverSettings.streamChanges.pipe(
Stream.map((settings) => settings.providers.claudeAgent),
),
haveSettingsChanged: (previous, next) => !Equal.equals(previous, next),
checkProvider,
});
}).pipe(Effect.map((service) => service satisfies ClaudeProviderShape)),
);
@@ -22,6 +22,7 @@ import {
type CodexAppServerSendTurnInput,
} from "../../codexAppServerManager.ts";
import { ServerConfig } from "../../config.ts";
import { ServerSettingsService } from "../../serverSettings.ts";
import { ProviderAdapterValidationError } from "../Errors.ts";
import { CodexAdapter } from "../Services/CodexAdapter.ts";
import { ProviderSessionDirectory } from "../Services/ProviderSessionDirectory.ts";
@@ -151,6 +152,7 @@ const validationManager = new FakeCodexManager();
const validationLayer = it.layer(
makeCodexAdapterLive({ manager: validationManager }).pipe(
Layer.provideMerge(ServerConfig.layerTest(process.cwd(), process.cwd())),
Layer.provideMerge(ServerSettingsService.layerTest()),
Layer.provideMerge(providerSessionDirectoryTestLayer),
Layer.provideMerge(NodeServices.layer),
),
@@ -201,6 +203,7 @@ validationLayer("CodexAdapterLive validation", (it) => {
assert.deepStrictEqual(validationManager.startSessionImpl.mock.calls[0]?.[0], {
provider: "codex",
threadId: asThreadId("thread-1"),
binaryPath: "codex",
model: "gpt-5.3-codex",
serviceTier: "fast",
runtimeMode: "full-access",
@@ -216,6 +219,7 @@ sessionErrorManager.sendTurnImpl.mockImplementation(async () => {
const sessionErrorLayer = it.layer(
makeCodexAdapterLive({ manager: sessionErrorManager }).pipe(
Layer.provideMerge(ServerConfig.layerTest(process.cwd(), process.cwd())),
Layer.provideMerge(ServerSettingsService.layerTest()),
Layer.provideMerge(providerSessionDirectoryTestLayer),
Layer.provideMerge(NodeServices.layer),
),
@@ -284,6 +288,7 @@ const lifecycleManager = new FakeCodexManager();
const lifecycleLayer = it.layer(
makeCodexAdapterLive({ manager: lifecycleManager }).pipe(
Layer.provideMerge(ServerConfig.layerTest(process.cwd(), process.cwd())),
Layer.provideMerge(ServerSettingsService.layerTest()),
Layer.provideMerge(providerSessionDirectoryTestLayer),
Layer.provideMerge(NodeServices.layer),
),
+27 -12
View File
@@ -38,6 +38,7 @@ import {
} from "../../codexAppServerManager.ts";
import { resolveAttachmentPath } from "../../attachmentStore.ts";
import { ServerConfig } from "../../config.ts";
import { ServerSettingsService } from "../../serverSettings.ts";
import { type EventNdjsonLogger, makeEventNdjsonLogger } from "./EventNdjsonLogger.ts";
const PROVIDER = "codex" as const;
@@ -1342,25 +1343,39 @@ const makeCodexAdapter = (options?: CodexAdapterLiveOptions) =>
}
}),
);
const serverSettingsService = yield* ServerSettingsService;
const startSession: CodexAdapterShape["startSession"] = (input) => {
const startSession: CodexAdapterShape["startSession"] = Effect.fn(function* (input) {
if (input.provider !== undefined && input.provider !== PROVIDER) {
return Effect.fail(
new ProviderAdapterValidationError({
provider: PROVIDER,
operation: "startSession",
issue: `Expected provider '${PROVIDER}' but received '${input.provider}'.`,
}),
);
return yield* new ProviderAdapterValidationError({
provider: PROVIDER,
operation: "startSession",
issue: `Expected provider '${PROVIDER}' but received '${input.provider}'.`,
});
}
const codexSettings = yield* serverSettingsService.getSettings.pipe(
Effect.map((settings) => settings.providers.codex),
Effect.mapError(
(error) =>
new ProviderAdapterProcessError({
provider: PROVIDER,
threadId: input.threadId,
detail: error.message,
cause: error,
}),
),
);
const binaryPath = codexSettings.binaryPath;
const homePath = codexSettings.homePath;
const managerInput: CodexAppServerStartSessionInput = {
threadId: input.threadId,
provider: "codex",
...(input.cwd !== undefined ? { cwd: input.cwd } : {}),
...(input.resumeCursor !== undefined ? { resumeCursor: input.resumeCursor } : {}),
...(input.providerOptions !== undefined ? { providerOptions: input.providerOptions } : {}),
runtimeMode: input.runtimeMode,
binaryPath,
...(homePath ? { homePath } : {}),
...(input.modelSelection?.provider === "codex"
? { model: input.modelSelection.model }
: {}),
@@ -1369,7 +1384,7 @@ const makeCodexAdapter = (options?: CodexAdapterLiveOptions) =>
: {}),
};
return Effect.tryPromise({
return yield* Effect.tryPromise({
try: () => manager.startSession(managerInput),
catch: (cause) =>
new ProviderAdapterProcessError({
@@ -1378,8 +1393,8 @@ const makeCodexAdapter = (options?: CodexAdapterLiveOptions) =>
detail: toMessage(cause, "Failed to start Codex adapter session."),
cause,
}),
}).pipe(Effect.map((session) => session));
};
});
});
const sendTurn: CodexAdapterShape["sendTurn"] = (input) =>
Effect.gen(function* () {
@@ -0,0 +1,524 @@
import * as OS from "node:os";
import type {
ModelCapabilities,
CodexModelOptions,
CodexSettings,
ServerProvider,
ServerProviderModel,
ServerProviderAuthStatus,
ServerProviderState,
} from "@t3tools/contracts";
import { Effect, Equal, FileSystem, Layer, Option, Path, Result, Stream } from "effect";
import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process";
import { getDefaultEffort, trimOrNull } from "@t3tools/shared/model";
import {
buildServerProvider,
collectStreamAsString,
DEFAULT_TIMEOUT_MS,
detailFromResult,
extractAuthBoolean,
isCommandMissingCause,
parseGenericCliVersion,
providerModelsFromSettings,
type CommandResult,
} from "../providerSnapshot";
import { makeManagedServerProvider } from "../makeManagedServerProvider";
import {
formatCodexCliUpgradeMessage,
isCodexCliVersionSupported,
parseCodexCliVersion,
} from "../codexCliVersion";
import { CodexProvider, type CodexProviderShape } from "../Services/CodexProvider";
import { ServerSettingsService } from "../../serverSettings";
const PROVIDER = "codex" as const;
const OPENAI_AUTH_PROVIDERS = new Set(["openai"]);
const BUILT_IN_MODELS: ReadonlyArray<ServerProviderModel> = [
{
slug: "gpt-5.4",
name: "GPT-5.4",
isCustom: false,
capabilities: {
reasoningEffortLevels: [
{ value: "xhigh", label: "Extra High" },
{ value: "high", label: "High", isDefault: true },
{ value: "medium", label: "Medium" },
{ value: "low", label: "Low" },
],
supportsFastMode: true,
supportsThinkingToggle: false,
promptInjectedEffortLevels: [],
} satisfies ModelCapabilities,
},
{
slug: "gpt-5.4-mini",
name: "GPT-5.4 Mini",
isCustom: false,
capabilities: {
reasoningEffortLevels: [
{ value: "xhigh", label: "Extra High" },
{ value: "high", label: "High", isDefault: true },
{ value: "medium", label: "Medium" },
{ value: "low", label: "Low" },
],
supportsFastMode: true,
supportsThinkingToggle: false,
promptInjectedEffortLevels: [],
} satisfies ModelCapabilities,
},
{
slug: "gpt-5.3-codex",
name: "GPT-5.3 Codex",
isCustom: false,
capabilities: {
reasoningEffortLevels: [
{ value: "xhigh", label: "Extra High" },
{ value: "high", label: "High", isDefault: true },
{ value: "medium", label: "Medium" },
{ value: "low", label: "Low" },
],
supportsFastMode: true,
supportsThinkingToggle: false,
promptInjectedEffortLevels: [],
} satisfies ModelCapabilities,
},
{
slug: "gpt-5.3-codex-spark",
name: "GPT-5.3 Codex Spark",
isCustom: false,
capabilities: {
reasoningEffortLevels: [
{ value: "xhigh", label: "Extra High" },
{ value: "high", label: "High", isDefault: true },
{ value: "medium", label: "Medium" },
{ value: "low", label: "Low" },
],
supportsFastMode: true,
supportsThinkingToggle: false,
promptInjectedEffortLevels: [],
} satisfies ModelCapabilities,
},
{
slug: "gpt-5.2-codex",
name: "GPT-5.2 Codex",
isCustom: false,
capabilities: {
reasoningEffortLevels: [
{ value: "xhigh", label: "Extra High" },
{ value: "high", label: "High", isDefault: true },
{ value: "medium", label: "Medium" },
{ value: "low", label: "Low" },
],
supportsFastMode: true,
supportsThinkingToggle: false,
promptInjectedEffortLevels: [],
} satisfies ModelCapabilities,
},
{
slug: "gpt-5.2",
name: "GPT-5.2",
isCustom: false,
capabilities: {
reasoningEffortLevels: [
{ value: "xhigh", label: "Extra High" },
{ value: "high", label: "High", isDefault: true },
{ value: "medium", label: "Medium" },
{ value: "low", label: "Low" },
],
supportsFastMode: true,
supportsThinkingToggle: false,
promptInjectedEffortLevels: [],
} satisfies ModelCapabilities,
},
];
export function getCodexModelCapabilities(model: string | null | undefined): ModelCapabilities {
const slug = model?.trim();
return (
BUILT_IN_MODELS.find((candidate) => candidate.slug === slug)?.capabilities ?? {
reasoningEffortLevels: [],
supportsFastMode: false,
supportsThinkingToggle: false,
promptInjectedEffortLevels: [],
}
);
}
export function normalizeCodexModelOptions(
model: string | null | undefined,
modelOptions: CodexModelOptions | null | undefined,
): CodexModelOptions | undefined {
const caps = getCodexModelCapabilities(model);
const defaultReasoningEffort = getDefaultEffort(caps);
const reasoningEffort = trimOrNull(modelOptions?.reasoningEffort) ?? defaultReasoningEffort;
const fastModeEnabled = modelOptions?.fastMode === true;
const nextOptions: CodexModelOptions = {
...(reasoningEffort && reasoningEffort !== defaultReasoningEffort
? { reasoningEffort: reasoningEffort as CodexModelOptions["reasoningEffort"] }
: {}),
...(fastModeEnabled ? { fastMode: true } : {}),
};
return Object.keys(nextOptions).length > 0 ? nextOptions : undefined;
}
export function parseAuthStatusFromOutput(result: CommandResult): {
readonly status: Exclude<ServerProviderState, "disabled">;
readonly authStatus: ServerProviderAuthStatus;
readonly message?: string;
} {
const lowerOutput = `${result.stdout}\n${result.stderr}`.toLowerCase();
if (
lowerOutput.includes("unknown command") ||
lowerOutput.includes("unrecognized command") ||
lowerOutput.includes("unexpected argument")
) {
return {
status: "warning",
authStatus: "unknown",
message: "Codex CLI authentication status command is unavailable in this Codex version.",
};
}
if (
lowerOutput.includes("not logged in") ||
lowerOutput.includes("login required") ||
lowerOutput.includes("authentication required") ||
lowerOutput.includes("run `codex login`") ||
lowerOutput.includes("run codex login")
) {
return {
status: "error",
authStatus: "unauthenticated",
message: "Codex CLI is not authenticated. Run `codex login` and try again.",
};
}
const parsedAuth = (() => {
const trimmed = result.stdout.trim();
if (!trimmed || (!trimmed.startsWith("{") && !trimmed.startsWith("["))) {
return { attemptedJsonParse: false as const, auth: undefined as boolean | undefined };
}
try {
return {
attemptedJsonParse: true as const,
auth: extractAuthBoolean(JSON.parse(trimmed)),
};
} catch {
return { attemptedJsonParse: false as const, auth: undefined as boolean | undefined };
}
})();
if (parsedAuth.auth === true) {
return { status: "ready", authStatus: "authenticated" };
}
if (parsedAuth.auth === false) {
return {
status: "error",
authStatus: "unauthenticated",
message: "Codex CLI is not authenticated. Run `codex login` and try again.",
};
}
if (parsedAuth.attemptedJsonParse) {
return {
status: "warning",
authStatus: "unknown",
message:
"Could not verify Codex authentication status from JSON output (missing auth marker).",
};
}
if (result.code === 0) {
return { status: "ready", authStatus: "authenticated" };
}
const detail = detailFromResult(result);
return {
status: "warning",
authStatus: "unknown",
message: detail
? `Could not verify Codex authentication status. ${detail}`
: "Could not verify Codex authentication status.",
};
}
export const readCodexConfigModelProvider = Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const settingsService = yield* ServerSettingsService;
const codexHome = yield* settingsService.getSettings.pipe(
Effect.map(
(settings) =>
settings.providers.codex.homePath ||
process.env.CODEX_HOME ||
path.join(OS.homedir(), ".codex"),
),
);
const configPath = path.join(codexHome, "config.toml");
const content = yield* fileSystem
.readFileString(configPath)
.pipe(Effect.orElseSucceed(() => undefined));
if (content === undefined) {
return undefined;
}
let inTopLevel = true;
for (const line of content.split("\n")) {
const trimmed = line.trim();
if (!trimmed || trimmed.startsWith("#")) continue;
if (trimmed.startsWith("[")) {
inTopLevel = false;
continue;
}
if (!inTopLevel) continue;
const match = trimmed.match(/^model_provider\s*=\s*["']([^"']+)["']/);
if (match) return match[1];
}
return undefined;
});
export const hasCustomModelProvider = readCodexConfigModelProvider.pipe(
Effect.map((provider) => provider !== undefined && !OPENAI_AUTH_PROVIDERS.has(provider)),
Effect.orElseSucceed(() => false),
);
const runCodexCommand = (args: ReadonlyArray<string>) =>
Effect.gen(function* () {
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
const settingsService = yield* ServerSettingsService;
const codexSettings = yield* settingsService.getSettings.pipe(
Effect.map((settings) => settings.providers.codex),
);
const command = ChildProcess.make(codexSettings.binaryPath, [...args], {
shell: process.platform === "win32",
env: {
...process.env,
...(codexSettings.homePath ? { CODEX_HOME: codexSettings.homePath } : {}),
},
});
const child = yield* spawner.spawn(command);
const [stdout, stderr, exitCode] = yield* Effect.all(
[
collectStreamAsString(child.stdout),
collectStreamAsString(child.stderr),
child.exitCode.pipe(Effect.map(Number)),
],
{ concurrency: "unbounded" },
);
return { stdout, stderr, code: exitCode } satisfies CommandResult;
}).pipe(Effect.scoped);
export const checkCodexProviderStatus: Effect.Effect<
ServerProvider,
never,
| ChildProcessSpawner.ChildProcessSpawner
| FileSystem.FileSystem
| Path.Path
| ServerSettingsService
> = Effect.gen(function* () {
const settingsService = yield* ServerSettingsService;
const settings = yield* settingsService.getSettings.pipe(Effect.orDie);
const codexSettings = settings.providers.codex;
const checkedAt = new Date().toISOString();
const models = providerModelsFromSettings(BUILT_IN_MODELS, PROVIDER, codexSettings.customModels);
if (!codexSettings.enabled) {
return buildServerProvider({
provider: PROVIDER,
enabled: false,
checkedAt,
models,
probe: {
installed: false,
version: null,
status: "warning",
authStatus: "unknown",
message: "Codex is disabled in T3 Code settings.",
},
});
}
const versionProbe = yield* runCodexCommand(["--version"]).pipe(
Effect.timeoutOption(DEFAULT_TIMEOUT_MS),
Effect.result,
);
if (Result.isFailure(versionProbe)) {
const error = versionProbe.failure;
return buildServerProvider({
provider: PROVIDER,
enabled: codexSettings.enabled,
checkedAt,
models,
probe: {
installed: !isCommandMissingCause(error),
version: null,
status: "error",
authStatus: "unknown",
message: isCommandMissingCause(error)
? "Codex CLI (`codex`) is not installed or not on PATH."
: `Failed to execute Codex CLI health check: ${error instanceof Error ? error.message : String(error)}.`,
},
});
}
if (Option.isNone(versionProbe.success)) {
return buildServerProvider({
provider: PROVIDER,
enabled: codexSettings.enabled,
checkedAt,
models,
probe: {
installed: true,
version: null,
status: "error",
authStatus: "unknown",
message: "Codex CLI is installed but failed to run. Timed out while running command.",
},
});
}
const version = versionProbe.success.value;
const parsedVersion =
parseCodexCliVersion(`${version.stdout}\n${version.stderr}`) ??
parseGenericCliVersion(`${version.stdout}\n${version.stderr}`);
if (version.code !== 0) {
const detail = detailFromResult(version);
return buildServerProvider({
provider: PROVIDER,
enabled: codexSettings.enabled,
checkedAt,
models,
probe: {
installed: true,
version: parsedVersion,
status: "error",
authStatus: "unknown",
message: detail
? `Codex CLI is installed but failed to run. ${detail}`
: "Codex CLI is installed but failed to run.",
},
});
}
if (parsedVersion && !isCodexCliVersionSupported(parsedVersion)) {
return buildServerProvider({
provider: PROVIDER,
enabled: codexSettings.enabled,
checkedAt,
models,
probe: {
installed: true,
version: parsedVersion,
status: "error",
authStatus: "unknown",
message: formatCodexCliUpgradeMessage(parsedVersion),
},
});
}
if (yield* hasCustomModelProvider) {
return buildServerProvider({
provider: PROVIDER,
enabled: codexSettings.enabled,
checkedAt,
models,
probe: {
installed: true,
version: parsedVersion,
status: "ready",
authStatus: "unknown",
message: "Using a custom Codex model provider; OpenAI login check skipped.",
},
});
}
const authProbe = yield* runCodexCommand(["login", "status"]).pipe(
Effect.timeoutOption(DEFAULT_TIMEOUT_MS),
Effect.result,
);
if (Result.isFailure(authProbe)) {
const error = authProbe.failure;
return buildServerProvider({
provider: PROVIDER,
enabled: codexSettings.enabled,
checkedAt,
models,
probe: {
installed: true,
version: parsedVersion,
status: "warning",
authStatus: "unknown",
message:
error instanceof Error
? `Could not verify Codex authentication status: ${error.message}.`
: "Could not verify Codex authentication status.",
},
});
}
if (Option.isNone(authProbe.success)) {
return buildServerProvider({
provider: PROVIDER,
enabled: codexSettings.enabled,
checkedAt,
models,
probe: {
installed: true,
version: parsedVersion,
status: "warning",
authStatus: "unknown",
message: "Could not verify Codex authentication status. Timed out while running command.",
},
});
}
const parsed = parseAuthStatusFromOutput(authProbe.success.value);
return buildServerProvider({
provider: PROVIDER,
enabled: codexSettings.enabled,
checkedAt,
models,
probe: {
installed: true,
version: parsedVersion,
status: parsed.status,
authStatus: parsed.authStatus,
...(parsed.message ? { message: parsed.message } : {}),
},
});
});
export const CodexProviderLive = Layer.effect(
CodexProvider,
Effect.gen(function* () {
const serverSettings = yield* ServerSettingsService;
const fileSystem = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
const checkProvider = checkCodexProviderStatus.pipe(
Effect.provideService(ServerSettingsService, serverSettings),
Effect.provideService(FileSystem.FileSystem, fileSystem),
Effect.provideService(Path.Path, path),
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
);
return yield* makeManagedServerProvider<CodexSettings>({
getSettings: serverSettings.getSettings.pipe(
Effect.map((settings) => settings.providers.codex),
Effect.orDie,
),
streamSettings: serverSettings.streamChanges.pipe(
Stream.map((settings) => settings.providers.codex),
),
haveSettingsChanged: (previous, next) => !Equal.equals(previous, next),
checkProvider,
});
}).pipe(Effect.map((service) => service satisfies CodexProviderShape)),
);
@@ -1,640 +0,0 @@
import * as NodeServices from "@effect/platform-node/NodeServices";
import { describe, it, assert } from "@effect/vitest";
import { Effect, FileSystem, Layer, Path, Sink, Stream } from "effect";
import * as PlatformError from "effect/PlatformError";
import { ChildProcessSpawner } from "effect/unstable/process";
import {
checkClaudeProviderStatus,
checkCodexProviderStatus,
hasCustomModelProvider,
parseAuthStatusFromOutput,
parseClaudeAuthStatusFromOutput,
readCodexConfigModelProvider,
} from "./ProviderHealth";
// ── Test helpers ────────────────────────────────────────────────────
const encoder = new TextEncoder();
function mockHandle(result: { stdout: string; stderr: string; code: number }) {
return ChildProcessSpawner.makeHandle({
pid: ChildProcessSpawner.ProcessId(1),
exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(result.code)),
isRunning: Effect.succeed(false),
kill: () => Effect.void,
stdin: Sink.drain,
stdout: Stream.make(encoder.encode(result.stdout)),
stderr: Stream.make(encoder.encode(result.stderr)),
all: Stream.empty,
getInputFd: () => Sink.drain,
getOutputFd: () => Stream.empty,
});
}
function mockSpawnerLayer(
handler: (args: ReadonlyArray<string>) => { stdout: string; stderr: string; code: number },
) {
return Layer.succeed(
ChildProcessSpawner.ChildProcessSpawner,
ChildProcessSpawner.make((command) => {
const cmd = command as unknown as { args: ReadonlyArray<string> };
return Effect.succeed(mockHandle(handler(cmd.args)));
}),
);
}
function failingSpawnerLayer(description: string) {
return Layer.succeed(
ChildProcessSpawner.ChildProcessSpawner,
ChildProcessSpawner.make(() =>
Effect.fail(
PlatformError.systemError({
_tag: "NotFound",
module: "ChildProcess",
method: "spawn",
description,
}),
),
),
);
}
/**
* Create a temporary CODEX_HOME scoped to the current Effect test.
* Cleanup is registered in the test scope rather than via Vitest hooks.
*/
function withTempCodexHome(configContent?: string) {
return Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const tmpDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-test-codex-" });
yield* Effect.acquireRelease(
Effect.sync(() => {
const originalCodexHome = process.env.CODEX_HOME;
process.env.CODEX_HOME = tmpDir;
return originalCodexHome;
}),
(originalCodexHome) =>
Effect.sync(() => {
if (originalCodexHome !== undefined) {
process.env.CODEX_HOME = originalCodexHome;
} else {
delete process.env.CODEX_HOME;
}
}),
);
if (configContent !== undefined) {
yield* fileSystem.writeFileString(path.join(tmpDir, "config.toml"), configContent);
}
return { tmpDir } as const;
});
}
it.layer(NodeServices.layer)("ProviderHealth", (it) => {
// ── checkCodexProviderStatus tests ────────────────────────────────
//
// These tests control CODEX_HOME to ensure the custom-provider detection
// in hasCustomModelProvider() does not interfere with the auth-probe
// path being tested.
describe("checkCodexProviderStatus", () => {
it.effect("returns ready when codex is installed and authenticated", () =>
Effect.gen(function* () {
// Point CODEX_HOME at an empty tmp dir (no config.toml) so the
// default code path (OpenAI provider, auth probe runs) is exercised.
yield* withTempCodexHome();
const status = yield* checkCodexProviderStatus;
assert.strictEqual(status.provider, "codex");
assert.strictEqual(status.status, "ready");
assert.strictEqual(status.available, true);
assert.strictEqual(status.authStatus, "authenticated");
}).pipe(
Effect.provide(
mockSpawnerLayer((args) => {
const joined = args.join(" ");
if (joined === "--version") return { stdout: "codex 1.0.0\n", stderr: "", code: 0 };
if (joined === "login status") return { stdout: "Logged in\n", stderr: "", code: 0 };
throw new Error(`Unexpected args: ${joined}`);
}),
),
),
);
it.effect("returns unavailable when codex is missing", () =>
Effect.gen(function* () {
yield* withTempCodexHome();
const status = yield* checkCodexProviderStatus;
assert.strictEqual(status.provider, "codex");
assert.strictEqual(status.status, "error");
assert.strictEqual(status.available, false);
assert.strictEqual(status.authStatus, "unknown");
assert.strictEqual(status.message, "Codex CLI (`codex`) is not installed or not on PATH.");
}).pipe(Effect.provide(failingSpawnerLayer("spawn codex ENOENT"))),
);
it.effect("returns unavailable when codex is below the minimum supported version", () =>
Effect.gen(function* () {
yield* withTempCodexHome();
const status = yield* checkCodexProviderStatus;
assert.strictEqual(status.provider, "codex");
assert.strictEqual(status.status, "error");
assert.strictEqual(status.available, false);
assert.strictEqual(status.authStatus, "unknown");
assert.strictEqual(
status.message,
"Codex CLI v0.36.0 is too old for T3 Code. Upgrade to v0.37.0 or newer and restart T3 Code.",
);
}).pipe(
Effect.provide(
mockSpawnerLayer((args) => {
const joined = args.join(" ");
if (joined === "--version") return { stdout: "codex 0.36.0\n", stderr: "", code: 0 };
throw new Error(`Unexpected args: ${joined}`);
}),
),
),
);
it.effect("returns unauthenticated when auth probe reports login required", () =>
Effect.gen(function* () {
yield* withTempCodexHome();
const status = yield* checkCodexProviderStatus;
assert.strictEqual(status.provider, "codex");
assert.strictEqual(status.status, "error");
assert.strictEqual(status.available, true);
assert.strictEqual(status.authStatus, "unauthenticated");
assert.strictEqual(
status.message,
"Codex CLI is not authenticated. Run `codex login` and try again.",
);
}).pipe(
Effect.provide(
mockSpawnerLayer((args) => {
const joined = args.join(" ");
if (joined === "--version") return { stdout: "codex 1.0.0\n", stderr: "", code: 0 };
if (joined === "login status") {
return { stdout: "", stderr: "Not logged in. Run codex login.", code: 1 };
}
throw new Error(`Unexpected args: ${joined}`);
}),
),
),
);
it.effect("returns unauthenticated when login status output includes 'not logged in'", () =>
Effect.gen(function* () {
yield* withTempCodexHome();
const status = yield* checkCodexProviderStatus;
assert.strictEqual(status.provider, "codex");
assert.strictEqual(status.status, "error");
assert.strictEqual(status.available, true);
assert.strictEqual(status.authStatus, "unauthenticated");
assert.strictEqual(
status.message,
"Codex CLI is not authenticated. Run `codex login` and try again.",
);
}).pipe(
Effect.provide(
mockSpawnerLayer((args) => {
const joined = args.join(" ");
if (joined === "--version") return { stdout: "codex 1.0.0\n", stderr: "", code: 0 };
if (joined === "login status")
return { stdout: "Not logged in\n", stderr: "", code: 1 };
throw new Error(`Unexpected args: ${joined}`);
}),
),
),
);
it.effect("returns warning when login status command is unsupported", () =>
Effect.gen(function* () {
yield* withTempCodexHome();
const status = yield* checkCodexProviderStatus;
assert.strictEqual(status.provider, "codex");
assert.strictEqual(status.status, "warning");
assert.strictEqual(status.available, true);
assert.strictEqual(status.authStatus, "unknown");
assert.strictEqual(
status.message,
"Codex CLI authentication status command is unavailable in this Codex version.",
);
}).pipe(
Effect.provide(
mockSpawnerLayer((args) => {
const joined = args.join(" ");
if (joined === "--version") return { stdout: "codex 1.0.0\n", stderr: "", code: 0 };
if (joined === "login status") {
return { stdout: "", stderr: "error: unknown command 'login'", code: 2 };
}
throw new Error(`Unexpected args: ${joined}`);
}),
),
),
);
});
// ── Custom model provider: checkCodexProviderStatus integration ───
describe("checkCodexProviderStatus with custom model provider", () => {
it.effect("skips auth probe and returns ready when a custom model provider is configured", () =>
Effect.gen(function* () {
yield* withTempCodexHome(
[
'model_provider = "portkey"',
"",
"[model_providers.portkey]",
'base_url = "https://api.portkey.ai/v1"',
'env_key = "PORTKEY_API_KEY"',
].join("\n"),
);
const status = yield* checkCodexProviderStatus;
assert.strictEqual(status.provider, "codex");
assert.strictEqual(status.status, "ready");
assert.strictEqual(status.available, true);
assert.strictEqual(status.authStatus, "unknown");
assert.strictEqual(
status.message,
"Using a custom Codex model provider; OpenAI login check skipped.",
);
}).pipe(
Effect.provide(
// The spawner only handles --version; if the test attempts
// "login status" the throw proves the auth probe was NOT skipped.
mockSpawnerLayer((args) => {
const joined = args.join(" ");
if (joined === "--version") return { stdout: "codex 1.0.0\n", stderr: "", code: 0 };
throw new Error(`Auth probe should have been skipped but got args: ${joined}`);
}),
),
),
);
it.effect("still reports error when codex CLI is missing even with custom provider", () =>
Effect.gen(function* () {
yield* withTempCodexHome(
[
'model_provider = "portkey"',
"",
"[model_providers.portkey]",
'base_url = "https://api.portkey.ai/v1"',
'env_key = "PORTKEY_API_KEY"',
].join("\n"),
);
const status = yield* checkCodexProviderStatus;
assert.strictEqual(status.status, "error");
assert.strictEqual(status.available, false);
}).pipe(Effect.provide(failingSpawnerLayer("spawn codex ENOENT"))),
);
});
describe("checkCodexProviderStatus with openai model provider", () => {
it.effect("still runs auth probe when model_provider is openai", () =>
Effect.gen(function* () {
yield* withTempCodexHome('model_provider = "openai"\n');
const status = yield* checkCodexProviderStatus;
// The auth probe runs and sees "not logged in" → error
assert.strictEqual(status.status, "error");
assert.strictEqual(status.authStatus, "unauthenticated");
}).pipe(
Effect.provide(
mockSpawnerLayer((args) => {
const joined = args.join(" ");
if (joined === "--version") return { stdout: "codex 1.0.0\n", stderr: "", code: 0 };
if (joined === "login status")
return { stdout: "Not logged in\n", stderr: "", code: 1 };
throw new Error(`Unexpected args: ${joined}`);
}),
),
),
);
});
// ── parseAuthStatusFromOutput pure tests ──────────────────────────
describe("parseAuthStatusFromOutput", () => {
it("exit code 0 with no auth markers is ready", () => {
const parsed = parseAuthStatusFromOutput({ stdout: "OK\n", stderr: "", code: 0 });
assert.strictEqual(parsed.status, "ready");
assert.strictEqual(parsed.authStatus, "authenticated");
});
it("JSON with authenticated=false is unauthenticated", () => {
const parsed = parseAuthStatusFromOutput({
stdout: '[{"authenticated":false}]\n',
stderr: "",
code: 0,
});
assert.strictEqual(parsed.status, "error");
assert.strictEqual(parsed.authStatus, "unauthenticated");
});
it("JSON without auth marker is warning", () => {
const parsed = parseAuthStatusFromOutput({
stdout: '[{"ok":true}]\n',
stderr: "",
code: 0,
});
assert.strictEqual(parsed.status, "warning");
assert.strictEqual(parsed.authStatus, "unknown");
});
});
// ── readCodexConfigModelProvider tests ─────────────────────────────
describe("readCodexConfigModelProvider", () => {
it.effect("returns undefined when config file does not exist", () =>
Effect.gen(function* () {
yield* withTempCodexHome();
assert.strictEqual(yield* readCodexConfigModelProvider, undefined);
}),
);
it.effect("returns undefined when config has no model_provider key", () =>
Effect.gen(function* () {
yield* withTempCodexHome('model = "gpt-5-codex"\n');
assert.strictEqual(yield* readCodexConfigModelProvider, undefined);
}),
);
it.effect("returns the provider when model_provider is set at top level", () =>
Effect.gen(function* () {
yield* withTempCodexHome('model = "gpt-5-codex"\nmodel_provider = "portkey"\n');
assert.strictEqual(yield* readCodexConfigModelProvider, "portkey");
}),
);
it.effect("returns openai when model_provider is openai", () =>
Effect.gen(function* () {
yield* withTempCodexHome('model_provider = "openai"\n');
assert.strictEqual(yield* readCodexConfigModelProvider, "openai");
}),
);
it.effect("ignores model_provider inside section headers", () =>
Effect.gen(function* () {
yield* withTempCodexHome(
[
'model = "gpt-5-codex"',
"",
"[model_providers.portkey]",
'base_url = "https://api.portkey.ai/v1"',
'model_provider = "should-be-ignored"',
"",
].join("\n"),
);
assert.strictEqual(yield* readCodexConfigModelProvider, undefined);
}),
);
it.effect("handles comments and whitespace", () =>
Effect.gen(function* () {
yield* withTempCodexHome(
[
"# This is a comment",
"",
' model_provider = "azure" ',
"",
"[profiles.deep-review]",
'model = "gpt-5-pro"',
].join("\n"),
);
assert.strictEqual(yield* readCodexConfigModelProvider, "azure");
}),
);
it.effect("handles single-quoted values in TOML", () =>
Effect.gen(function* () {
yield* withTempCodexHome("model_provider = 'mistral'\n");
assert.strictEqual(yield* readCodexConfigModelProvider, "mistral");
}),
);
});
// ── hasCustomModelProvider tests ───────────────────────────────────
describe("hasCustomModelProvider", () => {
it.effect("returns false when no config file exists", () =>
Effect.gen(function* () {
yield* withTempCodexHome();
assert.strictEqual(yield* hasCustomModelProvider, false);
}),
);
it.effect("returns false when model_provider is not set", () =>
Effect.gen(function* () {
yield* withTempCodexHome('model = "gpt-5-codex"\n');
assert.strictEqual(yield* hasCustomModelProvider, false);
}),
);
it.effect("returns false when model_provider is openai", () =>
Effect.gen(function* () {
yield* withTempCodexHome('model_provider = "openai"\n');
assert.strictEqual(yield* hasCustomModelProvider, false);
}),
);
it.effect("returns true when model_provider is portkey", () =>
Effect.gen(function* () {
yield* withTempCodexHome('model_provider = "portkey"\n');
assert.strictEqual(yield* hasCustomModelProvider, true);
}),
);
it.effect("returns true when model_provider is azure", () =>
Effect.gen(function* () {
yield* withTempCodexHome('model_provider = "azure"\n');
assert.strictEqual(yield* hasCustomModelProvider, true);
}),
);
it.effect("returns true when model_provider is ollama", () =>
Effect.gen(function* () {
yield* withTempCodexHome('model_provider = "ollama"\n');
assert.strictEqual(yield* hasCustomModelProvider, true);
}),
);
it.effect("returns true when model_provider is a custom proxy", () =>
Effect.gen(function* () {
yield* withTempCodexHome('model_provider = "my-company-proxy"\n');
assert.strictEqual(yield* hasCustomModelProvider, true);
}),
);
});
// ── checkClaudeProviderStatus tests ──────────────────────────
describe("checkClaudeProviderStatus", () => {
it.effect("returns ready when claude is installed and authenticated", () =>
Effect.gen(function* () {
const status = yield* checkClaudeProviderStatus;
assert.strictEqual(status.provider, "claudeAgent");
assert.strictEqual(status.status, "ready");
assert.strictEqual(status.available, true);
assert.strictEqual(status.authStatus, "authenticated");
}).pipe(
Effect.provide(
mockSpawnerLayer((args) => {
const joined = args.join(" ");
if (joined === "--version") return { stdout: "1.0.0\n", stderr: "", code: 0 };
if (joined === "auth status")
return {
stdout: '{"loggedIn":true,"authMethod":"claude.ai"}\n',
stderr: "",
code: 0,
};
throw new Error(`Unexpected args: ${joined}`);
}),
),
),
);
it.effect("returns unavailable when claude is missing", () =>
Effect.gen(function* () {
const status = yield* checkClaudeProviderStatus;
assert.strictEqual(status.provider, "claudeAgent");
assert.strictEqual(status.status, "error");
assert.strictEqual(status.available, false);
assert.strictEqual(status.authStatus, "unknown");
assert.strictEqual(
status.message,
"Claude Agent CLI (`claude`) is not installed or not on PATH.",
);
}).pipe(Effect.provide(failingSpawnerLayer("spawn claude ENOENT"))),
);
it.effect("returns error when version check fails with non-zero exit code", () =>
Effect.gen(function* () {
const status = yield* checkClaudeProviderStatus;
assert.strictEqual(status.provider, "claudeAgent");
assert.strictEqual(status.status, "error");
assert.strictEqual(status.available, false);
}).pipe(
Effect.provide(
mockSpawnerLayer((args) => {
const joined = args.join(" ");
if (joined === "--version")
return { stdout: "", stderr: "Something went wrong", code: 1 };
throw new Error(`Unexpected args: ${joined}`);
}),
),
),
);
it.effect("returns unauthenticated when auth status reports not logged in", () =>
Effect.gen(function* () {
const status = yield* checkClaudeProviderStatus;
assert.strictEqual(status.provider, "claudeAgent");
assert.strictEqual(status.status, "error");
assert.strictEqual(status.available, true);
assert.strictEqual(status.authStatus, "unauthenticated");
assert.strictEqual(
status.message,
"Claude is not authenticated. Run `claude auth login` and try again.",
);
}).pipe(
Effect.provide(
mockSpawnerLayer((args) => {
const joined = args.join(" ");
if (joined === "--version") return { stdout: "1.0.0\n", stderr: "", code: 0 };
if (joined === "auth status")
return {
stdout: '{"loggedIn":false}\n',
stderr: "",
code: 1,
};
throw new Error(`Unexpected args: ${joined}`);
}),
),
),
);
it.effect("returns unauthenticated when output includes 'not logged in'", () =>
Effect.gen(function* () {
const status = yield* checkClaudeProviderStatus;
assert.strictEqual(status.provider, "claudeAgent");
assert.strictEqual(status.status, "error");
assert.strictEqual(status.available, true);
assert.strictEqual(status.authStatus, "unauthenticated");
}).pipe(
Effect.provide(
mockSpawnerLayer((args) => {
const joined = args.join(" ");
if (joined === "--version") return { stdout: "1.0.0\n", stderr: "", code: 0 };
if (joined === "auth status") return { stdout: "Not logged in\n", stderr: "", code: 1 };
throw new Error(`Unexpected args: ${joined}`);
}),
),
),
);
it.effect("returns warning when auth status command is unsupported", () =>
Effect.gen(function* () {
const status = yield* checkClaudeProviderStatus;
assert.strictEqual(status.provider, "claudeAgent");
assert.strictEqual(status.status, "warning");
assert.strictEqual(status.available, true);
assert.strictEqual(status.authStatus, "unknown");
assert.strictEqual(
status.message,
"Claude Agent authentication status command is unavailable in this version of Claude.",
);
}).pipe(
Effect.provide(
mockSpawnerLayer((args) => {
const joined = args.join(" ");
if (joined === "--version") return { stdout: "1.0.0\n", stderr: "", code: 0 };
if (joined === "auth status")
return { stdout: "", stderr: "error: unknown command 'auth'", code: 2 };
throw new Error(`Unexpected args: ${joined}`);
}),
),
),
);
});
// ── parseClaudeAuthStatusFromOutput pure tests ────────────────────
describe("parseClaudeAuthStatusFromOutput", () => {
it("exit code 0 with no auth markers is ready", () => {
const parsed = parseClaudeAuthStatusFromOutput({ stdout: "OK\n", stderr: "", code: 0 });
assert.strictEqual(parsed.status, "ready");
assert.strictEqual(parsed.authStatus, "authenticated");
});
it("JSON with loggedIn=true is authenticated", () => {
const parsed = parseClaudeAuthStatusFromOutput({
stdout: '{"loggedIn":true,"authMethod":"claude.ai"}\n',
stderr: "",
code: 0,
});
assert.strictEqual(parsed.status, "ready");
assert.strictEqual(parsed.authStatus, "authenticated");
});
it("JSON with loggedIn=false is unauthenticated", () => {
const parsed = parseClaudeAuthStatusFromOutput({
stdout: '{"loggedIn":false}\n',
stderr: "",
code: 0,
});
assert.strictEqual(parsed.status, "error");
assert.strictEqual(parsed.authStatus, "unauthenticated");
});
it("JSON without auth marker is warning", () => {
const parsed = parseClaudeAuthStatusFromOutput({
stdout: '{"ok":true}\n',
stderr: "",
code: 0,
});
assert.strictEqual(parsed.status, "warning");
assert.strictEqual(parsed.authStatus, "unknown");
});
});
});
@@ -1,603 +0,0 @@
/**
* ProviderHealthLive - Startup-time provider health checks.
*
* Performs one-time provider readiness probes when the server starts and
* keeps the resulting snapshot in memory for `server.getConfig`.
*
* Uses effect's ChildProcessSpawner to run CLI probes natively.
*
* @module ProviderHealthLive
*/
import * as OS from "node:os";
import type {
ServerProviderAuthStatus,
ServerProviderStatus,
ServerProviderStatusState,
} from "@t3tools/contracts";
import { Array, Effect, Fiber, FileSystem, Layer, Option, Path, Result, Stream } from "effect";
import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process";
import {
formatCodexCliUpgradeMessage,
isCodexCliVersionSupported,
parseCodexCliVersion,
} from "../codexCliVersion";
import { ProviderHealth, type ProviderHealthShape } from "../Services/ProviderHealth";
const DEFAULT_TIMEOUT_MS = 4_000;
const CODEX_PROVIDER = "codex" as const;
const CLAUDE_AGENT_PROVIDER = "claudeAgent" as const;
// ── Pure helpers ────────────────────────────────────────────────────
export interface CommandResult {
readonly stdout: string;
readonly stderr: string;
readonly code: number;
}
function nonEmptyTrimmed(value: string | undefined): string | undefined {
if (!value) return undefined;
const trimmed = value.trim();
return trimmed.length > 0 ? trimmed : undefined;
}
function isCommandMissingCause(error: unknown): boolean {
if (!(error instanceof Error)) return false;
const lower = error.message.toLowerCase();
return lower.includes("enoent") || lower.includes("notfound");
}
function detailFromResult(
result: CommandResult & { readonly timedOut?: boolean },
): string | undefined {
if (result.timedOut) return "Timed out while running command.";
const stderr = nonEmptyTrimmed(result.stderr);
if (stderr) return stderr;
const stdout = nonEmptyTrimmed(result.stdout);
if (stdout) return stdout;
if (result.code !== 0) {
return `Command exited with code ${result.code}.`;
}
return undefined;
}
function extractAuthBoolean(value: unknown): boolean | undefined {
if (Array.isArray(value)) {
for (const entry of value) {
const nested = extractAuthBoolean(entry);
if (nested !== undefined) return nested;
}
return undefined;
}
if (!value || typeof value !== "object") return undefined;
const record = value as Record<string, unknown>;
for (const key of ["authenticated", "isAuthenticated", "loggedIn", "isLoggedIn"] as const) {
if (typeof record[key] === "boolean") return record[key];
}
for (const key of ["auth", "status", "session", "account"] as const) {
const nested = extractAuthBoolean(record[key]);
if (nested !== undefined) return nested;
}
return undefined;
}
export function parseAuthStatusFromOutput(result: CommandResult): {
readonly status: ServerProviderStatusState;
readonly authStatus: ServerProviderAuthStatus;
readonly message?: string;
} {
const lowerOutput = `${result.stdout}\n${result.stderr}`.toLowerCase();
if (
lowerOutput.includes("unknown command") ||
lowerOutput.includes("unrecognized command") ||
lowerOutput.includes("unexpected argument")
) {
return {
status: "warning",
authStatus: "unknown",
message: "Codex CLI authentication status command is unavailable in this Codex version.",
};
}
if (
lowerOutput.includes("not logged in") ||
lowerOutput.includes("login required") ||
lowerOutput.includes("authentication required") ||
lowerOutput.includes("run `codex login`") ||
lowerOutput.includes("run codex login")
) {
return {
status: "error",
authStatus: "unauthenticated",
message: "Codex CLI is not authenticated. Run `codex login` and try again.",
};
}
const parsedAuth = (() => {
const trimmed = result.stdout.trim();
if (!trimmed || (!trimmed.startsWith("{") && !trimmed.startsWith("["))) {
return { attemptedJsonParse: false as const, auth: undefined as boolean | undefined };
}
try {
return {
attemptedJsonParse: true as const,
auth: extractAuthBoolean(JSON.parse(trimmed)),
};
} catch {
return { attemptedJsonParse: false as const, auth: undefined as boolean | undefined };
}
})();
if (parsedAuth.auth === true) {
return { status: "ready", authStatus: "authenticated" };
}
if (parsedAuth.auth === false) {
return {
status: "error",
authStatus: "unauthenticated",
message: "Codex CLI is not authenticated. Run `codex login` and try again.",
};
}
if (parsedAuth.attemptedJsonParse) {
return {
status: "warning",
authStatus: "unknown",
message:
"Could not verify Codex authentication status from JSON output (missing auth marker).",
};
}
if (result.code === 0) {
return { status: "ready", authStatus: "authenticated" };
}
const detail = detailFromResult(result);
return {
status: "warning",
authStatus: "unknown",
message: detail
? `Could not verify Codex authentication status. ${detail}`
: "Could not verify Codex authentication status.",
};
}
// ── Codex CLI config detection ──────────────────────────────────────
/**
* Providers that use OpenAI-native authentication via `codex login`.
* When the configured `model_provider` is one of these, the `codex login
* status` probe still runs. For any other provider value the auth probe
* is skipped because authentication is handled externally (e.g. via
* environment variables like `PORTKEY_API_KEY` or `AZURE_API_KEY`).
*/
const OPENAI_AUTH_PROVIDERS = new Set(["openai"]);
/**
* Read the `model_provider` value from the Codex CLI config file.
*
* Looks for the file at `$CODEX_HOME/config.toml` (falls back to
* `~/.codex/config.toml`). Uses a simple line-by-line scan rather than
* a full TOML parser to avoid adding a dependency for a single key.
*
* Returns `undefined` when the file does not exist or does not set
* `model_provider`.
*/
export const readCodexConfigModelProvider = Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const codexHome = process.env.CODEX_HOME || path.join(OS.homedir(), ".codex");
const configPath = path.join(codexHome, "config.toml");
const content = yield* fileSystem
.readFileString(configPath)
.pipe(Effect.orElseSucceed(() => undefined));
if (content === undefined) {
return undefined;
}
// We need to find `model_provider = "..."` at the top level of the
// TOML file (i.e. before any `[section]` header). Lines inside
// `[profiles.*]`, `[model_providers.*]`, etc. are ignored.
let inTopLevel = true;
for (const line of content.split("\n")) {
const trimmed = line.trim();
// Skip comments and empty lines.
if (!trimmed || trimmed.startsWith("#")) continue;
// Detect section headers — once we leave the top level, stop.
if (trimmed.startsWith("[")) {
inTopLevel = false;
continue;
}
if (!inTopLevel) continue;
const match = trimmed.match(/^model_provider\s*=\s*["']([^"']+)["']/);
if (match) return match[1];
}
return undefined;
});
/**
* Returns `true` when the Codex CLI is configured with a custom
* (non-OpenAI) model provider, meaning `codex login` auth is not
* required because authentication is handled through provider-specific
* environment variables.
*/
export const hasCustomModelProvider = Effect.map(
readCodexConfigModelProvider,
(provider) => provider !== undefined && !OPENAI_AUTH_PROVIDERS.has(provider),
);
// ── Effect-native command execution ─────────────────────────────────
const collectStreamAsString = <E>(stream: Stream.Stream<Uint8Array, E>): Effect.Effect<string, E> =>
Stream.runFold(
stream,
() => "",
(acc, chunk) => acc + new TextDecoder().decode(chunk),
);
const runCodexCommand = (args: ReadonlyArray<string>) =>
Effect.gen(function* () {
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
const command = ChildProcess.make("codex", [...args], {
shell: process.platform === "win32",
});
const child = yield* spawner.spawn(command);
const [stdout, stderr, exitCode] = yield* Effect.all(
[
collectStreamAsString(child.stdout),
collectStreamAsString(child.stderr),
child.exitCode.pipe(Effect.map(Number)),
],
{ concurrency: "unbounded" },
);
return { stdout, stderr, code: exitCode } satisfies CommandResult;
}).pipe(Effect.scoped);
const runClaudeCommand = (args: ReadonlyArray<string>) =>
Effect.gen(function* () {
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
const command = ChildProcess.make("claude", [...args], {
shell: process.platform === "win32",
});
const child = yield* spawner.spawn(command);
const [stdout, stderr, exitCode] = yield* Effect.all(
[
collectStreamAsString(child.stdout),
collectStreamAsString(child.stderr),
child.exitCode.pipe(Effect.map(Number)),
],
{ concurrency: "unbounded" },
);
return { stdout, stderr, code: exitCode } satisfies CommandResult;
}).pipe(Effect.scoped);
// ── Health check ────────────────────────────────────────────────────
export const checkCodexProviderStatus: Effect.Effect<
ServerProviderStatus,
never,
ChildProcessSpawner.ChildProcessSpawner | FileSystem.FileSystem | Path.Path
> = Effect.gen(function* () {
const checkedAt = new Date().toISOString();
// Probe 1: `codex --version` — is the CLI reachable?
const versionProbe = yield* runCodexCommand(["--version"]).pipe(
Effect.timeoutOption(DEFAULT_TIMEOUT_MS),
Effect.result,
);
if (Result.isFailure(versionProbe)) {
const error = versionProbe.failure;
return {
provider: CODEX_PROVIDER,
status: "error" as const,
available: false,
authStatus: "unknown" as const,
checkedAt,
message: isCommandMissingCause(error)
? "Codex CLI (`codex`) is not installed or not on PATH."
: `Failed to execute Codex CLI health check: ${error instanceof Error ? error.message : String(error)}.`,
};
}
if (Option.isNone(versionProbe.success)) {
return {
provider: CODEX_PROVIDER,
status: "error" as const,
available: false,
authStatus: "unknown" as const,
checkedAt,
message: "Codex CLI is installed but failed to run. Timed out while running command.",
};
}
const version = versionProbe.success.value;
if (version.code !== 0) {
const detail = detailFromResult(version);
return {
provider: CODEX_PROVIDER,
status: "error" as const,
available: false,
authStatus: "unknown" as const,
checkedAt,
message: detail
? `Codex CLI is installed but failed to run. ${detail}`
: "Codex CLI is installed but failed to run.",
};
}
const parsedVersion = parseCodexCliVersion(`${version.stdout}\n${version.stderr}`);
if (parsedVersion && !isCodexCliVersionSupported(parsedVersion)) {
return {
provider: CODEX_PROVIDER,
status: "error" as const,
available: false,
authStatus: "unknown" as const,
checkedAt,
message: formatCodexCliUpgradeMessage(parsedVersion),
};
}
// Probe 2: `codex login status` — is the user authenticated?
//
// Custom model providers (e.g. Portkey, Azure OpenAI proxy) handle
// authentication through their own environment variables, so `codex
// login status` will report "not logged in" even when the CLI works
// fine. Skip the auth probe entirely for non-OpenAI providers.
if (yield* hasCustomModelProvider) {
return {
provider: CODEX_PROVIDER,
status: "ready" as const,
available: true,
authStatus: "unknown" as const,
checkedAt,
message: "Using a custom Codex model provider; OpenAI login check skipped.",
} satisfies ServerProviderStatus;
}
const authProbe = yield* runCodexCommand(["login", "status"]).pipe(
Effect.timeoutOption(DEFAULT_TIMEOUT_MS),
Effect.result,
);
if (Result.isFailure(authProbe)) {
const error = authProbe.failure;
return {
provider: CODEX_PROVIDER,
status: "warning" as const,
available: true,
authStatus: "unknown" as const,
checkedAt,
message:
error instanceof Error
? `Could not verify Codex authentication status: ${error.message}.`
: "Could not verify Codex authentication status.",
};
}
if (Option.isNone(authProbe.success)) {
return {
provider: CODEX_PROVIDER,
status: "warning" as const,
available: true,
authStatus: "unknown" as const,
checkedAt,
message: "Could not verify Codex authentication status. Timed out while running command.",
};
}
const parsed = parseAuthStatusFromOutput(authProbe.success.value);
return {
provider: CODEX_PROVIDER,
status: parsed.status,
available: true,
authStatus: parsed.authStatus,
checkedAt,
...(parsed.message ? { message: parsed.message } : {}),
} satisfies ServerProviderStatus;
});
// ── Claude Agent health check ───────────────────────────────────────
export function parseClaudeAuthStatusFromOutput(result: CommandResult): {
readonly status: ServerProviderStatusState;
readonly authStatus: ServerProviderAuthStatus;
readonly message?: string;
} {
const lowerOutput = `${result.stdout}\n${result.stderr}`.toLowerCase();
if (
lowerOutput.includes("unknown command") ||
lowerOutput.includes("unrecognized command") ||
lowerOutput.includes("unexpected argument")
) {
return {
status: "warning",
authStatus: "unknown",
message:
"Claude Agent authentication status command is unavailable in this version of Claude.",
};
}
if (
lowerOutput.includes("not logged in") ||
lowerOutput.includes("login required") ||
lowerOutput.includes("authentication required") ||
lowerOutput.includes("run `claude login`") ||
lowerOutput.includes("run claude login")
) {
return {
status: "error",
authStatus: "unauthenticated",
message: "Claude is not authenticated. Run `claude auth login` and try again.",
};
}
// `claude auth status` returns JSON with a `loggedIn` boolean.
const parsedAuth = (() => {
const trimmed = result.stdout.trim();
if (!trimmed || (!trimmed.startsWith("{") && !trimmed.startsWith("["))) {
return { attemptedJsonParse: false as const, auth: undefined as boolean | undefined };
}
try {
return {
attemptedJsonParse: true as const,
auth: extractAuthBoolean(JSON.parse(trimmed)),
};
} catch {
return { attemptedJsonParse: false as const, auth: undefined as boolean | undefined };
}
})();
if (parsedAuth.auth === true) {
return { status: "ready", authStatus: "authenticated" };
}
if (parsedAuth.auth === false) {
return {
status: "error",
authStatus: "unauthenticated",
message: "Claude is not authenticated. Run `claude auth login` and try again.",
};
}
if (parsedAuth.attemptedJsonParse) {
return {
status: "warning",
authStatus: "unknown",
message:
"Could not verify Claude authentication status from JSON output (missing auth marker).",
};
}
if (result.code === 0) {
return { status: "ready", authStatus: "authenticated" };
}
const detail = detailFromResult(result);
return {
status: "warning",
authStatus: "unknown",
message: detail
? `Could not verify Claude authentication status. ${detail}`
: "Could not verify Claude authentication status.",
};
}
export const checkClaudeProviderStatus: Effect.Effect<
ServerProviderStatus,
never,
ChildProcessSpawner.ChildProcessSpawner
> = Effect.gen(function* () {
const checkedAt = new Date().toISOString();
// Probe 1: `claude --version` — is the CLI reachable?
const versionProbe = yield* runClaudeCommand(["--version"]).pipe(
Effect.timeoutOption(DEFAULT_TIMEOUT_MS),
Effect.result,
);
if (Result.isFailure(versionProbe)) {
const error = versionProbe.failure;
return {
provider: CLAUDE_AGENT_PROVIDER,
status: "error" as const,
available: false,
authStatus: "unknown" as const,
checkedAt,
message: isCommandMissingCause(error)
? "Claude Agent CLI (`claude`) is not installed or not on PATH."
: `Failed to execute Claude Agent CLI health check: ${error instanceof Error ? error.message : String(error)}.`,
};
}
if (Option.isNone(versionProbe.success)) {
return {
provider: CLAUDE_AGENT_PROVIDER,
status: "error" as const,
available: false,
authStatus: "unknown" as const,
checkedAt,
message: "Claude Agent CLI is installed but failed to run. Timed out while running command.",
};
}
const version = versionProbe.success.value;
if (version.code !== 0) {
const detail = detailFromResult(version);
return {
provider: CLAUDE_AGENT_PROVIDER,
status: "error" as const,
available: false,
authStatus: "unknown" as const,
checkedAt,
message: detail
? `Claude Agent CLI is installed but failed to run. ${detail}`
: "Claude Agent CLI is installed but failed to run.",
};
}
// Probe 2: `claude auth status` — is the user authenticated?
const authProbe = yield* runClaudeCommand(["auth", "status"]).pipe(
Effect.timeoutOption(DEFAULT_TIMEOUT_MS),
Effect.result,
);
if (Result.isFailure(authProbe)) {
const error = authProbe.failure;
return {
provider: CLAUDE_AGENT_PROVIDER,
status: "warning" as const,
available: true,
authStatus: "unknown" as const,
checkedAt,
message:
error instanceof Error
? `Could not verify Claude authentication status: ${error.message}.`
: "Could not verify Claude authentication status.",
};
}
if (Option.isNone(authProbe.success)) {
return {
provider: CLAUDE_AGENT_PROVIDER,
status: "warning" as const,
available: true,
authStatus: "unknown" as const,
checkedAt,
message: "Could not verify Claude authentication status. Timed out while running command.",
};
}
const parsed = parseClaudeAuthStatusFromOutput(authProbe.success.value);
return {
provider: CLAUDE_AGENT_PROVIDER,
status: parsed.status,
available: true,
authStatus: parsed.authStatus,
checkedAt,
...(parsed.message ? { message: parsed.message } : {}),
} satisfies ServerProviderStatus;
});
// ── Layer ───────────────────────────────────────────────────────────
export const ProviderHealthLive = Layer.effect(
ProviderHealth,
Effect.gen(function* () {
const statusesFiber = yield* Effect.all([checkCodexProviderStatus, checkClaudeProviderStatus], {
concurrency: "unbounded",
}).pipe(Effect.forkScoped);
return {
getStatuses: Fiber.join(statusesFiber),
} satisfies ProviderHealthShape;
}),
);
@@ -0,0 +1,869 @@
import * as NodeServices from "@effect/platform-node/NodeServices";
import { describe, it, assert } from "@effect/vitest";
import {
Effect,
Exit,
FileSystem,
Layer,
Path,
PubSub,
Ref,
Schema,
Scope,
Sink,
Stream,
} from "effect";
import {
DEFAULT_SERVER_SETTINGS,
ServerSettings,
type ServerSettings as ContractServerSettings,
} from "@t3tools/contracts";
import * as PlatformError from "effect/PlatformError";
import { ChildProcessSpawner } from "effect/unstable/process";
import { deepMerge } from "@t3tools/shared/Struct";
import {
checkCodexProviderStatus,
hasCustomModelProvider,
parseAuthStatusFromOutput,
readCodexConfigModelProvider,
} from "./CodexProvider";
import { checkClaudeProviderStatus, parseClaudeAuthStatusFromOutput } from "./ClaudeProvider";
import { ProviderRegistryLive } from "./ProviderRegistry";
import { ServerSettingsService, type ServerSettingsShape } from "../../serverSettings";
import { ProviderRegistry } from "../Services/ProviderRegistry";
// ── Test helpers ────────────────────────────────────────────────────
const encoder = new TextEncoder();
function mockHandle(result: { stdout: string; stderr: string; code: number }) {
return ChildProcessSpawner.makeHandle({
pid: ChildProcessSpawner.ProcessId(1),
exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(result.code)),
isRunning: Effect.succeed(false),
kill: () => Effect.void,
stdin: Sink.drain,
stdout: Stream.make(encoder.encode(result.stdout)),
stderr: Stream.make(encoder.encode(result.stderr)),
all: Stream.empty,
getInputFd: () => Sink.drain,
getOutputFd: () => Stream.empty,
});
}
function mockSpawnerLayer(
handler: (args: ReadonlyArray<string>) => { stdout: string; stderr: string; code: number },
) {
return Layer.succeed(
ChildProcessSpawner.ChildProcessSpawner,
ChildProcessSpawner.make((command) => {
const cmd = command as unknown as { args: ReadonlyArray<string> };
return Effect.succeed(mockHandle(handler(cmd.args)));
}),
);
}
function mockCommandSpawnerLayer(
handler: (
command: string,
args: ReadonlyArray<string>,
) => { stdout: string; stderr: string; code: number },
) {
return Layer.succeed(
ChildProcessSpawner.ChildProcessSpawner,
ChildProcessSpawner.make((command) => {
const cmd = command as unknown as { command: string; args: ReadonlyArray<string> };
return Effect.succeed(mockHandle(handler(cmd.command, cmd.args)));
}),
);
}
function failingSpawnerLayer(description: string) {
return Layer.succeed(
ChildProcessSpawner.ChildProcessSpawner,
ChildProcessSpawner.make(() =>
Effect.fail(
PlatformError.systemError({
_tag: "NotFound",
module: "ChildProcess",
method: "spawn",
description,
}),
),
),
);
}
function makeMutableServerSettingsService(
initial: ContractServerSettings = DEFAULT_SERVER_SETTINGS,
) {
return Effect.gen(function* () {
const settingsRef = yield* Ref.make(initial);
const changes = yield* PubSub.unbounded<ContractServerSettings>();
return {
start: Effect.void,
ready: Effect.void,
getSettings: Ref.get(settingsRef),
updateSettings: (patch) =>
Effect.gen(function* () {
const current = yield* Ref.get(settingsRef);
const next = Schema.decodeSync(ServerSettings)(deepMerge(current, patch));
yield* Ref.set(settingsRef, next);
yield* PubSub.publish(changes, next);
return next;
}),
streamChanges: Stream.fromPubSub(changes),
} satisfies ServerSettingsShape;
});
}
/**
* Create a temporary CODEX_HOME scoped to the current Effect test.
* Cleanup is registered in the test scope rather than via Vitest hooks.
*/
function withTempCodexHome(configContent?: string) {
return Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const tmpDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-test-codex-" });
yield* Effect.acquireRelease(
Effect.sync(() => {
const originalCodexHome = process.env.CODEX_HOME;
process.env.CODEX_HOME = tmpDir;
return originalCodexHome;
}),
(originalCodexHome) =>
Effect.sync(() => {
if (originalCodexHome !== undefined) {
process.env.CODEX_HOME = originalCodexHome;
} else {
delete process.env.CODEX_HOME;
}
}),
);
if (configContent !== undefined) {
yield* fileSystem.writeFileString(path.join(tmpDir, "config.toml"), configContent);
}
return { tmpDir } as const;
});
}
it.layer(Layer.mergeAll(NodeServices.layer, ServerSettingsService.layerTest()))(
"ProviderRegistry",
(it) => {
// ── checkCodexProviderStatus tests ────────────────────────────────
//
// These tests control CODEX_HOME to ensure the custom-provider detection
// in hasCustomModelProvider() does not interfere with the auth-probe
// path being tested.
describe("checkCodexProviderStatus", () => {
it.effect("returns ready when codex is installed and authenticated", () =>
Effect.gen(function* () {
// Point CODEX_HOME at an empty tmp dir (no config.toml) so the
// default code path (OpenAI provider, auth probe runs) is exercised.
yield* withTempCodexHome();
const status = yield* checkCodexProviderStatus;
assert.strictEqual(status.provider, "codex");
assert.strictEqual(status.status, "ready");
assert.strictEqual(status.installed, true);
assert.strictEqual(status.authStatus, "authenticated");
}).pipe(
Effect.provide(
mockSpawnerLayer((args) => {
const joined = args.join(" ");
if (joined === "--version") return { stdout: "codex 1.0.0\n", stderr: "", code: 0 };
if (joined === "login status") return { stdout: "Logged in\n", stderr: "", code: 0 };
throw new Error(`Unexpected args: ${joined}`);
}),
),
),
);
it.effect("inherits PATH when launching the codex probe with a CODEX_HOME override", () =>
Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const binDir = yield* fileSystem.makeTempDirectoryScoped({
prefix: "t3-test-codex-bin-",
});
const codexPath = path.join(binDir, "codex");
yield* fileSystem.writeFileString(
codexPath,
[
"#!/bin/sh",
'if [ "$1" = "--version" ]; then',
' echo "codex-cli 1.0.0"',
" exit 0",
"fi",
'if [ "$1" = "login" ] && [ "$2" = "status" ]; then',
' echo "Logged in using ChatGPT"',
" exit 0",
"fi",
'echo "unexpected args: $*" >&2',
"exit 1",
"",
].join("\n"),
);
yield* fileSystem.chmod(codexPath, 0o755);
const customCodexHome = yield* fileSystem.makeTempDirectoryScoped({
prefix: "t3-test-codex-home-",
});
const previousPath = process.env.PATH;
process.env.PATH = binDir;
try {
const serverSettingsLayer = Layer.succeed(ServerSettingsService, {
start: Effect.void,
ready: Effect.void,
getSettings: Effect.succeed({
...DEFAULT_SERVER_SETTINGS,
providers: {
...DEFAULT_SERVER_SETTINGS.providers,
codex: {
...DEFAULT_SERVER_SETTINGS.providers.codex,
homePath: customCodexHome,
},
},
}),
updateSettings: () => Effect.die("not used"),
streamChanges: Stream.empty,
} satisfies ServerSettingsShape);
const status = yield* checkCodexProviderStatus.pipe(
Effect.provide(serverSettingsLayer),
);
assert.strictEqual(status.provider, "codex");
assert.strictEqual(status.installed, true);
assert.strictEqual(status.status, "ready");
assert.strictEqual(status.authStatus, "authenticated");
} finally {
process.env.PATH = previousPath;
}
}),
);
it.effect("returns unavailable when codex is missing", () =>
Effect.gen(function* () {
yield* withTempCodexHome();
const status = yield* checkCodexProviderStatus;
assert.strictEqual(status.provider, "codex");
assert.strictEqual(status.status, "error");
assert.strictEqual(status.installed, false);
assert.strictEqual(status.authStatus, "unknown");
assert.strictEqual(
status.message,
"Codex CLI (`codex`) is not installed or not on PATH.",
);
}).pipe(Effect.provide(failingSpawnerLayer("spawn codex ENOENT"))),
);
it.effect("returns unavailable when codex is below the minimum supported version", () =>
Effect.gen(function* () {
yield* withTempCodexHome();
const status = yield* checkCodexProviderStatus;
assert.strictEqual(status.provider, "codex");
assert.strictEqual(status.status, "error");
assert.strictEqual(status.installed, true);
assert.strictEqual(status.authStatus, "unknown");
assert.strictEqual(
status.message,
"Codex CLI v0.36.0 is too old for T3 Code. Upgrade to v0.37.0 or newer and restart T3 Code.",
);
}).pipe(
Effect.provide(
mockSpawnerLayer((args) => {
const joined = args.join(" ");
if (joined === "--version") return { stdout: "codex 0.36.0\n", stderr: "", code: 0 };
throw new Error(`Unexpected args: ${joined}`);
}),
),
),
);
it.effect("returns unauthenticated when auth probe reports login required", () =>
Effect.gen(function* () {
yield* withTempCodexHome();
const status = yield* checkCodexProviderStatus;
assert.strictEqual(status.provider, "codex");
assert.strictEqual(status.status, "error");
assert.strictEqual(status.installed, true);
assert.strictEqual(status.authStatus, "unauthenticated");
assert.strictEqual(
status.message,
"Codex CLI is not authenticated. Run `codex login` and try again.",
);
}).pipe(
Effect.provide(
mockSpawnerLayer((args) => {
const joined = args.join(" ");
if (joined === "--version") return { stdout: "codex 1.0.0\n", stderr: "", code: 0 };
if (joined === "login status") {
return { stdout: "", stderr: "Not logged in. Run codex login.", code: 1 };
}
throw new Error(`Unexpected args: ${joined}`);
}),
),
),
);
it.effect("returns unauthenticated when login status output includes 'not logged in'", () =>
Effect.gen(function* () {
yield* withTempCodexHome();
const status = yield* checkCodexProviderStatus;
assert.strictEqual(status.provider, "codex");
assert.strictEqual(status.status, "error");
assert.strictEqual(status.installed, true);
assert.strictEqual(status.authStatus, "unauthenticated");
assert.strictEqual(
status.message,
"Codex CLI is not authenticated. Run `codex login` and try again.",
);
}).pipe(
Effect.provide(
mockSpawnerLayer((args) => {
const joined = args.join(" ");
if (joined === "--version") return { stdout: "codex 1.0.0\n", stderr: "", code: 0 };
if (joined === "login status")
return { stdout: "Not logged in\n", stderr: "", code: 1 };
throw new Error(`Unexpected args: ${joined}`);
}),
),
),
);
it.effect("returns warning when login status command is unsupported", () =>
Effect.gen(function* () {
yield* withTempCodexHome();
const status = yield* checkCodexProviderStatus;
assert.strictEqual(status.provider, "codex");
assert.strictEqual(status.status, "warning");
assert.strictEqual(status.installed, true);
assert.strictEqual(status.authStatus, "unknown");
assert.strictEqual(
status.message,
"Codex CLI authentication status command is unavailable in this Codex version.",
);
}).pipe(
Effect.provide(
mockSpawnerLayer((args) => {
const joined = args.join(" ");
if (joined === "--version") return { stdout: "codex 1.0.0\n", stderr: "", code: 0 };
if (joined === "login status") {
return { stdout: "", stderr: "error: unknown command 'login'", code: 2 };
}
throw new Error(`Unexpected args: ${joined}`);
}),
),
),
);
});
describe("ProviderRegistryLive", () => {
it.effect("reruns codex health when codex provider settings change", () =>
Effect.gen(function* () {
const serverSettings = yield* makeMutableServerSettingsService();
const scope = yield* Scope.make();
yield* Effect.addFinalizer(() => Scope.close(scope, Exit.void));
const providerRegistryLayer = ProviderRegistryLive.pipe(
Layer.provideMerge(Layer.succeed(ServerSettingsService, serverSettings)),
Layer.provideMerge(
mockCommandSpawnerLayer((command, args) => {
const joined = args.join(" ");
if (joined === "--version") {
if (command === "codex") {
return { stdout: "codex 1.0.0\n", stderr: "", code: 0 };
}
return { stdout: "", stderr: "spawn ENOENT", code: 1 };
}
if (joined === "login status") {
return { stdout: "Logged in\n", stderr: "", code: 0 };
}
throw new Error(`Unexpected args: ${joined}`);
}),
),
);
const runtimeServices = yield* Layer.build(
Layer.mergeAll(
Layer.succeed(ServerSettingsService, serverSettings),
providerRegistryLayer,
),
).pipe(Scope.provide(scope));
yield* Effect.gen(function* () {
const registry = yield* ProviderRegistry;
const initial = yield* registry.getProviders;
assert.strictEqual(
initial.find((status) => status.provider === "codex")?.status,
"ready",
);
yield* serverSettings.updateSettings({
providers: {
codex: {
binaryPath: "/custom/codex",
},
},
});
for (let attempt = 0; attempt < 20; attempt += 1) {
const updated = yield* registry.getProviders;
if (updated.find((status) => status.provider === "codex")?.status === "error") {
return;
}
yield* Effect.promise(() => new Promise((resolve) => setTimeout(resolve, 0)));
}
const updated = yield* registry.getProviders;
assert.strictEqual(
updated.find((status) => status.provider === "codex")?.status,
"error",
);
}).pipe(Effect.provide(runtimeServices));
}),
);
it.effect("skips codex probes entirely when the provider is disabled", () =>
Effect.gen(function* () {
const serverSettingsLayer = Layer.succeed(ServerSettingsService, {
start: Effect.void,
ready: Effect.void,
getSettings: Effect.succeed({
...DEFAULT_SERVER_SETTINGS,
providers: {
...DEFAULT_SERVER_SETTINGS.providers,
codex: {
...DEFAULT_SERVER_SETTINGS.providers.codex,
enabled: false,
},
},
}),
updateSettings: () => Effect.die("not used"),
streamChanges: Stream.empty,
} satisfies ServerSettingsShape);
const status = yield* checkCodexProviderStatus.pipe(
Effect.provide(
Layer.mergeAll(serverSettingsLayer, failingSpawnerLayer("spawn codex ENOENT")),
),
);
assert.strictEqual(status.provider, "codex");
assert.strictEqual(status.enabled, false);
assert.strictEqual(status.status, "disabled");
assert.strictEqual(status.installed, false);
assert.strictEqual(status.message, "Codex is disabled in T3 Code settings.");
}),
);
});
// ── Custom model provider: checkCodexProviderStatus integration ───
describe("checkCodexProviderStatus with custom model provider", () => {
it.effect(
"skips auth probe and returns ready when a custom model provider is configured",
() =>
Effect.gen(function* () {
yield* withTempCodexHome(
[
'model_provider = "portkey"',
"",
"[model_providers.portkey]",
'base_url = "https://api.portkey.ai/v1"',
'env_key = "PORTKEY_API_KEY"',
].join("\n"),
);
const status = yield* checkCodexProviderStatus;
assert.strictEqual(status.provider, "codex");
assert.strictEqual(status.status, "ready");
assert.strictEqual(status.installed, true);
assert.strictEqual(status.authStatus, "unknown");
assert.strictEqual(
status.message,
"Using a custom Codex model provider; OpenAI login check skipped.",
);
}).pipe(
Effect.provide(
// The spawner only handles --version; if the test attempts
// "login status" the throw proves the auth probe was NOT skipped.
mockSpawnerLayer((args) => {
const joined = args.join(" ");
if (joined === "--version") return { stdout: "codex 1.0.0\n", stderr: "", code: 0 };
throw new Error(`Auth probe should have been skipped but got args: ${joined}`);
}),
),
),
);
it.effect("still reports error when codex CLI is missing even with custom provider", () =>
Effect.gen(function* () {
yield* withTempCodexHome(
[
'model_provider = "portkey"',
"",
"[model_providers.portkey]",
'base_url = "https://api.portkey.ai/v1"',
'env_key = "PORTKEY_API_KEY"',
].join("\n"),
);
const status = yield* checkCodexProviderStatus;
assert.strictEqual(status.status, "error");
assert.strictEqual(status.installed, false);
}).pipe(Effect.provide(failingSpawnerLayer("spawn codex ENOENT"))),
);
});
describe("checkCodexProviderStatus with openai model provider", () => {
it.effect("still runs auth probe when model_provider is openai", () =>
Effect.gen(function* () {
yield* withTempCodexHome('model_provider = "openai"\n');
const status = yield* checkCodexProviderStatus;
// The auth probe runs and sees "not logged in" → error
assert.strictEqual(status.status, "error");
assert.strictEqual(status.authStatus, "unauthenticated");
}).pipe(
Effect.provide(
mockSpawnerLayer((args) => {
const joined = args.join(" ");
if (joined === "--version") return { stdout: "codex 1.0.0\n", stderr: "", code: 0 };
if (joined === "login status")
return { stdout: "Not logged in\n", stderr: "", code: 1 };
throw new Error(`Unexpected args: ${joined}`);
}),
),
),
);
});
// ── parseAuthStatusFromOutput pure tests ──────────────────────────
describe("parseAuthStatusFromOutput", () => {
it("exit code 0 with no auth markers is ready", () => {
const parsed = parseAuthStatusFromOutput({ stdout: "OK\n", stderr: "", code: 0 });
assert.strictEqual(parsed.status, "ready");
assert.strictEqual(parsed.authStatus, "authenticated");
});
it("JSON with authenticated=false is unauthenticated", () => {
const parsed = parseAuthStatusFromOutput({
stdout: '[{"authenticated":false}]\n',
stderr: "",
code: 0,
});
assert.strictEqual(parsed.status, "error");
assert.strictEqual(parsed.authStatus, "unauthenticated");
});
it("JSON without auth marker is warning", () => {
const parsed = parseAuthStatusFromOutput({
stdout: '[{"ok":true}]\n',
stderr: "",
code: 0,
});
assert.strictEqual(parsed.status, "warning");
assert.strictEqual(parsed.authStatus, "unknown");
});
});
// ── readCodexConfigModelProvider tests ─────────────────────────────
describe("readCodexConfigModelProvider", () => {
it.effect("returns undefined when config file does not exist", () =>
Effect.gen(function* () {
yield* withTempCodexHome();
assert.strictEqual(yield* readCodexConfigModelProvider, undefined);
}),
);
it.effect("returns undefined when config has no model_provider key", () =>
Effect.gen(function* () {
yield* withTempCodexHome('model = "gpt-5-codex"\n');
assert.strictEqual(yield* readCodexConfigModelProvider, undefined);
}),
);
it.effect("returns the provider when model_provider is set at top level", () =>
Effect.gen(function* () {
yield* withTempCodexHome('model = "gpt-5-codex"\nmodel_provider = "portkey"\n');
assert.strictEqual(yield* readCodexConfigModelProvider, "portkey");
}),
);
it.effect("returns openai when model_provider is openai", () =>
Effect.gen(function* () {
yield* withTempCodexHome('model_provider = "openai"\n');
assert.strictEqual(yield* readCodexConfigModelProvider, "openai");
}),
);
it.effect("ignores model_provider inside section headers", () =>
Effect.gen(function* () {
yield* withTempCodexHome(
[
'model = "gpt-5-codex"',
"",
"[model_providers.portkey]",
'base_url = "https://api.portkey.ai/v1"',
'model_provider = "should-be-ignored"',
"",
].join("\n"),
);
assert.strictEqual(yield* readCodexConfigModelProvider, undefined);
}),
);
it.effect("handles comments and whitespace", () =>
Effect.gen(function* () {
yield* withTempCodexHome(
[
"# This is a comment",
"",
' model_provider = "azure" ',
"",
"[profiles.deep-review]",
'model = "gpt-5-pro"',
].join("\n"),
);
assert.strictEqual(yield* readCodexConfigModelProvider, "azure");
}),
);
it.effect("handles single-quoted values in TOML", () =>
Effect.gen(function* () {
yield* withTempCodexHome("model_provider = 'mistral'\n");
assert.strictEqual(yield* readCodexConfigModelProvider, "mistral");
}),
);
});
// ── hasCustomModelProvider tests ───────────────────────────────────
describe("hasCustomModelProvider", () => {
it.effect("returns false when no config file exists", () =>
Effect.gen(function* () {
yield* withTempCodexHome();
assert.strictEqual(yield* hasCustomModelProvider, false);
}),
);
it.effect("returns false when model_provider is not set", () =>
Effect.gen(function* () {
yield* withTempCodexHome('model = "gpt-5-codex"\n');
assert.strictEqual(yield* hasCustomModelProvider, false);
}),
);
it.effect("returns false when model_provider is openai", () =>
Effect.gen(function* () {
yield* withTempCodexHome('model_provider = "openai"\n');
assert.strictEqual(yield* hasCustomModelProvider, false);
}),
);
it.effect("returns true when model_provider is portkey", () =>
Effect.gen(function* () {
yield* withTempCodexHome('model_provider = "portkey"\n');
assert.strictEqual(yield* hasCustomModelProvider, true);
}),
);
it.effect("returns true when model_provider is azure", () =>
Effect.gen(function* () {
yield* withTempCodexHome('model_provider = "azure"\n');
assert.strictEqual(yield* hasCustomModelProvider, true);
}),
);
it.effect("returns true when model_provider is ollama", () =>
Effect.gen(function* () {
yield* withTempCodexHome('model_provider = "ollama"\n');
assert.strictEqual(yield* hasCustomModelProvider, true);
}),
);
it.effect("returns true when model_provider is a custom proxy", () =>
Effect.gen(function* () {
yield* withTempCodexHome('model_provider = "my-company-proxy"\n');
assert.strictEqual(yield* hasCustomModelProvider, true);
}),
);
});
// ── checkClaudeProviderStatus tests ──────────────────────────
describe("checkClaudeProviderStatus", () => {
it.effect("returns ready when claude is installed and authenticated", () =>
Effect.gen(function* () {
const status = yield* checkClaudeProviderStatus;
assert.strictEqual(status.provider, "claudeAgent");
assert.strictEqual(status.status, "ready");
assert.strictEqual(status.installed, true);
assert.strictEqual(status.authStatus, "authenticated");
}).pipe(
Effect.provide(
mockSpawnerLayer((args) => {
const joined = args.join(" ");
if (joined === "--version") return { stdout: "1.0.0\n", stderr: "", code: 0 };
if (joined === "auth status")
return {
stdout: '{"loggedIn":true,"authMethod":"claude.ai"}\n',
stderr: "",
code: 0,
};
throw new Error(`Unexpected args: ${joined}`);
}),
),
),
);
it.effect("returns unavailable when claude is missing", () =>
Effect.gen(function* () {
const status = yield* checkClaudeProviderStatus;
assert.strictEqual(status.provider, "claudeAgent");
assert.strictEqual(status.status, "error");
assert.strictEqual(status.installed, false);
assert.strictEqual(status.authStatus, "unknown");
assert.strictEqual(
status.message,
"Claude Agent CLI (`claude`) is not installed or not on PATH.",
);
}).pipe(Effect.provide(failingSpawnerLayer("spawn claude ENOENT"))),
);
it.effect("returns error when version check fails with non-zero exit code", () =>
Effect.gen(function* () {
const status = yield* checkClaudeProviderStatus;
assert.strictEqual(status.provider, "claudeAgent");
assert.strictEqual(status.status, "error");
assert.strictEqual(status.installed, true);
}).pipe(
Effect.provide(
mockSpawnerLayer((args) => {
const joined = args.join(" ");
if (joined === "--version")
return { stdout: "", stderr: "Something went wrong", code: 1 };
throw new Error(`Unexpected args: ${joined}`);
}),
),
),
);
it.effect("returns unauthenticated when auth status reports not logged in", () =>
Effect.gen(function* () {
const status = yield* checkClaudeProviderStatus;
assert.strictEqual(status.provider, "claudeAgent");
assert.strictEqual(status.status, "error");
assert.strictEqual(status.installed, true);
assert.strictEqual(status.authStatus, "unauthenticated");
assert.strictEqual(
status.message,
"Claude is not authenticated. Run `claude auth login` and try again.",
);
}).pipe(
Effect.provide(
mockSpawnerLayer((args) => {
const joined = args.join(" ");
if (joined === "--version") return { stdout: "1.0.0\n", stderr: "", code: 0 };
if (joined === "auth status")
return {
stdout: '{"loggedIn":false}\n',
stderr: "",
code: 1,
};
throw new Error(`Unexpected args: ${joined}`);
}),
),
),
);
it.effect("returns unauthenticated when output includes 'not logged in'", () =>
Effect.gen(function* () {
const status = yield* checkClaudeProviderStatus;
assert.strictEqual(status.provider, "claudeAgent");
assert.strictEqual(status.status, "error");
assert.strictEqual(status.installed, true);
assert.strictEqual(status.authStatus, "unauthenticated");
}).pipe(
Effect.provide(
mockSpawnerLayer((args) => {
const joined = args.join(" ");
if (joined === "--version") return { stdout: "1.0.0\n", stderr: "", code: 0 };
if (joined === "auth status")
return { stdout: "Not logged in\n", stderr: "", code: 1 };
throw new Error(`Unexpected args: ${joined}`);
}),
),
),
);
it.effect("returns warning when auth status command is unsupported", () =>
Effect.gen(function* () {
const status = yield* checkClaudeProviderStatus;
assert.strictEqual(status.provider, "claudeAgent");
assert.strictEqual(status.status, "warning");
assert.strictEqual(status.installed, true);
assert.strictEqual(status.authStatus, "unknown");
assert.strictEqual(
status.message,
"Claude Agent authentication status command is unavailable in this version of Claude.",
);
}).pipe(
Effect.provide(
mockSpawnerLayer((args) => {
const joined = args.join(" ");
if (joined === "--version") return { stdout: "1.0.0\n", stderr: "", code: 0 };
if (joined === "auth status")
return { stdout: "", stderr: "error: unknown command 'auth'", code: 2 };
throw new Error(`Unexpected args: ${joined}`);
}),
),
),
);
});
// ── parseClaudeAuthStatusFromOutput pure tests ────────────────────
describe("parseClaudeAuthStatusFromOutput", () => {
it("exit code 0 with no auth markers is ready", () => {
const parsed = parseClaudeAuthStatusFromOutput({ stdout: "OK\n", stderr: "", code: 0 });
assert.strictEqual(parsed.status, "ready");
assert.strictEqual(parsed.authStatus, "authenticated");
});
it("JSON with loggedIn=true is authenticated", () => {
const parsed = parseClaudeAuthStatusFromOutput({
stdout: '{"loggedIn":true,"authMethod":"claude.ai"}\n',
stderr: "",
code: 0,
});
assert.strictEqual(parsed.status, "ready");
assert.strictEqual(parsed.authStatus, "authenticated");
});
it("JSON with loggedIn=false is unauthenticated", () => {
const parsed = parseClaudeAuthStatusFromOutput({
stdout: '{"loggedIn":false}\n',
stderr: "",
code: 0,
});
assert.strictEqual(parsed.status, "error");
assert.strictEqual(parsed.authStatus, "unauthenticated");
});
it("JSON without auth marker is warning", () => {
const parsed = parseClaudeAuthStatusFromOutput({
stdout: '{"ok":true}\n',
stderr: "",
code: 0,
});
assert.strictEqual(parsed.status, "warning");
assert.strictEqual(parsed.authStatus, "unknown");
});
});
},
);
@@ -0,0 +1,82 @@
/**
* ProviderRegistryLive - Aggregates provider-specific snapshot services.
*
* @module ProviderRegistryLive
*/
import type { ProviderKind, ServerProvider } from "@t3tools/contracts";
import { Effect, Layer, PubSub, Ref, Stream } from "effect";
import { ClaudeProviderLive } from "./ClaudeProvider";
import { CodexProviderLive } from "./CodexProvider";
import type { ClaudeProviderShape } from "../Services/ClaudeProvider";
import { ClaudeProvider } from "../Services/ClaudeProvider";
import type { CodexProviderShape } from "../Services/CodexProvider";
import { CodexProvider } from "../Services/CodexProvider";
import { ProviderRegistry, type ProviderRegistryShape } from "../Services/ProviderRegistry";
const loadProviders = (
codexProvider: CodexProviderShape,
claudeProvider: ClaudeProviderShape,
): Effect.Effect<readonly [ServerProvider, ServerProvider]> =>
Effect.all([codexProvider.getSnapshot, claudeProvider.getSnapshot], {
concurrency: "unbounded",
});
export const ProviderRegistryLive = Layer.effect(
ProviderRegistry,
Effect.gen(function* () {
const codexProvider = yield* CodexProvider;
const claudeProvider = yield* ClaudeProvider;
const changesPubSub = yield* Effect.acquireRelease(
PubSub.unbounded<ReadonlyArray<ServerProvider>>(),
PubSub.shutdown,
);
const providersRef = yield* Ref.make<ReadonlyArray<ServerProvider>>(
yield* loadProviders(codexProvider, claudeProvider),
);
const syncProviders = Effect.gen(function* () {
const providers = yield* loadProviders(codexProvider, claudeProvider);
yield* Ref.set(providersRef, providers);
yield* PubSub.publish(changesPubSub, providers);
return providers;
});
yield* Stream.runForEach(codexProvider.streamChanges, () => syncProviders).pipe(
Effect.forkScoped,
);
yield* Stream.runForEach(claudeProvider.streamChanges, () => syncProviders).pipe(
Effect.forkScoped,
);
return {
getProviders: syncProviders.pipe(
Effect.tapError(Effect.logError),
Effect.orElseSucceed(() => []),
),
refresh: (provider?: ProviderKind) =>
Effect.gen(function* () {
switch (provider) {
case "codex":
yield* codexProvider.refresh;
break;
case "claudeAgent":
yield* claudeProvider.refresh;
break;
default:
yield* Effect.all([codexProvider.refresh, claudeProvider.refresh], {
concurrency: "unbounded",
});
break;
}
return yield* syncProviders;
}).pipe(
Effect.tapError(Effect.logError),
Effect.orElseSucceed(() => []),
),
get streamChanges() {
return Stream.fromPubSub(changesPubSub);
},
} satisfies ProviderRegistryShape;
}),
).pipe(Layer.provideMerge(CodexProviderLive), Layer.provideMerge(ClaudeProviderLive));
@@ -36,14 +36,24 @@ import { ProviderSessionDirectory } from "../Services/ProviderSessionDirectory.t
import { makeProviderServiceLive } from "./ProviderService.ts";
import { ProviderSessionDirectoryLive } from "./ProviderSessionDirectory.ts";
import * as NodeServices from "@effect/platform-node/NodeServices";
import { DEFAULT_SERVER_SETTINGS } from "@t3tools/contracts/settings";
import { ProviderSessionRuntimeRepositoryLive } from "../../persistence/Layers/ProviderSessionRuntime.ts";
import { ProviderSessionRuntimeRepository } from "../../persistence/Services/ProviderSessionRuntime.ts";
import {
makeSqlitePersistenceLive,
SqlitePersistenceMemory,
} from "../../persistence/Layers/Sqlite.ts";
import { ServerSettingsService, type ServerSettingsShape } from "../../serverSettings.ts";
import { AnalyticsService } from "../../telemetry/Services/AnalyticsService.ts";
const defaultServerSettingsLayer = Layer.succeed(ServerSettingsService, {
start: Effect.void,
ready: Effect.void,
getSettings: Effect.succeed(DEFAULT_SERVER_SETTINGS),
updateSettings: () => Effect.succeed(DEFAULT_SERVER_SETTINGS),
streamChanges: Stream.empty,
} satisfies ServerSettingsShape);
const asRequestId = (value: string): ApprovalRequestId => ApprovalRequestId.makeUnsafe(value);
const asEventId = (value: string): EventId => EventId.makeUnsafe(value);
const asThreadId = (value: string): ThreadId => ThreadId.makeUnsafe(value);
@@ -251,6 +261,7 @@ function makeProviderServiceLayer() {
makeProviderServiceLive().pipe(
Layer.provide(providerAdapterLayer),
Layer.provide(directoryLayer),
Layer.provide(defaultServerSettingsLayer),
Layer.provideMerge(AnalyticsService.layerTest),
),
directoryLayer,
@@ -267,6 +278,64 @@ function makeProviderServiceLayer() {
};
}
it.effect("ProviderServiceLive rejects new sessions for disabled providers", () =>
Effect.gen(function* () {
const codex = makeFakeCodexAdapter();
const claude = makeFakeCodexAdapter("claudeAgent");
const registry: typeof ProviderAdapterRegistry.Service = {
getByProvider: (provider) =>
provider === "codex"
? Effect.succeed(codex.adapter)
: provider === "claudeAgent"
? Effect.succeed(claude.adapter)
: Effect.fail(new ProviderUnsupportedError({ provider })),
listProviders: () => Effect.succeed(["codex", "claudeAgent"]),
};
const providerAdapterLayer = Layer.succeed(ProviderAdapterRegistry, registry);
const serverSettingsLayer = Layer.succeed(ServerSettingsService, {
start: Effect.void,
ready: Effect.void,
getSettings: Effect.succeed({
...DEFAULT_SERVER_SETTINGS,
providers: {
...DEFAULT_SERVER_SETTINGS.providers,
claudeAgent: {
...DEFAULT_SERVER_SETTINGS.providers.claudeAgent,
enabled: false,
},
},
}),
updateSettings: () => Effect.die("not used"),
streamChanges: Stream.empty,
} satisfies ServerSettingsShape);
const runtimeRepositoryLayer = ProviderSessionRuntimeRepositoryLive.pipe(
Layer.provide(SqlitePersistenceMemory),
);
const directoryLayer = ProviderSessionDirectoryLive.pipe(Layer.provide(runtimeRepositoryLayer));
const providerLayer = makeProviderServiceLive().pipe(
Layer.provide(providerAdapterLayer),
Layer.provide(directoryLayer),
Layer.provide(serverSettingsLayer),
Layer.provide(AnalyticsService.layerTest),
);
const failure = yield* Effect.flip(
Effect.gen(function* () {
const provider = yield* ProviderService;
return yield* provider.startSession(asThreadId("thread-disabled"), {
provider: "claudeAgent",
threadId: asThreadId("thread-disabled"),
runtimeMode: "full-access",
});
}).pipe(Effect.provide(providerLayer)),
);
assert.instanceOf(failure, ProviderValidationError);
assert.include(failure.issue, "Provider 'claudeAgent' is disabled in T3 Code settings.");
assert.equal(claude.startSession.mock.calls.length, 0);
}).pipe(Effect.provide(NodeServices.layer)),
);
const routing = makeProviderServiceLayer();
it.effect("ProviderServiceLive keeps persisted resumable sessions on startup", () =>
Effect.gen(function* () {
@@ -299,6 +368,7 @@ it.effect("ProviderServiceLive keeps persisted resumable sessions on startup", (
const providerLayer = makeProviderServiceLive().pipe(
Layer.provide(Layer.succeed(ProviderAdapterRegistry, registry)),
Layer.provide(directoryLayer),
Layer.provide(defaultServerSettingsLayer),
Layer.provide(AnalyticsService.layerTest),
);
@@ -358,6 +428,7 @@ it.effect(
const firstProviderLayer = makeProviderServiceLive().pipe(
Layer.provide(Layer.succeed(ProviderAdapterRegistry, firstRegistry)),
Layer.provide(firstDirectoryLayer),
Layer.provide(defaultServerSettingsLayer),
Layer.provide(AnalyticsService.layerTest),
);
const updatedResumeCursor = {
@@ -409,6 +480,7 @@ it.effect(
const secondProviderLayer = makeProviderServiceLive().pipe(
Layer.provide(Layer.succeed(ProviderAdapterRegistry, secondRegistry)),
Layer.provide(secondDirectoryLayer),
Layer.provide(defaultServerSettingsLayer),
Layer.provide(AnalyticsService.layerTest),
);
@@ -769,6 +841,7 @@ routing.layer("ProviderServiceLive routing", (it) => {
const firstProviderLayer = makeProviderServiceLive().pipe(
Layer.provide(Layer.succeed(ProviderAdapterRegistry, firstRegistry)),
Layer.provide(firstDirectoryLayer),
Layer.provide(defaultServerSettingsLayer),
Layer.provide(AnalyticsService.layerTest),
);
@@ -801,6 +874,7 @@ routing.layer("ProviderServiceLive routing", (it) => {
const secondProviderLayer = makeProviderServiceLive().pipe(
Layer.provide(Layer.succeed(ProviderAdapterRegistry, secondRegistry)),
Layer.provide(secondDirectoryLayer),
Layer.provide(defaultServerSettingsLayer),
Layer.provide(AnalyticsService.layerTest),
);
@@ -33,6 +33,7 @@ import {
} from "../Services/ProviderSessionDirectory.ts";
import { type EventNdjsonLogger, makeEventNdjsonLogger } from "./EventNdjsonLogger.ts";
import { AnalyticsService } from "../../telemetry/Services/AnalyticsService.ts";
import { ServerSettingsService } from "../../serverSettings.ts";
export interface ProviderServiceLiveOptions {
readonly canonicalEventLogPath?: string;
@@ -91,7 +92,6 @@ function toRuntimePayloadFromSession(
session: ProviderSession,
extra?: {
readonly modelSelection?: unknown;
readonly providerOptions?: unknown;
readonly lastRuntimeEvent?: string;
readonly lastRuntimeEventAt?: string;
},
@@ -102,7 +102,6 @@ function toRuntimePayloadFromSession(
activeTurnId: session.activeTurnId ?? null,
lastError: session.lastError ?? null,
...(extra?.modelSelection !== undefined ? { modelSelection: extra.modelSelection } : {}),
...(extra?.providerOptions !== undefined ? { providerOptions: extra.providerOptions } : {}),
...(extra?.lastRuntimeEvent !== undefined ? { lastRuntimeEvent: extra.lastRuntimeEvent } : {}),
...(extra?.lastRuntimeEventAt !== undefined
? { lastRuntimeEventAt: extra.lastRuntimeEventAt }
@@ -120,17 +119,6 @@ function readPersistedModelSelection(
return Schema.is(ModelSelection)(raw) ? raw : undefined;
}
function readPersistedProviderOptions(
runtimePayload: ProviderRuntimeBinding["runtimePayload"],
): Record<string, unknown> | undefined {
if (!runtimePayload || typeof runtimePayload !== "object" || Array.isArray(runtimePayload)) {
return undefined;
}
const raw = "providerOptions" in runtimePayload ? runtimePayload.providerOptions : undefined;
if (!raw || typeof raw !== "object" || Array.isArray(raw)) return undefined;
return raw as Record<string, unknown>;
}
function readPersistedCwd(
runtimePayload: ProviderRuntimeBinding["runtimePayload"],
): string | undefined {
@@ -146,6 +134,7 @@ function readPersistedCwd(
const makeProviderService = (options?: ProviderServiceLiveOptions) =>
Effect.gen(function* () {
const analytics = yield* Effect.service(AnalyticsService);
const serverSettings = yield* ServerSettingsService;
const canonicalEventLogger =
options?.canonicalEventLogger ??
(options?.canonicalEventLogPath !== undefined
@@ -173,7 +162,6 @@ const makeProviderService = (options?: ProviderServiceLiveOptions) =>
threadId: ThreadId,
extra?: {
readonly modelSelection?: unknown;
readonly providerOptions?: unknown;
readonly lastRuntimeEvent?: string;
readonly lastRuntimeEventAt?: string;
},
@@ -239,14 +227,12 @@ const makeProviderService = (options?: ProviderServiceLiveOptions) =>
const persistedCwd = readPersistedCwd(input.binding.runtimePayload);
const persistedModelSelection = readPersistedModelSelection(input.binding.runtimePayload);
const persistedProviderOptions = readPersistedProviderOptions(input.binding.runtimePayload);
const resumed = yield* adapter.startSession({
threadId: input.binding.threadId,
provider: input.binding.provider,
...(persistedCwd ? { cwd: persistedCwd } : {}),
...(persistedModelSelection ? { modelSelection: persistedModelSelection } : {}),
...(persistedProviderOptions ? { providerOptions: persistedProviderOptions } : {}),
...(hasResumeCursor ? { resumeCursor: input.binding.resumeCursor } : {}),
runtimeMode: input.binding.runtimeMode ?? "full-access",
});
@@ -308,6 +294,21 @@ const makeProviderService = (options?: ProviderServiceLiveOptions) =>
threadId,
provider: parsed.provider ?? "codex",
};
const settings = yield* serverSettings.getSettings.pipe(
Effect.mapError((error) =>
toValidationError(
"ProviderService.startSession",
`Failed to load provider settings: ${error.message}`,
error,
),
),
);
if (!settings.providers[input.provider].enabled) {
return yield* toValidationError(
"ProviderService.startSession",
`Provider '${input.provider}' is disabled in T3 Code settings.`,
);
}
const persistedBinding = Option.getOrUndefined(yield* directory.getBinding(threadId));
const effectiveResumeCursor =
input.resumeCursor ??
@@ -329,7 +330,6 @@ const makeProviderService = (options?: ProviderServiceLiveOptions) =>
yield* upsertSessionBinding(session, threadId, {
modelSelection: input.modelSelection,
providerOptions: input.providerOptions,
});
yield* analytics.record("provider.session.started", {
provider: session.provider,
@@ -0,0 +1,9 @@
import { ServiceMap } from "effect";
import type { ServerProviderShape } from "./ServerProvider";
export interface ClaudeProviderShape extends ServerProviderShape {}
export class ClaudeProvider extends ServiceMap.Service<ClaudeProvider, ClaudeProviderShape>()(
"t3/provider/Services/ClaudeProvider",
) {}
@@ -0,0 +1,9 @@
import { ServiceMap } from "effect";
import type { ServerProviderShape } from "./ServerProvider";
export interface CodexProviderShape extends ServerProviderShape {}
export class CodexProvider extends ServiceMap.Service<CodexProvider, CodexProviderShape>()(
"t3/provider/Services/CodexProvider",
) {}
@@ -1,22 +0,0 @@
/**
* ProviderHealth - Provider readiness snapshot service.
*
* Owns provider health checks (install/auth reachability) and exposes the
* latest results to transport layers.
*
* @module ProviderHealth
*/
import type { ServerProviderStatus } from "@t3tools/contracts";
import { ServiceMap } from "effect";
import type { Effect } from "effect";
export interface ProviderHealthShape {
/**
* Read the latest provider health statuses.
*/
readonly getStatuses: Effect.Effect<ReadonlyArray<ServerProviderStatus>>;
}
export class ProviderHealth extends ServiceMap.Service<ProviderHealth, ProviderHealthShape>()(
"t3/provider/Services/ProviderHealth",
) {}
@@ -0,0 +1,32 @@
/**
* ProviderRegistry - Provider snapshot service.
*
* Owns provider install/auth/version/model snapshots and exposes the latest
* provider state to transport layers.
*
* @module ProviderRegistry
*/
import type { ProviderKind, ServerProvider } from "@t3tools/contracts";
import { ServiceMap } from "effect";
import type { Effect, Stream } from "effect";
export interface ProviderRegistryShape {
/**
* Read the latest provider snapshots.
*/
readonly getProviders: Effect.Effect<ReadonlyArray<ServerProvider>>;
/**
* Refresh all providers, or a single provider when specified.
*/
readonly refresh: (provider?: ProviderKind) => Effect.Effect<ReadonlyArray<ServerProvider>>;
/**
* Stream of provider snapshot updates.
*/
readonly streamChanges: Stream.Stream<ReadonlyArray<ServerProvider>>;
}
export class ProviderRegistry extends ServiceMap.Service<ProviderRegistry, ProviderRegistryShape>()(
"t3/provider/Services/ProviderRegistry",
) {}
@@ -0,0 +1,8 @@
import type { ServerProvider } from "@t3tools/contracts";
import type { Effect, Stream } from "effect";
export interface ServerProviderShape {
readonly getSnapshot: Effect.Effect<ServerProvider>;
readonly refresh: Effect.Effect<ServerProvider>;
readonly streamChanges: Stream.Stream<ServerProvider>;
}
@@ -0,0 +1,69 @@
import type { ServerProvider } from "@t3tools/contracts";
import { Duration, Effect, PubSub, Ref, Scope, Stream } from "effect";
import * as Semaphore from "effect/Semaphore";
import type { ServerProviderShape } from "./Services/ServerProvider";
export function makeManagedServerProvider<Settings>(input: {
readonly getSettings: Effect.Effect<Settings>;
readonly streamSettings: Stream.Stream<Settings>;
readonly haveSettingsChanged: (previous: Settings, next: Settings) => boolean;
readonly checkProvider: Effect.Effect<ServerProvider>;
readonly refreshInterval?: Duration.Input;
}): Effect.Effect<ServerProviderShape, never, Scope.Scope> {
return Effect.gen(function* () {
const refreshSemaphore = yield* Semaphore.make(1);
const changesPubSub = yield* Effect.acquireRelease(
PubSub.unbounded<ServerProvider>(),
PubSub.shutdown,
);
const initialSettings = yield* input.getSettings;
const initialSnapshot = yield* input.checkProvider;
const snapshotRef = yield* Ref.make(initialSnapshot);
const settingsRef = yield* Ref.make(initialSettings);
const refreshSnapshot = refreshSemaphore.withPermits(1)(
Effect.gen(function* () {
const nextSettings = yield* input.getSettings;
const nextSnapshot = yield* input.checkProvider;
yield* Ref.set(settingsRef, nextSettings);
yield* Ref.set(snapshotRef, nextSnapshot);
yield* PubSub.publish(changesPubSub, nextSnapshot);
return nextSnapshot;
}),
);
const reconcileSnapshot = (nextSettings: Settings) =>
Effect.gen(function* () {
const previousSettings = yield* Ref.get(settingsRef);
if (!input.haveSettingsChanged(previousSettings, nextSettings)) {
yield* Ref.set(settingsRef, nextSettings);
return yield* Ref.get(snapshotRef);
}
return yield* refreshSnapshot;
});
yield* Stream.runForEach(input.streamSettings, (nextSettings) =>
Effect.asVoid(reconcileSnapshot(nextSettings)),
).pipe(Effect.forkScoped);
yield* Effect.forever(
Effect.sleep(input.refreshInterval ?? "60 seconds").pipe(
Effect.flatMap(() => refreshSnapshot),
Effect.ignoreCause({ log: true }),
),
).pipe(Effect.forkScoped);
return {
getSnapshot: input.getSettings.pipe(
Effect.flatMap(reconcileSnapshot),
Effect.tapError(Effect.logError),
Effect.orDie,
),
refresh: refreshSnapshot.pipe(Effect.tapError(Effect.logError), Effect.orDie),
get streamChanges() {
return Stream.fromPubSub(changesPubSub);
},
} satisfies ServerProviderShape;
});
}
@@ -0,0 +1,132 @@
import type {
ServerProvider,
ServerProviderAuthStatus,
ServerProviderModel,
ServerProviderState,
} from "@t3tools/contracts";
import { Effect, Stream } from "effect";
import { normalizeModelSlug } from "@t3tools/shared/model";
export const DEFAULT_TIMEOUT_MS = 4_000;
export interface CommandResult {
readonly stdout: string;
readonly stderr: string;
readonly code: number;
}
export interface ProviderProbeResult {
readonly installed: boolean;
readonly version: string | null;
readonly status: Exclude<ServerProviderState, "disabled">;
readonly authStatus: ServerProviderAuthStatus;
readonly message?: string;
}
export function nonEmptyTrimmed(value: string | undefined): string | undefined {
if (!value) return undefined;
const trimmed = value.trim();
return trimmed.length > 0 ? trimmed : undefined;
}
export function isCommandMissingCause(error: unknown): boolean {
if (!(error instanceof Error)) return false;
const lower = error.message.toLowerCase();
return lower.includes("enoent") || lower.includes("notfound");
}
export function detailFromResult(
result: CommandResult & { readonly timedOut?: boolean },
): string | undefined {
if (result.timedOut) return "Timed out while running command.";
const stderr = nonEmptyTrimmed(result.stderr);
if (stderr) return stderr;
const stdout = nonEmptyTrimmed(result.stdout);
if (stdout) return stdout;
if (result.code !== 0) {
return `Command exited with code ${result.code}.`;
}
return undefined;
}
export function extractAuthBoolean(value: unknown): boolean | undefined {
if (globalThis.Array.isArray(value)) {
for (const entry of value) {
const nested = extractAuthBoolean(entry);
if (nested !== undefined) return nested;
}
return undefined;
}
if (!value || typeof value !== "object") return undefined;
const record = value as Record<string, unknown>;
for (const key of ["authenticated", "isAuthenticated", "loggedIn", "isLoggedIn"] as const) {
if (typeof record[key] === "boolean") return record[key];
}
for (const key of ["auth", "status", "session", "account"] as const) {
const nested = extractAuthBoolean(record[key]);
if (nested !== undefined) return nested;
}
return undefined;
}
export function parseGenericCliVersion(output: string): string | null {
const match = output.match(/\b(\d+\.\d+\.\d+)\b/);
return match?.[1] ?? null;
}
export function providerModelsFromSettings(
builtInModels: ReadonlyArray<ServerProviderModel>,
provider: ServerProvider["provider"],
customModels: ReadonlyArray<string>,
): ReadonlyArray<ServerProviderModel> {
const resolvedBuiltInModels = [...builtInModels];
const seen = new Set(resolvedBuiltInModels.map((model) => model.slug));
const customEntries: ServerProviderModel[] = [];
for (const candidate of customModels) {
const normalized = normalizeModelSlug(candidate, provider);
if (!normalized || seen.has(normalized)) {
continue;
}
seen.add(normalized);
customEntries.push({
slug: normalized,
name: normalized,
isCustom: true,
capabilities: null,
});
}
return [...resolvedBuiltInModels, ...customEntries];
}
export function buildServerProvider(input: {
provider: ServerProvider["provider"];
enabled: boolean;
checkedAt: string;
models: ReadonlyArray<ServerProviderModel>;
probe: ProviderProbeResult;
}): ServerProvider {
return {
provider: input.provider,
enabled: input.enabled,
installed: input.probe.installed,
version: input.probe.version,
status: input.enabled ? input.probe.status : "disabled",
authStatus: input.probe.authStatus,
checkedAt: input.checkedAt,
...(input.probe.message ? { message: input.probe.message } : {}),
models: input.models,
};
}
export const collectStreamAsString = <E>(
stream: Stream.Stream<Uint8Array, E>,
): Effect.Effect<string, E> =>
Stream.runFold(
stream,
() => "",
(acc, chunk) => acc + new TextDecoder().decode(chunk),
);
+6 -1
View File
@@ -24,6 +24,7 @@ import { makeProviderServiceLive } from "./provider/Layers/ProviderService";
import { ProviderSessionDirectoryLive } from "./provider/Layers/ProviderSessionDirectory";
import { ProviderService } from "./provider/Services/ProviderService";
import { makeEventNdjsonLogger } from "./provider/Layers/EventNdjsonLogger";
import { ServerSettingsService } from "./serverSettings";
import { TerminalManagerLive } from "./terminal/Layers/Manager";
import { KeybindingsLive } from "./keybindings";
@@ -54,7 +55,11 @@ const makeRuntimePtyAdapterLayer = () =>
export function makeServerProviderLayer(): Layer.Layer<
ProviderService,
ProviderUnsupportedError,
SqlClient.SqlClient | ServerConfig | FileSystem.FileSystem | AnalyticsService
| SqlClient.SqlClient
| ServerConfig
| ServerSettingsService
| FileSystem.FileSystem
| AnalyticsService
> {
return Effect.gen(function* () {
const { providerEventLogPath } = yield* ServerConfig;
+182
View File
@@ -0,0 +1,182 @@
import * as NodeServices from "@effect/platform-node/NodeServices";
import { DEFAULT_SERVER_SETTINGS, ServerSettingsPatch } from "@t3tools/contracts";
import { assert, it } from "@effect/vitest";
import { Effect, FileSystem, Layer, Schema } from "effect";
import { ServerConfig } from "./config";
import { ServerSettingsLive, ServerSettingsService } from "./serverSettings";
const makeServerSettingsLayer = () =>
ServerSettingsLive.pipe(
Layer.provideMerge(
Layer.fresh(
ServerConfig.layerTest(process.cwd(), {
prefix: "t3code-server-settings-test-",
}),
),
),
);
it.layer(NodeServices.layer)("server settings", (it) => {
it.effect("decodes nested settings patches", () =>
Effect.sync(() => {
const decodePatch = Schema.decodeUnknownSync(ServerSettingsPatch);
assert.deepEqual(decodePatch({ providers: { codex: { binaryPath: "/tmp/codex" } } }), {
providers: { codex: { binaryPath: "/tmp/codex" } },
});
assert.deepEqual(
decodePatch({
textGenerationModelSelection: {
options: {
fastMode: false,
},
},
}),
{
textGenerationModelSelection: {
options: {
fastMode: false,
},
},
},
);
}),
);
it.effect("deep merges nested settings updates without dropping siblings", () =>
Effect.gen(function* () {
const serverSettings = yield* ServerSettingsService;
yield* serverSettings.updateSettings({
providers: {
codex: {
binaryPath: "/usr/local/bin/codex",
homePath: "/Users/julius/.codex",
},
claudeAgent: {
binaryPath: "/usr/local/bin/claude",
customModels: ["claude-custom"],
},
},
textGenerationModelSelection: {
provider: "codex",
model: DEFAULT_SERVER_SETTINGS.textGenerationModelSelection.model,
options: {
reasoningEffort: "high",
fastMode: true,
},
},
});
const next = yield* serverSettings.updateSettings({
providers: {
codex: {
binaryPath: "/opt/homebrew/bin/codex",
},
},
textGenerationModelSelection: {
options: {
fastMode: false,
},
},
});
assert.deepEqual(next.providers.codex, {
enabled: true,
binaryPath: "/opt/homebrew/bin/codex",
homePath: "/Users/julius/.codex",
customModels: [],
});
assert.deepEqual(next.providers.claudeAgent, {
enabled: true,
binaryPath: "/usr/local/bin/claude",
customModels: ["claude-custom"],
});
assert.deepEqual(next.textGenerationModelSelection, {
provider: "codex",
model: DEFAULT_SERVER_SETTINGS.textGenerationModelSelection.model,
options: {
reasoningEffort: "high",
fastMode: false,
},
});
}).pipe(Effect.provide(makeServerSettingsLayer())),
);
it.effect("trims provider path settings when updates are applied", () =>
Effect.gen(function* () {
const serverSettings = yield* ServerSettingsService;
const next = yield* serverSettings.updateSettings({
providers: {
codex: {
binaryPath: " /opt/homebrew/bin/codex ",
homePath: " ",
},
claudeAgent: {
binaryPath: " /opt/homebrew/bin/claude ",
},
},
});
assert.deepEqual(next.providers.codex, {
enabled: true,
binaryPath: "/opt/homebrew/bin/codex",
homePath: "",
customModels: [],
});
assert.deepEqual(next.providers.claudeAgent, {
enabled: true,
binaryPath: "/opt/homebrew/bin/claude",
customModels: [],
});
}).pipe(Effect.provide(makeServerSettingsLayer())),
);
it.effect("defaults blank binary paths to provider executables", () =>
Effect.gen(function* () {
const serverSettings = yield* ServerSettingsService;
const next = yield* serverSettings.updateSettings({
providers: {
codex: {
binaryPath: " ",
},
claudeAgent: {
binaryPath: "",
},
},
});
assert.equal(next.providers.codex.binaryPath, "codex");
assert.equal(next.providers.claudeAgent.binaryPath, "claude");
}).pipe(Effect.provide(makeServerSettingsLayer())),
);
it.effect("writes only non-default server settings to disk", () =>
Effect.gen(function* () {
const serverSettings = yield* ServerSettingsService;
const serverConfig = yield* ServerConfig;
const fileSystem = yield* FileSystem.FileSystem;
const next = yield* serverSettings.updateSettings({
providers: {
codex: {
binaryPath: "/opt/homebrew/bin/codex",
},
},
});
assert.equal(next.providers.codex.binaryPath, "/opt/homebrew/bin/codex");
const raw = yield* fileSystem.readFileString(serverConfig.settingsPath);
assert.deepEqual(JSON.parse(raw), {
providers: {
codex: {
binaryPath: "/opt/homebrew/bin/codex",
},
},
});
}).pipe(Effect.provide(makeServerSettingsLayer())),
);
});
+326
View File
@@ -0,0 +1,326 @@
/**
* ServerSettings - Server-authoritative settings service.
*
* Owns persistence, validation, and change notification of settings that affect
* server-side behavior (binary paths, streaming mode, env mode, custom models,
* text generation model selection).
*
* Follows the same pattern as `keybindings.ts`: JSON file + Cache + PubSub +
* Semaphore + FileSystem.watch for concurrency and external edit detection.
*
* @module ServerSettings
*/
import {
DEFAULT_GIT_TEXT_GENERATION_MODEL_BY_PROVIDER,
DEFAULT_SERVER_SETTINGS,
type ModelSelection,
type ProviderKind,
ServerSettings,
type ServerSettingsPatch,
} from "@t3tools/contracts";
import {
Cache,
Deferred,
Duration,
Effect,
Exit,
FileSystem,
Layer,
Path,
PubSub,
Ref,
Schema,
SchemaIssue,
Scope,
ServiceMap,
Stream,
} from "effect";
import * as Semaphore from "effect/Semaphore";
import { ServerConfig } from "./config";
import { deepMerge } from "@t3tools/shared/Struct";
import { fromLenientJson } from "@t3tools/shared/schemaJson";
export class ServerSettingsError extends Schema.TaggedErrorClass<ServerSettingsError>()(
"ServerSettingsError",
{
settingsPath: Schema.String,
detail: Schema.String,
cause: Schema.optional(Schema.Defect),
},
) {
override get message(): string {
return `Server settings error at ${this.settingsPath}: ${this.detail}`;
}
}
export interface ServerSettingsShape {
/** Start the settings runtime and attach file watching. */
readonly start: Effect.Effect<void, ServerSettingsError>;
/** Await settings runtime readiness. */
readonly ready: Effect.Effect<void, ServerSettingsError>;
/** Read the current settings. */
readonly getSettings: Effect.Effect<ServerSettings, ServerSettingsError>;
/** Patch settings and persist. Returns the new full settings object. */
readonly updateSettings: (
patch: ServerSettingsPatch,
) => Effect.Effect<ServerSettings, ServerSettingsError>;
/** Stream of settings change events. */
readonly streamChanges: Stream.Stream<ServerSettings>;
}
export class ServerSettingsService extends ServiceMap.Service<
ServerSettingsService,
ServerSettingsShape
>()("t3/serverSettings/ServerSettingsService") {
static readonly layerTest = (overrides: Partial<ServerSettings> = {}) =>
Layer.succeed(ServerSettingsService, {
start: Effect.void,
ready: Effect.void,
getSettings: Effect.succeed({ ...DEFAULT_SERVER_SETTINGS, ...overrides }),
updateSettings: () => Effect.succeed({ ...DEFAULT_SERVER_SETTINGS, ...overrides }),
streamChanges: Stream.empty,
} satisfies ServerSettingsShape);
}
const ServerSettingsJson = fromLenientJson(ServerSettings);
const PROVIDER_ORDER: readonly ProviderKind[] = ["codex", "claudeAgent"];
/**
* Ensure the `textGenerationModelSelection` points to an enabled provider.
* If the selected provider is disabled, fall back to the first enabled
* provider with its default model. This is applied at read-time so the
* persisted preference is preserved for when a provider is re-enabled.
*/
function resolveTextGenerationProvider(settings: ServerSettings): ServerSettings {
const selection = settings.textGenerationModelSelection;
if (settings.providers[selection.provider].enabled) {
return settings;
}
const fallback = PROVIDER_ORDER.find((p) => settings.providers[p].enabled);
if (!fallback) {
// No providers enabled — return as-is; callers will report the error.
return settings;
}
return {
...settings,
textGenerationModelSelection: {
provider: fallback,
model: DEFAULT_GIT_TEXT_GENERATION_MODEL_BY_PROVIDER[fallback],
} as ModelSelection,
};
}
function stripDefaultServerSettings(current: unknown, defaults: unknown): unknown | undefined {
if (Array.isArray(current) || Array.isArray(defaults)) {
return JSON.stringify(current) === JSON.stringify(defaults) ? undefined : current;
}
if (
current !== null &&
defaults !== null &&
typeof current === "object" &&
typeof defaults === "object"
) {
const currentRecord = current as Record<string, unknown>;
const defaultsRecord = defaults as Record<string, unknown>;
const next: Record<string, unknown> = {};
for (const key of Object.keys(currentRecord)) {
const stripped = stripDefaultServerSettings(currentRecord[key], defaultsRecord[key]);
if (stripped !== undefined) {
next[key] = stripped;
}
}
return Object.keys(next).length > 0 ? next : undefined;
}
return Object.is(current, defaults) ? undefined : current;
}
const makeServerSettings = Effect.gen(function* () {
const { settingsPath } = yield* ServerConfig;
const fs = yield* FileSystem.FileSystem;
const pathService = yield* Path.Path;
const writeSemaphore = yield* Semaphore.make(1);
const cacheKey = "settings" as const;
const changesPubSub = yield* PubSub.unbounded<ServerSettings>();
const startedRef = yield* Ref.make(false);
const startedDeferred = yield* Deferred.make<void, ServerSettingsError>();
const watcherScope = yield* Scope.make("sequential");
yield* Effect.addFinalizer(() => Scope.close(watcherScope, Exit.void));
const emitChange = (settings: ServerSettings) =>
PubSub.publish(changesPubSub, settings).pipe(Effect.asVoid);
const readConfigExists = fs.exists(settingsPath).pipe(
Effect.mapError(
(cause) =>
new ServerSettingsError({
settingsPath,
detail: "failed to check settings file existence",
cause,
}),
),
);
const readRawConfig = fs.readFileString(settingsPath).pipe(
Effect.mapError(
(cause) =>
new ServerSettingsError({
settingsPath,
detail: "failed to read settings file",
cause,
}),
),
);
const loadSettingsFromDisk = Effect.gen(function* () {
if (!(yield* readConfigExists)) {
return DEFAULT_SERVER_SETTINGS;
}
const raw = yield* readRawConfig;
const decoded = Schema.decodeUnknownExit(ServerSettingsJson)(raw);
if (decoded._tag === "Failure") {
yield* Effect.logWarning("failed to parse settings.json, using defaults", {
path: settingsPath,
});
return DEFAULT_SERVER_SETTINGS;
}
return decoded.value;
});
const settingsCache = yield* Cache.make<typeof cacheKey, ServerSettings, ServerSettingsError>({
capacity: 1,
lookup: () => loadSettingsFromDisk,
});
const getSettingsFromCache = Cache.get(settingsCache, cacheKey);
const writeSettingsAtomically = (settings: ServerSettings) => {
const tempPath = `${settingsPath}.${process.pid}.${Date.now()}.tmp`;
const sparseSettings = stripDefaultServerSettings(settings, DEFAULT_SERVER_SETTINGS) ?? {};
return Effect.succeed(`${JSON.stringify(sparseSettings, null, 2)}\n`).pipe(
Effect.tap(() => fs.makeDirectory(pathService.dirname(settingsPath), { recursive: true })),
Effect.tap((encoded) => fs.writeFileString(tempPath, encoded)),
Effect.flatMap(() => fs.rename(tempPath, settingsPath)),
Effect.mapError(
(cause) =>
new ServerSettingsError({
settingsPath,
detail: "failed to write settings file",
cause,
}),
),
);
};
const revalidateAndEmit = writeSemaphore.withPermits(1)(
Effect.gen(function* () {
yield* Cache.invalidate(settingsCache, cacheKey);
const settings = yield* getSettingsFromCache;
yield* emitChange(settings);
}),
);
const startWatcher = Effect.gen(function* () {
const settingsDir = pathService.dirname(settingsPath);
const settingsFile = pathService.basename(settingsPath);
const settingsPathResolved = pathService.resolve(settingsPath);
yield* fs.makeDirectory(settingsDir, { recursive: true }).pipe(
Effect.mapError(
(cause) =>
new ServerSettingsError({
settingsPath,
detail: "failed to prepare settings directory",
cause,
}),
),
);
const revalidateAndEmitSafely = revalidateAndEmit.pipe(Effect.ignoreCause({ log: true }));
// Debounce watch events so the file is fully written before we read it.
// Editors emit multiple events per save (truncate, write, rename) and
// `fs.watch` can fire before the content has been flushed to disk.
const debouncedSettingsEvents = fs.watch(settingsDir).pipe(
Stream.filter((event) => {
return (
event.path === settingsFile ||
event.path === settingsPath ||
pathService.resolve(settingsDir, event.path) === settingsPathResolved
);
}),
Stream.debounce(Duration.millis(100)),
);
yield* Stream.runForEach(debouncedSettingsEvents, () => revalidateAndEmitSafely).pipe(
Effect.ignoreCause({ log: true }),
Effect.forkIn(watcherScope),
Effect.asVoid,
);
});
const start = Effect.gen(function* () {
const alreadyStarted = yield* Ref.get(startedRef);
if (alreadyStarted) {
return yield* Deferred.await(startedDeferred);
}
yield* Ref.set(startedRef, true);
const startup = Effect.gen(function* () {
yield* startWatcher;
yield* Cache.invalidate(settingsCache, cacheKey);
yield* getSettingsFromCache;
});
const startupExit = yield* Effect.exit(startup);
if (startupExit._tag === "Failure") {
yield* Deferred.failCause(startedDeferred, startupExit.cause).pipe(Effect.orDie);
return yield* Effect.failCause(startupExit.cause);
}
yield* Deferred.succeed(startedDeferred, undefined).pipe(Effect.orDie);
});
return {
start,
ready: Deferred.await(startedDeferred),
getSettings: getSettingsFromCache.pipe(Effect.map(resolveTextGenerationProvider)),
updateSettings: (patch) =>
writeSemaphore.withPermits(1)(
Effect.gen(function* () {
const current = yield* getSettingsFromCache;
const next = yield* Schema.decodeEffect(ServerSettings)(deepMerge(current, patch)).pipe(
Effect.mapError(
(cause) =>
new ServerSettingsError({
settingsPath: "<memory>",
detail: `failed to normalize server settings: ${SchemaIssue.makeFormatterDefault()(cause.issue)}`,
cause,
}),
),
);
yield* writeSettingsAtomically(next);
yield* Cache.set(settingsCache, cacheKey, next);
yield* emitChange(next);
return resolveTextGenerationProvider(next);
}),
),
get streamChanges() {
return Stream.fromPubSub(changesPubSub).pipe(Stream.map(resolveTextGenerationProvider));
},
} satisfies ServerSettingsShape;
});
export const ServerSettingsLive = Layer.effect(ServerSettingsService, makeServerSettings);
+31 -19
View File
@@ -13,18 +13,20 @@ import { makeServerProviderLayer, makeServerRuntimeServicesLayer } from "./serve
import {
DEFAULT_TERMINAL_ID,
DEFAULT_SERVER_SETTINGS,
EDITORS,
EventId,
ORCHESTRATION_WS_CHANNELS,
ORCHESTRATION_WS_METHODS,
ProviderItemId,
type ServerSettings,
ThreadId,
TurnId,
WS_CHANNELS,
WS_METHODS,
type WebSocketResponse,
type ProviderRuntimeEvent,
type ServerProviderStatus,
type ServerProvider,
type KeybindingsConfig,
type ResolvedKeybindingsConfig,
type WsPushChannel,
@@ -45,7 +47,7 @@ import { TerminalManager, type TerminalManagerShape } from "./terminal/Services/
import { makeSqlitePersistenceLive, SqlitePersistenceMemory } from "./persistence/Layers/Sqlite";
import { SqlClient, SqlError } from "effect/unstable/sql";
import { ProviderService, type ProviderServiceShape } from "./provider/Services/ProviderService";
import { ProviderHealth, type ProviderHealthShape } from "./provider/Services/ProviderHealth";
import { ProviderRegistry, type ProviderRegistryShape } from "./provider/Services/ProviderRegistry";
import { Open, type OpenShape } from "./open";
import { GitManager, type GitManagerShape } from "./git/Services/GitManager.ts";
import type { GitCoreShape } from "./git/Services/GitCore.ts";
@@ -53,6 +55,7 @@ import { GitCore } from "./git/Services/GitCore.ts";
import { GitCommandError, GitManagerError } from "./git/Errors.ts";
import { MigrationError } from "@effect/sql-sqlite-bun/SqliteMigrator";
import { AnalyticsService } from "./telemetry/Services/AnalyticsService.ts";
import { ServerSettingsService } from "./serverSettings.ts";
const asEventId = (value: string): EventId => EventId.makeUnsafe(value);
const asProviderItemId = (value: string): ProviderItemId => ProviderItemId.makeUnsafe(value);
@@ -64,20 +67,27 @@ const defaultOpenService: OpenShape = {
openInEditor: () => Effect.void,
};
const defaultProviderStatuses: ReadonlyArray<ServerProviderStatus> = [
const defaultProviderStatuses: ReadonlyArray<ServerProvider> = [
{
provider: "codex",
enabled: true,
installed: true,
version: "0.116.0",
status: "ready",
available: true,
authStatus: "authenticated",
checkedAt: "2026-01-01T00:00:00.000Z",
models: [],
},
];
const defaultProviderHealthService: ProviderHealthShape = {
getStatuses: Effect.succeed(defaultProviderStatuses),
const defaultProviderRegistryService: ProviderRegistryShape = {
getProviders: Effect.succeed(defaultProviderStatuses),
refresh: () => Effect.succeed(defaultProviderStatuses),
streamChanges: Stream.empty,
};
const defaultServerSettings = DEFAULT_SERVER_SETTINGS;
class MockTerminalManager implements TerminalManagerShape {
private readonly sessions = new Map<string, TerminalSessionSnapshot>();
private readonly listeners = new Set<(event: TerminalEvent) => void>();
@@ -487,11 +497,12 @@ describe("WebSocket Server", () => {
baseDir?: string;
staticDir?: string;
providerLayer?: Layer.Layer<ProviderService, never>;
providerHealth?: ProviderHealthShape;
providerRegistry?: ProviderRegistryShape;
open?: OpenShape;
gitManager?: GitManagerShape;
gitCore?: Pick<GitCoreShape, "listBranches" | "initRepo" | "pullCurrentBranch">;
terminalManager?: TerminalManagerShape;
serverSettings?: Partial<ServerSettings>;
} = {},
): Promise<Http.Server> {
if (serverScope) {
@@ -504,9 +515,9 @@ describe("WebSocket Server", () => {
const scope = await Effect.runPromise(Scope.make("sequential"));
const persistenceLayer = options.persistenceLayer ?? SqlitePersistenceMemory;
const providerLayer = options.providerLayer ?? makeServerProviderLayer();
const providerHealthLayer = Layer.succeed(
ProviderHealth,
options.providerHealth ?? defaultProviderHealthService,
const providerRegistryLayer = Layer.succeed(
ProviderRegistry,
options.providerRegistry ?? defaultProviderRegistryService,
);
const openLayer = Layer.succeed(Open, options.open ?? defaultOpenService);
const serverConfigLayer = Layer.succeed(ServerConfig, {
@@ -543,8 +554,9 @@ describe("WebSocket Server", () => {
);
const dependenciesLayer = Layer.empty.pipe(
Layer.provideMerge(runtimeLayer),
Layer.provideMerge(providerHealthLayer),
Layer.provideMerge(providerRegistryLayer),
Layer.provideMerge(openLayer),
Layer.provideMerge(ServerSettingsService.layerTest(options.serverSettings)),
Layer.provideMerge(serverConfigLayer),
Layer.provideMerge(AnalyticsService.layerTest),
Layer.provideMerge(NodeServices.layer),
@@ -858,6 +870,7 @@ describe("WebSocket Server", () => {
issues: [],
providers: defaultProviderStatuses,
availableEditors: expect.any(Array),
settings: defaultServerSettings,
});
expectAvailableEditors((response.result as { availableEditors: unknown }).availableEditors);
});
@@ -883,6 +896,7 @@ describe("WebSocket Server", () => {
issues: [],
providers: defaultProviderStatuses,
availableEditors: expect.any(Array),
settings: defaultServerSettings,
});
expectAvailableEditors((response.result as { availableEditors: unknown }).availableEditors);
@@ -919,6 +933,7 @@ describe("WebSocket Server", () => {
],
providers: defaultProviderStatuses,
availableEditors: expect.any(Array),
settings: defaultServerSettings,
});
expectAvailableEditors((response.result as { availableEditors: unknown }).availableEditors);
expect(fs.readFileSync(keybindingsPath, "utf8")).toBe("{ not-json");
@@ -952,7 +967,7 @@ describe("WebSocket Server", () => {
keybindingsConfigPath: string;
keybindings: ResolvedKeybindingsConfig;
issues: Array<{ kind: string; index?: number; message: string }>;
providers: ReadonlyArray<ServerProviderStatus>;
providers: ReadonlyArray<ServerProvider>;
availableEditors: unknown;
};
expect(result.cwd).toBe("/my/workspace");
@@ -1000,7 +1015,6 @@ describe("WebSocket Server", () => {
);
expect(malformedPush.data).toEqual({
issues: [{ kind: "keybindings.malformed-config", message: expect.any(String) }],
providers: defaultProviderStatuses,
});
const successPush = await rewriteKeybindingsAndWaitForPush(
@@ -1009,7 +1023,7 @@ describe("WebSocket Server", () => {
"[]",
(push) => Array.isArray(push.data.issues) && push.data.issues.length === 0,
);
expect(successPush.data).toEqual({ issues: [], providers: defaultProviderStatuses });
expect(successPush.data).toEqual({ issues: [] });
});
it("routes shell.openInEditor through the injected open service", async () => {
@@ -1069,6 +1083,7 @@ describe("WebSocket Server", () => {
issues: [],
providers: defaultProviderStatuses,
availableEditors: expect.any(Array),
settings: defaultServerSettings,
});
expectAvailableEditors((response.result as { availableEditors: unknown }).availableEditors);
});
@@ -1117,6 +1132,7 @@ describe("WebSocket Server", () => {
issues: [],
providers: defaultProviderStatuses,
availableEditors: expect.any(Array),
settings: defaultServerSettings,
});
expectAvailableEditors(
(configResponse.result as { availableEditors: unknown }).availableEditors,
@@ -1273,6 +1289,7 @@ describe("WebSocket Server", () => {
server = await createTestServer({
cwd: "/test",
providerLayer,
serverSettings: { enableAssistantStreaming: true },
});
const addr = server.address();
const port = typeof addr === "object" && addr !== null ? addr.port : 0;
@@ -1323,7 +1340,6 @@ describe("WebSocket Server", () => {
text: "hello",
attachments: [],
},
assistantDeliveryMode: "streaming",
runtimeMode: "approval-required",
interactionMode: "default",
createdAt,
@@ -1850,10 +1866,6 @@ describe("WebSocket Server", () => {
actionId: "client-action-1",
cwd: "/test",
action: "commit_push",
modelSelection: {
provider: "codex",
model: "gpt-5.4-mini",
},
},
expect.objectContaining({
actionId: "client-action-1",
+43 -7
View File
@@ -49,12 +49,13 @@ import { createLogger } from "./logger";
import { GitManager } from "./git/Services/GitManager.ts";
import { TerminalManager } from "./terminal/Services/Manager.ts";
import { Keybindings } from "./keybindings";
import { ServerSettingsService } from "./serverSettings";
import { searchWorkspaceEntries } from "./workspaceEntries";
import { OrchestrationEngineService } from "./orchestration/Services/OrchestrationEngine";
import { ProjectionSnapshotQuery } from "./orchestration/Services/ProjectionSnapshotQuery";
import { OrchestrationReactor } from "./orchestration/Services/OrchestrationReactor";
import { ProviderService } from "./provider/Services/ProviderService";
import { ProviderHealth } from "./provider/Services/ProviderHealth";
import { ProviderRegistry } from "./provider/Services/ProviderRegistry";
import { CheckpointDiffQuery } from "./checkpointing/Services/CheckpointDiffQuery";
import { clamp } from "effect/Number";
import { Open, resolveAvailableEditors } from "./open";
@@ -208,7 +209,7 @@ export type ServerCoreRuntimeServices =
| CheckpointDiffQuery
| OrchestrationReactor
| ProviderService
| ProviderHealth;
| ProviderRegistry;
export type ServerRuntimeServices =
| ServerCoreRuntimeServices
@@ -216,6 +217,7 @@ export type ServerRuntimeServices =
| GitCore
| TerminalManager
| Keybindings
| ServerSettingsService
| Open
| AnalyticsService;
@@ -253,7 +255,8 @@ export const createServer = Effect.fn(function* (): Effect.fn.Return<
const gitManager = yield* GitManager;
const terminalManager = yield* TerminalManager;
const keybindingsManager = yield* Keybindings;
const providerHealth = yield* ProviderHealth;
const serverSettingsManager = yield* ServerSettingsService;
const providerRegistry = yield* ProviderRegistry;
const git = yield* GitCore;
const fileSystem = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
@@ -268,7 +271,7 @@ export const createServer = Effect.fn(function* (): Effect.fn.Return<
),
);
const providerStatuses = yield* providerHealth.getStatuses;
const providersRef = yield* Ref.make(yield* providerRegistry.getProviders);
const clients = yield* Ref.make(new Set<WebSocket>());
const logger = createLogger("ws");
@@ -295,6 +298,11 @@ export const createServer = Effect.fn(function* (): Effect.fn.Return<
),
);
yield* readiness.markKeybindingsReady;
yield* serverSettingsManager.start.pipe(
Effect.mapError(
(cause) => new ServerLifecycleError({ operation: "serverSettingsRuntimeStart", cause }),
),
);
const normalizeDispatchCommand = Effect.fnUntraced(function* (input: {
readonly command: ClientOrchestrationCommand;
@@ -614,7 +622,22 @@ export const createServer = Effect.fn(function* (): Effect.fn.Return<
yield* Stream.runForEach(keybindingsManager.streamChanges, (event) =>
pushBus.publishAll(WS_CHANNELS.serverConfigUpdated, {
issues: event.issues,
providers: providerStatuses,
}),
).pipe(Effect.forkIn(subscriptionsScope));
yield* Stream.runForEach(serverSettingsManager.streamChanges, (settings) =>
pushBus.publishAll(WS_CHANNELS.serverConfigUpdated, {
issues: [],
settings,
}),
).pipe(Effect.forkIn(subscriptionsScope));
yield* Stream.runForEach(providerRegistry.streamChanges, (providers) =>
Effect.gen(function* () {
yield* Ref.set(providersRef, providers);
yield* pushBus.publishAll(WS_CHANNELS.serverProvidersUpdated, {
providers,
});
}),
).pipe(Effect.forkIn(subscriptionsScope));
@@ -878,16 +901,20 @@ export const createServer = Effect.fn(function* (): Effect.fn.Return<
return yield* terminalManager.close(body);
}
case WS_METHODS.serverGetConfig:
case WS_METHODS.serverGetConfig: {
const keybindingsConfig = yield* keybindingsManager.loadConfigState;
const settings = yield* serverSettingsManager.getSettings;
const providers = yield* Ref.get(providersRef);
return {
cwd,
keybindingsConfigPath,
keybindings: keybindingsConfig.keybindings,
issues: keybindingsConfig.issues,
providers: providerStatuses,
providers,
availableEditors,
settings,
};
}
case WS_METHODS.serverUpsertKeybinding: {
const body = stripRequestTag(request.body);
@@ -895,6 +922,15 @@ export const createServer = Effect.fn(function* (): Effect.fn.Return<
return { keybindings: keybindingsConfig, issues: [] };
}
case WS_METHODS.serverGetSettings: {
return yield* serverSettingsManager.getSettings;
}
case WS_METHODS.serverUpdateSettings: {
const body = stripRequestTag(request.body);
return yield* serverSettingsManager.updateSettings(body.patch);
}
default: {
const _exhaustiveCheck: never = request.body;
return yield* new RouteRequestError({
-3
View File
@@ -53,7 +53,6 @@ describe("makeServerPushBus", () => {
yield* pushBus.publishAll(WS_CHANNELS.serverConfigUpdated, {
issues: [{ kind: "keybindings.malformed-config", message: "queued-before-connect" }],
providers: [],
});
const delivered = yield* pushBus.publishClient(
@@ -70,7 +69,6 @@ describe("makeServerPushBus", () => {
yield* pushBus.publishAll(WS_CHANNELS.serverConfigUpdated, {
issues: [],
providers: [],
});
yield* Effect.promise(() => client.waitForSentCount(2));
@@ -95,7 +93,6 @@ describe("makeServerPushBus", () => {
channel: WS_CHANNELS.serverConfigUpdated,
data: {
issues: [],
providers: [],
},
});
}),
-325
View File
@@ -1,325 +0,0 @@
import { Schema } from "effect";
import { describe, expect, it } from "vitest";
import {
AppSettingsSchema,
DEFAULT_SIDEBAR_PROJECT_SORT_ORDER,
DEFAULT_SIDEBAR_THREAD_SORT_ORDER,
DEFAULT_TIMESTAMP_FORMAT,
getProviderStartOptions,
} from "./appSettings";
import {
getAppModelOptions,
getCustomModelOptionsByProvider,
getCustomModelsByProvider,
getCustomModelsForProvider,
getDefaultCustomModelsForProvider,
MODEL_PROVIDER_SETTINGS,
normalizeCustomModelSlugs,
patchCustomModels,
resolveAppModelSelectionState,
resolveAppModelSelection,
} from "./modelSelection";
describe("normalizeCustomModelSlugs", () => {
it("normalizes aliases, removes built-ins, and deduplicates values", () => {
expect(
normalizeCustomModelSlugs([
" custom/internal-model ",
"gpt-5.3-codex",
"5.3",
"custom/internal-model",
"",
null,
]),
).toEqual(["custom/internal-model"]);
});
it("normalizes provider-specific aliases for claude", () => {
expect(normalizeCustomModelSlugs(["sonnet"], "claudeAgent")).toEqual([]);
expect(normalizeCustomModelSlugs(["claude/custom-sonnet"], "claudeAgent")).toEqual([
"claude/custom-sonnet",
]);
});
});
describe("getAppModelOptions", () => {
it("appends saved custom models after the built-in options", () => {
const options = getAppModelOptions("codex", ["custom/internal-model"]);
expect(options.map((option) => option.slug)).toEqual([
"gpt-5.4",
"gpt-5.4-mini",
"gpt-5.3-codex",
"gpt-5.3-codex-spark",
"gpt-5.2-codex",
"gpt-5.2",
"custom/internal-model",
]);
});
it("keeps the currently selected custom model available even if it is no longer saved", () => {
const options = getAppModelOptions("codex", [], "custom/selected-model");
expect(options.at(-1)).toEqual({
slug: "custom/selected-model",
name: "custom/selected-model",
isCustom: true,
});
});
it("keeps a saved custom provider model available as an exact slug option", () => {
const options = getAppModelOptions("claudeAgent", ["claude/custom-opus"], "claude/custom-opus");
expect(options.some((option) => option.slug === "claude/custom-opus" && option.isCustom)).toBe(
true,
);
});
});
describe("resolveAppModelSelection", () => {
it("preserves saved custom model slugs instead of falling back to the default", () => {
expect(
resolveAppModelSelection(
"codex",
{ codex: ["galapagos-alpha"], claudeAgent: [] },
"galapagos-alpha",
),
).toBe("galapagos-alpha");
});
it("falls back to the provider default when no model is selected", () => {
expect(resolveAppModelSelection("codex", { codex: [], claudeAgent: [] }, "")).toBe("gpt-5.4");
});
it("resolves display names through the shared resolver", () => {
expect(resolveAppModelSelection("codex", { codex: [], claudeAgent: [] }, "GPT-5.3 Codex")).toBe(
"gpt-5.3-codex",
);
});
it("resolves aliases through the shared resolver", () => {
expect(resolveAppModelSelection("claudeAgent", { codex: [], claudeAgent: [] }, "sonnet")).toBe(
"claude-sonnet-4-6",
);
});
it("resolves transient selected custom models included in app model options", () => {
expect(
resolveAppModelSelection("codex", { codex: [], claudeAgent: [] }, "custom/selected-model"),
).toBe("custom/selected-model");
});
});
describe("timestamp format defaults", () => {
it("defaults timestamp format to locale", () => {
expect(DEFAULT_TIMESTAMP_FORMAT).toBe("locale");
});
});
describe("sidebar sort defaults", () => {
it("defaults project sorting to updated_at", () => {
expect(DEFAULT_SIDEBAR_PROJECT_SORT_ORDER).toBe("updated_at");
});
it("defaults thread sorting to updated_at", () => {
expect(DEFAULT_SIDEBAR_THREAD_SORT_ORDER).toBe("updated_at");
});
});
describe("provider-specific custom models", () => {
it("includes provider-specific custom slugs in non-codex model lists", () => {
const claudeOptions = getAppModelOptions("claudeAgent", ["claude/custom-opus"]);
expect(claudeOptions.some((option) => option.slug === "claude/custom-opus")).toBe(true);
});
});
describe("getProviderStartOptions", () => {
it("returns only populated provider overrides", () => {
expect(
getProviderStartOptions({
claudeBinaryPath: "/usr/local/bin/claude",
codexBinaryPath: "",
codexHomePath: "/Users/you/.codex",
}),
).toEqual({
claudeAgent: {
binaryPath: "/usr/local/bin/claude",
},
codex: {
homePath: "/Users/you/.codex",
},
});
});
it("returns undefined when no provider overrides are configured", () => {
expect(
getProviderStartOptions({
claudeBinaryPath: "",
codexBinaryPath: "",
codexHomePath: "",
}),
).toBeUndefined();
});
});
describe("provider-indexed custom model settings", () => {
const settings = {
customCodexModels: ["custom/codex-model"],
customClaudeModels: ["claude/custom-opus"],
} as const;
it("exports one provider config per provider", () => {
expect(MODEL_PROVIDER_SETTINGS.map((config) => config.provider)).toEqual([
"codex",
"claudeAgent",
]);
});
it("reads custom models for each provider", () => {
expect(getCustomModelsForProvider(settings, "codex")).toEqual(["custom/codex-model"]);
expect(getCustomModelsForProvider(settings, "claudeAgent")).toEqual(["claude/custom-opus"]);
});
it("reads default custom models for each provider", () => {
const defaults = {
customCodexModels: ["default/codex-model"],
customClaudeModels: ["claude/default-opus"],
} as const;
expect(getDefaultCustomModelsForProvider(defaults, "codex")).toEqual(["default/codex-model"]);
expect(getDefaultCustomModelsForProvider(defaults, "claudeAgent")).toEqual([
"claude/default-opus",
]);
});
it("patches custom models for codex", () => {
expect(patchCustomModels("codex", ["custom/codex-model"])).toEqual({
customCodexModels: ["custom/codex-model"],
});
});
it("patches custom models for claude", () => {
expect(patchCustomModels("claudeAgent", ["claude/custom-opus"])).toEqual({
customClaudeModels: ["claude/custom-opus"],
});
});
it("builds a complete provider-indexed custom model record", () => {
expect(getCustomModelsByProvider(settings)).toEqual({
codex: ["custom/codex-model"],
claudeAgent: ["claude/custom-opus"],
});
});
it("builds provider-indexed model options including custom models", () => {
const modelOptionsByProvider = getCustomModelOptionsByProvider(settings);
expect(
modelOptionsByProvider.codex.some((option) => option.slug === "custom/codex-model"),
).toBe(true);
expect(
modelOptionsByProvider.claudeAgent.some((option) => option.slug === "claude/custom-opus"),
).toBe(true);
});
it("normalizes and deduplicates custom model options per provider", () => {
const modelOptionsByProvider = getCustomModelOptionsByProvider({
customCodexModels: [" custom/codex-model ", "gpt-5.4", "custom/codex-model"],
customClaudeModels: [" sonnet ", "claude/custom-opus", "claude/custom-opus"],
});
expect(
modelOptionsByProvider.codex.filter((option) => option.slug === "custom/codex-model"),
).toHaveLength(1);
expect(modelOptionsByProvider.codex.some((option) => option.slug === "gpt-5.4")).toBe(true);
expect(
modelOptionsByProvider.claudeAgent.filter((option) => option.slug === "claude/custom-opus"),
).toHaveLength(1);
expect(
modelOptionsByProvider.claudeAgent.some((option) => option.slug === "claude-sonnet-4-6"),
).toBe(true);
});
});
describe("AppSettingsSchema", () => {
it("fills decoding defaults for persisted settings that predate newer keys", () => {
const decode = Schema.decodeSync(Schema.fromJsonString(AppSettingsSchema));
expect(
decode(
JSON.stringify({
codexBinaryPath: "/usr/local/bin/codex",
confirmThreadDelete: false,
}),
),
).toMatchObject({
claudeBinaryPath: "",
codexBinaryPath: "/usr/local/bin/codex",
codexHomePath: "",
defaultThreadEnvMode: "local",
confirmThreadDelete: false,
enableAssistantStreaming: false,
sidebarProjectSortOrder: DEFAULT_SIDEBAR_PROJECT_SORT_ORDER,
sidebarThreadSortOrder: DEFAULT_SIDEBAR_THREAD_SORT_ORDER,
timestampFormat: DEFAULT_TIMESTAMP_FORMAT,
customCodexModels: [],
customClaudeModels: [],
});
});
});
describe("resolveAppModelSelectionState", () => {
it("falls back to the default git-writing codex selection", () => {
expect(
resolveAppModelSelectionState({
customCodexModels: [],
customClaudeModels: [],
textGenerationModelSelection: undefined,
}),
).toEqual({
provider: "codex",
model: "gpt-5.4-mini",
});
});
it("preserves the selected provider and resolves saved custom models", () => {
expect(
resolveAppModelSelectionState({
customCodexModels: [],
customClaudeModels: ["claude/custom-haiku"],
textGenerationModelSelection: {
provider: "claudeAgent",
model: "claude/custom-haiku",
},
}),
).toEqual({
provider: "claudeAgent",
model: "claude/custom-haiku",
});
});
it("normalizes provider options against the resolved model capabilities", () => {
expect(
resolveAppModelSelectionState({
customCodexModels: [],
customClaudeModels: [],
textGenerationModelSelection: {
provider: "claudeAgent",
model: "claude-haiku-4-5",
options: {
effort: "max",
thinking: false,
fastMode: true,
},
},
}),
).toEqual({
provider: "claudeAgent",
model: "claude-haiku-4-5",
options: {
thinking: false,
},
});
});
});
-121
View File
@@ -1,121 +0,0 @@
import { useCallback } from "react";
import { Option, Schema } from "effect";
import {
DEFAULT_GIT_TEXT_GENERATION_MODEL_BY_PROVIDER,
ModelSelection,
type ProviderStartOptions,
} from "@t3tools/contracts";
import { useLocalStorage } from "./hooks/useLocalStorage";
import { EnvMode } from "./components/BranchToolbar.logic";
import { normalizeCustomModelSlugs } from "./modelSelection";
const APP_SETTINGS_STORAGE_KEY = "t3code:app-settings:v1";
export const TimestampFormat = Schema.Literals(["locale", "12-hour", "24-hour"]);
export type TimestampFormat = typeof TimestampFormat.Type;
export const DEFAULT_TIMESTAMP_FORMAT: TimestampFormat = "locale";
export const SidebarProjectSortOrder = Schema.Literals(["updated_at", "created_at", "manual"]);
export type SidebarProjectSortOrder = typeof SidebarProjectSortOrder.Type;
export const DEFAULT_SIDEBAR_PROJECT_SORT_ORDER: SidebarProjectSortOrder = "updated_at";
export const SidebarThreadSortOrder = Schema.Literals(["updated_at", "created_at"]);
export type SidebarThreadSortOrder = typeof SidebarThreadSortOrder.Type;
export const DEFAULT_SIDEBAR_THREAD_SORT_ORDER: SidebarThreadSortOrder = "updated_at";
const withDefaults =
<
S extends Schema.Top & Schema.WithoutConstructorDefault,
D extends S["~type.make.in"] & S["Encoded"],
>(
fallback: () => D,
) =>
(schema: S) =>
schema.pipe(
Schema.withConstructorDefault(() => Option.some(fallback())),
Schema.withDecodingDefault(() => fallback()),
);
export const AppSettingsSchema = Schema.Struct({
claudeBinaryPath: Schema.String.check(Schema.isMaxLength(4096)).pipe(withDefaults(() => "")),
codexBinaryPath: Schema.String.check(Schema.isMaxLength(4096)).pipe(withDefaults(() => "")),
codexHomePath: Schema.String.check(Schema.isMaxLength(4096)).pipe(withDefaults(() => "")),
defaultThreadEnvMode: EnvMode.pipe(withDefaults(() => "local" as const satisfies EnvMode)),
confirmThreadDelete: Schema.Boolean.pipe(withDefaults(() => true)),
diffWordWrap: Schema.Boolean.pipe(withDefaults(() => false)),
enableAssistantStreaming: Schema.Boolean.pipe(withDefaults(() => false)),
sidebarProjectSortOrder: SidebarProjectSortOrder.pipe(
withDefaults(() => DEFAULT_SIDEBAR_PROJECT_SORT_ORDER),
),
sidebarThreadSortOrder: SidebarThreadSortOrder.pipe(
withDefaults(() => DEFAULT_SIDEBAR_THREAD_SORT_ORDER),
),
timestampFormat: TimestampFormat.pipe(withDefaults(() => DEFAULT_TIMESTAMP_FORMAT)),
customCodexModels: Schema.Array(Schema.String).pipe(withDefaults(() => [])),
customClaudeModels: Schema.Array(Schema.String).pipe(withDefaults(() => [])),
textGenerationModelSelection: ModelSelection.pipe(
withDefaults(() => ({
provider: "codex" as const,
model: DEFAULT_GIT_TEXT_GENERATION_MODEL_BY_PROVIDER.codex,
})),
),
});
export type AppSettings = typeof AppSettingsSchema.Type;
const DEFAULT_APP_SETTINGS = AppSettingsSchema.makeUnsafe({});
function normalizeAppSettings(settings: AppSettings): AppSettings {
return {
...settings,
customCodexModels: normalizeCustomModelSlugs(settings.customCodexModels, "codex"),
customClaudeModels: normalizeCustomModelSlugs(settings.customClaudeModels, "claudeAgent"),
};
}
export function getProviderStartOptions(
settings: Pick<AppSettings, "claudeBinaryPath" | "codexBinaryPath" | "codexHomePath">,
): ProviderStartOptions | undefined {
const providerOptions: ProviderStartOptions = {
...(settings.codexBinaryPath || settings.codexHomePath
? {
codex: {
...(settings.codexBinaryPath ? { binaryPath: settings.codexBinaryPath } : {}),
...(settings.codexHomePath ? { homePath: settings.codexHomePath } : {}),
},
}
: {}),
...(settings.claudeBinaryPath
? {
claudeAgent: {
binaryPath: settings.claudeBinaryPath,
},
}
: {}),
};
return Object.keys(providerOptions).length > 0 ? providerOptions : undefined;
}
export function useAppSettings() {
const [settings, setSettings] = useLocalStorage(
APP_SETTINGS_STORAGE_KEY,
DEFAULT_APP_SETTINGS,
AppSettingsSchema,
);
const updateSettings = useCallback(
(patch: Partial<AppSettings>) => {
setSettings((prev) => normalizeAppSettings({ ...prev, ...patch }));
},
[setSettings],
);
const resetSettings = useCallback(() => {
setSettings(DEFAULT_APP_SETTINGS);
}, [setSettings]);
return {
settings,
updateSettings,
resetSettings,
defaults: DEFAULT_APP_SETTINGS,
} as const;
}
+10 -1
View File
@@ -12,6 +12,7 @@ import {
WS_CHANNELS,
WS_METHODS,
OrchestrationSessionStatus,
DEFAULT_SERVER_SETTINGS,
} from "@t3tools/contracts";
import { RouterProvider, createMemoryHistory } from "@tanstack/react-router";
import { HttpResponse, http, ws } from "msw";
@@ -30,6 +31,7 @@ import { isMacPlatform } from "../lib/utils";
import { getRouter } from "../router";
import { useStore } from "../store";
import { estimateTimelineMessageHeight } from "./timelineHeight";
import { DEFAULT_CLIENT_SETTINGS } from "@t3tools/contracts/settings";
const THREAD_ID = "thread-browser-test" as ThreadId;
const UUID_ROUTE_RE = /^\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
@@ -110,13 +112,20 @@ function createBaseServerConfig(): ServerConfig {
providers: [
{
provider: "codex",
enabled: true,
installed: true,
version: "0.116.0",
status: "ready",
available: true,
authStatus: "authenticated",
checkedAt: NOW_ISO,
models: [],
},
],
availableEditors: [],
settings: {
...DEFAULT_SERVER_SETTINGS,
...DEFAULT_CLIENT_SETTINGS,
},
};
}
+77 -66
View File
@@ -13,7 +13,7 @@ import {
PROVIDER_SEND_TURN_MAX_ATTACHMENTS,
PROVIDER_SEND_TURN_MAX_IMAGE_BYTES,
type ResolvedKeybindingsConfig,
type ServerProviderStatus,
type ServerProvider,
type ThreadId,
type TurnId,
type EditorId,
@@ -22,11 +22,7 @@ import {
ProviderInteractionMode,
RuntimeMode,
} from "@t3tools/contracts";
import {
applyClaudePromptEffortPrefix,
getModelCapabilities,
normalizeModelSlug,
} from "@t3tools/shared/model";
import { applyClaudePromptEffortPrefix, normalizeModelSlug } from "@t3tools/shared/model";
import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from "react";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { useDebouncedValue } from "@tanstack/react-pacer";
@@ -120,12 +116,13 @@ import {
import { SidebarTrigger } from "./ui/sidebar";
import { newCommandId, newMessageId, newThreadId } from "~/lib/utils";
import { readNativeApi } from "~/nativeApi";
import { getProviderStartOptions, useAppSettings } from "../appSettings";
import {
getCustomModelOptionsByProvider,
getCustomModelsByProvider,
resolveAppModelSelection,
} from "../modelSelection";
getProviderModelCapabilities,
getProviderModels,
resolveSelectableProvider,
} from "../providerModels";
import { useSettings } from "../hooks/useSettings";
import { resolveAppModelSelection } from "../modelSelection";
import { isTerminalFocused } from "../lib/terminalFocus";
import {
type ComposerImageAttachment,
@@ -164,7 +161,7 @@ import {
renderProviderTraitsMenuContent,
renderProviderTraitsPicker,
} from "./chat/composerProviderRegistry";
import { ProviderHealthBanner } from "./chat/ProviderHealthBanner";
import { ProviderStatusBanner } from "./chat/ProviderStatusBanner";
import { ThreadErrorBanner } from "./chat/ThreadErrorBanner";
import {
buildExpiredTerminalContextToastCopy,
@@ -191,16 +188,17 @@ const EMPTY_ACTIVITIES: OrchestrationThreadActivity[] = [];
const EMPTY_KEYBINDINGS: ResolvedKeybindingsConfig = [];
const EMPTY_PROJECT_ENTRIES: ProjectEntry[] = [];
const EMPTY_AVAILABLE_EDITORS: EditorId[] = [];
const EMPTY_PROVIDER_STATUSES: ServerProviderStatus[] = [];
const EMPTY_PROVIDERS: ServerProvider[] = [];
const EMPTY_PENDING_USER_INPUT_ANSWERS: Record<string, PendingUserInputDraftAnswer> = {};
function formatOutgoingPrompt(params: {
provider: ProviderKind;
model: string | null;
models: ReadonlyArray<ServerProvider["models"][number]>;
effort: string | null;
text: string;
}): string {
const caps = getModelCapabilities(params.provider, params.model);
const caps = getProviderModelCapabilities(params.models, params.model, params.provider);
if (params.effort && caps.promptInjectedEffortLevels.includes(params.effort)) {
return applyClaudePromptEffortPrefix(params.text, params.effort as ClaudeCodeEffort | null);
}
@@ -249,7 +247,7 @@ export default function ChatView({ threadId }: ChatViewProps) {
const syncServerReadModel = useStore((store) => store.syncServerReadModel);
const setStoreThreadError = useStore((store) => store.setError);
const setStoreThreadBranch = useStore((store) => store.setThreadBranch);
const { settings } = useAppSettings();
const settings = useSettings();
const setStickyComposerModelSelection = useComposerDraftStore(
(store) => store.setStickyModelSelection,
);
@@ -608,25 +606,32 @@ export default function ChatView({ threadId }: ChatViewProps) {
const lockedProvider: ProviderKind | null = hasThreadStarted
? (sessionProvider ?? threadProvider ?? selectedProviderByThreadId ?? null)
: null;
const selectedProvider: ProviderKind =
lockedProvider ?? selectedProviderByThreadId ?? threadProvider ?? "codex";
const customModelsByProvider = useMemo(() => getCustomModelsByProvider(settings), [settings]);
const serverConfigQuery = useQuery(serverConfigQueryOptions());
const providerStatuses = serverConfigQuery.data?.providers ?? EMPTY_PROVIDERS;
const unlockedSelectedProvider = resolveSelectableProvider(
providerStatuses,
selectedProviderByThreadId ?? threadProvider ?? "codex",
);
const selectedProvider: ProviderKind = lockedProvider ?? unlockedSelectedProvider;
const { modelOptions: composerModelOptions, selectedModel } = useEffectiveComposerModelState({
threadId,
providers: providerStatuses,
selectedProvider,
threadModelSelection: activeThread?.modelSelection,
projectModelSelection: activeProject?.defaultModelSelection,
customModelsByProvider,
settings,
});
const selectedProviderModels = getProviderModels(providerStatuses, selectedProvider);
const composerProviderState = useMemo(
() =>
getComposerProviderState({
provider: selectedProvider,
model: selectedModel,
models: selectedProviderModels,
prompt,
modelOptions: composerModelOptions,
}),
[composerModelOptions, prompt, selectedModel, selectedProvider],
[composerModelOptions, prompt, selectedModel, selectedProvider, selectedProviderModels],
);
const selectedPromptEffort = composerProviderState.promptEffort;
const selectedModelOptionsForDispatch = composerProviderState.modelOptionsForDispatch;
@@ -638,35 +643,7 @@ export default function ChatView({ threadId }: ChatViewProps) {
}),
[selectedModel, selectedModelOptionsForDispatch, selectedProvider],
);
const providerOptionsForDispatch = useMemo(() => getProviderStartOptions(settings), [settings]);
const selectedModelForPicker = selectedModel;
const modelOptionsByProvider = useMemo(
() => getCustomModelOptionsByProvider(settings, selectedProvider, selectedModel),
[settings, selectedProvider, selectedModel],
);
const selectedModelForPickerWithCustomFallback = useMemo(() => {
const currentOptions = modelOptionsByProvider[selectedProvider];
return currentOptions.some((option) => option.slug === selectedModelForPicker)
? selectedModelForPicker
: (normalizeModelSlug(selectedModelForPicker, selectedProvider) ?? selectedModelForPicker);
}, [modelOptionsByProvider, selectedModelForPicker, selectedProvider]);
const searchableModelOptions = useMemo(
() =>
AVAILABLE_PROVIDER_OPTIONS.filter(
(option) => lockedProvider === null || option.value === lockedProvider,
).flatMap((option) =>
modelOptionsByProvider[option.value].map(({ slug, name }) => ({
provider: option.value,
providerLabel: option.label,
slug,
name,
searchSlug: slug.toLowerCase(),
searchName: name.toLowerCase(),
searchProvider: option.label.toLowerCase(),
})),
),
[lockedProvider, modelOptionsByProvider],
);
const phase = derivePhase(activeThread?.session ?? null);
const isSendBusy = sendPhase !== "idle";
const isPreparingWorktree = sendPhase === "preparing-worktree";
@@ -1029,7 +1006,39 @@ export default function ChatView({ threadId }: ChatViewProps) {
);
const effectivePathQuery = pathTriggerQuery.length > 0 ? debouncedPathQuery : "";
const branchesQuery = useQuery(gitBranchesQueryOptions(gitCwd));
const serverConfigQuery = useQuery(serverConfigQueryOptions());
const keybindings = serverConfigQuery.data?.keybindings ?? EMPTY_KEYBINDINGS;
const availableEditors = serverConfigQuery.data?.availableEditors ?? EMPTY_AVAILABLE_EDITORS;
const modelOptionsByProvider = useMemo(
() => ({
codex: providerStatuses.find((provider) => provider.provider === "codex")?.models ?? [],
claudeAgent:
providerStatuses.find((provider) => provider.provider === "claudeAgent")?.models ?? [],
}),
[providerStatuses],
);
const selectedModelForPickerWithCustomFallback = useMemo(() => {
const currentOptions = modelOptionsByProvider[selectedProvider];
return currentOptions.some((option) => option.slug === selectedModelForPicker)
? selectedModelForPicker
: (normalizeModelSlug(selectedModelForPicker, selectedProvider) ?? selectedModelForPicker);
}, [modelOptionsByProvider, selectedModelForPicker, selectedProvider]);
const searchableModelOptions = useMemo(
() =>
AVAILABLE_PROVIDER_OPTIONS.filter(
(option) => lockedProvider === null || option.value === lockedProvider,
).flatMap((option) =>
modelOptionsByProvider[option.value].map(({ slug, name }) => ({
provider: option.value,
providerLabel: option.label,
slug,
name,
searchSlug: slug.toLowerCase(),
searchName: name.toLowerCase(),
searchProvider: option.label.toLowerCase(),
})),
),
[lockedProvider, modelOptionsByProvider],
);
const workspaceEntriesQuery = useQuery(
projectSearchEntriesQueryOptions({
cwd: gitCwd,
@@ -1117,9 +1126,6 @@ export default function ChatView({ threadId }: ChatViewProps) {
() => new Set(nonPersistedComposerImageIds),
[nonPersistedComposerImageIds],
);
const keybindings = serverConfigQuery.data?.keybindings ?? EMPTY_KEYBINDINGS;
const availableEditors = serverConfigQuery.data?.availableEditors ?? EMPTY_AVAILABLE_EDITORS;
const providerStatuses = serverConfigQuery.data?.providers ?? EMPTY_PROVIDER_STATUSES;
const activeProviderStatus = useMemo(
() => providerStatuses.find((status) => status.provider === selectedProvider) ?? null,
[selectedProvider, providerStatuses],
@@ -2460,6 +2466,7 @@ export default function ChatView({ threadId }: ChatViewProps) {
const outgoingMessageText = formatOutgoingPrompt({
provider: selectedProvider,
model: selectedModel,
models: selectedProviderModels,
effort: selectedPromptEffort,
text: messageTextForSend || IMAGE_ONLY_BOOTSTRAP_PROMPT,
});
@@ -2645,8 +2652,6 @@ export default function ChatView({ threadId }: ChatViewProps) {
attachments: turnAttachments,
},
modelSelection: selectedModelSelection,
...(providerOptionsForDispatch ? { providerOptions: providerOptionsForDispatch } : {}),
assistantDeliveryMode: settings.enableAssistantStreaming ? "streaming" : "buffered",
runtimeMode,
interactionMode,
createdAt: messageCreatedAt,
@@ -2883,6 +2888,7 @@ export default function ChatView({ threadId }: ChatViewProps) {
const outgoingMessageText = formatOutgoingPrompt({
provider: selectedProvider,
model: selectedModel,
models: selectedProviderModels,
effort: selectedPromptEffort,
text: trimmed,
});
@@ -2927,8 +2933,6 @@ export default function ChatView({ threadId }: ChatViewProps) {
attachments: [],
},
modelSelection: selectedModelSelection,
...(providerOptionsForDispatch ? { providerOptions: providerOptionsForDispatch } : {}),
assistantDeliveryMode: settings.enableAssistantStreaming ? "streaming" : "buffered",
runtimeMode,
interactionMode: nextInteractionMode,
...(nextInteractionMode === "default" && activeProposedPlan
@@ -2974,11 +2978,10 @@ export default function ChatView({ threadId }: ChatViewProps) {
runtimeMode,
selectedPromptEffort,
selectedModelSelection,
providerOptionsForDispatch,
selectedProvider,
selectedProviderModels,
setComposerDraftInteractionMode,
setThreadError,
settings.enableAssistantStreaming,
selectedModel,
],
);
@@ -3005,6 +3008,7 @@ export default function ChatView({ threadId }: ChatViewProps) {
const outgoingImplementationPrompt = formatOutgoingPrompt({
provider: selectedProvider,
model: selectedModel,
models: selectedProviderModels,
effort: selectedPromptEffort,
text: implementationPrompt,
});
@@ -3044,8 +3048,6 @@ export default function ChatView({ threadId }: ChatViewProps) {
attachments: [],
},
modelSelection: selectedModelSelection,
...(providerOptionsForDispatch ? { providerOptions: providerOptionsForDispatch } : {}),
assistantDeliveryMode: settings.enableAssistantStreaming ? "streaming" : "buffered",
runtimeMode,
interactionMode: "default",
createdAt,
@@ -3096,9 +3098,8 @@ export default function ChatView({ threadId }: ChatViewProps) {
runtimeMode,
selectedPromptEffort,
selectedModelSelection,
providerOptionsForDispatch,
selectedProvider,
settings.enableAssistantStreaming,
selectedProviderModels,
syncServerReadModel,
selectedModel,
]);
@@ -3110,9 +3111,15 @@ export default function ChatView({ threadId }: ChatViewProps) {
scheduleComposerFocus();
return;
}
const resolvedModel = resolveAppModelSelection(provider, customModelsByProvider, model);
const resolvedProvider = resolveSelectableProvider(providerStatuses, provider);
const resolvedModel = resolveAppModelSelection(
resolvedProvider,
settings,
providerStatuses,
model,
);
const nextModelSelection: ModelSelection = {
provider,
provider: resolvedProvider,
model: resolvedModel,
};
setComposerDraftModelSelection(activeThread.id, nextModelSelection);
@@ -3125,7 +3132,8 @@ export default function ChatView({ threadId }: ChatViewProps) {
scheduleComposerFocus,
setComposerDraftModelSelection,
setStickyComposerModelSelection,
customModelsByProvider,
providerStatuses,
settings,
],
);
const setPromptFromTraits = useCallback(
@@ -3148,6 +3156,7 @@ export default function ChatView({ threadId }: ChatViewProps) {
provider: selectedProvider,
threadId,
model: selectedModel,
models: selectedProviderModels,
modelOptions: composerModelOptions?.[selectedProvider],
prompt,
onPromptChange: setPromptFromTraits,
@@ -3156,6 +3165,7 @@ export default function ChatView({ threadId }: ChatViewProps) {
provider: selectedProvider,
threadId,
model: selectedModel,
models: selectedProviderModels,
modelOptions: composerModelOptions?.[selectedProvider],
prompt,
onPromptChange: setPromptFromTraits,
@@ -3519,7 +3529,7 @@ export default function ChatView({ threadId }: ChatViewProps) {
</header>
{/* Error banner */}
<ProviderHealthBanner status={activeProviderStatus} />
<ProviderStatusBanner status={activeProviderStatus} />
<ThreadErrorBanner
error={activeThread.error}
onDismiss={() => setThreadError(activeThread.id, null)}
@@ -3796,6 +3806,7 @@ export default function ChatView({ threadId }: ChatViewProps) {
provider={selectedProvider}
model={selectedModelForPickerWithCustomFallback}
lockedProvider={lockedProvider}
providers={providerStatuses}
modelOptionsByProvider={modelOptionsByProvider}
{...(composerProviderState.modelPickerIconClassName
? {
+2 -2
View File
@@ -30,7 +30,7 @@ import { buildPatchCacheKey } from "../lib/diffRendering";
import { resolveDiffThemeName } from "../lib/diffRendering";
import { useTurnDiffSummaries } from "../hooks/useTurnDiffSummaries";
import { useStore } from "../store";
import { useAppSettings } from "../appSettings";
import { useSettings } from "../hooks/useSettings";
import { formatShortTimestamp } from "../timestampFormat";
import { DiffPanelLoadingState, DiffPanelShell, type DiffPanelMode } from "./DiffPanelShell";
import { ToggleGroup, Toggle } from "./ui/toggle-group";
@@ -166,7 +166,7 @@ export { DiffWorkerPoolProvider } from "./DiffWorkerPoolProvider";
export default function DiffPanel({ mode = "inline" }: DiffPanelProps) {
const navigate = useNavigate();
const { resolvedTheme } = useTheme();
const { settings } = useAppSettings();
const settings = useSettings();
const [diffRenderMode, setDiffRenderMode] = useState<DiffRenderMode>("stacked");
const [diffWordWrap, setDiffWordWrap] = useState(settings.diffWordWrap);
const patchViewportRef = useRef<HTMLDivElement>(null);
@@ -20,7 +20,6 @@ import {
resolveQuickAction,
summarizeGitResult,
} from "./GitActionsControl.logic";
import { useAppSettings } from "~/appSettings";
import { Button } from "~/components/ui/button";
import { Checkbox } from "~/components/ui/checkbox";
import {
@@ -205,7 +204,6 @@ function GitQuickActionIcon({ quickAction }: { quickAction: GitQuickAction }) {
}
export default function GitActionsControl({ gitCwd, activeThreadId }: GitActionsControlProps) {
const { settings } = useAppSettings();
const threadToastData = useMemo(
() => (activeThreadId ? { threadId: activeThreadId } : undefined),
[activeThreadId],
@@ -261,7 +259,6 @@ export default function GitActionsControl({ gitCwd, activeThreadId }: GitActions
gitRunStackedActionMutationOptions({
cwd: gitCwd,
queryClient,
modelSelection: settings.textGenerationModelSelection,
}),
);
const pullMutation = useMutation(gitPullMutationOptions({ cwd: gitCwd, queryClient }));
@@ -46,13 +46,25 @@ function createBaseServerConfig(): ServerConfig {
providers: [
{
provider: "codex",
enabled: true,
installed: true,
version: "0.116.0",
status: "ready",
available: true,
authStatus: "authenticated",
checkedAt: NOW_ISO,
models: [],
},
],
availableEditors: [],
settings: {
enableAssistantStreaming: false,
defaultThreadEnvMode: "local" as const,
textGenerationModelSelection: { provider: "codex" as const, model: "gpt-5.4-mini" },
providers: {
codex: { enabled: true, binaryPath: "", homePath: "", customModels: [] },
claudeAgent: { enabled: true, binaryPath: "", customModels: [] },
},
},
};
}
+1 -1
View File
@@ -1,5 +1,5 @@
import { memo, useState, useCallback } from "react";
import { type TimestampFormat } from "../appSettings";
import { type TimestampFormat } from "@t3tools/contracts/settings";
import { Badge } from "./ui/badge";
import { Button } from "./ui/button";
import { ScrollArea } from "./ui/scroll-area";
+1 -1
View File
@@ -1,4 +1,4 @@
import type { SidebarProjectSortOrder, SidebarThreadSortOrder } from "../appSettings";
import type { SidebarProjectSortOrder, SidebarThreadSortOrder } from "@t3tools/contracts/settings";
import type { Thread } from "../types";
import { cn } from "../lib/utils";
import {
+4 -3
View File
@@ -41,8 +41,7 @@ import { useLocation, useNavigate, useParams } from "@tanstack/react-router";
import {
type SidebarProjectSortOrder,
type SidebarThreadSortOrder,
useAppSettings,
} from "../appSettings";
} from "@t3tools/contracts/settings";
import { isElectron } from "../env";
import { APP_STAGE_LABEL, APP_VERSION } from "../branding";
import { isLinuxPlatform, isMacPlatform, newCommandId, newProjectId } from "../lib/utils";
@@ -102,6 +101,7 @@ import {
sortThreadsForSidebar,
} from "./Sidebar.logic";
import { useCopyToClipboard } from "~/hooks/useCopyToClipboard";
import { useSettings, useUpdateSettings } from "~/hooks/useSettings";
const EMPTY_KEYBINDINGS: ResolvedKeybindingsConfig = [];
const THREAD_PREVIEW_LIMIT = 6;
@@ -381,7 +381,8 @@ export default function Sidebar() {
);
const navigate = useNavigate();
const isOnSettings = useLocation({ select: (loc) => loc.pathname === "/settings" });
const { settings: appSettings, updateSettings } = useAppSettings();
const appSettings = useSettings();
const { updateSettings } = useUpdateSettings();
const { handleNewThread } = useHandleNewThread();
const routeThreadId = useParams({
strict: false,
@@ -43,6 +43,59 @@ async function mountMenu(props?: { modelSelection?: ModelSelection; prompt?: str
document.body.append(host);
const onPromptChange = vi.fn();
const providerOptions = props?.modelSelection?.options;
const models =
provider === "claudeAgent"
? [
{
slug: "claude-opus-4-6",
name: "Claude Opus 4.6",
isCustom: false,
capabilities: {
reasoningEffortLevels: [
{ value: "low", label: "Low" },
{ value: "medium", label: "Medium" },
{ value: "high", label: "High", isDefault: true },
{ value: "max", label: "Max" },
{ value: "ultrathink", label: "Ultrathink" },
],
supportsFastMode: true,
supportsThinkingToggle: false,
promptInjectedEffortLevels: ["ultrathink"],
},
},
{
slug: "claude-sonnet-4-6",
name: "Claude Sonnet 4.6",
isCustom: false,
capabilities: {
reasoningEffortLevels: [
{ value: "low", label: "Low" },
{ value: "medium", label: "Medium" },
{ value: "high", label: "High", isDefault: true },
{ value: "ultrathink", label: "Ultrathink" },
],
supportsFastMode: false,
supportsThinkingToggle: false,
promptInjectedEffortLevels: ["ultrathink"],
},
},
]
: [
{
slug: "gpt-5.4",
name: "GPT-5.4",
isCustom: false,
capabilities: {
reasoningEffortLevels: [
{ value: "xhigh", label: "Extra High" },
{ value: "high", label: "High", isDefault: true },
],
supportsFastMode: true,
supportsThinkingToggle: false,
promptInjectedEffortLevels: [],
},
},
];
const screen = await render(
<CompactComposerControlsMenu
activePlan={false}
@@ -52,6 +105,7 @@ async function mountMenu(props?: { modelSelection?: ModelSelection; prompt?: str
traitsMenuContent={
<TraitsMenuContent
provider={provider}
models={models}
threadId={threadId}
model={model}
prompt={props?.prompt ?? ""}
@@ -48,7 +48,7 @@ import {
type ParsedTerminalContextEntry,
} from "~/lib/terminalContext";
import { cn } from "~/lib/utils";
import { type TimestampFormat } from "../../appSettings";
import { type TimestampFormat } from "@t3tools/contracts/settings";
import { formatTimestamp } from "../../timestampFormat";
import {
buildInlineTerminalContextText,
@@ -1,37 +1,134 @@
import { type ModelSlug, type ProviderKind } from "@t3tools/contracts";
import { type ModelSlug, type ProviderKind, type ServerProvider } from "@t3tools/contracts";
import { page } from "vitest/browser";
import { afterEach, describe, expect, it, vi } from "vitest";
import { render } from "vitest-browser-react";
import { ProviderModelPicker } from "./ProviderModelPicker";
import { getCustomModelOptionsByProvider } from "../../modelSelection";
import { DEFAULT_UNIFIED_SETTINGS } from "@t3tools/contracts/settings";
const MODEL_OPTIONS_BY_PROVIDER = {
claudeAgent: [
{ slug: "claude-opus-4-6", name: "Claude Opus 4.6" },
{ slug: "claude-sonnet-4-6", name: "Claude Sonnet 4.6" },
{ slug: "claude-haiku-4-5", name: "Claude Haiku 4.5" },
],
codex: [
{ slug: "gpt-5-codex", name: "GPT-5 Codex" },
{ slug: "gpt-5.3-codex", name: "GPT-5.3 Codex" },
],
} as const satisfies Record<ProviderKind, ReadonlyArray<{ slug: ModelSlug; name: string }>>;
function effort(value: string, isDefault = false) {
return {
value,
label: value,
...(isDefault ? { isDefault: true } : {}),
};
}
const TEST_PROVIDERS: ReadonlyArray<ServerProvider> = [
{
provider: "codex",
enabled: true,
installed: true,
version: "0.116.0",
status: "ready",
authStatus: "authenticated",
checkedAt: new Date().toISOString(),
models: [
{
slug: "gpt-5-codex",
name: "GPT-5 Codex",
isCustom: false,
capabilities: {
reasoningEffortLevels: [effort("low"), effort("medium", true), effort("high")],
supportsFastMode: true,
supportsThinkingToggle: false,
promptInjectedEffortLevels: [],
},
},
{
slug: "gpt-5.3-codex",
name: "GPT-5.3 Codex",
isCustom: false,
capabilities: {
reasoningEffortLevels: [effort("low"), effort("medium", true), effort("high")],
supportsFastMode: true,
supportsThinkingToggle: false,
promptInjectedEffortLevels: [],
},
},
],
},
{
provider: "claudeAgent",
enabled: true,
installed: true,
version: "1.0.0",
status: "ready",
authStatus: "authenticated",
checkedAt: new Date().toISOString(),
models: [
{
slug: "claude-opus-4-6",
name: "Claude Opus 4.6",
isCustom: false,
capabilities: {
reasoningEffortLevels: [
effort("low"),
effort("medium", true),
effort("high"),
effort("max"),
],
supportsFastMode: false,
supportsThinkingToggle: true,
promptInjectedEffortLevels: [],
},
},
{
slug: "claude-sonnet-4-6",
name: "Claude Sonnet 4.6",
isCustom: false,
capabilities: {
reasoningEffortLevels: [
effort("low"),
effort("medium", true),
effort("high"),
effort("max"),
],
supportsFastMode: false,
supportsThinkingToggle: true,
promptInjectedEffortLevels: [],
},
},
{
slug: "claude-haiku-4-5",
name: "Claude Haiku 4.5",
isCustom: false,
capabilities: {
reasoningEffortLevels: [effort("low"), effort("medium", true), effort("high")],
supportsFastMode: false,
supportsThinkingToggle: true,
promptInjectedEffortLevels: [],
},
},
],
},
];
async function mountPicker(props: {
provider: ProviderKind;
model: ModelSlug;
lockedProvider: ProviderKind | null;
providers?: ReadonlyArray<ServerProvider>;
triggerVariant?: "ghost" | "outline";
}) {
const host = document.createElement("div");
document.body.append(host);
const onProviderModelChange = vi.fn();
const providers = props.providers ?? TEST_PROVIDERS;
const modelOptionsByProvider = getCustomModelOptionsByProvider(
DEFAULT_UNIFIED_SETTINGS,
providers,
props.provider,
props.model,
);
const screen = await render(
<ProviderModelPicker
provider={props.provider}
model={props.model}
lockedProvider={props.lockedProvider}
modelOptionsByProvider={MODEL_OPTIONS_BY_PROVIDER}
providers={providers}
modelOptionsByProvider={modelOptionsByProvider}
triggerVariant={props.triggerVariant}
onProviderModelChange={onProviderModelChange}
/>,
@@ -159,6 +256,39 @@ describe("ProviderModelPicker", () => {
}
});
it("shows disabled providers as non-selectable entries", async () => {
const disabledProviders = TEST_PROVIDERS.slice();
const claudeIndex = disabledProviders.findIndex(
(provider) => provider.provider === "claudeAgent",
);
if (claudeIndex >= 0) {
const claudeProvider = disabledProviders[claudeIndex]!;
disabledProviders[claudeIndex] = {
...claudeProvider,
enabled: false,
};
}
const mounted = await mountPicker({
provider: "codex",
model: "gpt-5-codex",
lockedProvider: null,
providers: disabledProviders,
});
try {
await page.getByRole("button").click();
await vi.waitFor(() => {
const text = document.body.textContent ?? "";
expect(text).toContain("Claude");
expect(text).toContain("Disabled");
expect(text).not.toContain("Claude Sonnet 4.6");
});
} finally {
await mounted.cleanup();
}
});
it("accepts outline trigger styling", async () => {
const mounted = await mountPicker({
provider: "codex",
@@ -1,4 +1,4 @@
import { type ModelSlug, type ProviderKind } from "@t3tools/contracts";
import { type ModelSlug, type ProviderKind, type ServerProvider } from "@t3tools/contracts";
import { resolveSelectableModel } from "@t3tools/shared/model";
import { memo, useState } from "react";
import type { VariantProps } from "class-variance-authority";
@@ -20,6 +20,7 @@ import {
} from "../ui/menu";
import { ClaudeAI, CursorIcon, Gemini, Icon, OpenAI, OpenCodeIcon } from "../Icons";
import { cn } from "~/lib/utils";
import { getProviderSnapshot } from "../../providerModels";
function isAvailableProviderOption(option: (typeof PROVIDER_OPTIONS)[number]): option is {
value: ProviderKind;
@@ -53,6 +54,7 @@ export const ProviderModelPicker = memo(function ProviderModelPicker(props: {
provider: ProviderKind;
model: ModelSlug;
lockedProvider: ProviderKind | null;
providers?: ReadonlyArray<ServerProvider>;
modelOptionsByProvider: Record<ProviderKind, ReadonlyArray<{ slug: string; name: string }>>;
activeProviderIconClassName?: string;
compact?: boolean;
@@ -145,6 +147,31 @@ export const ProviderModelPicker = memo(function ProviderModelPicker(props: {
<>
{AVAILABLE_PROVIDER_OPTIONS.map((option) => {
const OptionIcon = PROVIDER_ICON_BY_PROVIDER[option.value];
const liveProvider = props.providers
? getProviderSnapshot(props.providers, option.value)
: undefined;
if (liveProvider && liveProvider.status !== "ready") {
const unavailableLabel = !liveProvider.enabled
? "Disabled"
: !liveProvider.installed
? "Not installed"
: "Unavailable";
return (
<MenuItem key={option.value} disabled>
<OptionIcon
aria-hidden="true"
className={cn(
"size-4 shrink-0 opacity-80",
providerIconClassName(option.value, "text-muted-foreground/85"),
)}
/>
<span>{option.label}</span>
<span className="ms-auto text-[11px] text-muted-foreground/80 uppercase tracking-[0.08em]">
{unavailableLabel}
</span>
</MenuItem>
);
}
return (
<MenuSub key={option.value}>
<MenuSubTrigger>
@@ -1,14 +1,14 @@
import { PROVIDER_DISPLAY_NAMES, type ServerProviderStatus } from "@t3tools/contracts";
import { PROVIDER_DISPLAY_NAMES, type ServerProvider } from "@t3tools/contracts";
import { memo } from "react";
import { Alert, AlertDescription, AlertTitle } from "../ui/alert";
import { CircleAlertIcon } from "lucide-react";
export const ProviderHealthBanner = memo(function ProviderHealthBanner({
export const ProviderStatusBanner = memo(function ProviderStatusBanner({
status,
}: {
status: ServerProviderStatus | null;
status: ServerProvider | null;
}) {
if (!status || status.status === "ready") {
if (!status || status.status === "ready" || status.status === "disabled") {
return null;
}
@@ -5,7 +5,9 @@ import {
ClaudeModelOptions,
CodexModelOptions,
DEFAULT_MODEL_BY_PROVIDER,
DEFAULT_SERVER_SETTINGS,
ProjectId,
type ServerProvider,
ThreadId,
} from "@t3tools/contracts";
import { page } from "vitest/browser";
@@ -21,10 +23,93 @@ import {
useComposerThreadDraft,
useEffectiveComposerModelState,
} from "../../composerDraftStore";
import { DEFAULT_CLIENT_SETTINGS } from "@t3tools/contracts/settings";
// ── Claude TraitsPicker tests ─────────────────────────────────────────
const CLAUDE_THREAD_ID = ThreadId.makeUnsafe("thread-claude-traits");
const TEST_PROVIDERS: ReadonlyArray<ServerProvider> = [
{
provider: "codex",
enabled: true,
installed: true,
version: "0.1.0",
status: "ready",
authStatus: "authenticated",
checkedAt: "2026-01-01T00:00:00.000Z",
models: [
{
slug: "gpt-5.4",
name: "GPT-5.4",
isCustom: false,
capabilities: {
reasoningEffortLevels: [
{ value: "xhigh", label: "Extra High" },
{ value: "high", label: "High", isDefault: true },
],
supportsFastMode: true,
supportsThinkingToggle: false,
promptInjectedEffortLevels: [],
},
},
],
},
{
provider: "claudeAgent",
enabled: true,
installed: true,
version: "0.1.0",
status: "ready",
authStatus: "authenticated",
checkedAt: "2026-01-01T00:00:00.000Z",
models: [
{
slug: "claude-opus-4-6",
name: "Claude Opus 4.6",
isCustom: false,
capabilities: {
reasoningEffortLevels: [
{ value: "low", label: "Low" },
{ value: "medium", label: "Medium" },
{ value: "high", label: "High", isDefault: true },
{ value: "max", label: "Max" },
{ value: "ultrathink", label: "Ultrathink" },
],
supportsFastMode: true,
supportsThinkingToggle: false,
promptInjectedEffortLevels: ["ultrathink"],
},
},
{
slug: "claude-sonnet-4-6",
name: "Claude Sonnet 4.6",
isCustom: false,
capabilities: {
reasoningEffortLevels: [
{ value: "low", label: "Low" },
{ value: "medium", label: "Medium" },
{ value: "high", label: "High", isDefault: true },
{ value: "ultrathink", label: "Ultrathink" },
],
supportsFastMode: false,
supportsThinkingToggle: false,
promptInjectedEffortLevels: ["ultrathink"],
},
},
{
slug: "claude-haiku-4-5",
name: "Claude Haiku 4.5",
isCustom: false,
capabilities: {
reasoningEffortLevels: [],
supportsFastMode: false,
supportsThinkingToggle: true,
promptInjectedEffortLevels: [],
},
},
],
},
];
function ClaudeTraitsPickerHarness(props: {
model: string;
@@ -35,10 +120,14 @@ function ClaudeTraitsPickerHarness(props: {
const setPrompt = useComposerDraftStore((store) => store.setPrompt);
const { modelOptions, selectedModel } = useEffectiveComposerModelState({
threadId: CLAUDE_THREAD_ID,
providers: TEST_PROVIDERS,
selectedProvider: "claudeAgent",
threadModelSelection: props.fallbackModelSelection,
projectModelSelection: null,
customModelsByProvider: { codex: [], claudeAgent: [] },
settings: {
...DEFAULT_SERVER_SETTINGS,
...DEFAULT_CLIENT_SETTINGS,
},
});
const handlePromptChange = useCallback(
(nextPrompt: string) => {
@@ -50,6 +139,7 @@ function ClaudeTraitsPickerHarness(props: {
return (
<TraitsPicker
provider="claudeAgent"
models={TEST_PROVIDERS[1]!.models}
threadId={CLAUDE_THREAD_ID}
model={selectedModel ?? props.model}
prompt={prompt}
@@ -109,7 +199,7 @@ async function mountClaudePicker(props?: {
? ({
provider: "claudeAgent",
model,
options: props.fallbackModelOptions ?? undefined,
...(props.fallbackModelOptions ? { options: props.fallbackModelOptions } : {}),
} satisfies ModelSelection)
: null;
const screen = await render(
@@ -293,6 +383,7 @@ async function mountCodexPicker(props: { model?: string; options?: CodexModelOpt
const screen = await render(
<TraitsPicker
provider="codex"
models={TEST_PROVIDERS[0]!.models}
threadId={threadId}
model={props.model ?? DEFAULT_MODEL_BY_PROVIDER.codex}
prompt=""
+10 -4
View File
@@ -3,11 +3,11 @@ import {
type CodexModelOptions,
type ProviderKind,
type ProviderModelOptions,
type ServerProviderModel,
type ThreadId,
} from "@t3tools/contracts";
import {
applyClaudePromptEffortPrefix,
getModelCapabilities,
isClaudeUltrathinkPrompt,
trimOrNull,
getDefaultEffort,
@@ -27,6 +27,7 @@ import {
MenuTrigger,
} from "../ui/menu";
import { useComposerDraftStore } from "../../composerDraftStore";
import { getProviderModelCapabilities } from "../../providerModels";
import { cn } from "~/lib/utils";
type ProviderOptions = ProviderModelOptions[ProviderKind];
@@ -65,12 +66,13 @@ function buildNextOptions(
function getSelectedTraits(
provider: ProviderKind,
models: ReadonlyArray<ServerProviderModel>,
model: string | null | undefined,
prompt: string,
modelOptions: ProviderOptions | null | undefined,
allowPromptInjectedEffort: boolean,
) {
const caps = getModelCapabilities(provider, model);
const caps = getProviderModelCapabilities(models, model, provider);
const effortLevels = allowPromptInjectedEffort
? caps.reasoningEffortLevels
: caps.reasoningEffortLevels.filter(
@@ -120,6 +122,7 @@ function getSelectedTraits(
export interface TraitsMenuContentProps {
provider: ProviderKind;
models: ReadonlyArray<ServerProviderModel>;
model: string | null | undefined;
prompt: string;
onPromptChange: (prompt: string) => void;
@@ -131,6 +134,7 @@ export interface TraitsMenuContentProps {
export const TraitsMenuContent = memo(function TraitsMenuContentImpl({
provider,
models,
model,
prompt,
onPromptChange,
@@ -156,7 +160,7 @@ export const TraitsMenuContent = memo(function TraitsMenuContentImpl({
thinkingEnabled,
fastModeEnabled,
ultrathinkPromptControlled,
} = getSelectedTraits(provider, model, prompt, modelOptions, allowPromptInjectedEffort);
} = getSelectedTraits(provider, models, model, prompt, modelOptions, allowPromptInjectedEffort);
const defaultEffort = getDefaultEffort(caps);
const handleEffortChange = useCallback(
@@ -260,6 +264,7 @@ export const TraitsMenuContent = memo(function TraitsMenuContentImpl({
export const TraitsPicker = memo(function TraitsPicker({
provider,
models,
model,
prompt,
onPromptChange,
@@ -277,7 +282,7 @@ export const TraitsPicker = memo(function TraitsPicker({
thinkingEnabled,
fastModeEnabled,
ultrathinkPromptControlled,
} = getSelectedTraits(provider, model, prompt, modelOptions, allowPromptInjectedEffort);
} = getSelectedTraits(provider, models, model, prompt, modelOptions, allowPromptInjectedEffort);
const effortLabel = effort
? (effortLevels.find((l) => l.value === effort)?.label ?? effort)
@@ -333,6 +338,7 @@ export const TraitsPicker = memo(function TraitsPicker({
<MenuPopup align="start">
<TraitsMenuContent
provider={provider}
models={models}
model={model}
prompt={prompt}
onPromptChange={onPromptChange}
@@ -1,11 +1,78 @@
import { describe, expect, it } from "vitest";
import type { ServerProviderModel } from "@t3tools/contracts";
import { getComposerProviderState } from "./composerProviderRegistry";
const CODEX_MODELS: ReadonlyArray<ServerProviderModel> = [
{
slug: "gpt-5.4",
name: "GPT-5.4",
isCustom: false,
capabilities: {
reasoningEffortLevels: [
{ value: "xhigh", label: "Extra High" },
{ value: "high", label: "High", isDefault: true },
{ value: "medium", label: "Medium" },
{ value: "low", label: "Low" },
],
supportsFastMode: true,
supportsThinkingToggle: false,
promptInjectedEffortLevels: [],
},
},
];
const CLAUDE_MODELS: ReadonlyArray<ServerProviderModel> = [
{
slug: "claude-opus-4-6",
name: "Claude Opus 4.6",
isCustom: false,
capabilities: {
reasoningEffortLevels: [
{ value: "medium", label: "Medium" },
{ value: "high", label: "High", isDefault: true },
{ value: "max", label: "Max" },
{ value: "ultrathink", label: "Ultrathink" },
],
supportsFastMode: true,
supportsThinkingToggle: false,
promptInjectedEffortLevels: ["ultrathink"],
},
},
{
slug: "claude-sonnet-4-6",
name: "Claude Sonnet 4.6",
isCustom: false,
capabilities: {
reasoningEffortLevels: [
{ value: "low", label: "Low" },
{ value: "medium", label: "Medium" },
{ value: "high", label: "High", isDefault: true },
{ value: "ultrathink", label: "Ultrathink" },
],
supportsFastMode: false,
supportsThinkingToggle: false,
promptInjectedEffortLevels: ["ultrathink"],
},
},
{
slug: "claude-haiku-4-5",
name: "Claude Haiku 4.5",
isCustom: false,
capabilities: {
reasoningEffortLevels: [],
supportsFastMode: false,
supportsThinkingToggle: true,
promptInjectedEffortLevels: [],
},
},
];
describe("getComposerProviderState", () => {
it("returns codex defaults when no codex draft options exist", () => {
const state = getComposerProviderState({
provider: "codex",
model: "gpt-5.4",
models: CODEX_MODELS,
prompt: "",
modelOptions: undefined,
});
@@ -21,6 +88,7 @@ describe("getComposerProviderState", () => {
const state = getComposerProviderState({
provider: "codex",
model: "gpt-5.4",
models: CODEX_MODELS,
prompt: "",
modelOptions: {
codex: {
@@ -44,6 +112,7 @@ describe("getComposerProviderState", () => {
const state = getComposerProviderState({
provider: "codex",
model: "gpt-5.4",
models: CODEX_MODELS,
prompt: "",
modelOptions: {
codex: {
@@ -65,6 +134,7 @@ describe("getComposerProviderState", () => {
const state = getComposerProviderState({
provider: "codex",
model: "gpt-5.4",
models: CODEX_MODELS,
prompt: "",
modelOptions: {
codex: {
@@ -85,6 +155,7 @@ describe("getComposerProviderState", () => {
const state = getComposerProviderState({
provider: "claudeAgent",
model: "claude-sonnet-4-6",
models: CLAUDE_MODELS,
prompt: "",
modelOptions: undefined,
});
@@ -100,6 +171,7 @@ describe("getComposerProviderState", () => {
const state = getComposerProviderState({
provider: "claudeAgent",
model: "claude-sonnet-4-6",
models: CLAUDE_MODELS,
prompt: "Ultrathink:\nInvestigate this failure",
modelOptions: {
claudeAgent: {
@@ -124,6 +196,7 @@ describe("getComposerProviderState", () => {
const state = getComposerProviderState({
provider: "claudeAgent",
model: "claude-haiku-4-5",
models: CLAUDE_MODELS,
prompt: "",
modelOptions: {
claudeAgent: {
@@ -146,6 +219,7 @@ describe("getComposerProviderState", () => {
const state = getComposerProviderState({
provider: "claudeAgent",
model: "claude-opus-4-6",
models: CLAUDE_MODELS,
prompt: "",
modelOptions: {
claudeAgent: {
@@ -167,6 +241,7 @@ describe("getComposerProviderState", () => {
const state = getComposerProviderState({
provider: "claudeAgent",
model: "claude-opus-4-6",
models: CLAUDE_MODELS,
prompt: "",
modelOptions: {
claudeAgent: {
@@ -2,23 +2,27 @@ import {
type ModelSlug,
type ProviderKind,
type ProviderModelOptions,
type ServerProviderModel,
type ThreadId,
} from "@t3tools/contracts";
import {
getModelCapabilities,
isClaudeUltrathinkPrompt,
normalizeClaudeModelOptions,
normalizeCodexModelOptions,
trimOrNull,
getDefaultEffort,
hasEffortLevel,
} from "@t3tools/shared/model";
import type { ReactNode } from "react";
import {
getProviderModelCapabilities,
normalizeClaudeModelOptionsWithCapabilities,
normalizeCodexModelOptionsWithCapabilities,
} from "../../providerModels";
import { TraitsMenuContent, TraitsPicker } from "./TraitsPicker";
export type ComposerProviderStateInput = {
provider: ProviderKind;
model: ModelSlug;
models: ReadonlyArray<ServerProviderModel>;
prompt: string;
modelOptions: ProviderModelOptions | null | undefined;
};
@@ -37,6 +41,7 @@ type ProviderRegistryEntry = {
renderTraitsMenuContent: (input: {
threadId: ThreadId;
model: ModelSlug;
models: ReadonlyArray<ServerProviderModel>;
modelOptions: ProviderModelOptions[ProviderKind] | undefined;
prompt: string;
onPromptChange: (prompt: string) => void;
@@ -44,6 +49,7 @@ type ProviderRegistryEntry = {
renderTraitsPicker: (input: {
threadId: ThreadId;
model: ModelSlug;
models: ReadonlyArray<ServerProviderModel>;
modelOptions: ProviderModelOptions[ProviderKind] | undefined;
prompt: string;
onPromptChange: (prompt: string) => void;
@@ -53,8 +59,8 @@ type ProviderRegistryEntry = {
function getProviderStateFromCapabilities(
input: ComposerProviderStateInput,
): ComposerProviderState {
const { provider, model, prompt, modelOptions } = input;
const caps = getModelCapabilities(provider, model);
const { provider, model, models, prompt, modelOptions } = input;
const caps = getProviderModelCapabilities(models, model, provider);
const providerOptions = modelOptions?.[provider];
// Resolve effort
@@ -81,8 +87,8 @@ function getProviderStateFromCapabilities(
// Normalize options for dispatch
const normalizedOptions =
provider === "codex"
? normalizeCodexModelOptions(model, providerOptions)
: normalizeClaudeModelOptions(model, providerOptions);
? normalizeCodexModelOptionsWithCapabilities(caps, providerOptions)
: normalizeClaudeModelOptionsWithCapabilities(caps, providerOptions);
// Ultrathink styling (driven by capabilities data, not provider identity)
const ultrathinkActive =
@@ -103,9 +109,17 @@ function getProviderStateFromCapabilities(
const composerProviderRegistry: Record<ProviderKind, ProviderRegistryEntry> = {
codex: {
getState: (input) => getProviderStateFromCapabilities(input),
renderTraitsMenuContent: ({ threadId, model, modelOptions, prompt, onPromptChange }) => (
renderTraitsMenuContent: ({
threadId,
model,
models,
modelOptions,
prompt,
onPromptChange,
}) => (
<TraitsMenuContent
provider="codex"
models={models}
threadId={threadId}
model={model}
modelOptions={modelOptions}
@@ -113,9 +127,10 @@ const composerProviderRegistry: Record<ProviderKind, ProviderRegistryEntry> = {
onPromptChange={onPromptChange}
/>
),
renderTraitsPicker: ({ threadId, model, modelOptions, prompt, onPromptChange }) => (
renderTraitsPicker: ({ threadId, model, models, modelOptions, prompt, onPromptChange }) => (
<TraitsPicker
provider="codex"
models={models}
threadId={threadId}
model={model}
modelOptions={modelOptions}
@@ -126,9 +141,17 @@ const composerProviderRegistry: Record<ProviderKind, ProviderRegistryEntry> = {
},
claudeAgent: {
getState: (input) => getProviderStateFromCapabilities(input),
renderTraitsMenuContent: ({ threadId, model, modelOptions, prompt, onPromptChange }) => (
renderTraitsMenuContent: ({
threadId,
model,
models,
modelOptions,
prompt,
onPromptChange,
}) => (
<TraitsMenuContent
provider="claudeAgent"
models={models}
threadId={threadId}
model={model}
modelOptions={modelOptions}
@@ -136,9 +159,10 @@ const composerProviderRegistry: Record<ProviderKind, ProviderRegistryEntry> = {
onPromptChange={onPromptChange}
/>
),
renderTraitsPicker: ({ threadId, model, modelOptions, prompt, onPromptChange }) => (
renderTraitsPicker: ({ threadId, model, models, modelOptions, prompt, onPromptChange }) => (
<TraitsPicker
provider="claudeAgent"
models={models}
threadId={threadId}
model={model}
modelOptions={modelOptions}
@@ -157,6 +181,7 @@ export function renderProviderTraitsMenuContent(input: {
provider: ProviderKind;
threadId: ThreadId;
model: ModelSlug;
models: ReadonlyArray<ServerProviderModel>;
modelOptions: ProviderModelOptions[ProviderKind] | undefined;
prompt: string;
onPromptChange: (prompt: string) => void;
@@ -164,6 +189,7 @@ export function renderProviderTraitsMenuContent(input: {
return composerProviderRegistry[input.provider].renderTraitsMenuContent({
threadId: input.threadId,
model: input.model,
models: input.models,
modelOptions: input.modelOptions,
prompt: input.prompt,
onPromptChange: input.onPromptChange,
@@ -174,6 +200,7 @@ export function renderProviderTraitsPicker(input: {
provider: ProviderKind;
threadId: ThreadId;
model: ModelSlug;
models: ReadonlyArray<ServerProviderModel>;
modelOptions: ProviderModelOptions[ProviderKind] | undefined;
prompt: string;
onPromptChange: (prompt: string) => void;
@@ -181,6 +208,7 @@ export function renderProviderTraitsPicker(input: {
return composerProviderRegistry[input.provider].renderTraitsPicker({
threadId: input.threadId,
model: input.model,
models: input.models,
modelOptions: input.modelOptions,
prompt: input.prompt,
onPromptChange: input.onPromptChange,
+19 -16
View File
@@ -9,16 +9,13 @@ import {
ProviderKind,
ProviderModelOptions,
RuntimeMode,
type ServerProvider,
ThreadId,
} from "@t3tools/contracts";
import * as Schema from "effect/Schema";
import * as Equal from "effect/Equal";
import { DeepMutable } from "effect/Types";
import {
getDefaultModel,
normalizeModelSlug,
resolveModelSlugForProvider,
} from "@t3tools/shared/model";
import { getDefaultModel, normalizeModelSlug } from "@t3tools/shared/model";
import { useMemo } from "react";
import { getLocalStorageItem } from "./hooks/useLocalStorage";
import { resolveAppModelSelection } from "./modelSelection";
@@ -31,6 +28,8 @@ import {
import { create } from "zustand";
import { createJSONStorage, persist } from "zustand/middleware";
import { createDebouncedStorage, createMemoryStorage } from "./lib/storage";
import { getDefaultServerModel } from "./providerModels";
import { UnifiedSettings } from "@t3tools/contracts/settings";
export const COMPOSER_DRAFT_STORAGE_KEY = "t3code:composer-drafts:v1";
const COMPOSER_DRAFT_STORAGE_VERSION = 3;
@@ -613,22 +612,23 @@ export function deriveEffectiveComposerModelState(input: {
| Pick<ComposerThreadDraftState, "modelSelectionByProvider" | "activeProvider">
| null
| undefined;
providers: ReadonlyArray<ServerProvider>;
selectedProvider: ProviderKind;
threadModelSelection: ModelSelection | null | undefined;
projectModelSelection: ModelSelection | null | undefined;
customModelsByProvider: Record<ProviderKind, readonly string[]>;
settings: UnifiedSettings;
}): EffectiveComposerModelState {
const baseModel = resolveModelSlugForProvider(
input.selectedProvider,
input.threadModelSelection?.model ??
input.projectModelSelection?.model ??
getDefaultModel(input.selectedProvider),
);
const baseModel =
normalizeModelSlug(
input.threadModelSelection?.model ?? input.projectModelSelection?.model,
input.selectedProvider,
) ?? getDefaultServerModel(input.providers, input.selectedProvider);
const activeSelection = input.draft?.modelSelectionByProvider?.[input.selectedProvider];
const selectedModel = activeSelection?.model
? resolveAppModelSelection(
input.selectedProvider,
input.customModelsByProvider,
input.settings,
input.providers,
activeSelection.model,
)
: baseModel;
@@ -2157,10 +2157,11 @@ export function useComposerThreadDraft(threadId: ThreadId): ComposerThreadDraftS
export function useEffectiveComposerModelState(input: {
threadId: ThreadId;
providers: ReadonlyArray<ServerProvider>;
selectedProvider: ProviderKind;
threadModelSelection: ModelSelection | null | undefined;
projectModelSelection: ModelSelection | null | undefined;
customModelsByProvider: Record<ProviderKind, readonly string[]>;
settings: UnifiedSettings;
}): EffectiveComposerModelState {
const draft = useComposerThreadDraft(input.threadId);
@@ -2168,14 +2169,16 @@ export function useEffectiveComposerModelState(input: {
() =>
deriveEffectiveComposerModelState({
draft,
providers: input.providers,
selectedProvider: input.selectedProvider,
threadModelSelection: input.threadModelSelection,
projectModelSelection: input.projectModelSelection,
customModelsByProvider: input.customModelsByProvider,
settings: input.settings,
}),
[
draft,
input.customModelsByProvider,
input.providers,
input.settings,
input.projectModelSelection,
input.selectedProvider,
input.threadModelSelection,
+266
View File
@@ -0,0 +1,266 @@
/**
* Unified settings hook.
*
* Abstracts the split between server-authoritative settings (persisted in
* `settings.json` on the server, fetched via `server.getConfig`) and
* client-only settings (persisted in localStorage).
*
* Consumers use `useSettings(selector)` to read, and `useUpdateSettings()` to
* write. The hook transparently routes reads/writes to the correct backing
* store.
*/
import { useCallback, useMemo } from "react";
import { useQuery, useQueryClient } from "@tanstack/react-query";
import {
ServerSettings,
ServerSettingsPatch,
ServerConfig,
ModelSelection,
ThreadEnvMode,
} from "@t3tools/contracts";
import { DEFAULT_SERVER_SETTINGS } from "@t3tools/contracts";
import {
type ClientSettings,
ClientSettingsSchema,
DEFAULT_CLIENT_SETTINGS,
DEFAULT_UNIFIED_SETTINGS,
SidebarProjectSortOrder,
SidebarThreadSortOrder,
TimestampFormat,
UnifiedSettings,
} from "@t3tools/contracts/settings";
import { serverConfigQueryOptions, serverQueryKeys } from "~/lib/serverReactQuery";
import { ensureNativeApi } from "~/nativeApi";
import { useLocalStorage } from "./useLocalStorage";
import { normalizeCustomModelSlugs } from "~/modelSelection";
import { Predicate, Schema, Struct } from "effect";
import { DeepMutable } from "effect/Types";
import { deepMerge } from "@t3tools/shared/Struct";
const CLIENT_SETTINGS_STORAGE_KEY = "t3code:client-settings:v1";
const OLD_SETTINGS_KEY = "t3code:app-settings:v1";
// ── Key sets for routing patches ─────────────────────────────────────
const SERVER_SETTINGS_KEYS = new Set<string>(Struct.keys(ServerSettings.fields));
function splitPatch(patch: Partial<UnifiedSettings>): {
serverPatch: ServerSettingsPatch;
clientPatch: Partial<ClientSettings>;
} {
const serverPatch: Record<string, unknown> = {};
const clientPatch: Record<string, unknown> = {};
for (const [key, value] of Object.entries(patch)) {
if (SERVER_SETTINGS_KEYS.has(key)) {
serverPatch[key] = value;
} else {
clientPatch[key] = value;
}
}
return {
serverPatch: serverPatch as ServerSettingsPatch,
clientPatch: clientPatch as Partial<ClientSettings>,
};
}
// ── Hooks ────────────────────────────────────────────────────────────
/**
* Read merged settings. Selector narrows the subscription so components
* only re-render when the slice they care about changes.
*/
export function useSettings<T extends UnifiedSettings = UnifiedSettings>(
selector?: (s: UnifiedSettings) => T,
): T {
const { data: serverConfig } = useQuery(serverConfigQueryOptions());
const [clientSettings] = useLocalStorage(
CLIENT_SETTINGS_STORAGE_KEY,
DEFAULT_CLIENT_SETTINGS,
ClientSettingsSchema,
);
const merged = useMemo<UnifiedSettings>(
() => ({
...(serverConfig?.settings ?? DEFAULT_SERVER_SETTINGS),
...clientSettings,
}),
[serverConfig?.settings, clientSettings],
);
return useMemo(() => (selector ? selector(merged) : (merged as T)), [merged, selector]);
}
/**
* Returns an updater that routes each key to the correct backing store.
*
* Server keys are optimistically patched in the React Query cache, then
* persisted via RPC. Client keys go straight to localStorage.
*/
export function useUpdateSettings() {
const queryClient = useQueryClient();
const [, setClientSettings] = useLocalStorage(
CLIENT_SETTINGS_STORAGE_KEY,
DEFAULT_CLIENT_SETTINGS,
ClientSettingsSchema,
);
const updateSettings = useCallback(
(patch: Partial<UnifiedSettings>) => {
const { serverPatch, clientPatch } = splitPatch(patch);
if (Object.keys(serverPatch).length > 0) {
// Optimistic update of the React Query cache
queryClient.setQueryData<ServerConfig>(serverQueryKeys.config(), (old) => {
if (!old) return old;
return {
...old,
settings: deepMerge(old.settings, serverPatch),
};
});
// Fire-and-forget RPC — push will reconcile on success
void ensureNativeApi().server.updateSettings(serverPatch);
}
if (Object.keys(clientPatch).length > 0) {
setClientSettings((prev) => ({ ...prev, ...clientPatch }));
}
},
[queryClient, setClientSettings],
);
const resetSettings = useCallback(() => {
updateSettings(DEFAULT_UNIFIED_SETTINGS);
}, [updateSettings]);
return {
updateSettings,
resetSettings,
};
}
// ── One-time migration from localStorage ─────────────────────────────
export function buildLegacyServerSettingsMigrationPatch(legacySettings: Record<string, unknown>) {
const patch: DeepMutable<ServerSettingsPatch> = {};
if (Predicate.isBoolean(legacySettings.enableAssistantStreaming)) {
patch.enableAssistantStreaming = legacySettings.enableAssistantStreaming;
}
if (Schema.is(ThreadEnvMode)(legacySettings.defaultThreadEnvMode)) {
patch.defaultThreadEnvMode = legacySettings.defaultThreadEnvMode;
}
if (Schema.is(ModelSelection)(legacySettings.textGenerationModelSelection)) {
patch.textGenerationModelSelection = legacySettings.textGenerationModelSelection;
}
if (typeof legacySettings.codexBinaryPath === "string") {
patch.providers ??= {};
patch.providers.codex ??= {};
patch.providers.codex.binaryPath = legacySettings.codexBinaryPath;
}
if (typeof legacySettings.codexHomePath === "string") {
patch.providers ??= {};
patch.providers.codex ??= {};
patch.providers.codex.homePath = legacySettings.codexHomePath;
}
if (Array.isArray(legacySettings.customCodexModels)) {
patch.providers ??= {};
patch.providers.codex ??= {};
patch.providers.codex.customModels = normalizeCustomModelSlugs(
legacySettings.customCodexModels,
new Set<string>(),
"codex",
);
}
if (Predicate.isString(legacySettings.claudeBinaryPath)) {
patch.providers ??= {};
patch.providers.claudeAgent ??= {};
patch.providers.claudeAgent.binaryPath = legacySettings.claudeBinaryPath;
}
if (Array.isArray(legacySettings.customClaudeModels)) {
patch.providers ??= {};
patch.providers.claudeAgent ??= {};
patch.providers.claudeAgent.customModels = normalizeCustomModelSlugs(
legacySettings.customClaudeModels,
new Set<string>(),
"claudeAgent",
);
}
return patch;
}
export function buildLegacyClientSettingsMigrationPatch(
legacySettings: Record<string, unknown>,
): Partial<DeepMutable<ClientSettings>> {
const patch: Partial<DeepMutable<ClientSettings>> = {};
if (Predicate.isBoolean(legacySettings.confirmThreadDelete)) {
patch.confirmThreadDelete = legacySettings.confirmThreadDelete;
}
if (Predicate.isBoolean(legacySettings.diffWordWrap)) {
patch.diffWordWrap = legacySettings.diffWordWrap;
}
if (Schema.is(SidebarProjectSortOrder)(legacySettings.sidebarProjectSortOrder)) {
patch.sidebarProjectSortOrder = legacySettings.sidebarProjectSortOrder;
}
if (Schema.is(SidebarThreadSortOrder)(legacySettings.sidebarThreadSortOrder)) {
patch.sidebarThreadSortOrder = legacySettings.sidebarThreadSortOrder;
}
if (Schema.is(TimestampFormat)(legacySettings.timestampFormat)) {
patch.timestampFormat = legacySettings.timestampFormat;
}
return patch;
}
/**
* Call once on app startup.
* If the legacy localStorage key exists, migrate its values to the new server
* and client storage formats, then remove the legacy key so this only runs once.
*/
export function migrateLocalSettingsToServer(): void {
if (typeof window === "undefined") return;
const raw = localStorage.getItem(OLD_SETTINGS_KEY);
if (!raw) return;
try {
const old = JSON.parse(raw);
if (!Predicate.isObject(old)) return;
// Migrate server-relevant keys via RPC
const serverPatch = buildLegacyServerSettingsMigrationPatch(old);
if (Object.keys(serverPatch).length > 0) {
const api = ensureNativeApi();
void api.server.updateSettings(serverPatch);
}
// Migrate client-only keys to the new localStorage key
const clientPatch = buildLegacyClientSettingsMigrationPatch(old);
if (Object.keys(clientPatch).length > 0) {
const existing = localStorage.getItem(CLIENT_SETTINGS_STORAGE_KEY);
const current = existing ? (JSON.parse(existing) as Record<string, unknown>) : {};
localStorage.setItem(
CLIENT_SETTINGS_STORAGE_KEY,
JSON.stringify({ ...current, ...clientPatch }),
);
}
} catch (error) {
console.error("[MIGRATION] Error migrating local settings:", error);
} finally {
// Remove the legacy key regardless to keep migration one-shot behavior.
localStorage.removeItem(OLD_SETTINGS_KEY);
}
}
-4
View File
@@ -32,10 +32,6 @@ describe("git mutation options", () => {
const options = gitRunStackedActionMutationOptions({
cwd: "/repo/a",
queryClient,
modelSelection: {
provider: "codex",
model: "gpt-5.4",
},
});
expect(options.mutationKey).toEqual(gitMutationKeys.runStackedAction("/repo/a"));
});
+1 -3
View File
@@ -1,4 +1,4 @@
import { type GitStackedAction, type ModelSelection } from "@t3tools/contracts";
import { type GitStackedAction } from "@t3tools/contracts";
import { mutationOptions, queryOptions, type QueryClient } from "@tanstack/react-query";
import { ensureNativeApi } from "../nativeApi";
@@ -112,7 +112,6 @@ export function gitCheckoutMutationOptions(input: {
export function gitRunStackedActionMutationOptions(input: {
cwd: string | null;
queryClient: QueryClient;
modelSelection: ModelSelection;
}) {
return mutationOptions({
mutationKey: gitMutationKeys.runStackedAction(input.cwd),
@@ -134,7 +133,6 @@ export function gitRunStackedActionMutationOptions(input: {
return api.git.runStackedAction({
actionId,
cwd: input.cwd,
modelSelection: input.modelSelection,
action,
...(commitMessage ? { commitMessage } : {}),
...(featureBranch ? { featureBranch } : {}),
+7 -1
View File
@@ -6,6 +6,13 @@ export const serverQueryKeys = {
config: () => ["server", "config"] as const,
};
/**
* Server config query options.
*
* `staleTime` is kept short so that push-driven `invalidateQueries` calls in
* the EventRouter always trigger a refetch, and so the query re-fetches when
* the component re-mounts (e.g. navigating away from settings and back).
*/
export function serverConfigQueryOptions() {
return queryOptions({
queryKey: serverQueryKeys.config(),
@@ -13,6 +20,5 @@ export function serverConfigQueryOptions() {
const api = ensureNativeApi();
return api.server.getConfig();
},
staleTime: Infinity,
});
}
+49 -77
View File
@@ -2,27 +2,22 @@ import {
DEFAULT_GIT_TEXT_GENERATION_MODEL_BY_PROVIDER,
type ModelSelection,
type ProviderKind,
type ServerProvider,
} from "@t3tools/contracts";
import {
getDefaultModel,
getModelOptions,
normalizeModelSlug,
resolveSelectableModel,
} from "@t3tools/shared/model";
import { normalizeModelSlug, resolveSelectableModel } from "@t3tools/shared/model";
import { getComposerProviderState } from "./components/chat/composerProviderRegistry";
import { UnifiedSettings } from "@t3tools/contracts/settings";
import {
getDefaultServerModel,
getProviderModels,
resolveSelectableProvider,
} from "./providerModels";
const MAX_CUSTOM_MODEL_COUNT = 32;
export const MAX_CUSTOM_MODEL_LENGTH = 256;
export type CustomModelSettings = {
customCodexModels: readonly string[];
customClaudeModels: readonly string[];
};
export type ProviderCustomModelConfig = {
provider: ProviderKind;
settingsKey: keyof CustomModelSettings;
defaultSettingsKey: keyof CustomModelSettings;
title: string;
description: string;
placeholder: string;
@@ -38,8 +33,6 @@ export interface AppModelOption {
const PROVIDER_CUSTOM_MODEL_CONFIG: Record<ProviderKind, ProviderCustomModelConfig> = {
codex: {
provider: "codex",
settingsKey: "customCodexModels",
defaultSettingsKey: "customCodexModels",
title: "Codex",
description: "Save additional Codex model slugs for the picker and `/model` command.",
placeholder: "your-codex-model-slug",
@@ -47,8 +40,6 @@ const PROVIDER_CUSTOM_MODEL_CONFIG: Record<ProviderKind, ProviderCustomModelConf
},
claudeAgent: {
provider: "claudeAgent",
settingsKey: "customClaudeModels",
defaultSettingsKey: "customClaudeModels",
title: "Claude",
description: "Save additional Claude model slugs for the picker and `/model` command.",
placeholder: "your-claude-model-slug",
@@ -56,20 +47,15 @@ const PROVIDER_CUSTOM_MODEL_CONFIG: Record<ProviderKind, ProviderCustomModelConf
},
};
const BUILT_IN_MODEL_SLUGS_BY_PROVIDER: Record<ProviderKind, ReadonlySet<string>> = {
codex: new Set(getModelOptions("codex").map((option) => option.slug)),
claudeAgent: new Set(getModelOptions("claudeAgent").map((option) => option.slug)),
};
export const MODEL_PROVIDER_SETTINGS = Object.values(PROVIDER_CUSTOM_MODEL_CONFIG);
export function normalizeCustomModelSlugs(
models: Iterable<string | null | undefined>,
builtInModelSlugs: ReadonlySet<string>,
provider: ProviderKind = "codex",
): string[] {
const normalizedModels: string[] = [];
const seen = new Set<string>();
const builtInModelSlugs = BUILT_IN_MODEL_SLUGS_BY_PROVIDER[provider];
for (const candidate of models) {
const normalized = normalizeModelSlug(candidate, provider);
@@ -92,52 +78,29 @@ export function normalizeCustomModelSlugs(
return normalizedModels;
}
export function getCustomModelsForProvider(
settings: CustomModelSettings,
provider: ProviderKind,
): readonly string[] {
return settings[PROVIDER_CUSTOM_MODEL_CONFIG[provider].settingsKey];
}
export function getDefaultCustomModelsForProvider(
defaults: CustomModelSettings,
provider: ProviderKind,
): readonly string[] {
return defaults[PROVIDER_CUSTOM_MODEL_CONFIG[provider].defaultSettingsKey];
}
export function patchCustomModels(
provider: ProviderKind,
models: string[],
): Partial<CustomModelSettings> {
return {
[PROVIDER_CUSTOM_MODEL_CONFIG[provider].settingsKey]: models,
};
}
export function getCustomModelsByProvider(
settings: CustomModelSettings,
): Record<ProviderKind, readonly string[]> {
return {
codex: getCustomModelsForProvider(settings, "codex"),
claudeAgent: getCustomModelsForProvider(settings, "claudeAgent"),
};
}
export function getAppModelOptions(
settings: UnifiedSettings,
providers: ReadonlyArray<ServerProvider>,
provider: ProviderKind,
customModels: readonly string[],
selectedModel?: string | null,
): AppModelOption[] {
const options: AppModelOption[] = getModelOptions(provider).map(({ slug, name }) => ({
slug,
name,
isCustom: false,
}));
const options: AppModelOption[] = getProviderModels(providers, provider).map(
({ slug, name, isCustom }) => ({
slug,
name,
isCustom,
}),
);
const seen = new Set(options.map((option) => option.slug));
const trimmedSelectedModel = selectedModel?.trim().toLowerCase();
const builtInModelSlugs = new Set(
getProviderModels(providers, provider)
.filter((model) => !model.isCustom)
.map((model) => model.slug),
);
for (const slug of normalizeCustomModelSlugs(customModels, provider)) {
const customModels = settings.providers[provider].customModels;
for (const slug of normalizeCustomModelSlugs(customModels, builtInModelSlugs, provider)) {
if (seen.has(slug)) {
continue;
}
@@ -171,52 +134,61 @@ export function getAppModelOptions(
export function resolveAppModelSelection(
provider: ProviderKind,
customModels: Record<ProviderKind, readonly string[]>,
settings: UnifiedSettings,
providers: ReadonlyArray<ServerProvider>,
selectedModel: string | null | undefined,
): string {
const customModelsForProvider = customModels[provider];
const options = getAppModelOptions(provider, customModelsForProvider, selectedModel);
return resolveSelectableModel(provider, selectedModel, options) ?? getDefaultModel(provider);
const resolvedProvider = resolveSelectableProvider(providers, provider);
const options = getAppModelOptions(settings, providers, resolvedProvider, selectedModel);
return (
resolveSelectableModel(resolvedProvider, selectedModel, options) ??
getDefaultServerModel(providers, resolvedProvider)
);
}
export function getCustomModelOptionsByProvider(
settings: CustomModelSettings,
settings: UnifiedSettings,
providers: ReadonlyArray<ServerProvider>,
selectedProvider?: ProviderKind | null,
selectedModel?: string | null,
): Record<ProviderKind, ReadonlyArray<{ slug: string; name: string }>> {
const customModelsByProvider = getCustomModelsByProvider(settings);
return {
codex: getAppModelOptions(
settings,
providers,
"codex",
customModelsByProvider.codex,
selectedProvider === "codex" ? selectedModel : undefined,
),
claudeAgent: getAppModelOptions(
settings,
providers,
"claudeAgent",
customModelsByProvider.claudeAgent,
selectedProvider === "claudeAgent" ? selectedModel : undefined,
),
};
}
export function resolveAppModelSelectionState(
settings: CustomModelSettings & {
textGenerationModelSelection: ModelSelection | undefined;
},
settings: UnifiedSettings,
providers: ReadonlyArray<ServerProvider>,
): ModelSelection {
const selection = settings.textGenerationModelSelection ?? {
provider: "codex" as const,
model: DEFAULT_GIT_TEXT_GENERATION_MODEL_BY_PROVIDER.codex,
};
const provider = selection.provider;
const customModelsByProvider = getCustomModelsByProvider(settings);
const model = resolveAppModelSelection(provider, customModelsByProvider, selection.model);
const provider = resolveSelectableProvider(providers, selection.provider);
// When the provider changed due to fallback (e.g. selected provider was disabled),
// don't carry over the old provider's model — use the fallback provider's default.
const selectedModel = provider === selection.provider ? selection.model : null;
const model = resolveAppModelSelection(provider, settings, providers, selectedModel);
const { modelOptionsForDispatch } = getComposerProviderState({
provider,
model,
models: getProviderModels(providers, provider),
prompt: "",
modelOptions: {
[provider]: selection.options,
[provider]: provider === selection.provider ? selection.options : undefined,
},
});
+116
View File
@@ -0,0 +1,116 @@
import {
DEFAULT_MODEL_BY_PROVIDER,
type ClaudeModelOptions,
type CodexModelOptions,
type ModelCapabilities,
type ProviderKind,
type ServerProvider,
type ServerProviderModel,
} from "@t3tools/contracts";
import {
getDefaultEffort,
hasEffortLevel,
normalizeModelSlug,
trimOrNull,
} from "@t3tools/shared/model";
const EMPTY_CAPABILITIES: ModelCapabilities = {
reasoningEffortLevels: [],
supportsFastMode: false,
supportsThinkingToggle: false,
promptInjectedEffortLevels: [],
};
export function getProviderModels(
providers: ReadonlyArray<ServerProvider>,
provider: ProviderKind,
): ReadonlyArray<ServerProviderModel> {
return providers.find((candidate) => candidate.provider === provider)?.models ?? [];
}
export function getProviderSnapshot(
providers: ReadonlyArray<ServerProvider>,
provider: ProviderKind,
): ServerProvider | undefined {
return providers.find((candidate) => candidate.provider === provider);
}
export function isProviderEnabled(
providers: ReadonlyArray<ServerProvider>,
provider: ProviderKind,
): boolean {
return getProviderSnapshot(providers, provider)?.enabled ?? true;
}
export function resolveSelectableProvider(
providers: ReadonlyArray<ServerProvider>,
provider: ProviderKind | null | undefined,
): ProviderKind {
const requested = provider ?? "codex";
if (isProviderEnabled(providers, requested)) {
return requested;
}
return providers.find((candidate) => candidate.enabled)?.provider ?? requested;
}
export function getProviderModelCapabilities(
models: ReadonlyArray<ServerProviderModel>,
model: string | null | undefined,
provider: ProviderKind,
): ModelCapabilities {
const slug = normalizeModelSlug(model, provider);
return models.find((candidate) => candidate.slug === slug)?.capabilities ?? EMPTY_CAPABILITIES;
}
export function getDefaultServerModel(
providers: ReadonlyArray<ServerProvider>,
provider: ProviderKind,
): string {
const models = getProviderModels(providers, provider);
return (
models.find((model) => !model.isCustom)?.slug ??
models[0]?.slug ??
DEFAULT_MODEL_BY_PROVIDER[provider]
);
}
export function normalizeCodexModelOptionsWithCapabilities(
caps: ModelCapabilities,
modelOptions: CodexModelOptions | null | undefined,
): CodexModelOptions | undefined {
const defaultReasoningEffort = getDefaultEffort(caps);
const reasoningEffort = trimOrNull(modelOptions?.reasoningEffort) ?? defaultReasoningEffort;
const fastModeEnabled = modelOptions?.fastMode === true;
const nextOptions: CodexModelOptions = {
...(reasoningEffort && reasoningEffort !== defaultReasoningEffort
? { reasoningEffort: reasoningEffort as CodexModelOptions["reasoningEffort"] }
: {}),
...(fastModeEnabled ? { fastMode: true } : {}),
};
return Object.keys(nextOptions).length > 0 ? nextOptions : undefined;
}
export function normalizeClaudeModelOptionsWithCapabilities(
caps: ModelCapabilities,
modelOptions: ClaudeModelOptions | null | undefined,
): ClaudeModelOptions | undefined {
const defaultReasoningEffort = getDefaultEffort(caps);
const resolvedEffort = trimOrNull(modelOptions?.effort);
const isPromptInjected = caps.promptInjectedEffortLevels.includes(resolvedEffort ?? "");
const effort =
resolvedEffort &&
!isPromptInjected &&
hasEffortLevel(caps, resolvedEffort) &&
resolvedEffort !== defaultReasoningEffort
? resolvedEffort
: undefined;
const thinking =
caps.supportsThinkingToggle && modelOptions?.thinking === false ? false : undefined;
const fastMode = caps.supportsFastMode && modelOptions?.fastMode === true ? true : undefined;
const nextOptions: ClaudeModelOptions = {
...(thinking === false ? { thinking: false } : {}),
...(effort ? { effort } : {}),
...(fastMode ? { fastMode: true } : {}),
};
return Object.keys(nextOptions).length > 0 ? nextOptions : undefined;
}
+14 -1
View File
@@ -20,7 +20,8 @@ import { clearPromotedDraftThreads, useComposerDraftStore } from "../composerDra
import { useStore } from "../store";
import { useTerminalStateStore } from "../terminalStateStore";
import { terminalRunningSubprocessFromEvent } from "../terminalActivity";
import { onServerConfigUpdated, onServerWelcome } from "../wsNativeApi";
import { onServerConfigUpdated, onServerProvidersUpdated, onServerWelcome } from "../wsNativeApi";
import { migrateLocalSettingsToServer } from "../hooks/useSettings";
import { providerQueryKeys } from "../lib/providerReactQuery";
import { projectQueryKeys } from "../lib/projectReactQuery";
import { collectActiveTerminalThreadIds } from "../lib/terminalStateCleanup";
@@ -230,6 +231,8 @@ function EventRouter() {
);
});
const unsubWelcome = onServerWelcome((payload) => {
// Migrate old localStorage settings to server on first connect
migrateLocalSettingsToServer();
void (async () => {
await syncSnapshot();
if (disposed) {
@@ -260,8 +263,14 @@ function EventRouter() {
// don't produce duplicate toasts.
let subscribed = false;
const unsubServerConfigUpdated = onServerConfigUpdated((payload) => {
// Invalidate the config query so active observers refetch fresh data.
void queryClient.invalidateQueries({ queryKey: serverQueryKeys.config() });
if (!subscribed) return;
// Only show keybindings toasts for keybindings changes (no settings in payload)
if (payload.settings) return;
const issue = payload.issues.find((entry) => entry.kind.startsWith("keybindings."));
if (!issue) {
toastManager.add({
@@ -300,6 +309,9 @@ function EventRouter() {
},
});
});
const unsubProvidersUpdated = onServerProvidersUpdated(() => {
void queryClient.invalidateQueries({ queryKey: serverQueryKeys.config() });
});
subscribed = true;
return () => {
disposed = true;
@@ -309,6 +321,7 @@ function EventRouter() {
unsubTerminalEvent();
unsubWelcome();
unsubServerConfigUpdated();
unsubProvidersUpdated();
};
}, [
navigate,
File diff suppressed because it is too large. Load diff
+2 -2
View File
@@ -11,7 +11,7 @@ import { resolveShortcutCommand } from "../keybindings";
import { selectThreadTerminalState, useTerminalStateStore } from "../terminalStateStore";
import { useThreadSelectionStore } from "../threadSelectionStore";
import { resolveSidebarNewThreadEnvMode } from "~/components/Sidebar.logic";
import { useAppSettings } from "~/appSettings";
import { useSettings } from "~/hooks/useSettings";
import { Sidebar, SidebarProvider, SidebarRail } from "~/components/ui/sidebar";
const EMPTY_KEYBINDINGS: ResolvedKeybindingsConfig = [];
@@ -31,7 +31,7 @@ function ChatRouteGlobalShortcuts() {
? selectThreadTerminalState(state.terminalStateByThreadId, routeThreadId).terminalOpen
: false,
);
const { settings: appSettings } = useAppSettings();
const appSettings = useSettings();
useEffect(() => {
const onWindowKeyDown = (event: KeyboardEvent) => {
+1 -1
View File
@@ -1,4 +1,4 @@
import { type TimestampFormat } from "./appSettings";
import { type TimestampFormat } from "@t3tools/contracts/settings";
export function getTimestampFormatOptions(
timestampFormat: TimestampFormat,
+27 -15
View File
@@ -13,7 +13,7 @@ import {
WS_CHANNELS,
WS_METHODS,
type WsPush,
type ServerProviderStatus,
type ServerProvider,
} from "@t3tools/contracts";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
@@ -92,13 +92,16 @@ function getWindowForTest(): Window & typeof globalThis & { desktopBridge?: unkn
return testGlobal.window;
}
const defaultProviders: ReadonlyArray<ServerProviderStatus> = [
const defaultProviders: ReadonlyArray<ServerProvider> = [
{
provider: "codex",
enabled: true,
installed: true,
version: "0.116.0",
status: "ready",
available: true,
authStatus: "authenticated",
checkedAt: "2026-01-01T00:00:00.000Z",
models: [],
},
];
@@ -197,7 +200,6 @@ describe("wsNativeApi", () => {
message: "Entry at index 1 is invalid.",
},
],
providers: defaultProviders,
} as const;
emitPush(WS_CHANNELS.serverConfigUpdated, payload);
@@ -219,20 +221,38 @@ describe("wsNativeApi", () => {
emitPush(WS_CHANNELS.serverConfigUpdated, {
issues: [{ kind: "keybindings.malformed-config", message: "bad json" }],
providers: defaultProviders,
});
emitPush(WS_CHANNELS.serverConfigUpdated, {
issues: [],
providers: defaultProviders,
});
expect(listener).toHaveBeenCalledTimes(2);
expect(listener).toHaveBeenLastCalledWith({
issues: [],
providers: defaultProviders,
});
});
it("delivers and caches valid server.providersUpdated payloads", async () => {
const { createWsNativeApi, onServerProvidersUpdated } = await import("./wsNativeApi");
createWsNativeApi();
const listener = vi.fn();
onServerProvidersUpdated(listener);
const payload = {
providers: defaultProviders,
} as const;
emitPush(WS_CHANNELS.serverProvidersUpdated, payload);
expect(listener).toHaveBeenCalledTimes(1);
expect(listener).toHaveBeenCalledWith(payload);
const lateListener = vi.fn();
onServerProvidersUpdated(lateListener);
expect(lateListener).toHaveBeenCalledTimes(1);
expect(lateListener).toHaveBeenCalledWith(payload);
});
it("forwards valid terminal and orchestration events", async () => {
const { createWsNativeApi } = await import("./wsNativeApi");
@@ -357,10 +377,6 @@ describe("wsNativeApi", () => {
actionId: "action-1",
cwd: "/repo",
action: "commit",
modelSelection: {
provider: "codex",
model: "gpt-5.4-mini",
},
});
expect(requestMock).toHaveBeenCalledWith(
@@ -369,10 +385,6 @@ describe("wsNativeApi", () => {
actionId: "action-1",
cwd: "/repo",
action: "commit",
modelSelection: {
provider: "codex",
model: "gpt-5.4-mini",
},
},
{ timeoutMs: null },
);
+34
View File
@@ -5,6 +5,7 @@ import {
type ContextMenuItem,
type NativeApi,
ServerConfigUpdatedPayload,
ServerProviderUpdatedPayload,
WS_CHANNELS,
WS_METHODS,
type WsWelcomePayload,
@@ -16,6 +17,7 @@ import { WsTransport } from "./wsTransport";
let instance: { api: NativeApi; transport: WsTransport } | null = null;
const welcomeListeners = new Set<(payload: WsWelcomePayload) => void>();
const serverConfigUpdatedListeners = new Set<(payload: ServerConfigUpdatedPayload) => void>();
const providersUpdatedListeners = new Set<(payload: ServerProviderUpdatedPayload) => void>();
const gitActionProgressListeners = new Set<(payload: GitActionProgressEvent) => void>();
/**
@@ -64,6 +66,26 @@ export function onServerConfigUpdated(
};
}
export function onServerProvidersUpdated(
listener: (payload: ServerProviderUpdatedPayload) => void,
): () => void {
providersUpdatedListeners.add(listener);
const latestProviders =
instance?.transport.getLatestPush(WS_CHANNELS.serverProvidersUpdated)?.data ?? null;
if (latestProviders) {
try {
listener(latestProviders);
} catch {
// Swallow listener errors
}
}
return () => {
providersUpdatedListeners.delete(listener);
};
}
export function createWsNativeApi(): NativeApi {
if (instance) return instance.api;
@@ -89,6 +111,16 @@ export function createWsNativeApi(): NativeApi {
}
}
});
transport.subscribe(WS_CHANNELS.serverProvidersUpdated, (message) => {
const payload = message.data;
for (const listener of providersUpdatedListeners) {
try {
listener(payload);
} catch {
// Swallow listener errors
}
}
});
transport.subscribe(WS_CHANNELS.gitActionProgress, (message) => {
const payload = message.data;
for (const listener of gitActionProgressListeners) {
@@ -179,6 +211,8 @@ export function createWsNativeApi(): NativeApi {
server: {
getConfig: () => transport.request(WS_METHODS.serverGetConfig),
upsertKeybinding: (input) => transport.request(WS_METHODS.serverUpsertKeybinding, input),
getSettings: () => transport.request(WS_METHODS.serverGetSettings),
updateSettings: (patch) => transport.request(WS_METHODS.serverUpdateSettings, { patch }),
},
orchestration: {
getSnapshot: () => transport.request(ORCHESTRATION_WS_METHODS.getSnapshot),
+4 -4
View File
@@ -98,7 +98,7 @@ describe("WsTransport", () => {
type: "push",
sequence: 1,
channel: WS_CHANNELS.serverConfigUpdated,
data: { issues: [], providers: [] },
data: { issues: [] },
}),
);
@@ -107,7 +107,7 @@ describe("WsTransport", () => {
type: "push",
sequence: 1,
channel: WS_CHANNELS.serverConfigUpdated,
data: { issues: [], providers: [] },
data: { issues: [] },
});
transport.dispose();
@@ -160,7 +160,7 @@ describe("WsTransport", () => {
type: "push",
sequence: 3,
channel: WS_CHANNELS.serverConfigUpdated,
data: { issues: [], providers: [] },
data: { issues: [] },
}),
);
@@ -169,7 +169,7 @@ describe("WsTransport", () => {
type: "push",
sequence: 3,
channel: WS_CHANNELS.serverConfigUpdated,
data: { issues: [], providers: [] },
data: { issues: [] },
});
expect(warnSpy).toHaveBeenCalledTimes(2);
expect(warnSpy).toHaveBeenNthCalledWith(
+5
View File
@@ -10,6 +10,11 @@
"module": "./dist/index.mjs",
"types": "./src/index.ts",
"exports": {
"./settings": {
"types": "./src/settings.ts",
"import": "./src/settings.ts",
"require": "./src/settings.ts"
},
".": {
"types": "./src/index.ts",
"import": "./src/index.ts",
-15
View File
@@ -74,19 +74,4 @@ describe("GitRunStackedActionInput", () => {
expect(parsed.actionId).toBe("action-1");
expect(parsed.action).toBe("commit");
});
it("accepts git text generation as a modelSelection", () => {
const parsed = decodeRunStackedActionInput({
actionId: "action-1",
cwd: "/repo",
action: "commit_push_pr",
modelSelection: {
provider: "claudeAgent",
model: "claude-haiku-4-5",
},
});
expect(parsed.modelSelection?.provider).toBe("claudeAgent");
expect(parsed.modelSelection?.model).toBe("claude-haiku-4-5");
});
});
-2
View File
@@ -1,6 +1,5 @@
import { Schema } from "effect";
import { NonNegativeInt, PositiveInt, TrimmedNonEmptyString } from "./baseSchemas";
import { ModelSelection } from "./orchestration";
const TrimmedNonEmptyStringSchema = TrimmedNonEmptyString;
@@ -80,7 +79,6 @@ export const GitRunStackedActionInput = Schema.Struct({
filePaths: Schema.optional(
Schema.Array(TrimmedNonEmptyStringSchema).check(Schema.isMinLength(1)),
),
modelSelection: ModelSelection,
});
export type GitRunStackedActionInput = typeof GitRunStackedActionInput.Type;
+1
View File
@@ -7,6 +7,7 @@ export * from "./model";
export * from "./ws";
export * from "./keybindings";
export * from "./server";
export * from "./settings";
export * from "./git";
export * from "./orchestration";
export * from "./editor";
+5 -2
View File
@@ -25,7 +25,7 @@ import type {
ProjectWriteFileInput,
ProjectWriteFileResult,
} from "./project";
import type { ServerConfig } from "./server";
import type { ServerConfig, ServerUpsertKeybindingResult } from "./server";
import type {
TerminalClearInput,
TerminalCloseInput,
@@ -36,7 +36,7 @@ import type {
TerminalSessionSnapshot,
TerminalWriteInput,
} from "./terminal";
import type { ServerUpsertKeybindingInput, ServerUpsertKeybindingResult } from "./server";
import type { ServerUpsertKeybindingInput } from "./server";
import type {
ClientOrchestrationCommand,
OrchestrationGetFullThreadDiffInput,
@@ -47,6 +47,7 @@ import type {
OrchestrationReadModel,
} from "./orchestration";
import { EditorId } from "./editor";
import { ServerSettings, ServerSettingsPatch } from "./settings";
export interface ContextMenuItem<T extends string = string> {
id: T;
@@ -161,6 +162,8 @@ export interface NativeApi {
server: {
getConfig: () => Promise<ServerConfig>;
upsertKeybinding: (input: ServerUpsertKeybindingInput) => Promise<ServerUpsertKeybindingResult>;
getSettings: () => Promise<ServerSettings>;
updateSettings: (patch: ServerSettingsPatch) => Promise<ServerSettings>;
};
orchestration: {
getSnapshot: () => Promise<OrchestrationReadModel>;
+15 -173
View File
@@ -1,4 +1,5 @@
import { Schema } from "effect";
import { TrimmedNonEmptyString } from "./baseSchemas";
import type { ProviderKind } from "./orchestration";
export const CODEX_REASONING_EFFORT_OPTIONS = ["xhigh", "high", "medium", "low"] as const;
@@ -26,178 +27,28 @@ export const ProviderModelOptions = Schema.Struct({
});
export type ProviderModelOptions = typeof ProviderModelOptions.Type;
export type EffortOption = {
readonly value: string;
readonly label: string;
readonly isDefault?: true;
};
export const EffortOption = Schema.Struct({
value: TrimmedNonEmptyString,
label: TrimmedNonEmptyString,
isDefault: Schema.optional(Schema.Boolean),
});
export type EffortOption = typeof EffortOption.Type;
export type ModelCapabilities = {
readonly reasoningEffortLevels: readonly EffortOption[];
readonly supportsFastMode: boolean;
readonly supportsThinkingToggle: boolean;
readonly promptInjectedEffortLevels: readonly string[];
};
export const ModelCapabilities = Schema.Struct({
reasoningEffortLevels: Schema.Array(EffortOption),
supportsFastMode: Schema.Boolean,
supportsThinkingToggle: Schema.Boolean,
promptInjectedEffortLevels: Schema.Array(TrimmedNonEmptyString),
});
export type ModelCapabilities = typeof ModelCapabilities.Type;
type ModelDefinition = {
readonly slug: string;
readonly name: string;
readonly capabilities: ModelCapabilities;
};
/**
* TODO: This should not be a static array, each provider
* should return its own model list over the WS API.
*/
export const MODEL_OPTIONS_BY_PROVIDER = {
codex: [
{
slug: "gpt-5.4",
name: "GPT-5.4",
capabilities: {
reasoningEffortLevels: [
{ value: "xhigh", label: "Extra High" },
{ value: "high", label: "High", isDefault: true },
{ value: "medium", label: "Medium" },
{ value: "low", label: "Low" },
],
supportsFastMode: true,
supportsThinkingToggle: false,
promptInjectedEffortLevels: [],
},
},
{
slug: "gpt-5.4-mini",
name: "GPT-5.4 Mini",
capabilities: {
reasoningEffortLevels: [
{ value: "xhigh", label: "Extra High" },
{ value: "high", label: "High", isDefault: true },
{ value: "medium", label: "Medium" },
{ value: "low", label: "Low" },
],
supportsFastMode: true,
supportsThinkingToggle: false,
promptInjectedEffortLevels: [],
},
},
{
slug: "gpt-5.3-codex",
name: "GPT-5.3 Codex",
capabilities: {
reasoningEffortLevels: [
{ value: "xhigh", label: "Extra High" },
{ value: "high", label: "High", isDefault: true },
{ value: "medium", label: "Medium" },
{ value: "low", label: "Low" },
],
supportsFastMode: true,
supportsThinkingToggle: false,
promptInjectedEffortLevels: [],
},
},
{
slug: "gpt-5.3-codex-spark",
name: "GPT-5.3 Codex Spark",
capabilities: {
reasoningEffortLevels: [
{ value: "xhigh", label: "Extra High" },
{ value: "high", label: "High", isDefault: true },
{ value: "medium", label: "Medium" },
{ value: "low", label: "Low" },
],
supportsFastMode: true,
supportsThinkingToggle: false,
promptInjectedEffortLevels: [],
},
},
{
slug: "gpt-5.2-codex",
name: "GPT-5.2 Codex",
capabilities: {
reasoningEffortLevels: [
{ value: "xhigh", label: "Extra High" },
{ value: "high", label: "High", isDefault: true },
{ value: "medium", label: "Medium" },
{ value: "low", label: "Low" },
],
supportsFastMode: true,
supportsThinkingToggle: false,
promptInjectedEffortLevels: [],
},
},
{
slug: "gpt-5.2",
name: "GPT-5.2",
capabilities: {
reasoningEffortLevels: [
{ value: "xhigh", label: "Extra High" },
{ value: "high", label: "High", isDefault: true },
{ value: "medium", label: "Medium" },
{ value: "low", label: "Low" },
],
supportsFastMode: true,
supportsThinkingToggle: false,
promptInjectedEffortLevels: [],
},
},
],
claudeAgent: [
{
slug: "claude-opus-4-6",
name: "Claude Opus 4.6",
capabilities: {
reasoningEffortLevels: [
{ value: "low", label: "Low" },
{ value: "medium", label: "Medium" },
{ value: "high", label: "High", isDefault: true },
{ value: "max", label: "Max" },
{ value: "ultrathink", label: "Ultrathink" },
],
supportsFastMode: true,
supportsThinkingToggle: false,
promptInjectedEffortLevels: ["ultrathink"],
},
},
{
slug: "claude-sonnet-4-6",
name: "Claude Sonnet 4.6",
capabilities: {
reasoningEffortLevels: [
{ value: "low", label: "Low" },
{ value: "medium", label: "Medium" },
{ value: "high", label: "High", isDefault: true },
{ value: "ultrathink", label: "Ultrathink" },
],
supportsFastMode: false,
supportsThinkingToggle: false,
promptInjectedEffortLevels: ["ultrathink"],
},
},
{
slug: "claude-haiku-4-5",
name: "Claude Haiku 4.5",
capabilities: {
reasoningEffortLevels: [],
supportsFastMode: false,
supportsThinkingToggle: true,
promptInjectedEffortLevels: [],
},
},
],
} as const satisfies Record<ProviderKind, readonly ModelDefinition[]>;
export type ModelOptionsByProvider = typeof MODEL_OPTIONS_BY_PROVIDER;
type BuiltInModelSlug = (typeof MODEL_OPTIONS_BY_PROVIDER)[ProviderKind][number]["slug"];
export type ModelSlug = BuiltInModelSlug | (string & {});
export type ModelSlug = string & {};
export const DEFAULT_MODEL_BY_PROVIDER: Record<ProviderKind, ModelSlug> = {
codex: "gpt-5.4",
claudeAgent: "claude-sonnet-4-6",
};
// Backward compatibility for existing Codex-only call sites.
export const MODEL_OPTIONS = MODEL_OPTIONS_BY_PROVIDER.codex;
export const DEFAULT_MODEL = DEFAULT_MODEL_BY_PROVIDER.codex;
/** Per-provider text generation model defaults. */
@@ -230,15 +81,6 @@ export const MODEL_SLUG_ALIASES_BY_PROVIDER: Record<ProviderKind, Record<string,
},
};
// ── Model capabilities index ──────────────────────────────────────────
export const MODEL_CAPABILITIES_INDEX = Object.fromEntries(
Object.entries(MODEL_OPTIONS_BY_PROVIDER).map(([provider, models]) => [
provider,
Object.fromEntries(models.map((m) => [m.slug, m.capabilities])),
]),
) as unknown as Record<ProviderKind, Record<string, ModelCapabilities>>;
// ── Provider display names ────────────────────────────────────────────
export const PROVIDER_DISPLAY_NAMES: Record<ProviderKind, string> = {
+2 -24
View File
@@ -47,37 +47,20 @@ export const DEFAULT_PROVIDER_KIND: ProviderKind = "codex";
export const CodexModelSelection = Schema.Struct({
provider: Schema.Literal("codex"),
model: TrimmedNonEmptyString,
options: Schema.optional(CodexModelOptions),
options: Schema.optionalKey(CodexModelOptions),
});
export type CodexModelSelection = typeof CodexModelSelection.Type;
export const ClaudeModelSelection = Schema.Struct({
provider: Schema.Literal("claudeAgent"),
model: TrimmedNonEmptyString,
options: Schema.optional(ClaudeModelOptions),
options: Schema.optionalKey(ClaudeModelOptions),
});
export type ClaudeModelSelection = typeof ClaudeModelSelection.Type;
export const ModelSelection = Schema.Union([CodexModelSelection, ClaudeModelSelection]);
export type ModelSelection = typeof ModelSelection.Type;
export const CodexProviderStartOptions = Schema.Struct({
binaryPath: Schema.optional(TrimmedNonEmptyString),
homePath: Schema.optional(TrimmedNonEmptyString),
});
export const ClaudeProviderStartOptions = Schema.Struct({
binaryPath: Schema.optional(TrimmedNonEmptyString),
permissionMode: Schema.optional(TrimmedNonEmptyString),
maxThinkingTokens: Schema.optional(NonNegativeInt),
});
export const ProviderStartOptions = Schema.Struct({
codex: Schema.optional(CodexProviderStartOptions),
claudeAgent: Schema.optional(ClaudeProviderStartOptions),
});
export type ProviderStartOptions = typeof ProviderStartOptions.Type;
export const RuntimeMode = Schema.Literals(["approval-required", "full-access"]);
export type RuntimeMode = typeof RuntimeMode.Type;
export const DEFAULT_RUNTIME_MODE: RuntimeMode = "full-access";
@@ -402,8 +385,6 @@ export const ThreadTurnStartCommand = Schema.Struct({
attachments: Schema.Array(ChatAttachment),
}),
modelSelection: Schema.optional(ModelSelection),
providerOptions: Schema.optional(ProviderStartOptions),
assistantDeliveryMode: Schema.optional(AssistantDeliveryMode),
runtimeMode: RuntimeMode.pipe(Schema.withDecodingDefault(() => DEFAULT_RUNTIME_MODE)),
interactionMode: ProviderInteractionMode.pipe(
Schema.withDecodingDefault(() => DEFAULT_PROVIDER_INTERACTION_MODE),
@@ -423,8 +404,6 @@ const ClientThreadTurnStartCommand = Schema.Struct({
attachments: Schema.Array(UploadChatAttachment),
}),
modelSelection: Schema.optional(ModelSelection),
providerOptions: Schema.optional(ProviderStartOptions),
assistantDeliveryMode: Schema.optional(AssistantDeliveryMode),
runtimeMode: RuntimeMode,
interactionMode: ProviderInteractionMode,
sourceProposedPlan: Schema.optional(SourceProposedPlanReference),
@@ -702,7 +681,6 @@ export const ThreadTurnStartRequestedPayload = Schema.Struct({
threadId: ThreadId,
messageId: MessageId,
modelSelection: Schema.optional(ModelSelection),
providerOptions: Schema.optional(ProviderStartOptions),
assistantDeliveryMode: Schema.optional(AssistantDeliveryMode),
runtimeMode: RuntimeMode.pipe(Schema.withDecodingDefault(() => DEFAULT_RUNTIME_MODE)),
interactionMode: ProviderInteractionMode.pipe(
-18
View File
@@ -21,12 +21,6 @@ describe("ProviderSessionStartInput", () => {
},
},
runtimeMode: "full-access",
providerOptions: {
codex: {
binaryPath: "/usr/local/bin/codex",
homePath: "/tmp/.codex",
},
},
});
expect(parsed.runtimeMode).toBe("full-access");
expect(parsed.modelSelection?.provider).toBe("codex");
@@ -36,8 +30,6 @@ describe("ProviderSessionStartInput", () => {
}
expect(parsed.modelSelection.options?.reasoningEffort).toBe("high");
expect(parsed.modelSelection.options?.fastMode).toBe(true);
expect(parsed.providerOptions?.codex?.binaryPath).toBe("/usr/local/bin/codex");
expect(parsed.providerOptions?.codex?.homePath).toBe("/tmp/.codex");
});
it("rejects payloads without runtime mode", () => {
@@ -63,13 +55,6 @@ describe("ProviderSessionStartInput", () => {
fastMode: true,
},
},
providerOptions: {
claudeAgent: {
binaryPath: "/usr/local/bin/claude",
permissionMode: "plan",
maxThinkingTokens: 12_000,
},
},
runtimeMode: "full-access",
});
expect(parsed.provider).toBe("claudeAgent");
@@ -81,9 +66,6 @@ describe("ProviderSessionStartInput", () => {
expect(parsed.modelSelection.options?.thinking).toBe(true);
expect(parsed.modelSelection.options?.effort).toBe("max");
expect(parsed.modelSelection.options?.fastMode).toBe(true);
expect(parsed.providerOptions?.claudeAgent?.binaryPath).toBe("/usr/local/bin/claude");
expect(parsed.providerOptions?.claudeAgent?.permissionMode).toBe("plan");
expect(parsed.providerOptions?.claudeAgent?.maxThinkingTokens).toBe(12_000);
expect(parsed.runtimeMode).toBe("full-access");
});
});
-2
View File
@@ -19,7 +19,6 @@ import {
ProviderKind,
ProviderRequestKind,
ProviderSandboxMode,
ProviderStartOptions,
ProviderUserInputAnswers,
RuntimeMode,
} from "./orchestration";
@@ -55,7 +54,6 @@ export const ProviderSessionStartInput = Schema.Struct({
resumeCursor: Schema.optional(Schema.Unknown),
approvalPolicy: Schema.optional(ProviderApprovalPolicy),
sandboxMode: Schema.optional(ProviderSandboxMode),
providerOptions: Schema.optional(ProviderStartOptions),
runtimeMode: RuntimeMode,
});
export type ProviderSessionStartInput = typeof ProviderSessionStartInput.Type;
+28 -9
View File
@@ -2,7 +2,9 @@ import { Schema } from "effect";
import { IsoDateTime, TrimmedNonEmptyString } from "./baseSchemas";
import { KeybindingRule, ResolvedKeybindingsConfig } from "./keybindings";
import { EditorId } from "./editor";
import { ModelCapabilities } from "./model";
import { ProviderKind } from "./orchestration";
import { ServerSettings } from "./settings";
const KeybindingsMalformedConfigIssue = Schema.Struct({
kind: Schema.Literal("keybindings.malformed-config"),
@@ -23,8 +25,8 @@ export type ServerConfigIssue = typeof ServerConfigIssue.Type;
const ServerConfigIssues = Schema.Array(ServerConfigIssue);
export const ServerProviderStatusState = Schema.Literals(["ready", "warning", "error"]);
export type ServerProviderStatusState = typeof ServerProviderStatusState.Type;
export const ServerProviderState = Schema.Literals(["ready", "warning", "error", "disabled"]);
export type ServerProviderState = typeof ServerProviderState.Type;
export const ServerProviderAuthStatus = Schema.Literals([
"authenticated",
@@ -33,25 +35,37 @@ export const ServerProviderAuthStatus = Schema.Literals([
]);
export type ServerProviderAuthStatus = typeof ServerProviderAuthStatus.Type;
export const ServerProviderStatus = Schema.Struct({
export const ServerProviderModel = Schema.Struct({
slug: TrimmedNonEmptyString,
name: TrimmedNonEmptyString,
isCustom: Schema.Boolean,
capabilities: Schema.NullOr(ModelCapabilities),
});
export type ServerProviderModel = typeof ServerProviderModel.Type;
export const ServerProvider = Schema.Struct({
provider: ProviderKind,
status: ServerProviderStatusState,
available: Schema.Boolean,
enabled: Schema.Boolean,
installed: Schema.Boolean,
version: Schema.NullOr(TrimmedNonEmptyString),
status: ServerProviderState,
authStatus: ServerProviderAuthStatus,
checkedAt: IsoDateTime,
message: Schema.optional(TrimmedNonEmptyString),
models: Schema.Array(ServerProviderModel),
});
export type ServerProviderStatus = typeof ServerProviderStatus.Type;
export type ServerProvider = typeof ServerProvider.Type;
const ServerProviderStatuses = Schema.Array(ServerProviderStatus);
const ServerProviders = Schema.Array(ServerProvider);
export const ServerConfig = Schema.Struct({
cwd: TrimmedNonEmptyString,
keybindingsConfigPath: TrimmedNonEmptyString,
keybindings: ResolvedKeybindingsConfig,
issues: ServerConfigIssues,
providers: ServerProviderStatuses,
providers: ServerProviders,
availableEditors: Schema.Array(EditorId),
settings: ServerSettings,
});
export type ServerConfig = typeof ServerConfig.Type;
@@ -66,6 +80,11 @@ export type ServerUpsertKeybindingResult = typeof ServerUpsertKeybindingResult.T
export const ServerConfigUpdatedPayload = Schema.Struct({
issues: ServerConfigIssues,
providers: ServerProviderStatuses,
settings: Schema.optional(ServerSettings),
});
export type ServerConfigUpdatedPayload = typeof ServerConfigUpdatedPayload.Type;
export const ServerProviderUpdatedPayload = Schema.Struct({
providers: ServerProviders,
});
export type ServerProviderUpdatedPayload = typeof ServerProviderUpdatedPayload.Type;
+153
View File
@@ -0,0 +1,153 @@
import { Effect } from "effect";
import * as Schema from "effect/Schema";
import * as SchemaTransformation from "effect/SchemaTransformation";
import { TrimmedNonEmptyString, TrimmedString } from "./baseSchemas";
import {
ClaudeModelOptions,
CodexModelOptions,
DEFAULT_GIT_TEXT_GENERATION_MODEL_BY_PROVIDER,
} from "./model";
import { ModelSelection } from "./orchestration";
// ── Client Settings (local-only) ───────────────────────────────
export const TimestampFormat = Schema.Literals(["locale", "12-hour", "24-hour"]);
export type TimestampFormat = typeof TimestampFormat.Type;
export const DEFAULT_TIMESTAMP_FORMAT: TimestampFormat = "locale";
export const SidebarProjectSortOrder = Schema.Literals(["updated_at", "created_at", "manual"]);
export type SidebarProjectSortOrder = typeof SidebarProjectSortOrder.Type;
export const DEFAULT_SIDEBAR_PROJECT_SORT_ORDER: SidebarProjectSortOrder = "updated_at";
export const SidebarThreadSortOrder = Schema.Literals(["updated_at", "created_at"]);
export type SidebarThreadSortOrder = typeof SidebarThreadSortOrder.Type;
export const DEFAULT_SIDEBAR_THREAD_SORT_ORDER: SidebarThreadSortOrder = "updated_at";
export const ClientSettingsSchema = Schema.Struct({
confirmThreadDelete: Schema.Boolean.pipe(Schema.withDecodingDefault(() => true)),
diffWordWrap: Schema.Boolean.pipe(Schema.withDecodingDefault(() => false)),
sidebarProjectSortOrder: SidebarProjectSortOrder.pipe(
Schema.withDecodingDefault(() => DEFAULT_SIDEBAR_PROJECT_SORT_ORDER),
),
sidebarThreadSortOrder: SidebarThreadSortOrder.pipe(
Schema.withDecodingDefault(() => DEFAULT_SIDEBAR_THREAD_SORT_ORDER),
),
timestampFormat: TimestampFormat.pipe(Schema.withDecodingDefault(() => DEFAULT_TIMESTAMP_FORMAT)),
});
export type ClientSettings = typeof ClientSettingsSchema.Type;
export const DEFAULT_CLIENT_SETTINGS: ClientSettings = Schema.decodeSync(ClientSettingsSchema)({});
// ── Server Settings (server-authoritative) ────────────────────
export const ThreadEnvMode = Schema.Literals(["local", "worktree"]);
export type ThreadEnvMode = typeof ThreadEnvMode.Type;
const makeBinaryPathSetting = (fallback: string) =>
TrimmedString.pipe(
Schema.decodeTo(
Schema.String,
SchemaTransformation.transformOrFail({
decode: (value) => Effect.succeed(value || fallback),
encode: (value) => Effect.succeed(value),
}),
),
Schema.withDecodingDefault(() => fallback),
);
export const CodexSettings = Schema.Struct({
enabled: Schema.Boolean.pipe(Schema.withDecodingDefault(() => true)),
binaryPath: makeBinaryPathSetting("codex"),
homePath: TrimmedString.pipe(Schema.withDecodingDefault(() => "")),
customModels: Schema.Array(Schema.String).pipe(Schema.withDecodingDefault(() => [])),
});
export type CodexSettings = typeof CodexSettings.Type;
export const ClaudeSettings = Schema.Struct({
enabled: Schema.Boolean.pipe(Schema.withDecodingDefault(() => true)),
binaryPath: makeBinaryPathSetting("claude"),
customModels: Schema.Array(Schema.String).pipe(Schema.withDecodingDefault(() => [])),
});
export type ClaudeSettings = typeof ClaudeSettings.Type;
export const ServerSettings = Schema.Struct({
enableAssistantStreaming: Schema.Boolean.pipe(Schema.withDecodingDefault(() => false)),
defaultThreadEnvMode: ThreadEnvMode.pipe(
Schema.withDecodingDefault(() => "local" as const satisfies ThreadEnvMode),
),
textGenerationModelSelection: ModelSelection.pipe(
Schema.withDecodingDefault(() => ({
provider: "codex" as const,
model: DEFAULT_GIT_TEXT_GENERATION_MODEL_BY_PROVIDER.codex,
})),
),
// Provider specific settings
providers: Schema.Struct({
codex: CodexSettings.pipe(Schema.withDecodingDefault(() => ({}))),
claudeAgent: ClaudeSettings.pipe(Schema.withDecodingDefault(() => ({}))),
}).pipe(Schema.withDecodingDefault(() => ({}))),
});
export type ServerSettings = typeof ServerSettings.Type;
export const DEFAULT_SERVER_SETTINGS: ServerSettings = Schema.decodeSync(ServerSettings)({});
// ── Unified type ─────────────────────────────────────────────────────
export type UnifiedSettings = ServerSettings & ClientSettings;
export const DEFAULT_UNIFIED_SETTINGS: UnifiedSettings = {
...DEFAULT_SERVER_SETTINGS,
...DEFAULT_CLIENT_SETTINGS,
};
// ── Server Settings Patch (replace with a Schema.deepPartial if available) ──────────────────────────────────────────
const CodexModelOptionsPatch = Schema.Struct({
reasoningEffort: Schema.optionalKey(CodexModelOptions.fields.reasoningEffort),
fastMode: Schema.optionalKey(CodexModelOptions.fields.fastMode),
});
const ClaudeModelOptionsPatch = Schema.Struct({
thinking: Schema.optionalKey(ClaudeModelOptions.fields.thinking),
effort: Schema.optionalKey(ClaudeModelOptions.fields.effort),
fastMode: Schema.optionalKey(ClaudeModelOptions.fields.fastMode),
});
const ModelSelectionPatch = Schema.Union([
Schema.Struct({
provider: Schema.optionalKey(Schema.Literal("codex")),
model: Schema.optionalKey(TrimmedNonEmptyString),
options: Schema.optionalKey(CodexModelOptionsPatch),
}),
Schema.Struct({
provider: Schema.optionalKey(Schema.Literal("claudeAgent")),
model: Schema.optionalKey(TrimmedNonEmptyString),
options: Schema.optionalKey(ClaudeModelOptionsPatch),
}),
]);
const CodexSettingsPatch = Schema.Struct({
enabled: Schema.optionalKey(Schema.Boolean),
binaryPath: Schema.optionalKey(Schema.String),
homePath: Schema.optionalKey(Schema.String),
customModels: Schema.optionalKey(Schema.Array(Schema.String)),
});
const ClaudeSettingsPatch = Schema.Struct({
enabled: Schema.optionalKey(Schema.Boolean),
binaryPath: Schema.optionalKey(Schema.String),
customModels: Schema.optionalKey(Schema.Array(Schema.String)),
});
export const ServerSettingsPatch = Schema.Struct({
enableAssistantStreaming: Schema.optionalKey(Schema.Boolean),
defaultThreadEnvMode: Schema.optionalKey(ThreadEnvMode),
textGenerationModelSelection: Schema.optionalKey(ModelSelectionPatch),
providers: Schema.optionalKey(
Schema.Struct({
codex: Schema.optionalKey(CodexSettingsPatch),
claudeAgent: Schema.optionalKey(ClaudeSettingsPatch),
}),
),
});
export type ServerSettingsPatch = typeof ServerSettingsPatch.Type;
+19
View File
@@ -119,6 +119,25 @@ it.effect("accepts git.actionProgress push envelopes", () =>
}),
);
it.effect("accepts server.providersUpdated push envelopes", () =>
Effect.gen(function* () {
const parsed = yield* decodeWsResponse({
type: "push",
sequence: 4,
channel: WS_CHANNELS.serverProvidersUpdated,
data: {
providers: [],
},
});
if (!("type" in parsed) || parsed.type !== "push") {
assert.fail("expected websocket response to decode as a push envelope");
}
assert.strictEqual(parsed.channel, WS_CHANNELS.serverProvidersUpdated);
}),
);
it.effect("rejects push envelopes when channel payload does not match the channel schema", () =>
Effect.gen(function* () {
const result = yield* Effect.exit(
+14 -1
View File
@@ -37,7 +37,8 @@ import {
import { KeybindingRule } from "./keybindings";
import { ProjectSearchEntriesInput, ProjectWriteFileInput } from "./project";
import { OpenInEditorInput } from "./editor";
import { ServerConfigUpdatedPayload } from "./server";
import { ServerConfigUpdatedPayload, ServerProviderUpdatedPayload } from "./server";
import { ServerSettingsPatch } from "./settings";
// ── WebSocket RPC Method Names ───────────────────────────────────────
@@ -76,6 +77,8 @@ export const WS_METHODS = {
// Server meta
serverGetConfig: "server.getConfig",
serverUpsertKeybinding: "server.upsertKeybinding",
serverGetSettings: "server.getSettings",
serverUpdateSettings: "server.updateSettings",
} as const;
// ── Push Event Channels ──────────────────────────────────────────────
@@ -85,6 +88,7 @@ export const WS_CHANNELS = {
terminalEvent: "terminal.event",
serverWelcome: "server.welcome",
serverConfigUpdated: "server.configUpdated",
serverProvidersUpdated: "server.providersUpdated",
} as const;
// -- Tagged Union of all request body schemas ─────────────────────────
@@ -141,6 +145,8 @@ const WebSocketRequestBody = Schema.Union([
// Server meta
tagRequestBody(WS_METHODS.serverGetConfig, Schema.Struct({})),
tagRequestBody(WS_METHODS.serverUpsertKeybinding, KeybindingRule),
tagRequestBody(WS_METHODS.serverGetSettings, Schema.Struct({})),
tagRequestBody(WS_METHODS.serverUpdateSettings, Schema.Struct({ patch: ServerSettingsPatch })),
]);
export const WebSocketRequest = Schema.Struct({
@@ -174,6 +180,7 @@ export type WsWelcomePayload = typeof WsWelcomePayload.Type;
export interface WsPushPayloadByChannel {
readonly [WS_CHANNELS.serverWelcome]: WsWelcomePayload;
readonly [WS_CHANNELS.serverConfigUpdated]: typeof ServerConfigUpdatedPayload.Type;
readonly [WS_CHANNELS.serverProvidersUpdated]: typeof ServerProviderUpdatedPayload.Type;
readonly [WS_CHANNELS.gitActionProgress]: typeof GitActionProgressEvent.Type;
readonly [WS_CHANNELS.terminalEvent]: typeof TerminalEvent.Type;
readonly [ORCHESTRATION_WS_CHANNELS.domainEvent]: OrchestrationEvent;
@@ -198,6 +205,10 @@ export const WsPushServerConfigUpdated = makeWsPushSchema(
WS_CHANNELS.serverConfigUpdated,
ServerConfigUpdatedPayload,
);
export const WsPushServerProvidersUpdated = makeWsPushSchema(
WS_CHANNELS.serverProvidersUpdated,
ServerProviderUpdatedPayload,
);
export const WsPushGitActionProgress = makeWsPushSchema(
WS_CHANNELS.gitActionProgress,
GitActionProgressEvent,
@@ -212,6 +223,7 @@ export const WsPushChannelSchema = Schema.Literals([
WS_CHANNELS.gitActionProgress,
WS_CHANNELS.serverWelcome,
WS_CHANNELS.serverConfigUpdated,
WS_CHANNELS.serverProvidersUpdated,
WS_CHANNELS.terminalEvent,
ORCHESTRATION_WS_CHANNELS.domainEvent,
]);
@@ -220,6 +232,7 @@ export type WsPushChannelSchema = typeof WsPushChannelSchema.Type;
export const WsPush = Schema.Union([
WsPushServerWelcome,
WsPushServerConfigUpdated,
WsPushServerProvidersUpdated,
WsPushGitActionProgress,
WsPushTerminalEvent,
WsPushOrchestrationDomainEvent,
+4
View File
@@ -31,6 +31,10 @@
"./schemaJson": {
"types": "./src/schemaJson.ts",
"import": "./src/schemaJson.ts"
},
"./Struct": {
"types": "./src/Struct.ts",
"import": "./src/Struct.ts"
}
},
"scripts": {
+17
View File
@@ -0,0 +1,17 @@
import * as P from "effect/Predicate";
export function deepMerge<T>(current: T, patch: unknown): T {
if (!P.isObject(current) || !P.isObject(patch)) {
return patch as T;
}
const next = { ...current } as Record<string, unknown>;
for (const [key, value] of Object.entries(patch)) {
if (value === undefined) continue;
const existing = next[key];
next[key] = P.isObject(existing) && P.isObject(value) ? deepMerge(existing, value) : value;
}
return next as T;
}
+62 -275
View File
@@ -2,31 +2,46 @@ import { describe, expect, it } from "vitest";
import {
DEFAULT_MODEL,
DEFAULT_MODEL_BY_PROVIDER,
MODEL_OPTIONS,
MODEL_OPTIONS_BY_PROVIDER,
CODEX_REASONING_EFFORT_OPTIONS,
type ModelCapabilities,
} from "@t3tools/contracts";
import {
applyClaudePromptEffortPrefix,
getDefaultModel,
getModelCapabilities,
getModelOptions,
isClaudeUltrathinkPrompt,
normalizeClaudeModelOptions,
normalizeCodexModelOptions,
normalizeModelSlug,
resolveSelectableModel,
resolveModelSlug,
resolveModelSlugForProvider,
getDefaultEffort,
hasEffortLevel,
isClaudeUltrathinkPrompt,
normalizeModelSlug,
resolveModelSlug,
resolveModelSlugForProvider,
resolveSelectableModel,
trimOrNull,
} from "./model";
const codexCaps: ModelCapabilities = {
reasoningEffortLevels: [
{ value: "xhigh", label: "Extra High" },
{ value: "high", label: "High", isDefault: true },
],
supportsFastMode: true,
supportsThinkingToggle: false,
promptInjectedEffortLevels: [],
};
const claudeCaps: ModelCapabilities = {
reasoningEffortLevels: [
{ value: "medium", label: "Medium" },
{ value: "high", label: "High", isDefault: true },
{ value: "ultrathink", label: "Ultrathink" },
],
supportsFastMode: false,
supportsThinkingToggle: false,
promptInjectedEffortLevels: ["ultrathink"],
};
describe("normalizeModelSlug", () => {
it("maps known aliases to canonical slugs", () => {
expect(normalizeModelSlug("5.3")).toBe("gpt-5.3-codex");
expect(normalizeModelSlug("gpt-5.3")).toBe("gpt-5.3-codex");
expect(normalizeModelSlug("sonnet", "claudeAgent")).toBe("claude-sonnet-4-6");
});
it("returns null for empty or missing values", () => {
@@ -35,290 +50,62 @@ describe("normalizeModelSlug", () => {
expect(normalizeModelSlug(null)).toBeNull();
expect(normalizeModelSlug(undefined)).toBeNull();
});
it("preserves non-aliased model slugs", () => {
expect(normalizeModelSlug("gpt-5.2")).toBe("gpt-5.2");
expect(normalizeModelSlug("gpt-5.2-codex")).toBe("gpt-5.2-codex");
});
it("does not leak prototype properties as aliases", () => {
expect(normalizeModelSlug("toString")).toBe("toString");
expect(normalizeModelSlug("constructor")).toBe("constructor");
});
it("uses provider-specific aliases", () => {
expect(normalizeModelSlug("sonnet", "claudeAgent")).toBe("claude-sonnet-4-6");
expect(normalizeModelSlug("opus-4.6", "claudeAgent")).toBe("claude-opus-4-6");
expect(normalizeModelSlug("claude-haiku-4-5-20251001", "claudeAgent")).toBe("claude-haiku-4-5");
});
});
describe("resolveModelSlug", () => {
it("returns default only when the model is missing", () => {
it("returns defaults when the model is missing", () => {
expect(resolveModelSlug(undefined)).toBe(DEFAULT_MODEL);
expect(resolveModelSlug(null)).toBe(DEFAULT_MODEL);
});
it("preserves unknown custom models", () => {
expect(resolveModelSlug("gpt-4.1")).toBe(DEFAULT_MODEL);
expect(resolveModelSlug("custom/internal-model")).toBe(DEFAULT_MODEL);
});
it("resolves only supported model options", () => {
for (const model of MODEL_OPTIONS) {
expect(resolveModelSlug(model.slug)).toBe(model.slug);
}
});
it("supports provider-aware resolution", () => {
expect(resolveModelSlugForProvider("claudeAgent", undefined)).toBe(
DEFAULT_MODEL_BY_PROVIDER.claudeAgent,
);
expect(resolveModelSlugForProvider("claudeAgent", "sonnet")).toBe("claude-sonnet-4-6");
expect(resolveModelSlugForProvider("claudeAgent", "gpt-5.3-codex")).toBe(
DEFAULT_MODEL_BY_PROVIDER.claudeAgent,
);
});
it("keeps codex defaults for backward compatibility", () => {
expect(getDefaultModel()).toBe(DEFAULT_MODEL);
expect(getModelOptions()).toEqual(MODEL_OPTIONS);
expect(getModelOptions("claudeAgent")).toEqual(MODEL_OPTIONS_BY_PROVIDER.claudeAgent);
it("preserves normalized unknown models", () => {
expect(resolveModelSlug("custom/internal-model")).toBe("custom/internal-model");
});
});
describe("resolveSelectableModel", () => {
it("resolves exact slug matches", () => {
expect(
resolveSelectableModel("codex", "gpt-5.3-codex", [
{ slug: "gpt-5.4", name: "GPT-5.4" },
{ slug: "gpt-5.3-codex", name: "GPT-5.3 Codex" },
]),
).toBe("gpt-5.3-codex");
});
it("resolves case-insensitive display-name matches", () => {
expect(
resolveSelectableModel("codex", "gpt-5.3 codex", [
{ slug: "gpt-5.4", name: "GPT-5.4" },
{ slug: "gpt-5.3-codex", name: "GPT-5.3 Codex" },
]),
).toBe("gpt-5.3-codex");
});
it("resolves provider-specific aliases after normalization", () => {
expect(
resolveSelectableModel("claudeAgent", "sonnet", [
{ slug: "claude-opus-4-6", name: "Claude Opus 4.6" },
{ slug: "claude-sonnet-4-6", name: "Claude Sonnet 4.6" },
]),
).toBe("claude-sonnet-4-6");
});
it("returns null for empty input", () => {
expect(resolveSelectableModel("codex", "", [{ slug: "gpt-5.4", name: "GPT-5.4" }])).toBeNull();
expect(
resolveSelectableModel("codex", " ", [{ slug: "gpt-5.4", name: "GPT-5.4" }]),
).toBeNull();
expect(
resolveSelectableModel("codex", null, [{ slug: "gpt-5.4", name: "GPT-5.4" }]),
).toBeNull();
});
it("returns null for unknown values that are not present in options", () => {
expect(
resolveSelectableModel("codex", "gpt-4.1", [{ slug: "gpt-5.4", name: "GPT-5.4" }]),
).toBeNull();
});
it("does not accept normalized custom-looking slugs unless they exist in options", () => {
expect(
resolveSelectableModel("codex", "custom/internal-model", [
{ slug: "gpt-5.4", name: "GPT-5.4" },
]),
).toBeNull();
});
it("respects provider boundaries", () => {
expect(
resolveSelectableModel("codex", "sonnet", [{ slug: "gpt-5.3-codex", name: "GPT-5.3 Codex" }]),
).toBeNull();
expect(
resolveSelectableModel("claudeAgent", "5.3", [
{ slug: "claude-sonnet-4-6", name: "Claude Sonnet 4.6" },
]),
).toBeNull();
it("resolves exact slugs, labels, and aliases", () => {
const options = [
{ slug: "gpt-5.3-codex", name: "GPT-5.3 Codex" },
{ slug: "claude-sonnet-4-6", name: "Claude Sonnet 4.6" },
];
expect(resolveSelectableModel("codex", "gpt-5.3-codex", options)).toBe("gpt-5.3-codex");
expect(resolveSelectableModel("codex", "gpt-5.3 codex", options)).toBe("gpt-5.3-codex");
expect(resolveSelectableModel("claudeAgent", "sonnet", options)).toBe("claude-sonnet-4-6");
});
});
describe("getModelCapabilities reasoningEffortLevels", () => {
const values = (provider: "codex" | "claudeAgent", model: string | null) =>
getModelCapabilities(provider, model).reasoningEffortLevels.map((l) => l.value);
it("returns codex reasoning options for codex", () => {
expect(values("codex", "gpt-5.4")).toEqual([...CODEX_REASONING_EFFORT_OPTIONS]);
describe("capability helpers", () => {
it("reads default efforts", () => {
expect(getDefaultEffort(codexCaps)).toBe("high");
expect(getDefaultEffort(claudeCaps)).toBe("high");
});
it("returns claude effort options for Opus 4.6", () => {
expect(values("claudeAgent", "claude-opus-4-6")).toEqual([
"low",
"medium",
"high",
"max",
"ultrathink",
]);
});
it("returns claude effort options for Sonnet 4.6", () => {
expect(values("claudeAgent", "claude-sonnet-4-6")).toEqual([
"low",
"medium",
"high",
"ultrathink",
]);
});
it("returns no claude effort options for Haiku 4.5", () => {
expect(values("claudeAgent", "claude-haiku-4-5")).toEqual([]);
});
it("co-locates labels with effort values", () => {
const levels = getModelCapabilities("claudeAgent", "claude-opus-4-6").reasoningEffortLevels;
const high = levels.find((l) => l.value === "high");
expect(high).toEqual({ value: "high", label: "High", isDefault: true });
const xhigh = getModelCapabilities("codex", "gpt-5.4").reasoningEffortLevels.find(
(l) => l.value === "xhigh",
);
expect(xhigh).toEqual({ value: "xhigh", label: "Extra High" });
});
});
describe("getDefaultEffort", () => {
it("returns the default effort from capabilities", () => {
expect(getDefaultEffort(getModelCapabilities("codex", "gpt-5.4"))).toBe("high");
expect(getDefaultEffort(getModelCapabilities("claudeAgent", "claude-opus-4-6"))).toBe("high");
expect(getDefaultEffort(getModelCapabilities("claudeAgent", "claude-haiku-4-5"))).toBeNull();
});
});
describe("hasEffortLevel", () => {
it("validates effort against model capabilities", () => {
const opusCaps = getModelCapabilities("claudeAgent", "claude-opus-4-6");
expect(hasEffortLevel(opusCaps, "max")).toBe(true);
expect(hasEffortLevel(opusCaps, "xhigh")).toBe(false);
const codexCaps = getModelCapabilities("codex", "gpt-5.4");
it("checks effort support", () => {
expect(hasEffortLevel(codexCaps, "xhigh")).toBe(true);
expect(hasEffortLevel(codexCaps, "max")).toBe(false);
});
});
describe("applyClaudePromptEffortPrefix", () => {
it("prefixes ultrathink prompts exactly once", () => {
expect(applyClaudePromptEffortPrefix("Investigate this", "ultrathink")).toBe(
"Ultrathink:\nInvestigate this",
);
expect(applyClaudePromptEffortPrefix("Ultrathink:\nInvestigate this", "ultrathink")).toBe(
"Ultrathink:\nInvestigate this",
);
});
it("leaves non-ultrathink prompts unchanged", () => {
expect(applyClaudePromptEffortPrefix("Investigate this", "high")).toBe("Investigate this");
});
});
describe("normalizeCodexModelOptions", () => {
it("drops default-only codex options", () => {
expect(
normalizeCodexModelOptions("gpt-5.4", { reasoningEffort: "high", fastMode: false }),
).toBeUndefined();
});
it("preserves non-default codex options", () => {
expect(
normalizeCodexModelOptions("gpt-5.4", { reasoningEffort: "xhigh", fastMode: true }),
).toEqual({
reasoningEffort: "xhigh",
fastMode: true,
});
});
});
describe("normalizeClaudeModelOptions", () => {
it("drops unsupported fast mode and max effort for Sonnet", () => {
expect(
normalizeClaudeModelOptions("claude-sonnet-4-6", {
effort: "max",
fastMode: true,
}),
).toBeUndefined();
});
it("keeps the Haiku thinking toggle and removes unsupported effort", () => {
expect(
normalizeClaudeModelOptions("claude-haiku-4-5", {
thinking: false,
effort: "high",
}),
).toEqual({
thinking: false,
});
});
});
describe("getModelCapabilities Claude capability flags", () => {
it("only enables adaptive reasoning for Opus 4.6 and Sonnet 4.6", () => {
const has = (m: string | undefined) =>
getModelCapabilities("claudeAgent", m).reasoningEffortLevels.length > 0;
expect(has("claude-opus-4-6")).toBe(true);
expect(has("claude-sonnet-4-6")).toBe(true);
expect(has("claude-haiku-4-5")).toBe(false);
expect(has(undefined)).toBe(false);
});
it("only enables max effort for Opus 4.6", () => {
const has = (m: string | undefined) =>
getModelCapabilities("claudeAgent", m).reasoningEffortLevels.some((l) => l.value === "max");
expect(has("claude-opus-4-6")).toBe(true);
expect(has("claude-sonnet-4-6")).toBe(false);
expect(has("claude-haiku-4-5")).toBe(false);
expect(has(undefined)).toBe(false);
});
it("only enables Claude fast mode for Opus 4.6", () => {
const has = (m: string | undefined) => getModelCapabilities("claudeAgent", m).supportsFastMode;
expect(has("claude-opus-4-6")).toBe(true);
expect(has("opus")).toBe(true);
expect(has("claude-sonnet-4-6")).toBe(false);
expect(has("claude-haiku-4-5")).toBe(false);
expect(has(undefined)).toBe(false);
});
it("only enables ultrathink keyword handling for Opus 4.6 and Sonnet 4.6", () => {
const has = (m: string | undefined) =>
getModelCapabilities("claudeAgent", m).reasoningEffortLevels.length > 0;
expect(has("claude-opus-4-6")).toBe(true);
expect(has("claude-sonnet-4-6")).toBe(true);
expect(has("claude-haiku-4-5")).toBe(false);
});
it("only enables the Claude thinking toggle for Haiku 4.5", () => {
const has = (m: string | undefined) =>
getModelCapabilities("claudeAgent", m).supportsThinkingToggle;
expect(has("claude-opus-4-6")).toBe(false);
expect(has("claude-sonnet-4-6")).toBe(false);
expect(has("claude-haiku-4-5")).toBe(true);
expect(has("haiku")).toBe(true);
expect(has(undefined)).toBe(false);
});
});
describe("isClaudeUltrathinkPrompt", () => {
it("detects ultrathink prompts case-insensitively", () => {
expect(isClaudeUltrathinkPrompt("Please ultrathink about this")).toBe(true);
describe("misc helpers", () => {
it("detects ultrathink prompts", () => {
expect(isClaudeUltrathinkPrompt("Ultrathink:\nInvestigate")).toBe(true);
expect(isClaudeUltrathinkPrompt("Think hard about this")).toBe(false);
expect(isClaudeUltrathinkPrompt(undefined)).toBe(false);
expect(isClaudeUltrathinkPrompt("Investigate")).toBe(false);
});
it("prefixes ultrathink prompts once", () => {
expect(applyClaudePromptEffortPrefix("Investigate", "ultrathink")).toBe(
"Ultrathink:\nInvestigate",
);
expect(applyClaudePromptEffortPrefix("Ultrathink:\nInvestigate", "ultrathink")).toBe(
"Ultrathink:\nInvestigate",
);
});
it("trims strings to null", () => {
expect(trimOrNull(" hi ")).toBe("hi");
expect(trimOrNull(" ")).toBeNull();
});
});
+1 -77
View File
@@ -1,31 +1,17 @@
import {
DEFAULT_MODEL_BY_PROVIDER,
MODEL_CAPABILITIES_INDEX,
MODEL_OPTIONS_BY_PROVIDER,
MODEL_SLUG_ALIASES_BY_PROVIDER,
type ClaudeModelOptions,
type ClaudeCodeEffort,
type CodexModelOptions,
type ModelCapabilities,
type ModelSlug,
type ProviderKind,
CodexReasoningEffort,
} from "@t3tools/contracts";
const MODEL_SLUG_SET_BY_PROVIDER: Record<ProviderKind, ReadonlySet<ModelSlug>> = {
claudeAgent: new Set(MODEL_OPTIONS_BY_PROVIDER.claudeAgent.map((option) => option.slug)),
codex: new Set(MODEL_OPTIONS_BY_PROVIDER.codex.map((option) => option.slug)),
};
export interface SelectableModelOption {
slug: string;
name: string;
}
export function getModelOptions(provider: ProviderKind = "codex") {
return MODEL_OPTIONS_BY_PROVIDER[provider];
}
export function getDefaultModel(provider: ProviderKind = "codex"): ModelSlug {
return DEFAULT_MODEL_BY_PROVIDER[provider];
}
@@ -42,24 +28,6 @@ export function getDefaultEffort(caps: ModelCapabilities): string | null {
return caps.reasoningEffortLevels.find((l) => l.isDefault)?.value ?? null;
}
// ── Data-driven capability resolver ───────────────────────────────────
export function getModelCapabilities(
provider: ProviderKind,
model: string | null | undefined,
): ModelCapabilities {
const slug = normalizeModelSlug(model, provider);
if (slug && MODEL_CAPABILITIES_INDEX[provider]?.[slug]) {
return MODEL_CAPABILITIES_INDEX[provider][slug];
}
return {
reasoningEffortLevels: [],
supportsFastMode: false,
supportsThinkingToggle: false,
promptInjectedEffortLevels: [],
};
}
export function isClaudeUltrathinkPrompt(text: string | null | undefined): boolean {
return typeof text === "string" && /\bultrathink\b/i.test(text);
}
@@ -125,10 +93,7 @@ export function resolveModelSlug(
if (!normalized) {
return DEFAULT_MODEL_BY_PROVIDER[provider];
}
return MODEL_SLUG_SET_BY_PROVIDER[provider].has(normalized)
? normalized
: DEFAULT_MODEL_BY_PROVIDER[provider];
return normalized;
}
export function resolveModelSlugForProvider(
@@ -145,47 +110,6 @@ export function trimOrNull<T extends string>(value: T | null | undefined): T | n
return trimmed || null;
}
export function normalizeCodexModelOptions(
model: string | null | undefined,
modelOptions: CodexModelOptions | null | undefined,
): CodexModelOptions | undefined {
const caps = getModelCapabilities("codex", model);
const defaultReasoningEffort = getDefaultEffort(caps) as CodexReasoningEffort;
const reasoningEffort = trimOrNull(modelOptions?.reasoningEffort) ?? defaultReasoningEffort;
const fastModeEnabled = modelOptions?.fastMode === true;
const nextOptions: CodexModelOptions = {
...(reasoningEffort !== defaultReasoningEffort ? { reasoningEffort } : {}),
...(fastModeEnabled ? { fastMode: true } : {}),
};
return Object.keys(nextOptions).length > 0 ? nextOptions : undefined;
}
export function normalizeClaudeModelOptions(
model: string | null | undefined,
modelOptions: ClaudeModelOptions | null | undefined,
): ClaudeModelOptions | undefined {
const caps = getModelCapabilities("claudeAgent", model);
const defaultReasoningEffort = getDefaultEffort(caps);
const resolvedEffort = trimOrNull(modelOptions?.effort);
const isPromptInjected = caps.promptInjectedEffortLevels.includes(resolvedEffort ?? "");
const effort =
resolvedEffort &&
!isPromptInjected &&
hasEffortLevel(caps, resolvedEffort) &&
resolvedEffort !== defaultReasoningEffort
? resolvedEffort
: undefined;
const thinking =
caps.supportsThinkingToggle && modelOptions?.thinking === false ? false : undefined;
const fastMode = caps.supportsFastMode && modelOptions?.fastMode === true ? true : undefined;
const nextOptions: ClaudeModelOptions = {
...(thinking === false ? { thinking: false } : {}),
...(effort ? { effort } : {}),
...(fastMode ? { fastMode: true } : {}),
};
return Object.keys(nextOptions).length > 0 ? nextOptions : undefined;
}
export function applyClaudePromptEffortPrefix(
text: string,
effort: ClaudeCodeEffort | null | undefined,
+63 -1
View File
@@ -1,4 +1,14 @@
import { Cause, Exit, Result, Schema, SchemaIssue } from "effect";
import {
Cause,
Effect,
Exit,
Option,
Result,
Schema,
SchemaGetter,
SchemaIssue,
SchemaTransformation,
} from "effect";
export const decodeJsonResult = <S extends Schema.Codec<unknown, unknown, never, never>>(
schema: S,
@@ -32,3 +42,55 @@ export const formatSchemaError = (cause: Cause.Cause<Schema.SchemaError>) => {
? SchemaIssue.makeFormatterDefault()(squashed.issue)
: Cause.pretty(cause);
};
/**
* A `Getter` that parses a lenient JSON string (tolerating trailing commas
* and JS-style comments) into an unknown value.
*
* Mirrors `SchemaGetter.parseJson()` but uses `parseLenientJson` instead
* of `JSON.parse`.
*/
const parseLenientJsonGetter = SchemaGetter.onSome((input: string) =>
Effect.try({
try: () => {
// Strip single-line comments — alternation preserves quoted strings.
let stripped = input.replace(
/("(?:[^"\\]|\\.)*")|\/\/[^\n]*/g,
(match, stringLiteral: string | undefined) => (stringLiteral ? match : ""),
);
// Strip multi-line comments.
stripped = stripped.replace(
/("(?:[^"\\]|\\.)*")|\/\*[\s\S]*?\*\//g,
(match, stringLiteral: string | undefined) => (stringLiteral ? match : ""),
);
// Strip trailing commas before `}` or `]`.
stripped = stripped.replace(/,(\s*[}\]])/g, "$1");
return Option.some(JSON.parse(stripped));
},
catch: (e) => new SchemaIssue.InvalidValue(Option.some(input), { message: String(e) }),
}),
);
/**
* Schema transformation: lenient JSONC string ↔ unknown.
*
* Same API as `SchemaTransformation.fromJsonString`, but the decode side
* strips trailing commas and JS-style comments before parsing.
* Encoding produces strict JSON via `JSON.stringify`.
*/
export const fromLenientJsonString = new SchemaTransformation.Transformation(
parseLenientJsonGetter,
SchemaGetter.stringifyJson(),
);
/**
* Build a schema that decodes a lenient JSON string into `A`.
*
* Drop-in replacement for `Schema.fromJsonString(schema)` that tolerates
* trailing commas and comments in the input.
*/
export const fromLenientJson = <S extends Schema.Top>(schema: S) =>
Schema.String.pipe(Schema.decodeTo(schema, fromLenientJsonString));