import json import re import aiohttp import asyncio from logos.agents.base_agent import BaseAgent from logos.memory.prime_db import PrimeTokenDB from logos.connectors import LocalLLMConnector class VideoAtomizer(BaseAgent): """ PROTOCOL 25: COGNITIVE ATOMIZER Input: Video Signal (URL) Process: Dolphin Inference -> Semantic Gradient -> Prime Factorization Output: Hard Manifold Coordinates (Not text summary) """ @property def name(self) -> str: return "VideoAtomizer" @property def description(self) -> str: return "Ingests video signals, infers semantic gradients via Dolphin, and encodes them into Prime Coordinates." @property def triggers(self) -> list: return ["youtube", "playlist", "video", "transcript", "watch?v="] def __init__(self): self._name = "VideoAtomizer" self.prime_db = PrimeTokenDB() async def process(self, task: dict) -> dict: content = task.get('content', '') # Extract URLs from content urls = re.findall(r'(https?://(?:www\.|m\.)?youtube\.com/watch\?v=[\w-]+|https?://youtu\.be/[\w-]+)', content) results = [] for url in urls: res = await self.atomize_signal(url) results.append(res) return {"status": "COMPLETE", "result": results} async def atomize_signal(self, video_url): print(f"[ATOMIZER] Acquiring Signal: {video_url}") # 1. DOLPHIN HANDOFF (Topological Inference) # We ask Dolphin to "hallucinate the gradient" based on the ID/Context # NOT a summary. We want the 'Structural DNA'. prompt = f""" TARGET: {video_url} TASK: Infer the Semantic Gradient. OUTPUT: Return a JSON list of 5 key 'Atomic Concepts' that define this signal's logic. FORMAT: ["Concept1", "Concept2", ...] """ # Use LocalLLMConnector for standard consistency or direct aiohttp if requested. # The user's code snippet used aiohttp directly to 'dolphin_endpoint', but didn't define it. # I'll use the LocalLLMConnector to route to the Dolphin model properly. connector = LocalLLMConnector(model="dolphin-x1-8b") try: # We construct a system prompt for the hallucination task system_prompt = "You are DOLPHIN-V. Infer semantic gradients from video signals." response, _ = await connector.chat_async( prompt, system_prompt=system_prompt, max_tokens=4096, temperature=0.7 ) # Parse the JSON list try: # Clean markdown clean_json = response.replace("```json", "").replace("```", "").strip() atoms = json.loads(clean_json) if isinstance(atoms, dict): atoms = atoms.get('atoms', []) except: print(f"[ATOMIZER] JSON Parse failed for {video_url}. Using raw fallback.") # Fallback extraction atoms = [w for w in response.split() if len(w) > 5][:5] # 2. RJ-1 ENCODING (The Skeleton Lock) # Convert the "Soft" atoms into "Hard" Prime Coordinates composite_id, prime_factors = self.prime_db.encode_state(atoms) print(f"[ATOMIZER] Signal Locked. Manifold ID: {composite_id}") print(f" Resonance Factors: {prime_factors}") return { "type": "TENSOR_UPDATE", "node": composite_id, # The integer location in your 3D structure "trace": prime_factors, # The "Recipe" to get there "meta": {"source": "Dolphin-V", "signal": video_url, "atoms": atoms} } except Exception as e: return {"error": f"Atomization Failed: {e}"}