# Files changed · PR \#10577 · pingdotgg/t3code

[View on GitCafe](https://git.cafe/pingdotgg/t3code/pull/10577/diffs)

Repository: [pingdotgg/t3code](https://git.cafe/pingdotgg/t3code)

Visibility: public

[refactor\(shared\): share model-selection command choice](https://git.cafe/pingdotgg/t3code/pull/10577.md)

Version: 2

Head commit: a645d4e33015b8657503ca7f978ab56b2d106f93

Base commit: 415ed0f73b97f1655b6282492f81d0b2bba3a9cc

Comparison base: 415ed0f73b97f1655b6282492f81d0b2bba3a9cc

Showing up to 20 entries from this revision. If the pull request changes, reopen the first page before continuing.

## packages/client-runtime/src/operations/commands.test.ts

Status: modified; +16; −1

```diff
@@ -534,12 +534,21 @@
       }).pipe(Effect.provide(TEST_CRYPTO_LAYER)),
   );
 
-  it.effect("uses provider.switch when model selection changes provider instance", () =>
+  it.effect("selects a model command by provider instance, not by model name", () =>
     Effect.gen(function* () {
       const commands: OrchestrationV2Command[] = [];
       const supervisor = yield* makeSupervisor({ commands, projects: [] });
 
       yield* updateThreadMetadata({
+        commandId: CommandId.make("same-provider"),
+        threadId: v2ThreadId,
+        modelSelection: {
+          instanceId: ProviderInstanceId.make("codex"),
+          model: "another-model",
+        },
+      }).pipe(Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor));
+
+      yield* updateThreadMetadata({
         commandId: CommandId.make("switch-provider"),
         threadId: v2ThreadId,
         modelSelection: {
@@ -550,6 +559,12 @@
 
       expect(commands).toEqual([
         {
+          type: "thread.model-selection.set",
+          commandId: "same-provider",
+          threadId: v2ThreadId,
+          modelSelection: { instanceId: "codex", model: "another-model" },
+        },
+        {
           type: "provider.switch",
           commandId: "switch-provider",
           threadId: v2ThreadId,
```

## packages/client-runtime/src/operations/commands.ts

Status: modified; +5; −4

```diff
@@ -25,6 +25,7 @@
 } from "@t3tools/contracts";
 import * as Crypto from "effect/Crypto";
 import * as Effect from "effect/Effect";
+import { modelSelectionCommandType } from "@t3tools/shared/model";
 
 import { request } from "../rpc/client.ts";
 
@@ -524,10 +525,10 @@
     }
     if (input.modelSelection !== undefined) {
       const projection = yield* getProjection(input.threadId);
+      const type = modelSelectionCommandType(
+        projection.thread.providerInstanceId,
+        input.modelSelection,
+      );
-      const type =
-        projection.thread.providerInstanceId === input.modelSelection.instanceId
-          ? ("thread.model-selection.set" as const)
-          : ("provider.switch" as const);
       result = yield* dispatch({
         type,
         commandId: result === null ? commandId : CommandId.make(`${commandId}:model-selection`),
```

## packages/shared/src/model.ts

Status: modified; +10; −0

```diff
@@ -14,6 +14,16 @@
 
 const DEFAULT_PROVIDER_DRIVER_KIND = ProviderDriverKind.make("codex");
 
+/** Choose the command for a model change against the thread's current provider instance. */
+export function modelSelectionCommandType(
+  currentInstanceId: ProviderInstanceId,
+  selection: ModelSelection,
+) {
+  return currentInstanceId === selection.instanceId
+    ? ("thread.model-selection.set" as const)
+    : ("provider.switch" as const);
+}
+
 export interface SelectableModelOption {
   slug: string;
   name: string;
```
