# hirokidaichi/imark architecture

> Architecture diagram of the GitHub repository hirokidaichi/imark, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-11). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/hirokidaichi/imark
- Repository: https://github.com/hirokidaichi/imark

## Overview

##### Project Type & Purpose
- This is a CLI tool for AI-powered image processing that performs tasks such as image recognition (caption generation), cataloging (organizing images from directories), and text-to-image generation. In addition, it can operate as an MCP (Model Context Protocol) server for interactive use with tools like Claude and Cursor.
- The README clearly lays out features like smart image recognition, multi-style image generation, and language support, while the file tree shows a collection of command implementations and supportive utility modules.

##### High-Level File Structure Analysis
- Top-Level Files: Configuration files (deno.json, deno.lock), licensing, README, and additional documentation in the inst/ directory.
- Source Code (src/): Contains the main logic divided into two key areas:
- commands/: Houses individual CLI commands such as:
◦ caption – for single image captioning.
◦ catalog – for processing a whole directory of images.
◦ gen – for generating images from text.
◦ mcp – for running the MCP server.
◦ log and configure – for logging of operations and setting up API keys.
- utils/: Provides core functionalities such as configuration management, file handling, image processing helpers (image, imagefx), and logging.
- Test Files: Most commands and utilities have corresponding test files (with .test.ts suffix) ensuring modularity and testability.

##### Insights from the README
- The README explains the image recognition/generation features, sample commands, supported options, and integrations with external AI APIs (Gemini API, ImageFX API).
- It also details how the MCP server is implemented demonstrating the use of StdioServerTransport for the MCP protocol.
- The installation instructions via Deno indicate that the project is utilizing Deno as its runtime, built with TypeScript.

a. Main Components to Represent:
- Command Line Interface (CLI) Entry Point:
◦ The main.ts file in src/commands acts as the starting point for processing command-line input.
- Command Handlers:
◦ Separate modules for each command (caption, catalog, gen, mcp, log, and configure) representing distinct functional units.
- Utility Modules:
◦ Utilities for configuration, file operations, image processing, and logging provide core support functions for the commands.
- External API Integration:
◦ Components interacting with the Gemini API, ImageFX API, and any other AI services.
- MCP Server & Transport:
◦ The MCP command and its submodules (within src/commands/mcp and its tool directory) that allow the project to run as a server.
- Testing Modules:

b. Determining Relationships and Flows:
- How the CLI input is received by the main command module, and then distributed to the appropriate command handler.
◦ CLI/command parser → specific command handler (e.g., caption, catalog, gen, mcp, log)
◦ Each command handler invoking utility modules (for configuration, file I/O, image processing, logging)
◦ Handlers that generate outputs (JSON/Markdown) and trigger external API calls.
- For the MCP server: illustrate how it uses the StdioServerTransport to handle protocol requests, calling into image caption and image generation tools.

c. Architectural Patterns & Design Principles:
- Modularity: Each command is implemented as its own module within the commands/ directory, making the design easy to extend.
- Separation of Concerns: Dividing the CLI handling, business logic (commands), utility functions, and external API connectors.
- Testability: Many components include dedicated test files promoting unit testing best practices.
- Dependency Management: Use of external configuration (via deno.json and environment variables) for API keys (e.g., GOOGLE_API_KEY).

d. Technologies, Frameworks, and Libraries:
- Deno as the runtime environment.
- TypeScript as the development language.
- External APIs: Gemini API and ImageFX API for image processing tasks.
- StdioServerTransport for implementing the MCP protocol.
- CLI libraries and native Deno modules for command parsing and file I/O.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% CLI Entry Point
    CLI["CLI Entry Point (main.ts)"]:::cli

    %% Command Handlers Subgraph
    subgraph "Command Handlers"
        Caption["Caption Command (caption.ts)"]:::command
        Catalog["Catalog Command (catalog.ts)"]:::command
        Gen["Gen Command (gen.ts)"]:::command
        MCP["MCP Command (mcp.ts)"]:::mcp
        Log["Log Command (log.ts)"]:::command
        Configure["Configure Command (configure.ts)"]:::command
    end

    %% Utilities Subgraph
    subgraph "Utility Modules"
        ConfigUtil["Config Utility (config.ts)"]:::utility
        FileUtil["File Utility (file.ts)"]:::utility
        ImageUtil["Image Utility (image.ts)"]:::utility
        ImageConst["Image Constants (image_constants.ts)"]:::utility
        ImageFXUtil["ImageFX Utility (imagefx.ts)"]:::utility
        LoggerUtil["Logger Utility (logger.ts)"]:::utility
    end

    %% External Services Subgraph
    subgraph "External Services"
        GeminiAPI["Gemini API"]:::external
        ImageFXAPI["ImageFX API"]:::external
    end

    %% MCP Server Components Subgraph
    subgraph "MCP Server Components"
        stdio["StdioServerTransport"]:::mcp
        MCPTools["MCP Tools Directory"]:::mcp
        CaptionTool["Caption Tool (caption.ts)"]:::mcp
        GenerateTool["Generate Tool (generate.ts)"]:::mcp
    end

    %% Connections from CLI Entry to Command Handlers
    CLI -->|"dispatch"| Caption
    CLI -->|"dispatch"| Catalog
    CLI -->|"dispatch"| Gen
    CLI -->|"dispatch"| MCP
    CLI -->|"dispatch"| Log
    CLI -->|"dispatch"| Configure

    %% Connections from Command Handlers to Utility Modules and External Services
    Caption -->|"uses"| ImageUtil
    Caption -->|"uses"| ImageFXUtil
    Caption -->|"logs_to"| LoggerUtil
    Caption -->|"calls"| GeminiAPI

    Catalog -->|"uses"| FileUtil
    Catalog -->|"logs_to"| LoggerUtil

    Gen -->|"uses"| ImageFXUtil
    Gen -->|"calls"| GeminiAPI

    Log -->|"logs_to"| LoggerUtil

    Configure -->|"reads/writes"| ConfigUtil

    %% MCP Command Connections to MCP Server Components
    MCP -->|"initializes"| stdio
    MCP -->|"loads"| MCPTools
    MCPTools -->|"provides"| CaptionTool
    MCPTools -->|"provides"| GenerateTool

    %% Some common utility usage for MCP Tools (implied)
    CaptionTool -->|"uses"| ImageUtil
    CaptionTool -->|"logs_to"| LoggerUtil
    GenerateTool -->|"uses"| ImageFXUtil

    %% External connection from Gen and Caption to ImageFX API as additional service
    ImageFXUtil -->|"integrates with"| ImageFXAPI

    %% Click Events for Command Handlers
    click CLI "https://github.com/hirokidaichi/imark/blob/main/src/commands/main.ts"
    click Caption "https://github.com/hirokidaichi/imark/blob/main/src/commands/caption.ts"
    click Catalog "https://github.com/hirokidaichi/imark/blob/main/src/commands/catalog.ts"
    click Gen "https://github.com/hirokidaichi/imark/blob/main/src/commands/gen.ts"
    click MCP "https://github.com/hirokidaichi/imark/blob/main/src/commands/mcp.ts"
    click Log "https://github.com/hirokidaichi/imark/blob/main/src/commands/log.ts"
    click Configure "https://github.com/hirokidaichi/imark/blob/main/src/commands/configure.ts"

    %% Click Events for Utility Modules
    click ConfigUtil "https://github.com/hirokidaichi/imark/blob/main/src/utils/config.ts"
    click FileUtil "https://github.com/hirokidaichi/imark/blob/main/src/utils/file.ts"
    click ImageUtil "https://github.com/hirokidaichi/imark/blob/main/src/utils/image.ts"
    click ImageConst "https://github.com/hirokidaichi/imark/blob/main/src/utils/image_constants.ts"
    click ImageFXUtil "https://github.com/hirokidaichi/imark/blob/main/src/utils/imagefx.ts"
    click LoggerUtil "https://github.com/hirokidaichi/imark/blob/main/src/utils/logger.ts"
    click GeminiAPI "https://github.com/hirokidaichi/imark/blob/main/src/utils/gemini.ts"

    %% Click Events for MCP Tools Directory and Submodules
    click MCPTools "https://github.com/hirokidaichi/imark/tree/main/src/commands/mcp/tool"
    click CaptionTool "https://github.com/hirokidaichi/imark/blob/main/src/commands/mcp/tool/caption.ts"
    click GenerateTool "https://github.com/hirokidaichi/imark/blob/main/src/commands/mcp/tool/generate.ts"

    %% Style Definitions
    classDef cli fill:#a8dadc,stroke:#457b9d,stroke-width:2px;
    classDef command fill:#f4a261,stroke:#e76f51,stroke-width:2px;
    classDef utility fill:#aed581,stroke:#7cb342,stroke-width:2px;
    classDef external fill:#ffcc80,stroke:#fb8c00,stroke-width:2px;
    classDef mcp fill:#ce93d8,stroke:#8e24aa,stroke-width:2px;
```
