(
backend: AcpSdkBackend,
sessionId: string,
modelId: string | null | undefined
)
| 106 | * Only wire ids present in `availableModels` are accepted. |
| 107 | */ |
| 108 | export async function applyCursorAcpModel( |
| 109 | backend: AcpSdkBackend, |
| 110 | sessionId: string, |
| 111 | modelId: string | null | undefined |
| 112 | ): Promise<ApplyCursorAcpModelResult> { |
| 113 | const trimmed = modelId?.trim(); |
| 114 | if (!trimmed || isDefaultCursorModelId(trimmed)) { |
| 115 | return { applied: false }; |
| 116 | } |
| 117 | |
| 118 | const metadata = backend.getSessionModelsMetadata(sessionId); |
| 119 | const available = metadata?.availableModels ?? []; |
| 120 | const modelOption = backend.getConfigOptionByCategory?.(sessionId, 'model'); |
| 121 | const optionWireIds = modelOption?.options?.map((option) => ({ modelId: option.value })) ?? []; |
| 122 | const catalog = [...available, ...optionWireIds]; |
| 123 | const resolved = resolveCursorAcpWireId(trimmed, catalog); |
| 124 | if (!resolved) { |
| 125 | logger.debug(`[cursor-acp] Model ${trimmed} is not in ACP configOptions; skipping`); |
| 126 | return { applied: false }; |
| 127 | } |
| 128 | |
| 129 | const trySetConfigOption = async (): Promise<boolean> => { |
| 130 | if (!modelOption || !backend.setConfigOption) { |
| 131 | return false; |
| 132 | } |
| 133 | try { |
| 134 | await backend.setConfigOption(sessionId, modelOption.id, resolved); |
| 135 | return true; |
| 136 | } catch (error) { |
| 137 | logger.debug('[cursor-acp] session/set_config_option failed, trying set_model', error); |
| 138 | return false; |
| 139 | } |
| 140 | }; |
| 141 | |
| 142 | for (let attempt = 0; attempt < 2; attempt += 1) { |
| 143 | if (await trySetConfigOption()) { |
| 144 | backend.pinSessionModelWireId(sessionId, resolved); |
| 145 | return { applied: true, resolvedWireId: resolved, requestedWireId: trimmed }; |
| 146 | } |
| 147 | if (attempt === 0) { |
| 148 | await new Promise((resolve) => setTimeout(resolve, 150)); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | return { applied: false }; |
| 153 | } |
no test coverage detected