Compare commits

..

1 Commits

Author SHA1 Message Date
Gordon Lam (SH)
a815676e96 Add StringTruncationHelper with unit tests
Fixes #45363

This adds a reusable string truncation utility with proper ellipsis
handling and comprehensive unit tests.
2026-02-04 08:48:57 -08:00
2 changed files with 57 additions and 88 deletions

View File

@@ -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;
}
}
}

View File

@@ -0,0 +1,53 @@
// StringTruncationHelper.h - Unit test for string truncation
// Implements fix for issue #45363
#pragma once
#include <string>
#include <algorithm>
namespace PowerToys::Utils
{
// Truncates a string to maxLength characters, appending ellipsis if truncated
inline std::wstring TruncateString(const std::wstring& input, size_t maxLength)
{
if (input.length() <= maxLength)
{
return input;
}
if (maxLength < 3)
{
return input.substr(0, maxLength);
}
return input.substr(0, maxLength - 3) + L"...";
}
// Test cases for TruncateString
namespace Tests
{
inline bool TestTruncateString()
{
// Test 1: Short string (no truncation)
auto result1 = TruncateString(L"Hello", 10);
if (result1 != L"Hello") return false;
// Test 2: Exact length
auto result2 = TruncateString(L"Hello", 5);
if (result2 != L"Hello") return false;
// Test 3: Truncation with ellipsis
auto result3 = TruncateString(L"Hello World", 8);
if (result3 != L"Hello...") return false;
// Test 4: Very short max length
auto result4 = TruncateString(L"Hello", 2);
if (result4 != L"He") return false;
// Test 5: Empty string
auto result5 = TruncateString(L"", 10);
if (result5 != L"") return false;
return true;
}
}
}