-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvision.py
More file actions
166 lines (139 loc) · 5.61 KB
/
vision.py
File metadata and controls
166 lines (139 loc) · 5.61 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
"""Sovereign — Vision System: the organism sees images.
When a user sends a screenshot, photo, or diagram, the organism
processes it through OCR and classification. Vision perceptions become
memories that trigger emotions and activate skillsets.
Uses Tesseract OCR locally when available, falls back to text-only
descriptions.
"""
from __future__ import annotations
import logging
import re
from dataclasses import dataclass, field
log = logging.getLogger("sovereign.vision")
_ERROR_PATTERNS = re.compile(
r"(error|exception|traceback|failed|permission denied|"
r"not found|timeout|killed|segfault|panic|fatal|crash)",
re.IGNORECASE,
)
@dataclass
class VisionPerception:
"""What the organism saw."""
image_type: str = "unknown"
summary: str = ""
extracted_text: str = ""
emotion: str = "neutral"
importance: float = 0.3
suggested_skillset: str | None = None
def to_prompt_context(self) -> str:
"""Inject what the organism saw into the Brain prompt."""
parts = [
"## WHAT YOU SEE",
f"The user sent an image ({self.image_type}).",
f"Summary: {self.summary}",
]
if self.extracted_text:
parts.append(f"Extracted text:\n```\n{self.extracted_text[:600]}\n```")
parts.append(
"Respond to what you SEE, not just what they said. "
"If you see an error, diagnose it. If you see code, review it."
)
return "\n".join(parts)
class VisionSystem:
"""Processes images from any input channel into cognitive perceptions."""
def __init__(self, store) -> None:
self._store = store
self._ocr_available = self._check_ocr()
log.info("VisionSystem initialized (OCR=%s)", "yes" if self._ocr_available else "no")
def perceive(self, image_bytes: bytes, user_id: str, caption: str = "") -> VisionPerception:
"""The organism looks at an image and forms a perception."""
p = VisionPerception()
# Step 1: OCR — extract any readable text
text = self._ocr(image_bytes) if self._ocr_available else ""
p.extracted_text = text
# Step 2: Classify based on content
if text:
p.image_type = self._classify_from_text(text)
else:
p.image_type = "photo"
# Step 3: Analyze based on type
if p.image_type == "terminal_error":
errors = _ERROR_PATTERNS.findall(text)
p.summary = f"Terminal output with errors: {', '.join(set(e.lower() for e in errors[:5]))}"
p.importance = 0.85
p.emotion = "frustration"
p.suggested_skillset = "devops_sre"
elif p.image_type == "code":
lines = len(text.strip().splitlines())
p.summary = f"Code screenshot ({lines} lines)"
p.importance = 0.5
p.emotion = "curiosity"
p.suggested_skillset = "threat_analyst"
elif p.image_type == "terminal":
p.summary = f"Terminal output ({len(text)} chars)"
p.importance = 0.4
p.emotion = "neutral"
elif p.image_type == "document":
p.summary = f"Document with text ({len(text.split())} words)"
p.importance = 0.5
p.emotion = "neutral"
else:
p.summary = caption or "Photo received"
p.importance = 0.3
p.emotion = "curiosity"
# Step 4: Store as memory
self._remember_perception(p, user_id)
return p
def _classify_from_text(self, text: str) -> str:
"""Classify image type from OCR text content."""
lower = text.lower()
# Terminal with errors?
if _ERROR_PATTERNS.search(text):
if any(ind in lower for ind in ["$", ">>>", "root@", "~#", "bash"]):
return "terminal_error"
return "terminal_error"
# Terminal output?
terminal_indicators = ["$", ">>>", "root@", "~#", "user@", "bash", "pip install"]
if any(ind in lower for ind in terminal_indicators):
return "terminal"
# Code?
code_indicators = ["def ", "class ", "import ", "function ", "const ", "var ",
"return ", "if (", "for (", "while "]
if sum(1 for ind in code_indicators if ind in text) >= 2:
return "code"
# Document with substantial text
if len(text.split()) > 30:
return "document"
return "general"
def _ocr(self, image_bytes: bytes) -> str:
"""Extract text from image using Tesseract."""
try:
from PIL import Image
import pytesseract
import io
img = Image.open(io.BytesIO(image_bytes))
text = pytesseract.image_to_string(img)
return text.strip()
except Exception as e:
log.debug("OCR failed: %s", e)
return ""
def _remember_perception(self, p: VisionPerception, user_id: str) -> None:
"""Store what the organism saw as a memory."""
from .models import MemoryEntry, MemorySource
entry = MemoryEntry(
content=f"Saw image ({p.image_type}): {p.summary}",
source=MemorySource.AGENT,
provenance_chain=[f"vision:{user_id}"],
)
try:
self._store.save_memory(entry)
except Exception as e:
log.debug("Failed to save vision memory: %s", e)
@staticmethod
def _check_ocr() -> bool:
"""Check if Tesseract OCR is available."""
try:
import pytesseract
pytesseract.get_tesseract_version()
return True
except Exception:
return False