#compdef llm

(( $+functions[_llm_commands] )) ||
_llm_commands() {
  local -a commands
  commands=(
    'prompt:Execute a prompt'
    'aliases:Manage model aliases'
    'chat:Hold an ongoing chat with a model'
    'collections:View and manage collections of embeddings'
    'embed:Embed text and store or return the result'
    'embed-models:Manage available embedding models'
    'embed-multi:Store embeddings for multiple strings at once'
    'fragments:Manage fragments that are stored in the database'
    'install:Install packages from PyPI into the same environment as LLM'
    'keys:Manage stored API keys for different models'
    'logs:Tools for exploring logged prompts and responses'
    'models:Manage available models'
    'openai:Commands for working directly with the OpenAI API'
    'plugins:List installed plugins'
    'schemas:Manage stored schemas'
    'similar:Return top N similar IDs from a collection using cosine'
    'templates:Manage stored prompt templates'
    'tools:Manage tools that can be made available to LLMs'
    'uninstall:Uninstall Python packages from the LLM environment'
  )
  _describe 'commands' commands
}

_llm() {
  # local context state line curcontext="$curcontext"
  local context curcontext="$curcontext" state line
  typeset -A opt_args
  local ret=1

_arguments -C \
  '(-h)-h[Show this message and exit]' \
  '(--help)--help[Show this message and exit]' \
  '--version[Show version and exit]' \
  '1:: :_llm_commands' \
  '*:: :->command_args' && ret=0

local rules

case "$state" in
  (command_args)
    case "$words[1]" in
      (aliases)
        _arguments \
          '-h[Show this message and exit]' \
          '--help[Show this message and exit]' \
          && ret=0
          ;;
      (embed)
        _arguments \
          '-i[File to embed]:PATH:' \
          '--input[File to embed]:PATH:' \
          '-m[Embedding model to use]:TEXT:' \
          '--model[Embedding model to use]:TEXT:' \
          '--store[Store the text itself in the database]' \
          '-d:FILE:' \
          '--database:FILE:' \
          '-c[Content to embed]:TEXT:' \
          '--content[Content to embed]:TEXT:' \
          '--binary[Treat input as binary data]' \
          '--metadata[JSON object metadata to store]:TEXT:' \
          '-f[Output format json|blob|base64|hex]' \
          '--format[Output format json|blob|base64|hex]' \
          '-h[Show this message and exit]' \
          '--help[Show this message and exit]' \
          && ret=0
          ;;
      (install)
        _arguments \
          '-U[Upgrade packages to latest version]' \
          '--upgrade[Upgrade packages to latest version]' \
          '-e[Install a project in editable mode from this path]:TEXT:' \
          '--editable[Install a project in editable mode from this path]:TEXT:' \
          '--force-reinstall[Reinstall all packages even if they are already up-to-date]' \
          '--no-cache-dir[Disable the cache]' \
          '--pre[Include pre-release and development versions]' \
          '-h[Show this message and exit]' \
          '--help[Show this message and exit]' \
          && ret=0
          ;;
      (plugins)
        _arguments \
          '--all[Include built-in default plugins]' \
          '--hook[Filter for plugins that implement this hook]:TEXT:' \
          '-h[Show this message and exit]' \
          '--help[Show this message and exit]' \
          && ret=0
          ;;
      (prompt)
        _arguments \
          '-s[System prompt to use]:TEXT:' \
          '--system[System prompt to use]:TEXT:' \
          '-m[Model to use]:TEXT:' \
          '--model[Model to use]:TEXT:' \
          '-d[Path to log database]:FILE:' \
          '--database[Path to log database]:FILE:' \
          '-q[Use first model matching these strings]:TEXT:' \
          '--query[Use first model matching these strings]:TEXT:' \
          '-a[Attachment path or URL or -]:ATTACHMENT:' \
          '--attachment[Attachment path or URL or -]:ATTACHMENT:' \
          '--at[Attachment with explicit mimetype, --at image.jpg image/jpeg]:TEXT:TEXT:' \
          '--attachment-type[Attachment with explicit mimetype, --at image.jpg image/jpeg]:TEXT:TEXT:' \
          '-T[Name of a tool to make available to the model]:TEXT:' \
          '--tool[Name of a tool to make available to the model]:TEXT:' \
          '--functions[Python code block or file path defining functions to register as tools]:TEXT:' \
          '--td[Show full details of tool executions]:' \
          '--tools-debug[Show full details of tool executions]:' \
          '--ta[Manually approve every tool execution]' \
          '--tools-approve[Manually approve every tool execution]' \
          '--cl[How many chained tool responses to allow, default 5, set 0 for unlimited]:INTEGER:' \
          '--chain-limit[How many chained tool responses to allow, default 5, set 0 for unlimited]:INTEGER:' \
          '-o[key/value options for the model]:TEXT:TEXT:' \
          '--option[key/value options for the model]:TEXT:TEXT:' \
          '--schema[JSON schema, filepath or ID]:TEXT:' \
          '--schema-multi[JSON schema to use for multiple results]:TEXT:' \
          '-f[Fragment (alias, URL, hash or file path) to add to the prompt]:TEXT:' \
          '--fragment[Fragment (alias, URL, hash or file path) to add to the prompt]:TEXT:' \
          '--sf[Fragment to add to system prompt]:TEXT:' \
          '--system-fragment[Fragment to add to system prompt]:TEXT:' \
          '-t[Template to use]:TEXT:' \
          '--template[Template to use]:TEXT:' \
          '-p[Parameters for template]:TEXT:TEXT:' \
          '--param[Parameters for template]:TEXT:TEXT:' \
          '--no-stream[Do not stream output]' \
          '-n[Do not log to database]:' \
          '--no-log[Do not log to database]:' \
          '--log[Log prompt and response to the database]:' \
          '-c[Continue the most recent conversation]' \
          '--continue[Continue the most recent conversation]' \
          '--cid[Continue the conversation with the given ID]:TEXT:' \
          '--conversation[Continue the conversation with the given ID]:TEXT:' \
          '--key[API key to use]:TEXT:' \
          '--save[Save prompt with this template name]:TEXT:' \
          '--async[Run prompt asynchronously]' \
          '-u[Show token usage]' \
          '--usage[Show token usage]' \
          '-x[Extract first fenced code block]' \
          '--extract[Extract first fenced code block]' \
          '--xl[Extract last fenced code block]' \
          '--extract-last[Extract last fenced code block]' \
          '-h[Show this message and exit]' \
          '--help[Show this message and exit]' \
          && ret=0
          ;;
      (similar)
        _arguments \
          '-i[File to embed for comparison]:PATH:' \
          '--input[File to embed for comparison]:PATH:' \
          '-c[Content to embed for comparison]:TEXT:' \
          '--content[Content to embed for comparison]:TEXT:' \
          '--binary[Treat input as binary data]' \
          '-n[Number of results to return]:INTEGER:' \
          '--number[Number of results to return]:INTEGER:' \
          '-p[Output in plain text format]' \
          '--plain[Output in plain text format]' \
          '-d:FILE:' \
          '--database:FILE:' \
          '--prefix[Just IDs with this prefix]:TEXT:' \
          '-h[Show this message and exit]' \
          '--help[Show this message and exit]' \
          && ret=0
          ;;
      (uninstall)
        _arguments \
          '-y[Do not ask for confirmation]' \
          '--yes[Do not ask for confirmation]' \
          '-h[Show this message and exit]' \
          '--help[Show this message and exit]' \
          && ret=0
          ;;
    esac
    ;;
esac

return ret
}

_llm "$@"
