fitness-web/app/services/opencode_proxy.py

43 lines
1.2 KiB
Python

import asyncio
import json
from typing import AsyncGenerator
from app.config import OPENCODE_SERVE_URL
async def query_opencode(
message: str,
session_id: str,
user_context: str = "",
) -> AsyncGenerator[str, None]:
prompt = message
if user_context:
prompt = f"[User context: {user_context}]\n\n{message}"
try:
proc = await asyncio.create_subprocess_exec(
"opencode", "run", "--attach", OPENCODE_SERVE_URL,
"--format", "json",
prompt,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
while True:
line = await proc.stdout.readline()
if not line:
break
line = line.decode().strip()
if line:
try:
data = json.loads(line)
content = data.get("content", data.get("text", line))
yield content
except json.JSONDecodeError:
yield line
await proc.wait()
except FileNotFoundError:
yield "AI coach is not available. Install opencode and run `opencode serve` to enable."
except Exception as e:
yield f"Error connecting to AI coach: {e}"