Expand description
Shared utility surface for trusty-* projects.
Why: Port auto-detect, data-directory resolution, tracing init, NO_COLOR handling, and the OpenRouter chat-completions client appeared in both trusty-memory and trusty-search with subtle divergence. Centralising keeps them aligned and gives future trusty-* binaries a one-import surface.
What: pure utility functions — no global state. Each subsystem is a free function or a small helper struct.
Test: cargo test -p trusty-common covers port walking, data-dir creation,
and the OpenRouter request shape (without hitting the network).
§Test isolation: TRUSTY_DATA_DIR_OVERRIDE
macOS’s dirs::data_dir() resolves the application-support directory via
NSFileManager, a native Cocoa API that completely ignores the HOME and
XDG_DATA_HOME environment variables. This makes it impossible to redirect
data-directory access in tests using ordinary env-var tricks, because the
kernel query bypasses the environment entirely.
To work around this, resolve_data_dir checks the
DATA_DIR_OVERRIDE_ENV (TRUSTY_DATA_DIR_OVERRIDE) environment variable
before consulting dirs::data_dir(). When set, the variable’s value is used
as the base directory verbatim, and dirs::data_dir() is never called.
This escape hatch is intended for testing only. Do not set it in production deployments; rely on the OS-standard data directory instead.
Re-exports§
pub use chat::ChatEvent;pub use chat::ChatProvider;pub use chat::LocalModelConfig;pub use chat::OllamaProvider;pub use chat::OpenRouterProvider;pub use chat::ToolCall;pub use chat::ToolDef;pub use chat::auto_detect_local_provider;
Modules§
- chat
- Provider-agnostic streaming chat abstraction with tool-use support.
- claude_
config - Claude Code configuration discovery and patching.
- project_
discovery - Claude Code project directory discovery.
Structs§
- Chat
Message - OpenAI-compatible chat message.
Constants§
- DATA_
DIR_ OVERRIDE_ ENV - Environment variable name for the data-directory test escape hatch.
Functions§
- bind_
with_ auto_ port - Bind to
addr; if the port is in use, walk forward up tomax_attemptsports and return the first listener that binds. - init_
tracing - Initialise the global tracing subscriber.
- is_dir
- Check whether a path exists and is a directory.
- maybe_
disable_ color - Disable coloured terminal output when requested or when stdout is not a TTY.
- openrouter_
chat Deprecated - Send a chat completion request to OpenRouter and return the assistant’s message content.
- openrouter_
chat_ stream Deprecated - Stream chat-completion deltas from OpenRouter through a tokio mpsc channel.
- read_
daemon_ addr - Read the daemon’s HTTP address from the app’s data directory.
- resolve_
data_ dir - Resolve
<data_dir>/<app_name>, creating it if it doesn’t exist. - write_
daemon_ addr - Write the daemon’s bound HTTP address to the app’s data directory.