loadFolderModelState function

Future<FolderModelState?> loadFolderModelState(
  1. ExecutionEnv env, {
  2. required String sessionsRoot,
  3. required String cwd,
})

Loads the model/provider triple saved for the folder cwd lives in, or null when absent/corrupt (the global config stays in charge).

Implementation

Future<FolderModelState?> loadFolderModelState(
  ExecutionEnv env, {
  required String sessionsRoot,
  required String cwd,
}) async {
  final text = (await env.readTextFile(
    folderModelStatePath(sessionsRoot: sessionsRoot, cwd: cwd),
  )).valueOrNull;
  if (text == null || text.isEmpty) return null;
  try {
    final decoded = jsonDecode(text);
    if (decoded is! Map<String, dynamic>) return null;
    final providerKind = decoded['providerKind'];
    final modelId = decoded['modelId'];
    final baseUrl = decoded['baseUrl'];
    if (providerKind is! String ||
        providerKind.isEmpty ||
        modelId is! String ||
        modelId.isEmpty) {
      return null;
    }
    if (baseUrl != null && baseUrl is! String) return null;
    return FolderModelState(
      providerKind: providerKind,
      modelId: modelId,
      baseUrl: baseUrl as String?,
    );
  } on FormatException {
    return null;
  }
}