Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions apps/server/src/provider/Layers/ClaudeAdapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2384,6 +2384,85 @@ describe("ClaudeAdapterLive", () => {
);
});

it.effect(
"does not re-set the Claude model when the session already uses the same effective API model",
() => {
const harness = makeHarness();
return Effect.gen(function* () {
const adapter = yield* ClaudeAdapter;
const modelSelection = {
provider: "claudeAgent" as const,
model: "claude-opus-4-6",
};

const session = yield* adapter.startSession({
threadId: THREAD_ID,
provider: "claudeAgent",
modelSelection,
runtimeMode: "full-access",
});

yield* adapter.sendTurn({
threadId: session.threadId,
input: "hello",
modelSelection,
attachments: [],
});
yield* adapter.sendTurn({
threadId: session.threadId,
input: "hello again",
modelSelection,
attachments: [],
});

assert.deepEqual(harness.query.setModelCalls, []);
}).pipe(
Effect.provideService(Random.Random, makeDeterministicRandomService()),
Effect.provide(harness.layer),
);
},
);

it.effect("re-sets the Claude model when the effective API model changes", () => {
const harness = makeHarness();
return Effect.gen(function* () {
const adapter = yield* ClaudeAdapter;

const session = yield* adapter.startSession({
threadId: THREAD_ID,
provider: "claudeAgent",
runtimeMode: "full-access",
});

yield* adapter.sendTurn({
threadId: session.threadId,
input: "hello",
modelSelection: {
provider: "claudeAgent",
model: "claude-opus-4-6",
options: {
contextWindow: "1m",
},
},
attachments: [],
});
yield* adapter.sendTurn({
threadId: session.threadId,
input: "hello again",
modelSelection: {
provider: "claudeAgent",
model: "claude-opus-4-6",
},
attachments: [],
});

assert.deepEqual(harness.query.setModelCalls, ["claude-opus-4-6[1m]", "claude-opus-4-6"]);
}).pipe(
Effect.provideService(Random.Random, makeDeterministicRandomService()),
Effect.provide(harness.layer),
);
});

it.effect("sets plan permission mode on sendTurn when interactionMode is plan", () => {
const harness = makeHarness();
return Effect.gen(function* () {
Expand Down
17 changes: 13 additions & 4 deletions apps/server/src/provider/Layers/ClaudeAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ interface ClaudeSessionContext {
streamFiber: Fiber.Fiber<void, Error> | undefined;
readonly startedAt: string;
readonly basePermissionMode: PermissionMode | undefined;
currentApiModelId: string | undefined;
resumeSessionId: string | undefined;
readonly pendingApprovals: Map<ApprovalRequestId, PendingApproval>;
readonly pendingUserInputs: Map<ApprovalRequestId, PendingUserInput>;
Expand Down Expand Up @@ -2809,6 +2810,7 @@ function makeClaudeAdapter(options?: ClaudeAdapterLiveOptions) {
streamFiber: undefined,
startedAt,
basePermissionMode: permissionMode,
currentApiModelId: apiModelId,
resumeSessionId: sessionId,
pendingApprovals,
pendingUserInputs,
Expand Down Expand Up @@ -2898,10 +2900,17 @@ function makeClaudeAdapter(options?: ClaudeAdapterLiveOptions) {

if (modelSelection?.model) {
const apiModelId = resolveApiModelId(modelSelection);
yield* Effect.tryPromise({
try: () => context.query.setModel(apiModelId),
catch: (cause) => toRequestError(input.threadId, "turn/setModel", cause),
});
if (context.currentApiModelId !== apiModelId) {
yield* Effect.tryPromise({
try: () => context.query.setModel(apiModelId),
catch: (cause) => toRequestError(input.threadId, "turn/setModel", cause),
});
context.currentApiModelId = apiModelId;
}
context.session = {
...context.session,
model: modelSelection.model,
};
}

// Apply interaction mode by switching the SDK's permission mode.
Expand Down