Skip to content
7 changes: 6 additions & 1 deletion packages/provider-cursor/src/server/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
InteractionUpdate,
McpServerConfig,
RunResult,
SDKModel,
SDKUserMessage,
SettingSource,
ToolCall,
Expand Down Expand Up @@ -312,11 +313,12 @@ export function makeCursorAgentOptions(input: {
readonly threadId: ThreadId;
readonly mcpSession: McpProviderSession.McpProviderSessionConfig | undefined;
readonly platform: NodeJS.Platform;
readonly sdkModels?: ReadonlyArray<Pick<SDKModel, "id" | "variants">>;
}): AgentOptions {
const policy = cursorRuntimeAgentPolicy(input.runtimePolicy, input.platform);
const mcpServers = cursorMcpServers(input.mcpSession);
return {
model: cursorSdkModelSelection(input.modelSelection, input.models),
model: cursorSdkModelSelection(input.modelSelection, input.models, input.sdkModels),
name: `T3 Code ${input.threadId}`,
mode: input.runtimePolicy.interactionMode === "plan" ? "plan" : "agent",
...(input.apiKey === undefined ? {} : { apiKey: input.apiKey }),
Expand Down Expand Up @@ -1016,6 +1018,7 @@ export interface CursorAdapterV2Options {
readonly instanceId: ProviderInstanceId;
readonly environment: NodeJS.ProcessEnv;
readonly getModels?: () => ReadonlyArray<ServerProviderModel>;
readonly getSdkModels?: () => ReadonlyArray<Pick<SDKModel, "id" | "variants">>;
}

export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* (
Expand Down Expand Up @@ -2551,6 +2554,7 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* (
platform,
threadId: openInput.threadId,
mcpSession: yield* mcpSessions.read(openInput.threadId),
sdkModels: adapterOptions.getSdkModels?.() ?? [],
});
if (
platform === "win32" &&
Expand Down Expand Up @@ -2771,6 +2775,7 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* (
model: cursorSdkModelSelection(
turnInput.modelSelection,
adapterOptions.getModels?.() ?? [],
adapterOptions.getSdkModels?.() ?? [],
),
mode: turnInput.runtimePolicy.interactionMode === "plan" ? "plan" : "agent",
...(Object.keys(turnMcpServers).length === 0
Expand Down
7 changes: 6 additions & 1 deletion packages/provider-cursor/src/server/driver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ it.layer(layerTest)("CursorDriver", (it) => {
{
displayName: "Claude Opus 5.5",
isDefault: true,
isMaxMode: true,
params: [{ id: "context", value: "1m" }],
},
],
Expand Down Expand Up @@ -389,7 +390,11 @@ it.layer(layerTest)("CursorDriver", (it) => {
allowance: DEFAULT_HANDOFF_TOKEN_CAP,
},
catalogDefault: {
model: { id: "claude-opus-5-5", params: [{ id: "context", value: "1m" }] },
model: {
id: "claude-opus-5-5",
params: [{ id: "context", value: "1m" }],
maxMode: true,
},
window: 1_000_000,
allowance: DEFAULT_HANDOFF_TOKEN_CAP,
},
Expand Down
13 changes: 13 additions & 0 deletions packages/provider-cursor/src/server/driver.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { SDKModel } from "@cursor/sdk";
import {
ProviderDriverKind,
ProviderSetupError,
Expand Down Expand Up @@ -164,10 +165,12 @@ export const CursorDriver: ProviderDriver<
});

let catalogModels: ReadonlyArray<ServerProviderModel> = [];
let catalogSdkModels: ReadonlyArray<SDKModel> = [];
const orchestrationAdapter = yield* makeCursorAdapterV2({
instanceId,
environment: processEnv,
getModels: () => catalogModels,
getSdkModels: () => catalogSdkModels,
}).pipe(
Effect.provideService(CursorAgentSdk.CursorAgentSdkRunner, {
...sdkRunner,
Expand Down Expand Up @@ -211,6 +214,7 @@ export const CursorDriver: ProviderDriver<
auth.requireApiKey,
auth.withAccess,
() => catalogModels,
() => catalogSdkModels,
);

const checkProvider = auth.readApiKey.pipe(
Expand All @@ -224,6 +228,15 @@ export const CursorDriver: ProviderDriver<
},
auth.usesApiKey ? "api-key" : "browser",
).pipe(
Effect.tap(({ sdkModels }) =>
Effect.sync(() => {
// A failed check carries no catalog, so keep the last loaded one.
if (sdkModels !== undefined) {
catalogSdkModels = sdkModels;
}
}),
),
Effect.map(({ provider }) => provider),
Effect.filterOrElse(
(snapshot) =>
!(
Expand Down
80 changes: 79 additions & 1 deletion packages/provider-cursor/src/server/sdkModel.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { SDKModel } from "@cursor/sdk";
import { ProviderInstanceId } from "@t3tools/contracts";
import { describe, expect, it } from "vite-plus/test";

import { cursorSdkModelSelection } from "./sdkModel.ts";
import { cursorSdkModelOffersOptionalMaxMode, cursorSdkModelSelection } from "./sdkModel.ts";
import { buildCursorDiscoveredModelsFromSdk } from "./status.ts";

const instanceId = ProviderInstanceId.make("cursor");
Expand Down Expand Up @@ -71,3 +72,80 @@ describe("cursorSdkModelSelection", () => {
});
});
});

describe("cursorSdkModelSelection Max Mode", () => {
const sdkModels = [
{
id: "composer-2",
displayName: "Composer 2",
parameters: [
{ id: "fast", displayName: "Fast", values: [{ value: "false" }, { value: "true" }] },
],
variants: [
{ displayName: "Composer", isDefault: true, params: [{ id: "fast", value: "false" }] },
{ displayName: "Composer Max", isMaxMode: true, params: [{ id: "fast", value: "false" }] },
{ displayName: "Composer Fast", params: [{ id: "fast", value: "true" }] },
],
},
{
id: "claude-opus-4-8",
displayName: "Claude Opus 4.8",
parameters: [{ id: "context", displayName: "Context", values: [{ value: "1m" }] }],
variants: [
{
displayName: "1M",
isDefault: true,
isMaxMode: true,
params: [{ id: "context", value: "1m" }],
},
],
},
] satisfies ReadonlyArray<SDKModel>;
const catalogModels = buildCursorDiscoveredModelsFromSdk(sdkModels);

it.each([
{
name: "leaves optional Max Mode off by default",
model: "composer-2",
options: undefined,
expected: { id: "composer-2", params: [{ id: "fast", value: "false" }] },
},
{
name: "turns on optional Max Mode without sending it as a parameter",
model: "composer-2",
options: [{ id: "maxMode", value: true }],
expected: { id: "composer-2", params: [{ id: "fast", value: "false" }], maxMode: true },
},
{
name: "drops Max Mode when no variant with the chosen parameters offers it",
model: "composer-2",
options: [
{ id: "fastMode", value: true },
{ id: "maxMode", value: true },
],
expected: { id: "composer-2", params: [{ id: "fast", value: "true" }] },
},
{
name: "turns on Max Mode when every matching variant requires it",
model: "claude-opus-4-8",
options: undefined,
expected: {
id: "claude-opus-4-8",
params: [{ id: "context", value: "1m" }],
maxMode: true,
},
},
])("$name", ({ model, options, expected }) => {
expect(
cursorSdkModelSelection(
{ instanceId, model, ...(options === undefined ? {} : { options }) },
catalogModels,
sdkModels,
),
).toEqual(expected);
});

it("offers a Max Mode toggle only where it is optional", () => {
expect(sdkModels.map(cursorSdkModelOffersOptionalMaxMode)).toEqual([true, false]);
});
});
116 changes: 105 additions & 11 deletions packages/provider-cursor/src/server/sdkModel.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import type { ModelSelection as CursorSdkModelSelection, ModelParameterValue } from "@cursor/sdk";
import type {
ModelSelection as CursorSdkModelSelection,
ModelParameterValue,
ModelVariant,
SDKModel,
} from "@cursor/sdk";
import type { ModelSelection, ServerProviderModel } from "@t3tools/contracts";
import { getProviderOptionCurrentValue } from "@t3tools/shared/model";

/** Composer option id for optional Cursor max mode; not an SDK model parameter. */
export const CURSOR_SDK_MAX_MODE_OPTION_ID = "maxMode";

const CURSOR_SDK_PARAMETER_TO_PROVIDER_OPTION: Readonly<Record<string, string>> = {
context: "contextWindow",
fast: "fastMode",
Expand Down Expand Up @@ -55,30 +63,116 @@ export function cursorContextWindowTokens(
return Number.isSafeInteger(tokens) && tokens > 0 ? tokens : undefined;
}

function variantMatchesParams(
variant: ModelVariant,
params: ReadonlyArray<ModelParameterValue> | undefined,
): boolean {
if (params === undefined || params.length === 0) {
return variant.isDefault === true || variant.params.length === 0;
Comment thread
Huijiro marked this conversation as resolved.
Resolved
}
return params.every((param) =>
variant.params.some((entry) => entry.id === param.id && entry.value === param.value),
);
}

function defaultVariantParams(
model: Pick<SDKModel, "variants">,
): ReadonlyArray<ModelParameterValue> | undefined {
return (model.variants ?? []).find((variant) => variant.isDefault === true)?.params;
}

function effectiveVariantParams(
model: Pick<SDKModel, "variants">,
params: ReadonlyArray<ModelParameterValue> | undefined,
): ReadonlyArray<ModelParameterValue> | undefined {
return params !== undefined && params.length > 0 ? params : defaultVariantParams(model);
}

function cursorSdkMaxModeForParams(
model: Pick<SDKModel, "variants">,
params: ReadonlyArray<ModelParameterValue> | undefined,
): boolean {
const variants = model.variants ?? [];
const matching = variants.filter((variant) =>
variantMatchesParams(variant, effectiveVariantParams(model, params)),
);
return matching.length > 0 && matching.every((variant) => variant.isMaxMode === true);
}

function cursorSdkParamsAdmitMaxMode(
model: Pick<SDKModel, "variants">,
params: ReadonlyArray<ModelParameterValue> | undefined,
): boolean {
const effective = effectiveVariantParams(model, params);
return (model.variants ?? []).some(
(variant) => variantMatchesParams(variant, effective) && variant.isMaxMode === true,
);
}

export function cursorSdkModelOffersOptionalMaxMode(model: Pick<SDKModel, "variants">): boolean {
const variants = model.variants ?? [];
if (
!variants.some((variant) => variant.isMaxMode === true) ||
!variants.some((variant) => variant.isMaxMode !== true)
) {
return false;
}

const candidateParams: Array<ReadonlyArray<ModelParameterValue> | undefined> = [undefined];
for (const variant of variants) {
candidateParams.push(variant.params);
}

for (const params of candidateParams) {
const matching = variants.filter((variant) => variantMatchesParams(variant, params));
if (
matching.some((variant) => variant.isMaxMode === true) &&
matching.some((variant) => variant.isMaxMode !== true)
) {
return true;
}
}
return false;
}

export function cursorSdkModelSelection(
modelSelection: ModelSelection,
models: ReadonlyArray<ServerProviderModel> = [],
sdkModels: ReadonlyArray<Pick<SDKModel, "id" | "variants">> = [],
): CursorSdkModelSelection {
const explicit = modelSelection.options ?? [];
const id = modelSelection.model === "auto" ? "default" : modelSelection.model;
const selected = modelSelection.options ?? [];
const userRequestedMaxMode = selected.some(
(option) => option.id === CURSOR_SDK_MAX_MODE_OPTION_ID && option.value === true,
);
const explicit = selected.filter((option) => option.id !== CURSOR_SDK_MAX_MODE_OPTION_ID);
const descriptors =
models.find((model) => model.slug === modelSelection.model)?.capabilities?.optionDescriptors ??
[];
// Some Cursor plans reject runs that leave a catalog parameter unset; T3 keeps Fast off until picked.
const defaults = descriptors.flatMap((descriptor) => {
if (descriptor.id === CURSOR_SDK_MAX_MODE_OPTION_ID) return [];
if (explicit.some((option) => option.id === descriptor.id)) return [];
const value = descriptor.id === "fastMode" ? false : getProviderOptionCurrentValue(descriptor);
return value === undefined ? [] : [{ id: descriptor.id, value }];
});
const options = [...explicit, ...defaults];
const params =
options.length === 0
? undefined
: options.map((option): ModelParameterValue => ({
id: cursorSdkParameterId(option.id),
value: String(option.value),
}));
const catalogModel = sdkModels.find((model) => model.id === id);
const forcedMaxMode =
catalogModel !== undefined && cursorSdkMaxModeForParams(catalogModel, params);
const optionalMaxMode =
userRequestedMaxMode &&
(catalogModel === undefined || cursorSdkParamsAdmitMaxMode(catalogModel, params));
return {
id: modelSelection.model === "auto" ? "default" : modelSelection.model,
...(options.length === 0
? {}
: {
params: options.map((option): ModelParameterValue => ({
id: cursorSdkParameterId(option.id),
value: String(option.value),
})),
}),
id,
...(params === undefined ? {} : { params }),
...(forcedMaxMode || optionalMaxMode ? { maxMode: true } : {}),
};
}
Loading
Loading