From 4df6165fa93c7caeea86385312415e312de4d11f Mon Sep 17 00:00:00 2001
From: Moli <13026959681@163.com>
Date: 星期二, 05 五月 2026 01:10:39 +0800
Subject: [PATCH] 暂存

---
 src/Moli.Desktop/WebView/PluginBridgeDispatcher.cs |  200 +++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 196 insertions(+), 4 deletions(-)

diff --git a/src/Moli.Desktop/WebView/PluginBridgeDispatcher.cs b/src/Moli.Desktop/WebView/PluginBridgeDispatcher.cs
index 0c8c988..e6f3e91 100644
--- a/src/Moli.Desktop/WebView/PluginBridgeDispatcher.cs
+++ b/src/Moli.Desktop/WebView/PluginBridgeDispatcher.cs
@@ -20,7 +20,14 @@
         "requestShow",
         "requestHide",
         "requestResize",
+        "llm.getConnectionMode",
         "llm.getModels",
+        "llm.getProjects",
+        "llm.getMemoryCapabilities",
+        "llm.getMemories",
+        "llm.createMemory",
+        "llm.deleteMemory",
+        "llm.recallMemories",
         "llm.streamChat",
         "llm.cancel"
     ];
@@ -78,17 +85,149 @@
             "requestShow" => await HandleEventAsync(request, context, "plugin.showRequested", cancellationToken).ConfigureAwait(false),
             "requestHide" => await HandleEventAsync(request, context, "plugin.hideRequested", cancellationToken).ConfigureAwait(false),
             "requestResize" => await HandleEventAsync(request, context, "plugin.resizeRequested", cancellationToken).ConfigureAwait(false),
-            "llm.getModels" => await HandleGetLlmModelsAsync(request, cancellationToken).ConfigureAwait(false),
+            "llm.getConnectionMode" => PluginBridgeResponse.Ok(request.Id ?? string.Empty, new { mode = ReadRuntimeConfig(context).Mode }),
+            "llm.getModels" => await HandleGetLlmModelsAsync(request, context, cancellationToken).ConfigureAwait(false),
+            "llm.getProjects" => await HandleGetAiChatProjectsAsync(request, context, cancellationToken).ConfigureAwait(false),
+            "llm.getMemoryCapabilities" => await HandleGetMemoryCapabilitiesAsync(request, context, cancellationToken).ConfigureAwait(false),
+            "llm.getMemories" => await HandleGetMemoriesAsync(request, context, cancellationToken).ConfigureAwait(false),
+            "llm.createMemory" => await HandleCreateMemoryAsync(request, context, cancellationToken).ConfigureAwait(false),
+            "llm.deleteMemory" => await HandleDeleteMemoryAsync(request, context, cancellationToken).ConfigureAwait(false),
+            "llm.recallMemories" => await HandleRecallMemoriesAsync(request, context, cancellationToken).ConfigureAwait(false),
             "llm.streamChat" => await HandleStreamLlmChatAsync(request, context, cancellationToken).ConfigureAwait(false),
             "llm.cancel" => await HandleCancelLlmChatAsync(request, cancellationToken).ConfigureAwait(false),
             _ => PluginBridgeResponse.Failure(request.Id, "BridgeMethodNotAllowed", $"Bridge method {request.Method} is not allowed.")
         };
     }
 
-    private async Task<PluginBridgeResponse> HandleGetLlmModelsAsync(BridgeRequest request, CancellationToken cancellationToken)
+    private async Task<PluginBridgeResponse> HandleGetLlmModelsAsync(
+        BridgeRequest request,
+        PluginBridgeContext context,
+        CancellationToken cancellationToken)
     {
-        var models = await _llmGateway.GetModelsAsync(cancellationToken).ConfigureAwait(false);
+        var models = await _llmGateway.GetModelsAsync(ReadRuntimeConfig(context), cancellationToken).ConfigureAwait(false);
         return PluginBridgeResponse.Ok(request.Id ?? string.Empty, models);
+    }
+
+    private async Task<PluginBridgeResponse> HandleGetAiChatProjectsAsync(
+        BridgeRequest request,
+        PluginBridgeContext context,
+        CancellationToken cancellationToken)
+    {
+        try
+        {
+            var projects = await _llmGateway.GetProjectsAsync(ReadRuntimeConfig(context), cancellationToken).ConfigureAwait(false);
+            return PluginBridgeResponse.Ok(request.Id ?? string.Empty, projects);
+        }
+        catch (AiChatClientException ex)
+        {
+            return PluginBridgeResponse.Failure(request.Id, ex.Code, ex.Message);
+        }
+    }
+
+    private async Task<PluginBridgeResponse> HandleGetMemoryCapabilitiesAsync(
+        BridgeRequest request,
+        PluginBridgeContext context,
+        CancellationToken cancellationToken)
+    {
+        try
+        {
+            var capabilities = await _llmGateway.GetMemoryCapabilitiesAsync(ReadRuntimeConfig(context), cancellationToken).ConfigureAwait(false);
+            return PluginBridgeResponse.Ok(request.Id ?? string.Empty, capabilities);
+        }
+        catch (AiChatClientException ex)
+        {
+            return PluginBridgeResponse.Failure(request.Id, ex.Code, ex.Message);
+        }
+    }
+
+    private async Task<PluginBridgeResponse> HandleGetMemoriesAsync(
+        BridgeRequest request,
+        PluginBridgeContext context,
+        CancellationToken cancellationToken)
+    {
+        var payload = DeserializePayload<AiChatGetMemoriesRequest>(request, out var error);
+        if (error is not null)
+        {
+            return error;
+        }
+
+        try
+        {
+            var result = await _llmGateway.GetMemoriesAsync(ReadRuntimeConfig(context), payload!, cancellationToken).ConfigureAwait(false);
+            return PluginBridgeResponse.Ok(request.Id ?? string.Empty, result.Data);
+        }
+        catch (AiChatClientException ex)
+        {
+            return PluginBridgeResponse.Failure(request.Id, ex.Code, ex.Message);
+        }
+    }
+
+    private async Task<PluginBridgeResponse> HandleCreateMemoryAsync(
+        BridgeRequest request,
+        PluginBridgeContext context,
+        CancellationToken cancellationToken)
+    {
+        var payload = DeserializePayload<AiChatCreateMemoryRequest>(request, out var error);
+        if (error is not null)
+        {
+            return error;
+        }
+
+        try
+        {
+            var result = await _llmGateway.CreateMemoryAsync(ReadRuntimeConfig(context), payload!, cancellationToken).ConfigureAwait(false);
+            return PluginBridgeResponse.Ok(request.Id ?? string.Empty, result.Data);
+        }
+        catch (AiChatClientException ex)
+        {
+            return PluginBridgeResponse.Failure(request.Id, ex.Code, ex.Message);
+        }
+    }
+
+    private async Task<PluginBridgeResponse> HandleDeleteMemoryAsync(
+        BridgeRequest request,
+        PluginBridgeContext context,
+        CancellationToken cancellationToken)
+    {
+        if (request.Params.ValueKind is not JsonValueKind.Object ||
+            !request.Params.TryGetProperty("memoryId", out var memoryIdElement) ||
+            memoryIdElement.ValueKind != JsonValueKind.String ||
+            string.IsNullOrWhiteSpace(memoryIdElement.GetString()))
+        {
+            return PluginBridgeResponse.Failure(request.Id, "BridgePayloadInvalid", "memoryId is required.");
+        }
+
+        try
+        {
+            await _llmGateway.DeleteMemoryAsync(ReadRuntimeConfig(context), memoryIdElement.GetString()!, cancellationToken).ConfigureAwait(false);
+            return PluginBridgeResponse.Ok(request.Id ?? string.Empty, new { accepted = true });
+        }
+        catch (AiChatClientException ex)
+        {
+            return PluginBridgeResponse.Failure(request.Id, ex.Code, ex.Message);
+        }
+    }
+
+    private async Task<PluginBridgeResponse> HandleRecallMemoriesAsync(
+        BridgeRequest request,
+        PluginBridgeContext context,
+        CancellationToken cancellationToken)
+    {
+        var payload = DeserializePayload<AiChatRecallMemoriesRequest>(request, out var error);
+        if (error is not null)
+        {
+            return error;
+        }
+
+        try
+        {
+            var result = await _llmGateway.RecallMemoriesAsync(ReadRuntimeConfig(context), payload!, cancellationToken).ConfigureAwait(false);
+            return PluginBridgeResponse.Ok(request.Id ?? string.Empty, result.Data);
+        }
+        catch (AiChatClientException ex)
+        {
+            return PluginBridgeResponse.Failure(request.Id, ex.Code, ex.Message);
+        }
     }
 
     private Task<PluginBridgeResponse> HandleStreamLlmChatAsync(
@@ -127,9 +266,12 @@
             RequestId = requestId,
             PluginId = context.RuntimeInfo.PluginId,
             InstanceId = context.HostId,
+            Mode = string.IsNullOrWhiteSpace(payload.Mode) ? ReadRuntimeConfig(context).Mode : payload.Mode!,
+            ProjectId = payload.ProjectId,
             ModelId = payload.ModelId,
             Messages = payload.Messages,
-            Options = payload.Options ?? new LlmRequestOptions()
+            Options = payload.Options ?? new LlmRequestOptions(),
+            RuntimeConfig = ReadRuntimeConfig(context)
         };
 
         _ = Task.Run(() => PumpLlmStreamAsync(chatRequest, context, CancellationToken.None));
@@ -212,6 +354,54 @@
     private static bool HasPermission(PluginBridgeContext context, string permission)
         => context.Manifest.Permissions.Any(item => string.Equals(item, permission, StringComparison.OrdinalIgnoreCase));
 
+    private T? DeserializePayload<T>(BridgeRequest request, out PluginBridgeResponse? error)
+    {
+        error = null;
+        if (request.Params.ValueKind is not JsonValueKind.Object)
+        {
+            error = PluginBridgeResponse.Failure(request.Id, "BridgePayloadInvalid", $"{request.Method} expects an object payload.");
+            return default;
+        }
+
+        try
+        {
+            return request.Params.Deserialize<T>(_jsonOptions);
+        }
+        catch (JsonException)
+        {
+            error = PluginBridgeResponse.Failure(request.Id, "BridgePayloadInvalid", $"{request.Method} payload could not be parsed.");
+            return default;
+        }
+    }
+
+    private LlmPluginRuntimeConfig ReadRuntimeConfig(PluginBridgeContext context)
+    {
+        if (context.Config.ValueKind is not JsonValueKind.Object)
+        {
+            return new LlmPluginRuntimeConfig();
+        }
+
+        try
+        {
+            var config = context.Config.Deserialize<LlmPluginRuntimeConfig>(_jsonOptions) ?? new LlmPluginRuntimeConfig();
+            if (string.IsNullOrWhiteSpace(config.Mode))
+            {
+                config = config with { Mode = LlmConnectionModes.ProviderDirect };
+            }
+
+            if (config.AiChat is null)
+            {
+                config = config with { AiChat = new AiChatRuntimeConfig() };
+            }
+
+            return config;
+        }
+        catch (JsonException)
+        {
+            return new LlmPluginRuntimeConfig();
+        }
+    }
+
     private sealed record BridgeRequest
     {
         public string? Id { get; init; }
@@ -222,6 +412,8 @@
     private sealed record LlmBridgeStreamRequest
     {
         public string? RequestId { get; init; }
+        public string? Mode { get; init; }
+        public string? ProjectId { get; init; }
         public required string ModelId { get; init; }
         public IReadOnlyList<LlmMessage> Messages { get; init; } = [];
         public LlmRequestOptions? Options { get; init; }

--
Gitblit v1.9.1