Dependency Injection & System Wiring
Book uses Metro, a compile-time dependency injection library for Kotlin. Unlike runtime DI frameworks, Metro generates standard Kotlin code to wire your dependencies, which keeps the binary small and start-up time fast.
The AppGraph
The AppGraph is the central definition of all dependencies in the system. It is located in jvm/cli/src/main/kotlin/us/nineworlds/book/cli/di/AppGraph.kt.
AppGraph is composed by inheriting four domain-aligned provider module interfaces:
-
TuiModule(us.nineworlds.book.cli.di.TuiModule): Provides TUI subsystem dependencies (Terminal,TuiRenderer,Tui,CliLogger,SlashCommandRegistry). -
AgentLoopModule(us.nineworlds.book.cli.di.AgentLoopModule): Provides agent loop and LLM execution dependencies (AgentLoopEnvironment,AgentLoopStateMachine,AgentLoopFactory,Agent,RetryConfig,ProviderConfig,PromptExecutorFactory,LLMClientAPI,CompactionPolicy). -
ToolsModule(us.nineworlds.book.cli.di.ToolsModule): Provides tools, skills, prompt loader, extensions, ACP, and MCP dependencies (ToolRegistry,ExtensionRegistry,SkillRegistry,ExtensionContext,SystemPromptLoader,McpClientFacade,AcpServerDependencies,AcpServer,ServeCommand,Set<Tool>). -
InfrastructureModule(us.nineworlds.book.cli.di.InfrastructureModule): Provides core infrastructure I/O, process, and session dependencies (FileSystem,ConfigManager,SessionManager,SessionAutoNamer,DynamicLogRouter,SessionOrchestrator,DefaultSessionResolver,ProcessRunner).
Key Components Provided:
-
AgentLoop(fromus.nineworlds.book.domain.agentloop): The agent orchestrator. Not a singleton โ one instance is created per active session viaAgentLoopFactory. -
Agent(fromus.nineworlds.book.domain): Provided as aKoogAgent(us.nineworlds.book.providers.koog) viaAgentLoopModule. -
ProviderConfig(fromus.nineworlds.book.infrastructure): Pure data class holding provider credentials (openAiKey,openAiBaseUrl,anthropicKey) resolved at application startup from environment variables viaAgentLoopModule.provideProviderConfig(). Injected directly intoKoogAgentandPromptExecutorFactorywithout mutating JVM system properties. -
LLMClientAPI(from Koogprompt-executor-clients): Provided asOpenAILLMClientviaAgentLoopModule.provideLlmClient(), scoped@SingleIn(AppScope::class). Injected intoKoogAgentfor model listing โ delegates toLLMClientAPI.models()and enriches results viaModelMetadataRegistry. -
FileSystem(fromus.nineworlds.book.domain.filesystem): Provided as aSandboxedOkioFileSystem(us.nineworlds.book.infrastructure.filesystem) viaInfrastructureModule. -
ToolRegistry(fromus.nineworlds.book.domain.tools): Wires the built-in tools (ReadFileTool,BashTool, etc.) fromus.nineworlds.book.infrastructure.toolsviaToolsModule. -
ProcessRunner(fromus.nineworlds.book.domain.process): Executes external processes. Implemented byProcessBuilderRunner(us.nineworlds.book.infrastructure.process) as a@SingleIn(AppScope::class)singleton viaInfrastructureModule. Injected intoKtsToolLoaderand forwarded to everyBookScriptReceiverso scripts can callprocessRunner.run(command)for non-blocking coroutine-safe process execution. -
ExtensionScriptLoader(fromus.nineworlds.book.domain.extensions): Seam for loading extension scripts asynchronously. Provided byKtsToolLoader(us.nineworlds.book.scriptsin:jvm:scripting) as a@SingleIn(AppScope::class)singleton viaInfrastructureModule. Exposed onAppGraphasval extensionScriptLoader: ExtensionScriptLoader. -
SystemPromptLoader(fromus.nineworlds.book.domain.systemprompt): Loads and merges system prompts viaToolsModule. -
Session stack:
SessionManager,SessionAutoNamer, andDefaultSessionResolver(all fromus.nineworlds.book.infrastructure.session) are wired viaInfrastructureModulefor persistence, resume flows, and automatic session naming.
@SingleIn(AppScope::class)
@DependencyGraph
interface AppGraph : TuiModule, AgentLoopModule, ToolsModule, InfrastructureModule {
val agentLoop: AgentLoop
val tui: Tui
// ...
}
Initialization Sequence
When you run book, the following happens in Main.kt:
-
Graph Creation:
createGraph<AppGraph>()is called. Metro generates the implementation and instantiates all the components across all feature provider modules. -
KTS Loading: The
ExtensionScriptLoader(implemented byKtsToolLoaderinus.nineworlds.book.scriptsin:jvm:scripting) scans~/.bk/toolsand./.bk/toolsfor.bk.ktsscripts. These are compiled and their tools/commands are registered in the registries provided byAppGraph.KtsToolLoaderinjectsProcessRunnerinto each script’sBookScriptReceiver, makingprocessRunneravailable as a top-level property in script scope โ scripts use it to launch external processes without blocking the agent UI. -
CLI Parsing: Clikt parses the command-line arguments.
-
Session Resolution: Optional
--session,--continue, or--resumeflags resolve and load session history before TUI start. -
TUI Start: If no sub-command is given, the
Tuiis started, taking control of the terminal.
Extending the Graph
To add a new dependency:
-
Define the interface in
:core:domain. -
Implement it in
:core:infrastructureor appropriate feature module. -
Add a
@Providesmethod to the relevant feature provider module (TuiModule,AgentLoopModule,ToolsModule, orInfrastructureModule), or an abstractvalonAppGraphin:jvm:cli. -
Rebuild the project (
./gradlew assemble). Metro will regenerate the graph implementation.