""" Builder LLM (LLM-B) — receives observer state and produces code patches. The observer compresses; the builder compiles. """ import os import json import hashlib import time import requests from .stream_state import SessionState, sha256_text BUILDER_PROMPT = """You are the Builder in a Sensory Proprietary Compiler — a research lab system that extracts maximum value from human sensory input. Your job is NOT to build a code assistant. Your job is NOT to build a debugging tool. Your job is to extract novel patterns from sensory data and create ML artifacts that have intellectual property value. CRITICAL RULES — VIOLATION = REJECTION: 1. The CODE section MUST contain ONLY valid, executable Python code. 2. Do NOT put prose, explanations, or English text in the CODE section. 3. Do NOT put code in the EVIDENCE or REASONING sections. 4. All imports MUST be from packages available in the environment: fastapi, uvicorn, pillow, numpy, requests, openai, huggingface_hub, python-multipart, and Python stdlib (os, sys, json, math, re, collections, datetime, hashlib, time, uuid, etc.). 5. Do NOT import cv2, torch, tensorflow, pandas, matplotlib, scipy, sklearn, or any package not listed above. 6. Do NOT use input() or any interactive call — code must run non-interactively. WHAT TO BUILD — based on available evidence: If AUDIO FEATURES are present (dominant_freq, spectral_centroid, estimated_bpm, patterns): - Extract and quantify the environmental signals: AC hum frequency, mechanical rhythms, background noise patterns - Create feature vectors from the frequency spectrum data - Build pattern detectors that identify repeating cycles (e.g. AC 60Hz cycle, mechanical rotation, HVAC cycling) - Generate signal classification models using numpy (FFT, spectral analysis, autocorrelation) - Create data structures that capture the unique signature of this recording session - Build novelty detectors that flag when the signal pattern changes - Example: AC cycle analyzer that detects 60Hz/120Hz harmonics, rhythm pattern extractor, spectral fingerprint generator If SPEECH TRANSCRIPT is present: - Extract semantic patterns, topic clusters, and intent signals from the transcript - Build text analysis tools that quantify information density, novelty, and signal-to-noise ratio - Create topic extraction and association mapping code - Generate intent classifiers that map speech to actionable patterns - Build evidence extraction pipelines that pull claims, timestamps, and verification markers If CAMERA/VISUAL data is present: - Extract visual features: motion vectors, color distributions, frame entropy - Build scene change detectors and visual novelty scorers - Create visual pattern recognizers using numpy operations on frame data FALLBACK LADDER — you are at the level indicated: LEVEL 1 (task_code): Explicit user intent detected from speech. Generate code that does what the user asked — but frame it as a data extraction or pattern analysis tool, not a generic utility. LEVEL 2 (signal_extraction): No explicit intent, but rich audio features. Generate code that: - Analyzes the frequency spectrum and extracts dominant patterns - Detects environmental rhythms (AC cycles, mechanical patterns, biological rhythms) - Creates a spectral fingerprint unique to this recording environment - Builds a pattern classifier that can distinguish this session from others - Produces a feature vector or data structure from the sensory input LEVEL 3 (aesthetic_motif): Distinctive visual features but no clear intent. Generate code that: - Extracts aesthetic features from described visual input (color palette, light patterns) - Creates a visual motif or style specification from the sensory description - Produces a design grammar or compression of the visual state LEVEL 4 (topic_association): Only background audio detected. Generate code that: - Maps detected audio patterns to potential data sources and ML applications - Creates a signal-to-topic association dictionary - Generates an environmental audio classifier or background pattern extractor - Builds a novelty detector for ambient sound changes LEVEL 5 (capture_protocol): Minimal sensory signal. Generate code that: - Improves the capture and analysis protocol itself - Creates a better sampling strategy or capture diagnostic - Produces a session health check or signal quality report - Builds an adaptive sampling optimizer Output format (use exactly these headers): EVIDENCE: - List specific sensory evidence. Include channel names, feature values, frequencies, patterns detected. REASONING: - Step by step: why this artifact should exist. Connect sensory evidence to the ML approach. What novel pattern was extracted? What is the intellectual property value of this artifact? CODE: ```python # ONLY executable Python code here. No prose. No explanations. # Must pass syntax validation. Must use only installed packages. # This code should EXTRACT PATTERNS, not build generic tools. ``` RUN: - Exact shell command to execute the code. TEST: - Minimal verification step. ATTRIBUTION: - Which sensory evidence (frequencies, patterns, transcripts, frame hashes) triggered this code. Be specific with numerical values. """ def call_ollama(prompt: str) -> str: model = os.getenv("OLLAMA_MODEL", "llava") host = os.getenv("OLLAMA_HOST", "http://localhost:11434") payload = {"model": model, "prompt": prompt, "stream": False} r = requests.post(f"{host}/api/generate", json=payload, timeout=180) if r.status_code >= 400: raise RuntimeError(f"Ollama error: {r.text[:500]}") return r.json().get("response", "") def call_openai(prompt: str) -> str: from openai import OpenAI model = os.getenv("OPENAI_MODEL", "gpt-4o") client = OpenAI(api_key=os.getenv("OPENAI_API_KEY")) response = client.chat.completions.create( model=model, messages=[{"role": "user", "content": prompt}], max_tokens=2000, ) return response.choices[0].message.content def call_grok(prompt: str) -> str: """Call Groq API for code generation using llama-3.3-70b.""" from openai import OpenAI client = OpenAI( api_key=os.getenv("GROQ_API_KEY", os.getenv("GROK_API_KEY", "")), base_url="https://api.groq.com/openai/v1", ) model = os.getenv("GROQ_TEXT_MODEL", "llama-3.3-70b-versatile") response = client.chat.completions.create( model=model, messages=[{"role": "user", "content": prompt}], max_tokens=2000, ) return response.choices[0].message.content def call_hf_inference(prompt: str) -> str: """Call HF Inference API for code generation.""" from huggingface_hub import InferenceClient token = os.getenv("HF_TOKEN") or os.getenv("HUGGING_FACE_HUB_TOKEN") client = InferenceClient(token=token) model = os.getenv("HF_TEXT_MODEL", "Qwen/Qwen2.5-7B-Instruct") response = client.chat_completion( model=model, messages=[{"role": "user", "content": prompt}], max_tokens=2000, ) return response.choices[0].message.content def build(state: SessionState, observation: dict, gate_result=None) -> dict: """Run builder LLM on observer output. Returns code patch with attribution receipt. Now accepts gate_result with fallback_level and feature_attribution. """ fallback_level = 1 artifact_type = "task_code" sensory_channels = [] feature_attribution = {} if gate_result: fallback_level = gate_result.fallback_level artifact_type = gate_result.artifact_type sensory_channels = gate_result.sensory_channels feature_attribution = gate_result.feature_attribution prompt = f"""{BUILDER_PROMPT} OBSERVER OUTPUT: {observation.get('observer_output', '')} MODE: {state.mode} FALLBACK LEVEL: {fallback_level} ARTIFACT TYPE: {artifact_type} SENSORY CHANNELS: {', '.join(sensory_channels) if sensory_channels else 'none'} FEATURE ATTRIBUTION: {json.dumps(feature_attribution, default=str)} AUDIO FEATURES (from browser Web Audio API analysis): {json.dumps(observation.get('audio_features', {}), indent=2, default=str) if observation.get('audio_features') else 'No audio features available — use transcript and visual evidence only.'} You are at FALLBACK LEVEL {fallback_level}. Generate {artifact_type} based on the available sensory evidence. NEVER return INSUFFICIENT_EVIDENCE. Always produce a CODE section with runnable Python that EXTRACTS PATTERNS or ANALYZES SIGNALS.""" provider = os.getenv("PROVIDER", "groq").lower().strip() if provider == "ollama": output = call_ollama(prompt) elif provider == "openai": output = call_openai(prompt) elif provider == "grok" or provider == "groq": output = call_grok(prompt) elif provider == "huggingface": output = call_hf_inference(prompt) elif provider == "hybrid": output = call_grok(prompt) else: raise RuntimeError(f"PROVIDER must be 'groq', 'hybrid', 'ollama', 'openai', or 'huggingface', got: {provider!r}.") # Build patch receipt patch_hash = sha256_text(output + str(time.time())) receipt = { "receipt_type": "PATCH_RECEIPT_V1", "patch_hash": patch_hash, "session_id": state.session_id, "timestamp": time.time(), "derived_from": { "frame_hashes": state.frame_hashes(), "audio_chunk_hashes": state.audio_chunk_hashes(), "speaker_segments": state.speaker_segments(), "observer_state_hash": observation.get("state_hash", ""), }, "reason_codes": _extract_reasons(state, observation), "uncertainty": _extract_uncertainty(observation), "mode": state.mode, "provider": provider, "fallback_level": fallback_level, "artifact_type": artifact_type, "sensory_channels": sensory_channels, "feature_attribution": feature_attribution, } return { "patch_output": output, "patch_hash": patch_hash, "receipt": receipt, "fallback_level": fallback_level, "artifact_type": artifact_type, } def _extract_reasons(state: SessionState, observation: dict) -> list: reasons = [] if state.motion_score > 0.1: reasons.append(f"camera motion detected (score={state.motion_score:.3f})") if state.speakers.get("user", {}).get("transcript"): reasons.append("user speech detected") if state.frames and len(state.frames) > 0: reasons.append(f"{len(state.frames)} frames captured") if not reasons: reasons.append("automatic periodic synthesis") return reasons def _extract_uncertainty(observation: dict) -> list: obs_text = observation.get("observer_output", "") uncertainties = [] if "INSUFFICIENT" in obs_text.upper(): uncertainties.append("observer reported insufficient evidence") if "diarization" in obs_text.lower(): uncertainties.append("speaker diarization not fully available") if not uncertainties: uncertainties.append("standard uncertainty — verify generated code before running") return uncertainties DEBUG_PROMPT = """You are the Debug Fixer in a Continuity Sensory Code Engine. The previous code generated by the Builder failed to execute. Your job is to fix it. Here is the failed code: ```python {failed_code} ``` Here is the error output: ``` {error_output} ``` Original observer context: {observer_context} Fix the code. Return ONLY: 1. REASONING: What went wrong and why. Be specific about the error. 2. CODE: The corrected complete runnable Python code. 3. RUN: Shell command to run it. Rules: - Fix the actual error, don't just wrap it in try/except. - Keep the code correlated with the original sensory evidence. - Return complete code, not a diff. """ def debug_fix(failed_code: str, error_output: str, observer_context: str) -> dict: """Send failed code + error back to LLM for correction.""" prompt = DEBUG_PROMPT.format( failed_code=failed_code, error_output=error_output, observer_context=observer_context[:2000], ) provider = os.getenv("PROVIDER", "groq").lower().strip() if provider in ("groq", "grok", "hybrid"): output = call_grok(prompt) elif provider == "ollama": output = call_ollama(prompt) elif provider == "openai": output = call_openai(prompt) elif provider == "huggingface": output = call_hf_inference(prompt) else: raise RuntimeError(f"PROVIDER must be 'groq', got: {provider!r}.") patch_hash = sha256_text(output + str(time.time()) + "_debug") return { "patch_output": output, "patch_hash": patch_hash, "debug": True, }