User Input (Tab×2 or CLI) ↓ ┌───────────────────────────────────────┐ │ Entry Points │ ├───────────────────────────────────────┤ │ • _finishsh() - Tab completion │ │ • command_command() - CLI mode │ └───────────────┬───────────────────────┘ ↓ ┌───────────────────────────────────────┐ │ Configuration Layer │ ├───────────────────────────────────────┤ │ acsh_load_config() │ │ • Read ~/.finish/config │ │ • Set ACSH_PROVIDER, ACSH_ENDPOINT │ │ • Set ACSH_ACTIVE_API_KEY (if needed) │ └───────────────┬───────────────────────┘ ↓ ┌───────────────────────────────────────┐ │ Cache Layer │ ├───────────────────────────────────────┤ │ • Check cache_dir/acsh-{hash}.txt │ │ • Return cached completions if exists │ └───────────────┬───────────────────────┘ ↓ (cache miss) ┌───────────────────────────────────────┐ │ Context Builder │ ├───────────────────────────────────────┤ │ _build_prompt() │ │ • Command history (sanitized) │ │ • Terminal info (env vars, cwd) │ │ • Recent files (ls -ld) │ │ • Help message (cmd --help) │ └───────────────┬───────────────────────┘ ↓ ┌───────────────────────────────────────┐ │ Payload Builder │ ├───────────────────────────────────────┤ │ _build_payload() │ │ • Format: {model, messages, temp} │ │ • Provider-specific options: │ │ - Ollama: {format:"json"} │ │ - LMStudio: {stream:true} │ │ - OpenAI: {response_format} │ └───────────────┬───────────────────────┘ ↓ ┌───────────────────────────────────────┐ │ LLM Provider Layer │ ├───────────────────────────────────────┤ │ openai_completion() │ │ • curl to endpoint │ │ • Ollama: no auth header │ │ • Others: Authorization header if key │ │ • Parse response (JSON/streaming) │ └───────────────┬───────────────────────┘ ↓ ┌───────────────────────────────────────┐ │ Response Parser │ ├───────────────────────────────────────┤ │ • Extract completions array │ │ • Fallback parsing for non-JSON │ │ • Filter command-like lines │ └───────────────┬───────────────────────┘ ↓ ┌───────────────────────────────────────┐ │ Output Layer │ ├───────────────────────────────────────┤ │ • Write to cache │ │ • Log usage (tokens, cost) │ │ • Return COMPREPLY array (completion) │ │ • or stdout (CLI mode) │ └───────────────────────────────────────┘