From b84db8983d63a9a981083ebce1a139860c6bdf25 Mon Sep 17 00:00:00 2001
From: chunhualin1 <chunhualin1@lenovo.com>
Date: 星期五, 14 八月 2026 15:35:21 +0800
Subject: [PATCH] commit

---
 src/Moli.Desktop/Llm/OpenAICompatibleAdapter.cs |   57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 56 insertions(+), 1 deletions(-)

diff --git a/src/Moli.Desktop/Llm/OpenAICompatibleAdapter.cs b/src/Moli.Desktop/Llm/OpenAICompatibleAdapter.cs
index 2b5c1cd..5700ec8 100644
--- a/src/Moli.Desktop/Llm/OpenAICompatibleAdapter.cs
+++ b/src/Moli.Desktop/Llm/OpenAICompatibleAdapter.cs
@@ -24,7 +24,13 @@
         LlmProviderRequest request,
         [EnumeratorCancellation] CancellationToken cancellationToken = default)
     {
-        using var httpRequest = new HttpRequestMessage(HttpMethod.Post, request.Model.Endpoint);
+        var chatUrl = request.Model.GetChatCompletionsUrl();
+        if (!LlmOpenAiUrls.IsAbsoluteHttpUrl(chatUrl))
+        {
+            throw new LlmProviderException(LlmErrorCodes.RequestInvalid, "Model baseUrl must be an absolute HTTP or HTTPS URL.");
+        }
+
+        using var httpRequest = new HttpRequestMessage(HttpMethod.Post, chatUrl);
         if (!string.IsNullOrWhiteSpace(request.ApiKey))
         {
             httpRequest.Headers.Authorization = new AuthenticationHeaderValue("Bearer", request.ApiKey);
@@ -91,6 +97,55 @@
         yield return new LlmProviderChunk { IsCompleted = true };
     }
 
+    public async Task<IReadOnlyList<string>> ListModelIdsAsync(
+        string baseUrl,
+        string? apiKey,
+        CancellationToken cancellationToken = default)
+    {
+        var modelsUrl = LlmOpenAiUrls.ToModelsUrl(baseUrl);
+        if (!LlmOpenAiUrls.IsAbsoluteHttpUrl(modelsUrl))
+        {
+            throw new LlmProviderException(LlmErrorCodes.RequestInvalid, "Provider baseUrl must be an absolute HTTP or HTTPS URL.");
+        }
+
+        using var httpRequest = new HttpRequestMessage(HttpMethod.Get, modelsUrl);
+        if (!string.IsNullOrWhiteSpace(apiKey))
+        {
+            httpRequest.Headers.Authorization = new AuthenticationHeaderValue("Bearer", apiKey);
+        }
+
+        using var response = await _httpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);
+        if (!response.IsSuccessStatusCode)
+        {
+            throw new LlmProviderException(MapStatusCode(response.StatusCode), $"LLM provider returned {(int)response.StatusCode}.");
+        }
+
+        await using var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
+        using var document = await JsonDocument.ParseAsync(stream, cancellationToken: cancellationToken).ConfigureAwait(false);
+        if (!document.RootElement.TryGetProperty("data", out var data) || data.ValueKind != JsonValueKind.Array)
+        {
+            return [];
+        }
+
+        return data.EnumerateArray()
+            .Select(ReadModelId)
+            .Where(id => !string.IsNullOrWhiteSpace(id))
+            .Select(id => id!)
+            .ToList();
+    }
+
+    private static string? ReadModelId(JsonElement item)
+    {
+        if (item.ValueKind == JsonValueKind.Object &&
+            item.TryGetProperty("id", out var id) &&
+            id.ValueKind == JsonValueKind.String)
+        {
+            return id.GetString();
+        }
+
+        return null;
+    }
+
     private LlmProviderChunk? ParseChunk(string data)
     {
         using var document = JsonDocument.Parse(data);

--
Gitblit v1.9.1