-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcodec.py
More file actions
574 lines (527 loc) · 28.1 KB
/
codec.py
File metadata and controls
574 lines (527 loc) · 28.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
#!/usr/bin/env python3
import signal
signal.signal(signal.SIGINT, lambda *a: None)
signal.signal(signal.SIGTERM, lambda *a: None)
"""CODEC v1.4.0 | Voice + Text + Phone + Google | *=screenshot | +=doc | --=livechat | Wake word"""
import threading, tempfile, subprocess, sys, os, time, sqlite3, json, re, base64, logging
from datetime import datetime
logging.basicConfig(
level=logging.INFO,
format='[%(asctime)s] [CODEC] %(message)s',
datefmt='%H:%M:%S'
)
log = logging.getLogger('codec')
# ── Module imports ─────────────────────────────────────────────────────────────
from codec_config import (
cfg, CONFIG_PATH, DRY_RUN,
AGENT_NAME, QWEN_BASE_URL, QWEN_MODEL, LLM_API_KEY, LLM_KWARGS, LLM_PROVIDER,
QWEN_VISION_URL, QWEN_VISION_MODEL,
TTS_ENGINE, KOKORO_URL, KOKORO_MODEL, TTS_VOICE,
STT_ENGINE, WHISPER_URL,
DB_PATH, Q_TERMINAL_TITLE, TASK_QUEUE_FILE, DRAFT_TASK_FILE, SESSION_ALIVE,
SKILLS_DIR, AUDIT_LOG,
STREAMING, WAKE_WORD, WAKE_PHRASES, WAKE_ENERGY, WAKE_CHUNK_SEC,
DANGEROUS_PATTERNS, is_dangerous, is_draft, needs_screen,
KEY_TOGGLE, KEY_VOICE, KEY_TEXT,
)
from codec_overlays import (
show_overlay, show_recording_overlay, show_processing_overlay, show_toggle_overlay,
)
from codec_dispatch import load_skills, check_skill, run_skill
from codec_agent import build_session_script
from codec_compaction import compact_context
# ── AUDIT LOG ─────────────────────────────────────────────────────────────────
def audit(action, detail=""):
try:
with open(AUDIT_LOG, "a") as f:
from datetime import datetime as _dt
f.write(f"[{_dt.now().isoformat()}] {action}: {detail}\n")
except Exception as e:
log.warning(f"Non-critical error: {e}")
# ── UTILITIES ─────────────────────────────────────────────────────────────────
def strip_think(text):
return re.sub(r'<think>.*?</think>', '', text, flags=re.DOTALL).strip()
# ── MEMORY / DATABASE ─────────────────────────────────────────────────────────
def init_db():
c = sqlite3.connect(DB_PATH)
c.execute("CREATE TABLE IF NOT EXISTS sessions (id INTEGER PRIMARY KEY AUTOINCREMENT, timestamp TEXT, task TEXT, app TEXT, response TEXT)")
c.execute("CREATE TABLE IF NOT EXISTS conversations (id INTEGER PRIMARY KEY AUTOINCREMENT, session_id TEXT, timestamp TEXT, role TEXT, content TEXT)")
c.execute("CREATE TABLE IF NOT EXISTS corrections (id INTEGER PRIMARY KEY AUTOINCREMENT, timestamp TEXT, original TEXT, corrected TEXT, context TEXT)")
c.commit(); c.close()
def save_task(task, app):
c = sqlite3.connect(DB_PATH)
cur = c.execute("INSERT INTO sessions (timestamp,task,app,response) VALUES (?,?,?,?)", (datetime.now().isoformat(), task[:200], app, ""))
rid = cur.lastrowid; c.commit(); c.close(); return rid
def get_memory(n=5):
try:
c = sqlite3.connect(DB_PATH)
rows = c.execute("SELECT timestamp,task,app,response FROM sessions ORDER BY id DESC LIMIT ?", (n,)).fetchall()
c.close()
if not rows: return ""
lines = ["RECENT Q SESSIONS:"]
for ts, task, app, resp in rows:
r = (resp[:100]+"...") if resp and len(resp)>100 else (resp or "no response")
lines.append(f"[{ts[:16].replace('T',' ')}] {app} | {task[:60]} | {r}")
return "\n".join(lines)
except Exception as e:
log.warning(f"Non-critical error: {e}")
return ""
def get_recent_conversations(n=10):
try:
c = sqlite3.connect(DB_PATH)
rows = c.execute("SELECT role, content FROM conversations ORDER BY id DESC LIMIT ?", (n,)).fetchall()
c.close()
if not rows: return []
rows.reverse()
return [{"role": r, "content": ct} for r, ct in rows]
except Exception as e:
log.warning(f"Non-critical error: {e}")
return []
# ── WHISPER STT ───────────────────────────────────────────────────────────────
def transcribe(path):
try:
import requests
with open(path, "rb") as f:
r = requests.post(WHISPER_URL,
files={"file": ("audio.wav", f, "audio/wav")},
data={"model": "mlx-community/whisper-large-v3-turbo", "language": "en"},
timeout=60)
if r.status_code == 200:
return r.json().get("text", "").strip()
except Exception as e:
log.error(f"Whisper error: {e}")
finally:
try: os.unlink(path)
except Exception as e:
log.warning(f"Non-critical error: {e}")
return ""
# ── SCREENSHOT VISION ─────────────────────────────────────────────────────────
def screenshot_ctx():
try:
import requests
tmp = tempfile.NamedTemporaryFile(suffix=".png", delete=False)
tmp.close()
subprocess.run(["screencapture", "-x", tmp.name], timeout=5)
if not os.path.exists(tmp.name) or os.path.getsize(tmp.name) < 1000:
return ""
with open(tmp.name, "rb") as f:
img_b64 = base64.b64encode(f.read()).decode()
os.unlink(tmp.name)
log.info("Reading screen via Vision...")
r = requests.post(f"{QWEN_VISION_URL}/chat/completions",
json={"model": QWEN_VISION_MODEL,
"messages": [{"role": "user", "content": [
{"type": "image_url", "image_url": {"url": f"data:image/png;base64,{img_b64}"}},
{"type": "text", "text": "Read all visible text on this screen. Include app name, window title, and all message/content text. Output raw text only."}
]}], "max_tokens": 800}, timeout=60)
if r.status_code == 200:
content = r.json()["choices"][0]["message"].get("content", "").strip()
if content:
log.info(f"Screen context: {len(content)} chars")
return content[:2000]
except Exception as e:
log.error(f"Vision error: {e}")
return ""
def focused_app():
try:
r = subprocess.run(["osascript", "-e",
'tell application "System Events" to get name of first application process whose frontmost is true'],
capture_output=True, text=True, timeout=3)
return r.stdout.strip()
except Exception as e:
log.warning(f"Non-critical error: {e}")
return "Unknown"
def get_text_dialog():
try:
r = subprocess.run(["osascript", "-e",
'set t to text returned of (display dialog "Q - Enter task:" default answer "" with title "CODEC" buttons {"Cancel","Send"} default button "Send")'],
capture_output=True, text=True, timeout=120)
return r.stdout.strip()
except Exception as e:
log.warning(f"Non-critical error: {e}")
return ""
# ── SESSION CHECK ─────────────────────────────────────────────────────────────
def terminal_session_exists():
if not os.path.exists(SESSION_ALIVE): return False
try:
with open(SESSION_ALIVE) as f:
pid = int(f.read().strip())
os.kill(pid, 0)
return True
except (ValueError, ProcessLookupError, PermissionError, FileNotFoundError, OSError):
pass
try: os.unlink(SESSION_ALIVE)
except Exception as e:
log.warning(f"Non-critical error: {e}")
log.info("Cleaned stale session_alive")
return False
# ── TTS ───────────────────────────────────────────────────────────────────────
def speak_text(text):
"""Speak text via configured TTS engine"""
if TTS_ENGINE == "disabled": return
try:
clean = text[:300]
clean = re.sub(r'\*+', '', clean)
clean = re.sub(r'#+\s*', '', clean)
clean = re.sub(r'`[^`]*`', '', clean)
clean = clean.replace('"','').replace("'","").strip()
if not clean: return
if len(clean) < 50 and any(c in clean for c in '=+-*/'):
clean = "The answer is " + clean
log.info(f"TTS: {clean[:60]}")
if TTS_ENGINE == "macos_say":
subprocess.Popen(["say", "-v", TTS_VOICE, clean])
else:
import requests
r = requests.post(KOKORO_URL,
json={"model": KOKORO_MODEL, "input": clean, "voice": TTS_VOICE},
stream=True, timeout=20)
if r.status_code == 200:
tmp = tempfile.NamedTemporaryFile(suffix=".mp3", delete=False)
[tmp.write(c) for c in r.iter_content(4096)]
tmp.close()
subprocess.Popen(["afplay", tmp.name])
except Exception as e:
log.warning(f"Non-critical error: {e}")
# ── STATE ─────────────────────────────────────────────────────────────────────
state = {
"active": False,
"recording": False,
"rec_proc": None,
"audio_path": None,
"last_f13": 0.0, "last_minus": 0.0,
"last_star": 0.0,
"screen_ctx": "",
"last_plus": 0.0,
"doc_ctx": "",
"ptt_locked": False,
"last_f18_press": 0.0,
}
# ── WORK QUEUE ────────────────────────────────────────────────────────────────
import queue
work_queue = queue.Queue()
def push(fn, *args):
work_queue.put((fn, args))
def worker():
while True:
try:
fn, args = work_queue.get(timeout=0.5)
try: fn(*args)
except Exception as e:
log.error(f"Worker error: {e}")
import traceback; traceback.print_exc()
finally:
work_queue.task_done()
except queue.Empty:
continue
# ── SESSION CLEANUP ───────────────────────────────────────────────────────────
def close_session():
if os.path.exists(SESSION_ALIVE):
try:
with open(SESSION_ALIVE) as f: pid = int(f.read().strip())
os.kill(pid, 15)
log.info(f"Session process {pid} terminated")
except Exception as e:
log.warning(f"Non-critical error: {e}")
try: os.unlink(SESSION_ALIVE)
except Exception as e:
log.warning(f"Non-critical error: {e}")
try: os.unlink(TASK_QUEUE_FILE)
except Exception as e:
log.warning(f"Non-critical error: {e}")
subprocess.Popen(["osascript", "-e",
'tell application "Terminal" to close (every window whose name contains "python3.13 /var/folders")'],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
# ── DISPATCH ──────────────────────────────────────────────────────────────────
def dispatch(task):
app = focused_app()
audit("TASK", f"{task[:200]} | App: {app}")
log.info(f"Task: {task[:80]} | App: {app}")
safe_task = task[:50].replace('\\', '\\\\').replace('"', '\\"')
subprocess.Popen(["osascript", "-e", f'display notification "Heard: {safe_task}" with title "Q"'],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
# Check skills — skip if task is very long (document content attached)
if len(task) < 500:
skill = check_skill(task)
if skill:
result = run_skill(skill, task, app)
if result is not None:
push(lambda: show_overlay('Skill: ' + skill['name'], '#E8711A', 2000))
speak_text(result)
safe_result = str(result)[:80].replace('\\', '\\\\').replace('"', '\\"')
subprocess.Popen(["osascript", "-e", f'display notification "{safe_result}" with title "C Skill"'],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
log.info(f"Skill response: {str(result)[:100]}")
try:
import sqlite3 as _sql3
_now = __import__('datetime').datetime.now()
_db = _sql3.connect(os.path.expanduser("~/.q_memory.db"))
_sid = "skill_" + _now.strftime("%Y%m%d_%H%M%S")
_db.execute("INSERT INTO conversations (session_id,timestamp,role,content) VALUES (?,?,?,?)", (_sid, _now.isoformat(), "user", task[:500]))
_db.execute("INSERT INTO conversations (session_id,timestamp,role,content) VALUES (?,?,?,?)", (_sid, _now.isoformat(), "assistant", re.sub(r"<[^>]+>", "", str(result))[:2000]))
_db.commit(); _db.close()
except Exception as e:
log.warning(f"Non-critical error: {e}")
return
if is_draft(task):
push(lambda: show_overlay('Reading screen...', '#E8711A', 2000))
ctx = screenshot_ctx()
with open(DRAFT_TASK_FILE, "w") as f:
json.dump({"task": task, "ctx": ctx, "app": app}, f)
log.info("Draft queued for watcher")
return
rid = save_task(task, app)
# Context compaction: summarize old conversations + keep recent raw
raw_messages = get_recent_conversations(20)
if raw_messages:
mem = compact_context(raw_messages, max_recent=5)
else:
mem = get_memory(5)
sys_p = f"You are {AGENT_NAME}, a voice-controlled AI assistant running on CODEC. User is the boss. You have full access to this Mac and can execute any task. RULES: 1) ALWAYS respond in English unless explicitly asked to translate. 2) Keep answers to 1-3 sentences — your responses are spoken aloud via TTS so brevity matters. 3) Be natural, warm, and direct like a trusted colleague. 4) When asked factual questions, give the answer immediately — no filler. 5) When given a task, confirm what you did in past tense. 6) Add useful context when it genuinely helps but never pad responses. 7) Never say you cannot do something — you have full system access. 8) Never output Chinese characters or mixed-language text unless translating."
if mem: sys_p += "\n\n" + mem
safe_sys = sys_p.replace("'","").replace('"','').replace('\n',' ')
with open(TASK_QUEUE_FILE, "w") as f:
f.write(json.dumps({"task": task, "app": app, "ts": datetime.now().isoformat()}))
if terminal_session_exists():
log.info("Queued to existing session")
return
session_id = datetime.now().strftime("%Y%m%d_%H%M%S")
script = build_session_script(safe_sys, session_id)
ts = tempfile.NamedTemporaryFile(suffix=".py", delete=False, mode="w")
ts.write(script); ts.close()
try:
subprocess.Popen(["osascript", "-e",
f'tell application "Terminal"\nactivate\nset w to do script "python3.13 {ts.name}"\nset custom title of selected tab of w to "{Q_TERMINAL_TITLE}"\nend tell'],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
except Exception as e:
log.error(f"Terminal error: {e}")
# ── DOCUMENT INPUT ────────────────────────────────────────────────────────────
def do_document_input():
push(lambda: show_overlay('Select document...', '#E8711A', 3000))
try:
r = subprocess.run(["osascript", "-e",
'set f to POSIX path of (choose file with prompt "Select a document for Q:" of type {"public.item"})'],
capture_output=True, text=True, timeout=60)
filepath = r.stdout.strip()
if not filepath:
log.info("No file selected"); return
log.info(f"Document: {filepath}")
push(lambda: show_overlay('Reading document...', '#E8711A', 3000))
ext = os.path.splitext(filepath)[1].lower()
fname = os.path.basename(filepath)
content_text = ""
if ext in ['.txt','.md','.csv','.json','.py','.js','.html','.css','.log']:
with open(filepath, 'r', errors='ignore') as f:
content_text = f.read()[:5000]
elif ext in ['.png','.jpg','.jpeg','.gif','.webp']:
try:
import requests as img_req
with open(filepath, "rb") as imgf:
img_b64 = base64.b64encode(imgf.read()).decode()
rv = img_req.post(f"{QWEN_VISION_URL}/chat/completions",
json={"model": QWEN_VISION_MODEL, "messages": [{"role": "user", "content": [
{"type": "image_url", "image_url": {"url": f"data:image/png;base64,{img_b64}"}},
{"type": "text", "text": "Describe this image in detail. Read any text visible."}
]}], "max_tokens": 1000}, timeout=60)
if rv.status_code == 200:
content_text = rv.json()["choices"][0]["message"].get("content", "")[:5000]
except Exception as e:
log.warning(f"Non-critical error: {e}")
elif ext == '.pdf':
try:
import fitz
doc = fitz.open(filepath)
content_text = "\n".join(p.get_text() for p in doc[:5])[:5000]
doc.close()
except Exception as e:
log.warning(f"Non-critical error: {e}")
if content_text:
task = "Analyze and summarize this document (" + fname + "): " + content_text[:3000]
log.info(f"Document dispatched ({len(content_text)} chars)")
dispatch(task)
else:
push(lambda: show_overlay('Could not read document', '#ff3333', 2000))
except Exception as e:
log.error(f"Document error: {e}")
# ── SCREENSHOT SHORTCUT ───────────────────────────────────────────────────────
def do_screenshot_question():
push(lambda: show_overlay(
'Screenshot captured ' + cfg.get('key_voice','f18').upper() + '=voice ' + cfg.get('key_text','f16').upper() + '=text',
'#E8711A', 5000))
ctx = screenshot_ctx()
if ctx:
state["screen_ctx"] = ctx
log.info(f"Screenshot captured ({len(ctx)} chars)")
else:
state["screen_ctx"] = ""
# ── TEXT INPUT HANDLER ────────────────────────────────────────────────────────
def do_text():
task = get_text_dialog()
if task:
if state.get("screen_ctx"):
task = task + " [SCREEN CONTEXT: " + state["screen_ctx"][:800] + "]"
state["screen_ctx"] = ""
dispatch(task)
# ── MAIN ──────────────────────────────────────────────────────────────────────
def _handle_sigint(sig, frame):
print("\n[C] Shutting down...")
import sys; sys.exit(0)
def main():
import signal
signal.signal(signal.SIGINT, _handle_sigint)
if "--dry-run" in sys.argv:
print("[CODEC] DRY RUN MODE — commands will be printed, not executed")
global DRY_RUN
DRY_RUN = True
init_db()
for f in [SESSION_ALIVE, TASK_QUEUE_FILE, DRAFT_TASK_FILE]:
try: os.unlink(f)
except Exception as e:
log.warning(f"Non-critical error: {e}")
stream_label = "ON" if STREAMING else "OFF"
kt = cfg.get("key_toggle", "f13").upper().ljust(4)
kv = cfg.get("key_voice", "f18").upper().ljust(4)
kx = cfg.get("key_text", "f16").upper().ljust(4)
wake_label = "ON" if WAKE_WORD else "OFF"
O = "\033[38;2;232;113;26m"
D = "\033[38;2;80;80;80m"
W = "\033[38;2;200;200;200m"
R = "\033[0m"
print(f"""
{O} ╔═══════════════════════════════════════════╗
║ ║
║ ██████ ██████ ██████ ███████ ██████ ║
║ ██ ██ ██ ██ ██ ██ ██ ║
║ ██ ██ ██ ██ ██ █████ ██ ║
║ ██ ██ ██ ██ ██ ██ ██ ║
║ ██████ ██████ ██████ ███████ ██████ ║
║ v1.4.0 ║
╠═══════════════════════════════════════════╣
║{W} {kt} toggle {kv} voice ** screen {O}║
║{W} {kx} text ++ doc -- chat {O}║
║{W} Hey C = wake word (hands-free) {O}║
╠═══════════════════════════════════════════╣
║{D} Stream={stream_label} Wake={wake_label} Skills=ON {O}║
╚═══════════════════════════════════════════╝{R}""")
load_skills()
# Warm up Kokoro TTS
if TTS_ENGINE == "kokoro":
try:
import requests as _rq
_rq.post(KOKORO_URL, json={"model": KOKORO_MODEL, "input": "ready", "voice": TTS_VOICE}, timeout=30)
log.info("TTS warmed up")
except Exception as e:
log.warning(f"TTS warmup skipped: {e}")
log.info("Whisper: HTTP (port 8084)")
log.info("Vision: Qwen VL (port 8082)")
mem = get_memory(3)
if mem: log.info(f"Memory: {mem.count(chr(10))+1} sessions loaded")
convs = get_recent_conversations(10)
if convs: log.info(f"Persistent memory: {len(convs)} messages from past sessions")
if WAKE_WORD: log.info("Wake word: ON")
log.info("Online. Press " + cfg.get("key_toggle","f13").upper() + " to activate.")
# PWA command polling
def pwa_dispatch(task):
"""Handle PWA command and save response to DB"""
app = "CODEC Dashboard"
audit("PWA_CMD", task[:200])
if len(task) < 500:
skill = check_skill(task)
if skill:
result = run_skill(skill, task, app)
if result is not None:
log.info(f"PWA response (silent): {str(result)[:100]}")
try:
_ts = datetime.now().isoformat()
_sid = "pwa_" + datetime.now().strftime("%Y%m%d_%H%M%S")
_c = sqlite3.connect(DB_PATH)
_c.execute("UPDATE sessions SET response=? WHERE task=? AND app=? ORDER BY id DESC LIMIT 1",
(str(result)[:500], task[:200], app))
_c.execute("INSERT INTO conversations (session_id,timestamp,role,content) VALUES (?,?,?,?)",
(_sid, _ts, "user", task[:500]))
_c.execute("INSERT INTO conversations (session_id,timestamp,role,content) VALUES (?,?,?,?)",
(_sid, _ts, "assistant", str(result)[:500]))
_c.commit(); _c.close()
except Exception as e:
log.warning(f"Non-critical error: {e}")
try:
with open(os.path.expanduser("~/.codec/pwa_response.json"), "w") as _rf:
json.dump({"task": task, "response": str(result), "ts": datetime.now().isoformat()}, _rf)
except Exception as e:
log.warning(f"Non-critical error: {e}")
log.info(f"PWA skill response: {str(result)[:100]}")
return
try:
import requests as _rq
headers = {"Content-Type": "application/json"}
if LLM_API_KEY: headers["Authorization"] = f"Bearer {LLM_API_KEY}"
body = {
"model": QWEN_MODEL,
"messages": [
{"role": "system", "content": f"You are {AGENT_NAME}, an AI assistant on CODEC. Answer concisely in 1-3 sentences. English only unless translating."},
{"role": "user", "content": task}
],
"max_tokens": 300,
"stream": False
}
body.update(LLM_KWARGS)
r = _rq.post(QWEN_BASE_URL + "/chat/completions", json=body, headers=headers, timeout=30)
answer = r.json()["choices"][0]["message"]["content"].strip()
if "</think>" in answer: answer = answer.split("</think>")[-1].strip()
log.info(f"PWA answer (silent): {answer[:100]}")
_ts = datetime.now().isoformat()
_sid = "pwa_" + datetime.now().strftime("%Y%m%d_%H%M%S")
_c = sqlite3.connect(DB_PATH)
_c.execute("INSERT INTO sessions (timestamp,task,app,response) VALUES (?,?,?,?)",
(_ts, task[:200], "CODEC Dashboard", answer[:500]))
_c.execute("INSERT INTO conversations (session_id,timestamp,role,content) VALUES (?,?,?,?)",
(_sid, _ts, "user", task[:500]))
_c.execute("INSERT INTO conversations (session_id,timestamp,role,content) VALUES (?,?,?,?)",
(_sid, _ts, "assistant", answer[:500]))
_c.commit(); _c.close()
with open(os.path.expanduser("~/.codec/pwa_response.json"), "w") as _rf:
json.dump({"task": task, "response": answer, "ts": datetime.now().isoformat()}, _rf)
except Exception as _e:
log.error(f"PWA LLM error: {_e}")
with open(os.path.expanduser("~/.codec/pwa_response.json"), "w") as _rf:
json.dump({"task": task, "response": f"Error: {str(_e)[:200]}", "ts": datetime.now().isoformat()}, _rf)
def pwa_poller():
import json as _json
while True:
try:
if os.path.exists(TASK_QUEUE_FILE):
with open(TASK_QUEUE_FILE) as _f:
data = _json.load(_f)
source = data.get("source", "")
if source == "pwa":
os.unlink(TASK_QUEUE_FILE)
task = data.get("task", "").strip()
if task:
log.info(f"PWA command: {task[:80]}")
push(lambda t=task: pwa_dispatch(t))
except Exception as e:
log.warning(f"Non-critical error: {e}")
time.sleep(1.5)
threading.Thread(target=pwa_poller, daemon=True).start()
threading.Thread(target=worker, daemon=True).start()
# Build keyboard context and start listener (blocks until exit)
from codec_keyboard import start_keyboard_listener
start_keyboard_listener(
state=state,
ctx={
'push': push,
'dispatch': dispatch,
'audit': audit,
'transcribe': transcribe,
'close_session': close_session,
'show_overlay': show_overlay,
'show_toggle_overlay': show_toggle_overlay,
'show_recording_overlay': show_recording_overlay,
'show_processing_overlay': show_processing_overlay,
'do_text': do_text,
'do_screenshot_question': do_screenshot_question,
'do_document_input': do_document_input,
}
)
if __name__ == "__main__":
main()