sigoden/aichat · error

No agent

Error message

No agent

What it means

Thrown by GlobalConfig::agent_info() when `self.agent` is None. agent_info() exports the currently selected agent's configuration as a string, so without an agent selected there is nothing to export. It is a deliberate bail! rather than an Option because callers expect a Result<String>.

Solutions

  1. Select an agent first (config.use_agent(Some("<name>")) or the corresponding CLI command) before calling agent_info().
  2. Guard the call: check config.read().agent.is_some() before invoking agent_info().
  3. If no agent is intended, run `agent init <name>` to create one.

Example fix

// before
let info = config.agent_info()?;
// after
if config.read().agent.is_none() {
    eprintln!("No agent selected; run `agent init <name>` first");
    return Ok(());
}
let info = config.agent_info()?;
Defensive patterns

Strategy: type-guard

Validate before calling

if config.read().agent.is_none() { /* handle missing agent */ }

Type guard

fn has_agent(config: &GlobalConfig) -> bool { config.read().agent.is_some() }

Try / catch

match config.agent_info() { Ok(s) => println!("{s}"), Err(e) if e.to_string() == "No agent" => eprintln!("Select an agent first"), Err(e) => return Err(e) }

Prevention

When it happens

Trigger: Calling config.agent_info() (via a CLI subcommand like `aichat agent info` or `--agent-info`) before any agent has been selected with use_agent / set_agent, or after the configured agent failed to load.

Common situations: Running an agent-info command in a fresh installation where no agent is set in config.yaml; scripting agent info before `agent init`; config file present but the `agent` key is absent or null.

Understand the failure class

Background: "X is required", "must be set", "cannot be empty": the missing-required-config error family, from Vertex AI project/location to WeChat keys — this error's family across 18 libraries.

Related errors


AI-assisted analysis of sigoden/aichat@82976d349a (2026-09-09). Data as JSON: /api/errors/b99e7a27360b0206. Report an issue: GitHub.

Appendix: source

Thrown at src/config/mod.rs:1522

            } else {
                agent.agent_prelude().map(|v| v.to_string())
            }
        });
        config.write().rag = agent.rag();
        config.write().agent = Some(agent);
        if let Some(session) = session {
            config.write().use_session(Some(&session))?;
        } else {
            config.write().init_agent_shared_variables()?;
        }
        Ok(())
    }

    pub fn agent_info(&self) -> Result<String> {
        if let Some(agent) = &self.agent {
            agent.export()
        } else {
            bail!("No agent")
        }
    }

    pub fn agent_banner(&self) -> Result<String> {
        if let Some(agent) = &self.agent {
            Ok(agent.banner())
        } else {
            bail!("No agent")
        }
    }

    pub fn edit_agent_config(&self) -> Result<()> {
        let agent_name = match &self.agent {
            Some(agent) => agent.name(),
            None => bail!("No agent"),
        };
        let agent_config_path = Config::agent_config_file(agent_name);
        ensure_parent_exists(&agent_config_path)?;

View on GitHub (pinned to 82976d349a)