From 498d2b54aeba4ab9d3d5d79162ff52b22c622828 Mon Sep 17 00:00:00 2001 From: leo Date: Mon, 28 Oct 2024 21:31:40 +0800 Subject: [PATCH] feature: add per-repository setting for prefered OpenAI service * If there is only one OpenAI service available, discard the setting of prefered OpenAI service. Instead, use it directly * If there are multiple OpenAI service available, try to find the prefered one or show a context menu for users to choose the one they want to use Signed-off-by: leo --- src/Models/RepositorySettings.cs | 6 +++++ src/Resources/Locales/en_US.axaml | 3 +++ src/Resources/Locales/zh_CN.axaml | 3 +++ src/Resources/Locales/zh_TW.axaml | 3 +++ src/ViewModels/RepositoryConfigure.cs | 22 +++++++++++++++++- src/ViewModels/WorkingCopy.cs | 24 ++++++++++++++++--- src/Views/RepositoryConfigure.axaml | 33 +++++++++++++++++++++++++++ 7 files changed, 90 insertions(+), 4 deletions(-) diff --git a/src/Models/RepositorySettings.cs b/src/Models/RepositorySettings.cs index e8c66c04..ce4119f5 100644 --- a/src/Models/RepositorySettings.cs +++ b/src/Models/RepositorySettings.cs @@ -130,6 +130,12 @@ namespace SourceGit.Models set; } = false; + public string PreferedOpenAIService + { + get; + set; + } = "---"; + public void PushCommitMessage(string message) { var existIdx = CommitMessages.IndexOf(message); diff --git a/src/Resources/Locales/en_US.axaml b/src/Resources/Locales/en_US.axaml index 847bbd1b..16056b22 100644 --- a/src/Resources/Locales/en_US.axaml +++ b/src/Resources/Locales/en_US.axaml @@ -155,6 +155,9 @@ Rule Name: Result URL: Please use $1, $2 to access regex groups values. + OPEN AI + Prefered Service: + If the 'Prefered Service' is set, SourceGit will only use it in this repository. Otherwise, if there is more than one service available, a context menu to choose one of them will be shown. HTTP Proxy HTTP proxy used by this repository User Name diff --git a/src/Resources/Locales/zh_CN.axaml b/src/Resources/Locales/zh_CN.axaml index a9b25073..2e8e8843 100644 --- a/src/Resources/Locales/zh_CN.axaml +++ b/src/Resources/Locales/zh_CN.axaml @@ -158,6 +158,9 @@ 规则名 : 为ISSUE生成的URL链接 : 可在URL中使用$1,$2等变量填入正则表达式匹配的内容 + OPEN AI + 启用特定服务 : + 当【启用特定服务】被设置时,SourceGit将在本仓库中仅使用该服务。否则将弹出可用的OpenAI服务列表供用户选择。 HTTP代理 HTTP网络代理 用户名 diff --git a/src/Resources/Locales/zh_TW.axaml b/src/Resources/Locales/zh_TW.axaml index 2ea43167..5f771da8 100644 --- a/src/Resources/Locales/zh_TW.axaml +++ b/src/Resources/Locales/zh_TW.axaml @@ -158,6 +158,9 @@ 規則名稱: 為 Issue 產生的網址連結: 可在網址中使用 $1、$2 等變數填入正則表示式相符的內容 + OPEN AI + 启用特定服务 : + 当【启用特定服务】被设置时,SourceGit将在本仓库中仅使用该服务。否则将弹出可用的OpenAI服务列表供用户选择。 HTTP 代理 HTTP 網路代理 使用者名稱 diff --git a/src/ViewModels/RepositoryConfigure.cs b/src/ViewModels/RepositoryConfigure.cs index 3c969c4d..d7eaa1cb 100644 --- a/src/ViewModels/RepositoryConfigure.cs +++ b/src/ViewModels/RepositoryConfigure.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using Avalonia.Collections; using CommunityToolkit.Mvvm.ComponentModel; @@ -108,6 +109,18 @@ namespace SourceGit.ViewModels set => SetProperty(ref _selectedIssueTrackerRule, value); } + public List AvailableOpenAIServices + { + get; + private set; + } + + public string PreferedOpenAIService + { + get => _repo.Settings.PreferedOpenAIService; + set => _repo.Settings.PreferedOpenAIService = value; + } + public RepositoryConfigure(Repository repo) { _repo = repo; @@ -116,6 +129,13 @@ namespace SourceGit.ViewModels foreach (var remote in _repo.Remotes) Remotes.Add(remote.Name); + AvailableOpenAIServices = new List() { "---" }; + foreach (var service in Preference.Instance.OpenAIServices) + AvailableOpenAIServices.Add(service.Name); + + if (AvailableOpenAIServices.IndexOf(PreferedOpenAIService) == -1) + PreferedOpenAIService = "---"; + _cached = new Commands.Config(repo.FullPath).ListAll(); if (_cached.TryGetValue("user.name", out var name)) UserName = name; diff --git a/src/ViewModels/WorkingCopy.cs b/src/ViewModels/WorkingCopy.cs index f6fb81e2..020053f9 100644 --- a/src/ViewModels/WorkingCopy.cs +++ b/src/ViewModels/WorkingCopy.cs @@ -8,7 +8,6 @@ using Avalonia.Platform.Storage; using Avalonia.Threading; using CommunityToolkit.Mvvm.ComponentModel; -using SourceGit.Models; namespace SourceGit.ViewModels { @@ -884,7 +883,7 @@ namespace SourceGit.ViewModels var menu = new ContextMenu(); var ai = null as MenuItem; - var services = Preference.Instance.OpenAIServices; + var services = GetPreferedOpenAIServices(); if (services.Count > 0) { ai = new MenuItem(); @@ -1251,7 +1250,7 @@ namespace SourceGit.ViewModels return null; } - var services = Preference.Instance.OpenAIServices; + var services = GetPreferedOpenAIServices(); if (services.Count == 0) { App.RaiseException(_repo.FullPath, "Bad configuration for OpenAI"); @@ -1440,6 +1439,25 @@ namespace SourceGit.ViewModels return false; } + private IList GetPreferedOpenAIServices() + { + var services = Preference.Instance.OpenAIServices; + if (services == null || services.Count == 0) + return []; + + if (services.Count == 1) + return services; + + var prefered = _repo.Settings.PreferedOpenAIService; + foreach (var service in services) + { + if (service.Name.Equals(prefered, StringComparison.Ordinal)) + return [service]; + } + + return services; + } + private Repository _repo = null; private bool _isLoadingData = false; private bool _isStaging = false; diff --git a/src/Views/RepositoryConfigure.axaml b/src/Views/RepositoryConfigure.axaml index 6383d904..ced0dab6 100644 --- a/src/Views/RepositoryConfigure.axaml +++ b/src/Views/RepositoryConfigure.axaml @@ -340,6 +340,39 @@ + + + + + + + + + + + + + + + + + + + + + +