KiWA001 commited on
Commit
ef08391
·
1 Parent(s): bd8bbad

Update microservice to use OpenCode HTTP API for proper responses

Browse files
Files changed (1) hide show
  1. 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
- if not self.is_running:
171
- await self.start()
172
 
173
- # Start new chat
174
- await self.send_key('ctrl+x')
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
- import select
189
- while True:
190
- if self.process.stdout in select.select([self.process.stdout], [], [], 0.5)[0]:
191
- line = self.process.stdout.readline()
192
- if line:
193
- output += line
194
- else:
195
- break
196
- else:
197
- break
198
- except:
199
- pass
200
-
201
- self.message_count += 1
202
-
203
- # Auto-reset after 20 messages
204
- if self.message_count >= self.max_messages:
205
- logger.info("🔄 Auto-reset triggered")
206
- await self.stop()
207
- await self.start()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
208
 
209
- return output or "Message sent successfully"
 
 
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: