Nicholas Bumgarner commited on
Commit
76b2b96
·
1 Parent(s): ec34b87

Fix chat protocol: use named pipe (/tmp/phoenix_chat.pipe) per phoenix_brain.c chat mode

Browse files
Files changed (1) hide show
  1. app.py +34 -8
app.py CHANGED
@@ -318,10 +318,8 @@ def get_boot_status():
318
 
319
 
320
  def phox_chat(message, history):
321
- """Main chat function."""
322
- if not _ring_in_mm or not _ring_out_mm:
323
- return "Engine not initialized. Please wait for startup."
324
-
325
  with _state_lock:
326
  phoenix_running = _phoenix_proc is not None and _phoenix_proc.poll() is None
327
  if not phoenix_running:
@@ -338,11 +336,39 @@ def phox_chat(message, history):
338
  if not token_ids:
339
  return "Empty input."
340
 
341
- pushed = send_tokens_to_phoenix(token_ids)
342
- if pushed == 0:
343
- return "Ring buffer full. Try again."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
344
 
345
- pred_ids = poll_predictions()
346
  if not pred_ids:
347
  return "No prediction received (timeout). Try again."
348
 
 
318
 
319
 
320
  def phox_chat(message, history):
321
+ """Main chat function — uses named pipe protocol with phoenix binary."""
322
+ # Check phoenix is running
 
 
323
  with _state_lock:
324
  phoenix_running = _phoenix_proc is not None and _phoenix_proc.poll() is None
325
  if not phoenix_running:
 
336
  if not token_ids:
337
  return "Empty input."
338
 
339
+ # Write tokens to the named pipe
340
+ pipe_path = "/tmp/phoenix_chat.pipe"
341
+ try:
342
+ # Open pipe for writing (blocks until phoenix opens it for reading)
343
+ pipe_fd = os.open(pipe_path, os.O_WRONLY | os.O_NONBLOCK)
344
+ token_str = " ".join(str(t) for t in token_ids) + "\n"
345
+ os.write(pipe_fd, token_str.encode())
346
+ os.close(pipe_fd)
347
+ except (FileNotFoundError, OSError, BrokenPipeError):
348
+ return "Phoenix not ready for chat. Please wait for startup."
349
+
350
+ # Wait for predictions by reading the state JSONL
351
+ pred_ids = []
352
+ start = time.time()
353
+ while time.time() - start < CHAT_TIMEOUT_S:
354
+ try:
355
+ with open(STATE_FILE, 'r') as f:
356
+ lines = f.readlines()
357
+ for line in reversed(lines):
358
+ line = line.strip()
359
+ if line.startswith('{"event":"token"'):
360
+ # Parse the last token event
361
+ evt = json.loads(line)
362
+ if evt.get("tok") not in pred_ids:
363
+ pred_ids.append(evt.get("tok"))
364
+ if len(pred_ids) >= len(token_ids):
365
+ break
366
+ except (FileNotFoundError, PermissionError, json.JSONDecodeError):
367
+ pass
368
+ if pred_ids:
369
+ break
370
+ time.sleep(0.1)
371
 
 
372
  if not pred_ids:
373
  return "No prediction received (timeout). Try again."
374