AIAgent / init_agent
源码版本v2026.7.20
Responsibility
init_agent is the agent's "assembly workshop": it injects provider, toolsets, compression threshold, custom provider extra_body, memory and learning components into the agent instance so it has every dependency needed to run a turn of conversation. The AIAgent class itself is just a thin shell around its output.
Key Files
class AIAgent:400-497— class definition; fields are config__init__ forwarder:497-560—from agent.agent_init import init_agent; init_agent(...)def init_agent:276— assembly main function entrycustom provider extra_body helpers:183-275—_normalized_custom_base_url/_custom_provider_model_matches/_merge_custom_provider_extra_body_resolve_compression_threshold:93-127— compression threshold resolutionhelper function region:62-91—_ra, autoraise notice, etc.
Data Flow
AIAgent.__init__(run_agent.py:423) collects constructor parameters as-is.- It calls
init_agent:276, which in order:- resolves the provider name and base_url (
agent/agent_init.py:183) - merges the custom provider's
extra_body(agent/agent_init.py:257) - resolves the compression threshold (
agent/agent_init.py:93) - injects toolsets, memory manager, learning graph, etc.
- resolves the provider name and base_url (
- The assembled agent exposes the
run_conversationmethod (run_agent.py:6350), forwarding to the module-levelagent/conversation_loop.py:588. - The gateway's
GatewayRunner(gateway/run.py:3029) holds the assembled agent and hands inbound messages to it.
Summary
init_agent is the translation layer from config to runtime; every startup-time decision (provider selection, compression strategy, toolsets toggles) is settled here once. Afterward the agent enters a stateless "feed message → run loop" rhythm and no longer revisits config.