@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 106 lines 3.9 KB
Raw
1 import type {
2 ModelSelection as CursorSdkModelSelection,
3 ModelParameterValue,
4 SDKModel,
5 } from "@cursor/sdk";
6 import type { ModelSelection, ServerProviderModel } from "@t3tools/contracts";
7 import { getProviderOptionCurrentValue } from "@t3tools/shared/model";
8
9 const CURSOR_SDK_PARAMETER_TO_PROVIDER_OPTION: Readonly<Record<string, string>> = {
10 context: "contextWindow",
11 fast: "fastMode",
12 };
13
14 const PROVIDER_OPTION_TO_CURSOR_SDK_PARAMETER: Readonly<Record<string, string>> = {
15 contextWindow: "context",
16 fastMode: "fast",
17 };
18
19 export function cursorSdkProviderOptionId(parameterId: string): string {
20 return CURSOR_SDK_PARAMETER_TO_PROVIDER_OPTION[parameterId] ?? parameterId;
21 }
22
23 function cursorSdkParameterId(providerOptionId: string): string {
24 return PROVIDER_OPTION_TO_CURSOR_SDK_PARAMETER[providerOptionId] ?? providerOptionId;
25 }
26
27 export function cursorSdkParameterPriority(parameterId: string): number {
28 switch (parameterId) {
29 case "effort":
30 case "reasoning":
31 return 0;
32 case "context":
33 return 1;
34 case "fast":
35 return 2;
36 case "thinking":
37 return 3;
38 default:
39 return 4;
40 }
41 }
42
43 export function cursorContextWindowTokens(
44 modelSelection: ModelSelection,
45 models: ReadonlyArray<ServerProviderModel>,
46 ): number | undefined {
47 // The SDK forwards explicit values even when the catalog does not list them.
48 const explicit = modelSelection.options?.find((option) => option.id === "contextWindow");
49 let value = explicit?.value;
50 if (explicit === undefined) {
51 const descriptor = models
52 .find((model) => model.slug === modelSelection.model)
53 ?.capabilities?.optionDescriptors?.find((option) => option.id === "contextWindow");
54 value = getProviderOptionCurrentValue(descriptor);
55 }
56 const match = /^(\d+)([km])$/i.exec(typeof value === "string" ? value.trim() : "");
57 if (match === null) return undefined;
58 const tokens = Number(match[1]) * (match[2]?.toLowerCase() === "m" ? 1_000_000 : 1_000);
59 return Number.isSafeInteger(tokens) && tokens > 0 ? tokens : undefined;
60 }
61
62 // The SDK infers Max Mode from a model list it never refreshes, so pass it from T3's fresher catalog.
63 function cursorSdkMaxMode(
64 sdkModel: SDKModel | undefined,
65 params: ReadonlyArray<ModelParameterValue>,
66 ): boolean {
67 if (params.length === 0) return false;
68 const variants = (sdkModel?.variants ?? []).filter((variant) =>
69 params.every((param) =>
70 variant.params.some(
71 (candidate) =>
72 candidate.id.trim() === param.id.trim() && candidate.value.trim() === param.value.trim(),
73 ),
74 ),
75 );
76 return variants.length > 0 && variants.every((variant) => variant.isMaxMode === true);
77 }
78
79 export function cursorSdkModelSelection(
80 modelSelection: ModelSelection,
81 models: ReadonlyArray<ServerProviderModel> = [],
82 sdkModels: ReadonlyArray<SDKModel> = [],
83 ): CursorSdkModelSelection {
84 const explicit = modelSelection.options ?? [];
85 const descriptors =
86 models.find((model) => model.slug === modelSelection.model)?.capabilities?.optionDescriptors ??
87 [];
88 // Some Cursor plans reject runs that leave a catalog parameter unset; T3 keeps Fast off until picked.
89 const defaults = descriptors.flatMap((descriptor) => {
90 if (explicit.some((option) => option.id === descriptor.id)) return [];
91 const value = descriptor.id === "fastMode" ? false : getProviderOptionCurrentValue(descriptor);
92 return value === undefined ? [] : [{ id: descriptor.id, value }];
93 });
94 const params = [...explicit, ...defaults].map((option): ModelParameterValue => ({
95 id: cursorSdkParameterId(option.id),
96 value: String(option.value),
97 }));
98 const sdkModel = sdkModels.find(
99 (model) => model.id === modelSelection.model || model.aliases?.includes(modelSelection.model),
100 );
101 return {
102 id: modelSelection.model === "auto" ? "default" : modelSelection.model,
103 ...(params.length === 0 ? {} : { params }),
104 ...(cursorSdkMaxMode(sdkModel, params) ? { maxMode: true } : {}),
105 };
106 }