|
| 1 | +from typing import List |
| 2 | + |
| 3 | +import cecli.models as models |
| 4 | +from cecli.commands.utils.base_command import BaseCommand |
| 5 | +from cecli.commands.utils.helpers import format_command_result |
| 6 | +from cecli.helpers.conversation import ConversationManager, MessageTag |
| 7 | + |
| 8 | + |
| 9 | +class AgentModelCommand(BaseCommand): |
| 10 | + NORM_NAME = "agent-model" |
| 11 | + DESCRIPTION = "Switch the Agent Model to a new LLM" |
| 12 | + |
| 13 | + @classmethod |
| 14 | + async def execute(cls, io, coder, args, **kwargs): |
| 15 | + """Execute the agent-model command with given parameters.""" |
| 16 | + arg_split = args.split(" ", 1) |
| 17 | + model_name = arg_split[0].strip() |
| 18 | + if not model_name: |
| 19 | + # If no model name provided, show current agent model |
| 20 | + current_agent_model = coder.main_model.agent_model.name |
| 21 | + io.tool_output(f"Current agent model: {current_agent_model}") |
| 22 | + return format_command_result( |
| 23 | + io, "agent-model", f"Displayed current agent model: {current_agent_model}" |
| 24 | + ) |
| 25 | + |
| 26 | + # Create a new model with the same main model and editor model, but updated agent model |
| 27 | + model = models.Model( |
| 28 | + coder.main_model.name, |
| 29 | + editor_model=coder.main_model.editor_model.name, |
| 30 | + weak_model=coder.main_model.weak_model.name, |
| 31 | + agent_model=model_name, |
| 32 | + io=io, |
| 33 | + retries=coder.main_model.retries, |
| 34 | + debug=coder.main_model.debug, |
| 35 | + ) |
| 36 | + await models.sanity_check_models(io, model) |
| 37 | + |
| 38 | + if len(arg_split) > 1: |
| 39 | + # implement architect coder-like generation call for agent model |
| 40 | + message = arg_split[1].strip() |
| 41 | + |
| 42 | + # Store the original model configuration |
| 43 | + original_main_model = coder.main_model |
| 44 | + original_edit_format = coder.edit_format |
| 45 | + |
| 46 | + # Create a temporary coder with the new model |
| 47 | + from cecli.coders import Coder |
| 48 | + |
| 49 | + kwargs = dict() |
| 50 | + kwargs["main_model"] = model |
| 51 | + kwargs["edit_format"] = coder.edit_format # Keep the same edit format |
| 52 | + kwargs["suggest_shell_commands"] = False |
| 53 | + kwargs["total_cost"] = coder.total_cost |
| 54 | + kwargs["num_cache_warming_pings"] = 0 |
| 55 | + kwargs["summarize_from_coder"] = False |
| 56 | + kwargs["done_messages"] = [] |
| 57 | + kwargs["cur_messages"] = [] |
| 58 | + |
| 59 | + new_kwargs = dict(io=io, from_coder=coder) |
| 60 | + new_kwargs.update(kwargs) |
| 61 | + |
| 62 | + # Save current conversation state |
| 63 | + original_coder = coder |
| 64 | + |
| 65 | + temp_coder = await Coder.create(**new_kwargs) |
| 66 | + |
| 67 | + # Re-initialize ConversationManager with temp coder |
| 68 | + ConversationManager.initialize( |
| 69 | + temp_coder, |
| 70 | + reset=True, |
| 71 | + reformat=True, |
| 72 | + preserve_tags=[MessageTag.DONE, MessageTag.CUR], |
| 73 | + ) |
| 74 | + |
| 75 | + verbose = kwargs.get("verbose", False) |
| 76 | + if verbose: |
| 77 | + temp_coder.show_announcements() |
| 78 | + |
| 79 | + try: |
| 80 | + await temp_coder.generate(user_message=message, preproc=False) |
| 81 | + coder.total_cost = temp_coder.total_cost |
| 82 | + coder.coder_commit_hashes = temp_coder.coder_commit_hashes |
| 83 | + |
| 84 | + # Clear manager and restore original state |
| 85 | + ConversationManager.initialize( |
| 86 | + original_coder, |
| 87 | + reset=True, |
| 88 | + reformat=True, |
| 89 | + preserve_tags=[MessageTag.DONE, MessageTag.CUR], |
| 90 | + ) |
| 91 | + |
| 92 | + # Restore the original model configuration |
| 93 | + from cecli.commands import SwitchCoderSignal |
| 94 | + |
| 95 | + raise SwitchCoderSignal( |
| 96 | + main_model=original_main_model, edit_format=original_edit_format |
| 97 | + ) |
| 98 | + except Exception as e: |
| 99 | + # If there's an error, still restore the original model |
| 100 | + if not isinstance(e, SwitchCoderSignal): |
| 101 | + io.tool_error(str(e)) |
| 102 | + raise SwitchCoderSignal( |
| 103 | + main_model=original_main_model, edit_format=original_edit_format |
| 104 | + ) |
| 105 | + else: |
| 106 | + # Re-raise SwitchCoderSignal if that's what was thrown |
| 107 | + raise |
| 108 | + else: |
| 109 | + from cecli.commands import SwitchCoderSignal |
| 110 | + |
| 111 | + raise SwitchCoderSignal(main_model=model, edit_format=coder.edit_format) |
| 112 | + |
| 113 | + @classmethod |
| 114 | + def get_completions(cls, io, coder, args) -> List[str]: |
| 115 | + """Get completion options for agent-model command.""" |
| 116 | + return models.get_chat_model_names() |
| 117 | + |
| 118 | + @classmethod |
| 119 | + def get_help(cls) -> str: |
| 120 | + """Get help text for the agent-model command.""" |
| 121 | + help_text = super().get_help() |
| 122 | + help_text += "\nUsage:\n" |
| 123 | + help_text += " /agent-model <model-name> # Switch to a new agent model\n" |
| 124 | + help_text += ( |
| 125 | + " /agent-model <model-name> <prompt> # Use a specific agent model for a single" |
| 126 | + " prompt\n" |
| 127 | + ) |
| 128 | + help_text += "\nExamples:\n" |
| 129 | + help_text += ( |
| 130 | + " /agent-model gpt-4o-mini # Switch to GPT-4o Mini as agent model\n" |
| 131 | + ) |
| 132 | + help_text += ( |
| 133 | + " /agent-model claude-3-haiku # Switch to Claude 3 Haiku as agent model\n" |
| 134 | + ) |
| 135 | + help_text += ' /agent-model o1-mini "review this code" # Use o1-mini to review code\n' |
| 136 | + help_text += ( |
| 137 | + "\nWhen switching agent models, the main model and editor model remain unchanged.\n" |
| 138 | + ) |
| 139 | + help_text += ( |
| 140 | + "\nIf you provide a prompt after the model name, that agent model will be used\n" |
| 141 | + ) |
| 142 | + help_text += "just for that prompt, then you'll return to your original agent model.\n" |
| 143 | + return help_text |
0 commit comments