Skip to main content

Code Assistant

Build a coding assistant that understands your codebase and remembers patterns.

The Problem

Code assistants often:

  • Search the entire codebase for every question
  • Miss relevant context from other files
  • Forget patterns they've seen before

The Solution

A memory-enabled code assistant that:

  • Indexes your codebase intelligently
  • Retrieves only relevant code for each question
  • Remembers debugging sessions and solutions

Implementation

Indexing Code

import requests

MEMORY_API = "https://api.memory.tensorheart.com/v1"
API_KEY = "mem_live_..."

def index_function(name: str, file_path: str, code: str):
"""Index a function into memory."""
requests.post(
f"{MEMORY_API}/memories",
headers={"Authorization": f"Bearer {API_KEY}"},
json={
"text": f"Function `{name}` in {file_path}\n\n```\n{code}\n```",
"metadata": {"type": "function", "name": name, "file": file_path}
}
)

Querying Code

from openai import OpenAI

openai = OpenAI()

def get_code_context(query: str) -> list[str]:
"""Get relevant code for this query."""
response = requests.post(
f"{MEMORY_API}/query",
headers={"Authorization": f"Bearer {API_KEY}"},
json={"context": query, "max_memories": 10}
)
return [m["text"] for m in response.json().get("data", {}).get("memories", [])]

def code_assistant(question: str) -> str:
"""Answer coding questions with codebase context."""
code_context = get_code_context(question)
context = "\n\n".join(code_context) if code_context else "No code found."

response = openai.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": f"Codebase context:\n{context}"},
{"role": "user", "content": question}
]
)
return response.choices[0].message.content

Remembering Debug Sessions

def save_debug_session(error: str, solution: str):
"""Remember how a bug was fixed."""
requests.post(
f"{MEMORY_API}/memories",
headers={"Authorization": f"Bearer {API_KEY}"},
json={
"text": f"Bug: {error}\nFix: {solution}",
"metadata": {"type": "debug_session"}
}
)

Example

Developer: Where is payment processing handled?
Assistant: Payment processing is in `src/billing/payments.py`.
The main function is `process_payment()` which handles
Stripe integration and error logging.

Next Steps