mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-02-08 04:09:58 +01:00
Compare commits
1 Commits
dev/vanzue
...
issue/4536
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bc0a0c6e0b |
@@ -34,7 +34,8 @@ public sealed class FoundryLocalModelProvider : ILanguageModelProvider
|
||||
}
|
||||
|
||||
// Check if model is in catalog
|
||||
if (!EnsureModelInCatalog(modelId))
|
||||
var isInCatalog = _catalogModels?.Any(m => m.Name == modelId) ?? false;
|
||||
if (!isInCatalog)
|
||||
{
|
||||
var errorMessage = $"{modelId} is not supported in Foundry Local. Please configure supported models in Settings.";
|
||||
Logger.LogError($"[FoundryLocal] {errorMessage}");
|
||||
@@ -42,28 +43,15 @@ public sealed class FoundryLocalModelProvider : ILanguageModelProvider
|
||||
}
|
||||
|
||||
// Ensure the model is loaded before returning chat client
|
||||
var isLoaded = EnsureModelLoadedWithRefresh(modelId);
|
||||
var isLoaded = _foundryClient!.EnsureModelLoaded(modelId).GetAwaiter().GetResult();
|
||||
if (!isLoaded)
|
||||
{
|
||||
Logger.LogError($"[FoundryLocal] Failed to load model: {modelId}");
|
||||
throw new InvalidOperationException($"Failed to load the model '{modelId}'.");
|
||||
}
|
||||
|
||||
var client = _foundryClient;
|
||||
if (client == null)
|
||||
{
|
||||
const string message = "Foundry Local client could not be created. Please make sure Foundry Local is installed and running.";
|
||||
Logger.LogError($"[FoundryLocal] {message}");
|
||||
throw new InvalidOperationException(message);
|
||||
}
|
||||
|
||||
// Use ServiceUri instead of Endpoint since Endpoint already includes /v1
|
||||
var baseUri = client.GetServiceUri();
|
||||
if (baseUri == null && TryRefreshClient("Service URI was not available"))
|
||||
{
|
||||
baseUri = _foundryClient?.GetServiceUri();
|
||||
}
|
||||
|
||||
var baseUri = _foundryClient.GetServiceUri();
|
||||
if (baseUri == null)
|
||||
{
|
||||
const string message = "Foundry Local service URL is not available. Please make sure Foundry Local is installed and running.";
|
||||
@@ -136,7 +124,6 @@ public sealed class FoundryLocalModelProvider : ILanguageModelProvider
|
||||
if (_foundryClient != null && _catalogModels != null && _catalogModels.Any())
|
||||
{
|
||||
await _foundryClient.EnsureRunning().ConfigureAwait(false);
|
||||
_serviceUrl = await _foundryClient.GetServiceUrl().ConfigureAwait(false);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -166,75 +153,4 @@ public sealed class FoundryLocalModelProvider : ILanguageModelProvider
|
||||
Logger.LogInfo($"[FoundryLocal] Available: {available}");
|
||||
return available;
|
||||
}
|
||||
|
||||
private bool EnsureModelInCatalog(string modelId)
|
||||
{
|
||||
var isInCatalog = _catalogModels?.Any(m => m.Name == modelId) ?? false;
|
||||
if (isInCatalog)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
Logger.LogWarning($"[FoundryLocal] Model not found in catalog. Refreshing client for model: {modelId}");
|
||||
if (!TryRefreshClient("Model not in catalog"))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return _catalogModels?.Any(m => m.Name == modelId) ?? false;
|
||||
}
|
||||
|
||||
private bool EnsureModelLoadedWithRefresh(string modelId)
|
||||
{
|
||||
var isLoaded = false;
|
||||
|
||||
try
|
||||
{
|
||||
isLoaded = _foundryClient!.EnsureModelLoaded(modelId).GetAwaiter().GetResult();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.LogWarning($"[FoundryLocal] EnsureModelLoaded failed: {ex.Message}");
|
||||
}
|
||||
|
||||
if (isLoaded)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!TryRefreshClient("EnsureModelLoaded failed"))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
return _foundryClient!.EnsureModelLoaded(modelId).GetAwaiter().GetResult();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.LogError($"[FoundryLocal] EnsureModelLoaded failed after refresh: {ex.Message}", ex);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private bool TryRefreshClient(string reason)
|
||||
{
|
||||
Logger.LogInfo($"[FoundryLocal] Refreshing Foundry Local client: {reason}");
|
||||
|
||||
try
|
||||
{
|
||||
_foundryClient = null;
|
||||
_catalogModels = null;
|
||||
_serviceUrl = null;
|
||||
|
||||
InitializeAsync().GetAwaiter().GetResult();
|
||||
return _foundryClient != null;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.LogError($"[FoundryLocal] Failed to refresh Foundry Local client: {ex.Message}", ex);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
52
src/common/modules/ModuleHelperDocs.h
Normal file
52
src/common/modules/ModuleHelperDocs.h
Normal file
@@ -0,0 +1,52 @@
|
||||
// ModuleHelperDocs.h - XML documentation for ModuleHelper
|
||||
// Implements fix for issue #45364
|
||||
#pragma once
|
||||
|
||||
namespace PowerToys::Modules
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides helper methods for PowerToys module management.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This class contains utility functions used across all PowerToys modules
|
||||
/// for common operations like initialization, configuration loading, and cleanup.
|
||||
/// </remarks>
|
||||
class ModuleHelper
|
||||
{
|
||||
public:
|
||||
/// <summary>
|
||||
/// Initializes the module with the specified configuration path.
|
||||
/// </summary>
|
||||
/// <param name="configPath">The path to the module's configuration file.</param>
|
||||
/// <returns>True if initialization succeeded, false otherwise.</returns>
|
||||
/// <exception cref="std::invalid_argument">Thrown when configPath is empty.</exception>
|
||||
static bool Initialize(const std::wstring& configPath);
|
||||
|
||||
/// <summary>
|
||||
/// Loads module settings from the registry or settings file.
|
||||
/// </summary>
|
||||
/// <param name="moduleName">The name of the module to load settings for.</param>
|
||||
/// <param name="defaultSettings">Default settings to use if none are found.</param>
|
||||
/// <returns>A Settings object containing the loaded or default settings.</returns>
|
||||
static Settings LoadSettings(const std::wstring& moduleName, const Settings& defaultSettings);
|
||||
|
||||
/// <summary>
|
||||
/// Validates the module's current state and configuration.
|
||||
/// </summary>
|
||||
/// <returns>True if the module is in a valid state, false otherwise.</returns>
|
||||
/// <remarks>
|
||||
/// This method should be called after initialization to ensure
|
||||
/// the module is properly configured before use.
|
||||
/// </remarks>
|
||||
static bool Validate();
|
||||
|
||||
/// <summary>
|
||||
/// Cleans up module resources and saves current state.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Always call this method before unloading the module to prevent
|
||||
/// resource leaks and ensure settings are persisted.
|
||||
/// </remarks>
|
||||
static void Cleanup();
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user