Skip to content

Restructure key files#2

Merged
ScriptSmith merged 1 commit intomainfrom
key-file-restructure
Mar 3, 2026
Merged

Restructure key files#2
ScriptSmith merged 1 commit intomainfrom
key-file-restructure

Conversation

@ScriptSmith
Copy link
Owner

Several key files have grown too large and lack consistent organization. This refactoring improves maintainability without changing behavior:

  • src/main.rs (3,940 lines) — monolithic entry point mixing app setup, CLI, and service init
  • src/routes/api.rs (15,881 lines) — all API handlers in one file
  • src/middleware/ — mixes true middleware with utilities and services
  • migrations_sqlx/ — inconsistent comment styles and column ordering

@greptile-apps
Copy link

greptile-apps bot commented Mar 3, 2026

Greptile Summary

This PR successfully refactors three monolithic files into well-organized modules without changing behavior:

Key Improvements:

  • src/main.rs: Reduced from ~3,940 lines to 49 lines by extracting:

    • App setup logic → src/app.rs (2,014 lines)
    • CLI commands → src/cli/ directory (7 focused modules)
    • Init helpers → src/init.rs (385 lines)
  • src/routes/api.rs: Split 15,881-line monolith into 8 focused modules:

    • chat.rs, embeddings.rs, images.rs, audio.rs, files.rs, vector_stores.rs, models.rs, plus shared mod.rs
  • src/middleware/: Reorganized with clear documentation and proper encapsulation:

    • Moved file_searchsrc/services/file_search_tool.rs (correct location for a service)
    • Made internal helpers (budget, scope, usage) truly internal
    • Added comprehensive module documentation explaining the middleware pipeline
  • Migration files: Added consistent section headers (======) and reordered columns for readability; no schema changes

Import Updates: All affected files updated to use new module paths (middleware::FileSearchToolArgumentsservices::FileSearchToolArguments)

This refactoring significantly improves maintainability and code navigation without introducing functional changes.

Confidence Score: 5/5

  • This PR is safe to merge - it's a pure refactoring with no functional changes
  • Score of 5 reflects that this is purely organizational refactoring: no logic changes, no new features, no bug fixes - just moving code between files and updating import paths. All changes are straightforward code movement with consistent patterns throughout.
  • No files require special attention - all changes follow the same refactoring pattern

Important Files Changed

Filename Overview
migrations_sqlx/postgres/20250101000000_initial.sql Added consistent section headers and reordered table columns for better readability; no functional schema changes
migrations_sqlx/sqlite/20250101000000_initial.sql Added consistent section headers and reordered table columns to match PostgreSQL version; no functional schema changes
src/main.rs Reduced from ~3,940 lines to 49 lines by extracting app setup to app.rs, CLI handling to cli/, and init logic to init.rs
src/app.rs New file containing app setup logic extracted from main.rs (AppState, build_app, UI routes, docs routes)
src/cli/mod.rs New CLI module with Args parser and command dispatcher; delegates to submodules for each command
src/routes/api/mod.rs Replaced monolithic 15,881-line api.rs with modular structure; contains common helpers and re-exports submodules
src/routes/api/chat.rs New file containing chat completion handlers extracted from api.rs
src/middleware/mod.rs Improved organization with documentation, clear sections, and internal helpers no longer publicly exported
src/services/file_search_tool.rs Moved from middleware/file_search.rs to services/ where it belongs as a service, not middleware

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    subgraph Before["Before: Monolithic Structure"]
        A1["main.rs<br/>(3,940 lines)<br/>• App setup<br/>• CLI parsing<br/>• Server startup<br/>• UI routes<br/>• All logic"]
        A2["routes/api.rs<br/>(15,881 lines)<br/>• Chat handlers<br/>• Embeddings<br/>• Images<br/>• Audio<br/>• Files<br/>• Vector stores<br/>• Models"]
        A3["middleware/<br/>• Combined middleware<br/>• File search (misplaced)"]
    end
    
    subgraph After["After: Modular Structure"]
        B1["main.rs<br/>(49 lines)<br/>Entry point only"]
        
        B2["app.rs<br/>(2,014 lines)<br/>App setup & routing"]
        
        B3["cli/<br/>• mod.rs (289 lines)<br/>• server.rs<br/>• bootstrap.rs<br/>• migrate.rs<br/>• worker.rs<br/>• features.rs<br/>• init.rs<br/>• openapi.rs"]
        
        B4["init.rs<br/>(385 lines)<br/>Init helpers"]
        
        B5["routes/api/<br/>• mod.rs (shared)<br/>• chat.rs (2,323 lines)<br/>• embeddings.rs (312)<br/>• images.rs (874)<br/>• audio.rs (849)<br/>• files.rs (783)<br/>• vector_stores.rs (2,151)<br/>• models.rs (591)"]
        
        B6["services/<br/>• file_search_tool.rs<br/>(moved from middleware)"]
        
        B7["middleware/<br/>• Combined middleware<br/>• Internal helpers<br/>• Better docs"]
        
        B1 --> B2
        B1 --> B3
        B1 --> B4
    end
    
    A1 -.->|"Extracted to"| B2
    A1 -.->|"Extracted to"| B3
    A1 -.->|"Extracted to"| B4
    A2 -.->|"Split into"| B5
    A3 -.->|"Reorganized"| B6
    A3 -.->|"Cleaned up"| B7
    
    style Before fill:#ffebee
    style After fill:#e8f5e9
    style A1 fill:#ffcdd2
    style A2 fill:#ffcdd2
    style B1 fill:#c8e6c9
    style B5 fill:#c8e6c9
Loading

Last reviewed commit: 00d0d88

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors the gateway’s Rust code and SQL migrations to improve organization and maintainability, primarily by extracting functionality into more focused modules without intended behavioral changes.

Changes:

  • Moved file-search tool interception types/helpers from middleware into services and updated imports across routes/providers.
  • Split API handlers into dedicated route modules (e.g., routes/api/{models,embeddings,images,audio,files}.rs).
  • Added/expanded CLI subcommands and initialization helpers (serve, worker, migrate, bootstrap, openapi, schema, features) and standardized migration section comments/ordering.

Reviewed changes

Copilot reviewed 30 out of 33 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/services/mod.rs Exposes new file_search_tool module and re-exports tool-related types/functions.
src/services/file_search_tool.rs Updates module docs to reflect service/tool interception responsibilities.
src/services/file_search.rs Updates imports to use services::FileSearchAuthContext after refactor.
src/routes/execution.rs Switches preprocess_file_search_tools import from middleware to services.
src/routes/api/models.rs New models endpoint module with provider aggregation and enrichment.
src/routes/api/embeddings.rs New embeddings endpoint module (currently has a compile-scoping issue for json!).
src/routes/api/images.rs New images endpoints module.
src/routes/api/audio.rs New audio endpoints module.
src/routes/api/files.rs New files endpoints module.
src/providers/{vertex,bedrock,anthropic}/convert.rs Updates file-search tool argument imports to services.
src/middleware/mod.rs Adds module-level docs and narrows exports; keeps internal helpers private.
src/middleware/combined.rs Adjusts internal imports after middleware module re-organization.
src/init.rs Adds shared provider instantiation + worker embedding/vector-store init helpers.
src/guardrails/audit.rs Updates reference to new API module path.
src/config/features.rs Small refactor in queue config validation (match guards).
src/cli/* Adds modular CLI implementation (server/worker/migrate/bootstrap/openapi/schema/features).
migrations_sqlx/sqlite/20250101000000_initial.sql Re-organizes comments/sections and column ordering for consistency.
migrations_sqlx/postgres/20250101000000_initial.sql Re-organizes comments/sections and column ordering for consistency.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +23 to +36
request_body(
content = api_types::CreateEmbeddingPayload,
examples(
("Single text" = (
summary = "Embed a single text string",
value = json!({
"model": "openai/text-embedding-3-small",
"input": "Hello world"
})
)),
("Multiple texts" = (
summary = "Embed multiple texts in one request",
value = json!({
"model": "openai/text-embedding-3-large",
Copy link

Copilot AI Mar 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

json! is used in the utoipa::path examples here, but this module doesn’t import the macro (use serde_json::json;) or qualify it (serde_json::json!). As written, this won’t compile because json! won’t be in scope for this module.

Copilot uses AI. Check for mistakes.
Comment on lines +269 to +273
fn create_default_config() -> Result<(PathBuf, bool), String> {
let config_dir = default_config_dir().ok_or("Could not determine config directory")?;
let config_path = config_dir.join("hadrian.toml");
let data_dir = default_data_dir().ok_or("Could not determine data directory")?;

Copy link

Copilot AI Mar 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

create_default_config() requires default_config_dir() / default_data_dir() to return Some(...), but in non-wizard builds those helpers return None, causing an error instead of creating a default config. This makes fresh installs unusable under profiles like --no-default-features --features tiny unless a config path is always provided.

Copilot uses AI. Check for mistakes.
@ScriptSmith ScriptSmith merged commit 924f718 into main Mar 3, 2026
24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants