Do you want a ready-made CLI you can install now, or a small custom terminal assistant you can run and extend? Below I’ll give both: (A) recommended ready tools and quick usage; (B) a minimal Python-based terminal assistant you can copy, run, and extend to fit your workflow.
A — Ready-made CLI assistants (fastest)
- GitHub Copilot CLI (copilot)
- Install: brew install github/gh/gh (then follow Copilot CLI install) or check GitHub docs for copilot-cli.
- Usage examples:
- copilot chat "Explain why tests are failing"
- copilot code "Create a Node.js express endpoint that returns JSON"
- Pros: tuned for coding, integrates with GitHub, good for pair-programming style.
- OpenAI CLI (official)
- Install: npm i -g openai or check openai.com docs (or use pip for community wrappers).
- Set API key: export OPENAI_API_KEY="sk-..."
- Example:
- openai chat.completions.create -m gpt-4o --messages '[{"role":"user","content":"Write a Python function to reverse words in a string"}]'
- Pros: direct access to OpenAI models, flexible.
- Third-party CLI tools
- gpt-cli, aicode, aichat, etc. (availability varies). Search GitHub for “gpt cli” for a range of community projects.
- Tabnine CLI / Kite/Codeium: some offer CLI features or editor plugins; more for completions.
B — Minimal, adaptable Python terminal assistant
The script below runs in your terminal, sends prompts to OpenAI Chat Completions, supports streaming, and gives a simple interactive REPL for coding tasks. Requirements: Python 3.8+, pip install openai, and set OPENAI_API_KEY.
Save as ai_assistant.py:
#!/usr/bin/env python3
import os
import openai
import sys
import textwrap
openai.api_key = os.getenv("OPENAI_API_KEY")
if not openai.api_key:
print("Set OPENAI_API_KEY in your environment.")
sys.exit(1)
SYSTEM_PROMPT = """You are an expert programming assistant. Provide clear, concise code, explanations, and shell commands. If asked to run or modify files, instruct the user and provide commands rather than executing them."""
def chat(prompt, model="gpt-4o", temperature=0.2):
messages = [
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": prompt}
]
resp = openai.ChatCompletion.create(
model=model,
messages=messages,
temperature=temperature,
max_tokens=1200,
stream=True
)
out = ""
try:
for chunk in resp:
delta = chunk["choices"][0].get("delta", {})
text = delta.get("content", "")
print(text, end="", flush=True)
out += text
except KeyboardInterrupt:
print("\n[Interrupted]")
print()
return out
def repl():
print("AI Coding Assistant — type 'exit' to quit. Prefix commands:")
print(" :explain <file> — explain a file")
print(" :fix <message> — suggest fixes based on test failure message")
print(" :run <cmd> — ask how to run a command locally")
while True:
try:
line = input("\n> ").strip()
except EOFError:
break
if not line:
continue
if line in ("exit", "quit"):
break
Simple local helpers
if line.startswith(":explain "):
path = line.split(" ",1)[1]
if not os.path.exists(path):
print(f"File not found: {path}")
continue
with open(path, "r", encoding="utf-8") as f:
content = f.read()
prompt = f"Explain the following file and highlight potential bugs and improvements. Keep answer concise.\n\n{path}:\n\n{content}"
chat(prompt)
continue
if line.startswith(":fix "):
details = line.split(" ",1)[1]
prompt = f"Tests are failing with this output:\n{details}\nExplain the problem and propose code fixes or debug steps."
chat(prompt)
continue
if line.startswith(":run "):
cmd = line.split(" ",1)[1]
prompt = f"I have to run this command locally: {cmd}\nExplain what it does and any prerequisites or safety concerns."
chat(prompt)
continue
default: general coding question
chat(line)
if name == "main":
repl()
How to use:
- Install openai: pip install openai
- Set env var: export OPENAI_API_KEY="sk-..."
- Run: python ai_assistant.py
- Examples:
- > Implement a function to merge two sorted arrays in-place in Python.
- > :explain src/main.py
- > :fix pytest output: "AssertionError: assert 3 == 4"
Extending the assistant
- Add file editing: read file, ask model to produce a patch in unified-diff format, and optionally apply with user confirmation.
- Add testing flow: run pytest, capture output, pass to model with context to request fixes.
- Add code execution sandbox (use subprocess carefully; consider running in Docker or restricted environment).
- Integrate with git: create branches/commits automatically after user confirmation.
Security & safety notes
- Never feed secrets (API keys, passwords) to the model. Filter or mask them.
- Don’t auto-execute arbitrary code from the model without manual review.
- Use model temperature 0–0.3 for reproducible, safe coding suggestions.
Want one tailored to your stack?
Tell me: your OS, preferred model (Copilot, OpenAI), programming languages, and whether you want features like: apply patches to files, run tests and iterate, or integrate with git — and I’ll produce a ready-to-run script or install commands for the best CLI.