Spaces:
Running
Running
Update microservice to use OpenCode HTTP API for proper responses
Browse files- opencode_microservice.py +45 -37
opencode_microservice.py
CHANGED
|
@@ -166,47 +166,55 @@ class OpenCodeSession:
|
|
| 166 |
return False
|
| 167 |
|
| 168 |
async def chat(self, message: str) -> str:
|
| 169 |
-
"""Send message and get response"""
|
| 170 |
-
|
| 171 |
-
await self.start()
|
| 172 |
|
| 173 |
-
#
|
| 174 |
-
|
| 175 |
-
await asyncio.sleep(0.3)
|
| 176 |
-
await self.send_input('n')
|
| 177 |
-
await asyncio.sleep(0.5)
|
| 178 |
|
| 179 |
-
# Send message
|
| 180 |
-
await self.send_input(message)
|
| 181 |
-
|
| 182 |
-
# Wait for response
|
| 183 |
-
await asyncio.sleep(3)
|
| 184 |
-
|
| 185 |
-
# Read output
|
| 186 |
-
output = ""
|
| 187 |
try:
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 208 |
|
| 209 |
-
|
|
|
|
|
|
|
| 210 |
|
| 211 |
async def stop(self):
|
| 212 |
if self.process:
|
|
|
|
| 166 |
return False
|
| 167 |
|
| 168 |
async def chat(self, message: str) -> str:
|
| 169 |
+
"""Send message and get response via HTTP API"""
|
| 170 |
+
import aiohttp
|
|
|
|
| 171 |
|
| 172 |
+
# Use OpenCode's HTTP API instead of TUI parsing
|
| 173 |
+
url = "https://opencode.ai/zen/v1/chat/completions"
|
|
|
|
|
|
|
|
|
|
| 174 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
try:
|
| 176 |
+
async with aiohttp.ClientSession() as session:
|
| 177 |
+
async with session.post(
|
| 178 |
+
url,
|
| 179 |
+
json={
|
| 180 |
+
"model": "opencode-zen/kimi-k2.5-free",
|
| 181 |
+
"messages": [{"role": "user", "content": message}],
|
| 182 |
+
"stream": False
|
| 183 |
+
},
|
| 184 |
+
headers={"Content-Type": "application/json"},
|
| 185 |
+
timeout=aiohttp.ClientTimeout(total=60)
|
| 186 |
+
) as response:
|
| 187 |
+
if response.status == 200:
|
| 188 |
+
data = await response.json()
|
| 189 |
+
if "choices" in data and len(data["choices"]) > 0:
|
| 190 |
+
content = data["choices"][0]["message"]["content"]
|
| 191 |
+
|
| 192 |
+
# Track message count for disposable mode
|
| 193 |
+
self.message_count += 1
|
| 194 |
+
|
| 195 |
+
# Auto-reset after 20 messages
|
| 196 |
+
if self.message_count >= self.max_messages:
|
| 197 |
+
logger.info("🔄 Auto-reset triggered")
|
| 198 |
+
await self.reset_session()
|
| 199 |
+
|
| 200 |
+
return content
|
| 201 |
+
|
| 202 |
+
return "No response from OpenCode"
|
| 203 |
+
|
| 204 |
+
except Exception as e:
|
| 205 |
+
logger.error(f"Chat error: {e}")
|
| 206 |
+
return f"Error: {str(e)}"
|
| 207 |
+
|
| 208 |
+
async def reset_session(self):
|
| 209 |
+
"""Reset session for disposable mode"""
|
| 210 |
+
# Generate new identity
|
| 211 |
+
identity = self.generate_identity()
|
| 212 |
+
self.session_id = identity["session_id"][:8]
|
| 213 |
+
self.message_count = 0
|
| 214 |
|
| 215 |
+
# Clear any cached data
|
| 216 |
+
logger.info(f"🔄 Session reset - New ID: {self.session_id}")
|
| 217 |
+
return True
|
| 218 |
|
| 219 |
async def stop(self):
|
| 220 |
if self.process:
|