( request: ComposerStateRequest, provider: string, modelId: string, )
| 275 | } |
| 276 | |
| 277 | export async function setComposerModel( |
| 278 | request: ComposerStateRequest, |
| 279 | provider: string, |
| 280 | modelId: string, |
| 281 | ) { |
| 282 | const persistedSessionPath = getPersistedSessionPath(request.sessionPath) |
| 283 | if (!persistedSessionPath) { |
| 284 | const { AuthStorage, ModelRegistry, SettingsManager, getAgentDir } = await getPiModule() |
| 285 | const cwd = request.projectId ?? getDesktopWorkingDirectory() |
| 286 | const agentDir = getAgentDir() |
| 287 | const authStorage = AuthStorage.create() |
| 288 | const modelRegistry = normalizeModelRegistryContextWindows( |
| 289 | ModelRegistry.create(authStorage, `${agentDir}/models.json`), |
| 290 | ) |
| 291 | const model = modelRegistry.find(provider, modelId) |
| 292 | if (!model) throw new Error(`Unknown Pi model: ${provider}/${modelId}`) |
| 293 | const currentComposer = await buildComposerStateSnapshot({ projectId: cwd, sessionPath: null }) |
| 294 | const settingsManager = SettingsManager.create(cwd, agentDir) |
| 295 | settingsManager.setDefaultModelAndProvider(provider, modelId) |
| 296 | settingsManager.setDefaultThinkingLevel( |
| 297 | clampThinkingLevel( |
| 298 | currentComposer.currentThinkingLevel, |
| 299 | getAvailableThinkingLevelsForModel(model), |
| 300 | ), |
| 301 | ) |
| 302 | await emitComposerUpdate({ ...request, sessionPath: null }) |
| 303 | return { ok: true as const } |
| 304 | } |
| 305 | await withRuntimeMutationLock(persistedSessionPath, async () => { |
| 306 | await reloadRuntimeSettingsIfSafe(persistedSessionPath, { useMutationLock: false }) |
| 307 | const runtime = await getOrCreateRuntimeForSessionPath(persistedSessionPath, { |
| 308 | suspendDisposal: true, |
| 309 | settingsCwd: request.composerSessionDir ?? null, |
| 310 | chatGroupId: request.chatGroupId ?? null, |
| 311 | }) |
| 312 | const model = runtime.session.modelRegistry.find(provider, modelId) |
| 313 | if (!model) throw new Error(`Unknown Pi model: ${provider}/${modelId}`) |
| 314 | await runtime.session.setModel(model) |
| 315 | scheduleRuntimeDisposal(persistedSessionPath) |
| 316 | await emitComposerUpdate({ ...request, sessionPath: persistedSessionPath }) |
| 317 | }) |
| 318 | return { ok: true as const } |
| 319 | } |
| 320 | |
| 321 | export async function setComposerThinkingLevel( |
| 322 | request: ComposerStateRequest, |
no test coverage detected