|
| 1 | +using System.CommandLine; |
| 2 | +using System.CommandLine.Completions; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using AIShell.Abstraction; |
| 5 | + |
| 6 | +namespace AIShell.Ollama.Agent; |
| 7 | + |
| 8 | +internal sealed class ConfigCommand : CommandBase |
| 9 | +{ |
| 10 | + private readonly OllamaAgent _agnet; |
| 11 | + public ConfigCommand(OllamaAgent agent) |
| 12 | + : base("config", "Command for config management within the 'ollama' agent.") |
| 13 | + { |
| 14 | + _agnet = agent; |
| 15 | + |
| 16 | + var use = new Command("use", "Specify a config to use."); |
| 17 | + var useConfig = new Argument<string>( |
| 18 | + name: "Config", |
| 19 | + getDefaultValue: () => null, |
| 20 | + description: "Name of a configuration.").AddCompletions(ConfigNameCompleter); |
| 21 | + use.AddArgument(useConfig); |
| 22 | + use.SetHandler(UseConfigAction, useConfig); |
| 23 | + |
| 24 | + var list = new Command("list", "List a specific config, or all available configs."); |
| 25 | + var listConfig = new Argument<string>( |
| 26 | + name: "Config", |
| 27 | + getDefaultValue: () => null, |
| 28 | + description: "Name of a configuration.").AddCompletions(ConfigNameCompleter); |
| 29 | + list.AddArgument(listConfig); |
| 30 | + list.SetHandler(ListConfigAction, listConfig); |
| 31 | + |
| 32 | + AddCommand(list); |
| 33 | + AddCommand(use); |
| 34 | + } |
| 35 | + |
| 36 | + private void ListConfigAction(string name) |
| 37 | + { |
| 38 | + IHost host = Shell.Host; |
| 39 | + |
| 40 | + // Reload the setting file if needed. |
| 41 | + _agnet.ReloadSettings(); |
| 42 | + |
| 43 | + Settings settings = _agnet.Settings; |
| 44 | + |
| 45 | + if (settings is null) |
| 46 | + { |
| 47 | + host.WriteErrorLine("Invalid configuration."); |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + if (string.IsNullOrEmpty(name)) |
| 52 | + { |
| 53 | + settings.ListAllConfigs(host); |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + try |
| 58 | + { |
| 59 | + settings.ShowOneConfig(host, name); |
| 60 | + } |
| 61 | + catch (InvalidOperationException ex) |
| 62 | + { |
| 63 | + string availableConfigNames = ConfigNamesAsString(); |
| 64 | + host.WriteErrorLine($"{ex.Message} Available cofiguration(s): {availableConfigNames}."); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + private async Task UseConfigAction(string name) |
| 69 | + { |
| 70 | + // Reload the setting file if needed. |
| 71 | + _agnet.ReloadSettings(); |
| 72 | + |
| 73 | + var setting = _agnet.Settings; |
| 74 | + var host = Shell.Host; |
| 75 | + |
| 76 | + if (setting is null || setting.Configs.Count is 0) |
| 77 | + { |
| 78 | + host.WriteErrorLine("No configs configured."); |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + try |
| 83 | + { |
| 84 | + ModelConfig chosenConfig = (string.IsNullOrEmpty(name) |
| 85 | + ? host.PromptForSelectionAsync( |
| 86 | + title: "[orange1]Please select a [Blue]Configuration[/] to use[/]:", |
| 87 | + choices: setting.Configs, |
| 88 | + converter: ConfigName, |
| 89 | + CancellationToken.None).GetAwaiter().GetResult() |
| 90 | + : setting.Configs.FirstOrDefault(c => c.Name == name)) ?? throw new InvalidOperationException($"The configuration '{name}' doesn't exist."); |
| 91 | + await setting.UseConfg(host, chosenConfig); |
| 92 | + host.MarkupLine($"Using the config [green]{chosenConfig.Name}[/]:"); |
| 93 | + } |
| 94 | + catch (InvalidOperationException ex) |
| 95 | + { |
| 96 | + string availableConfigNames = ConfigNamesAsString(); |
| 97 | + host.WriteErrorLine($"{ex.Message} Available configurations: {availableConfigNames}."); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + private static string ConfigName(ModelConfig config) => config.Name.Any(Char.IsWhiteSpace) ? $"\"{config.Name}\"" : config.Name; |
| 102 | + private IEnumerable<string> ConfigNameCompleter(CompletionContext context) => _agnet.Settings?.Configs?.Select(ConfigName) ?? []; |
| 103 | + private string ConfigNamesAsString() => string.Join(", ", ConfigNameCompleter(null)); |
| 104 | +} |
| 105 | + |
| 106 | +internal sealed class SystemPromptCommand : CommandBase |
| 107 | +{ |
| 108 | + private readonly OllamaAgent _agnet; |
| 109 | + |
| 110 | + public SystemPromptCommand(OllamaAgent agent) |
| 111 | + : base("system-prompt", "Command for system prompt management within the 'ollama' agent.") |
| 112 | + { |
| 113 | + _agnet = agent; |
| 114 | + |
| 115 | + var show = new Command("show", "Show the current system prompt."); |
| 116 | + show.SetHandler(ShowSystemPromptAction); |
| 117 | + |
| 118 | + var set = new Command("set", "Sets the system prompt."); |
| 119 | + var systemPromptModel = new Argument<string>( |
| 120 | + name: "System-Prompt", |
| 121 | + getDefaultValue: () => null, |
| 122 | + description: "The system prompt"); |
| 123 | + set.AddArgument(systemPromptModel); |
| 124 | + set.SetHandler(SetSystemPromptAction, systemPromptModel); |
| 125 | + |
| 126 | + AddCommand(show); |
| 127 | + AddCommand(set); |
| 128 | + } |
| 129 | + |
| 130 | + private void ShowSystemPromptAction() |
| 131 | + { |
| 132 | + IHost host = Shell.Host; |
| 133 | + |
| 134 | + // Reload the setting file if needed. |
| 135 | + _agnet.ReloadSettings(); |
| 136 | + |
| 137 | + Settings settings = _agnet.Settings; |
| 138 | + |
| 139 | + if (settings is null) |
| 140 | + { |
| 141 | + host.WriteErrorLine("Invalid configuration."); |
| 142 | + return; |
| 143 | + } |
| 144 | + |
| 145 | + try |
| 146 | + { |
| 147 | + settings.ShowSystemPrompt(host); |
| 148 | + } |
| 149 | + catch (InvalidOperationException ex) |
| 150 | + { |
| 151 | + host.WriteErrorLine($"{ex.Message}"); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + private void SetSystemPromptAction(string prompt) |
| 156 | + { |
| 157 | + IHost host = Shell.Host; |
| 158 | + |
| 159 | + // Reload the setting file if needed. |
| 160 | + _agnet.ReloadSettings(); |
| 161 | + _agnet.ResetContext(); |
| 162 | + |
| 163 | + Settings settings = _agnet.Settings; |
| 164 | + |
| 165 | + if (settings is null) |
| 166 | + { |
| 167 | + host.WriteErrorLine("Invalid configuration."); |
| 168 | + return; |
| 169 | + } |
| 170 | + |
| 171 | + try |
| 172 | + { |
| 173 | + settings.SetSystemPrompt(host, prompt); |
| 174 | + } |
| 175 | + catch (InvalidOperationException ex) |
| 176 | + { |
| 177 | + host.WriteErrorLine($"{ex.Message}."); |
| 178 | + } |
| 179 | + } |
| 180 | +} |
| 181 | + |
| 182 | +internal sealed class ModelCommand : CommandBase |
| 183 | +{ |
| 184 | + private readonly OllamaAgent _agnet; |
| 185 | + |
| 186 | + public ModelCommand(OllamaAgent agent) |
| 187 | + : base("model", "Command for model management within the 'ollama' agent.") |
| 188 | + { |
| 189 | + _agnet = agent; |
| 190 | + |
| 191 | + var use = new Command("use", "Specify a model to use, or choose one from the available models."); |
| 192 | + var useModel = new Argument<string>( |
| 193 | + name: "Model", |
| 194 | + getDefaultValue: () => null, |
| 195 | + description: "Name of a model.").AddCompletions(ModelNameCompleter); |
| 196 | + use.AddArgument(useModel); |
| 197 | + use.SetHandler(UseModelAction, useModel); |
| 198 | + |
| 199 | + var list = new Command("list", "List a specific model, or all available models."); |
| 200 | + var listModel = new Argument<string>( |
| 201 | + name: "Model", |
| 202 | + getDefaultValue: () => null, |
| 203 | + description: "Name of a model.").AddCompletions(ModelNameCompleter); |
| 204 | + list.AddArgument(listModel); |
| 205 | + list.SetHandler(ListModelAction, listModel); |
| 206 | + |
| 207 | + AddCommand(list); |
| 208 | + AddCommand(use); |
| 209 | + } |
| 210 | + |
| 211 | + private void ListModelAction(string name) |
| 212 | + { |
| 213 | + IHost host = Shell.Host; |
| 214 | + |
| 215 | + // Reload the setting file if needed. |
| 216 | + _agnet.ReloadSettings(); |
| 217 | + |
| 218 | + Settings settings = _agnet.Settings; |
| 219 | + |
| 220 | + if (settings is null) |
| 221 | + { |
| 222 | + host.WriteErrorLine("Invalid configuration."); |
| 223 | + return; |
| 224 | + } |
| 225 | + |
| 226 | + if (string.IsNullOrEmpty(name)) |
| 227 | + { |
| 228 | + settings.ListAllModels(host).GetAwaiter().GetResult(); |
| 229 | + return; |
| 230 | + } |
| 231 | + |
| 232 | + try |
| 233 | + { |
| 234 | + settings.ShowOneModel(host, name).GetAwaiter().GetResult(); |
| 235 | + } |
| 236 | + catch (InvalidOperationException ex) |
| 237 | + { |
| 238 | + string availableModelNames = ModelNamesAsString(); |
| 239 | + host.WriteErrorLine($"{ex.Message} Available Models(s): {availableModelNames}."); |
| 240 | + } |
| 241 | + } |
| 242 | + |
| 243 | + private void UseModelAction(string name) |
| 244 | + { |
| 245 | + // Reload the setting file if needed. |
| 246 | + _agnet.ReloadSettings(); |
| 247 | + |
| 248 | + var setting = _agnet.Settings; |
| 249 | + var host = Shell.Host; |
| 250 | + |
| 251 | + if (setting is null || setting.GetAllModels().GetAwaiter().GetResult().Count is 0) |
| 252 | + { |
| 253 | + host.WriteErrorLine("No models configured."); |
| 254 | + return; |
| 255 | + } |
| 256 | + |
| 257 | + try |
| 258 | + { |
| 259 | + OllamaModel chosenModel = string.IsNullOrEmpty(name) |
| 260 | + ? host.PromptForSelectionAsync( |
| 261 | + title: "[orange1]Please select a [Blue]Model[/] to use[/]:", |
| 262 | + choices: setting.GetAllModels().GetAwaiter().GetResult(), |
| 263 | + converter: ModelName, |
| 264 | + CancellationToken.None).GetAwaiter().GetResult() |
| 265 | + : setting.GetModelByName(name).GetAwaiter().GetResult(); |
| 266 | + |
| 267 | + setting.UseModel(chosenModel); |
| 268 | + host.MarkupLine($"Using the model [green]{chosenModel.Name}[/]:"); |
| 269 | + } |
| 270 | + catch (InvalidOperationException ex) |
| 271 | + { |
| 272 | + string availableModelNames = ModelNamesAsString(); |
| 273 | + host.WriteErrorLine($"{ex.Message} Available Modless: {availableModelNames}."); |
| 274 | + } |
| 275 | + } |
| 276 | + |
| 277 | + private static string ModelName(OllamaModel model) => model.Name; |
| 278 | + private IEnumerable<string> ModelNameCompleter(CompletionContext context) => _agnet.Settings?.GetAllModels().GetAwaiter().GetResult().Select(ModelName) ?? []; |
| 279 | + private string ModelNamesAsString() => string.Join(", ", ModelNameCompleter(null)); |
| 280 | +} |
0 commit comments