crystalwindow 4.3__py3-none-any.whl → 4.4__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
crystalwindow/ai.py CHANGED
@@ -1,20 +1,18 @@
1
1
  # ==========================================================
2
- # CrystalAI v0.7Self-Contained Symbolic Engine
2
+ # CrystalAI v0.8Hybrid Engine (Groq + Symbolic Fallback)
3
3
  # ----------------------------------------------------------
4
- # Focus: Pure Python 'thinking' (Knowledge Graph/Rule-Based)
5
- # Changes:
6
- # - Refactored _local_ai to _symbolic_engine
7
- # - Added a simple knowledge base (K-Graph)
8
- # - Added context/intent matching for 'thinking'
9
- # - Removed Groq/External API integration
4
+ # Combines:
5
+ # - Groq API for general knowledge (Primary)
6
+ # - Pure Python Symbolic Engine for Code Analysis (Fallback)
7
+ # - Memory, File Reading, AST Parsing, and Diff Utilities
10
8
  # ==========================================================
11
9
 
12
10
  import os
13
11
  import ast
14
12
  import difflib
13
+ import requests # Re-added for API communication
15
14
  from typing import Optional, Dict, Any, List
16
- # Removed requests and groq imports as they are no longer needed
17
- # Removed import groq (if it was present)
15
+ # Removed groq import, using requests for simplicity
18
16
 
19
17
  # ==========================================================
20
18
  # Response Wrapper
@@ -32,41 +30,53 @@ class CrystalAIResponse:
32
30
  # MAIN ENGINE
33
31
  # ==========================================================
34
32
  class AI:
35
- DEFAULT_MODEL = "PURE_SYMBOLIC"
33
+ DEFAULT_MODEL = "llama-3.1-8b"
36
34
  DEFAULT_PERSONALITY = (
37
- "You are CrystalMind AI, a completely self-coded, symbolic engine. "
38
- "I process information using an internal rule-base and knowledge graph. "
39
- "I can reason about simple code and defined concepts."
35
+ "You are CrystalWindow AI. You help users with Python code, "
36
+ "debugging, errors, docs, and file analysis. You are currently running in "
37
+ "Hybrid Mode. Be friendly, technical, clear, and precise."
40
38
  )
41
- # Key is ignored in this self-contained mode, but kept for signature consistency
42
- PLACEHOLDER_KEY = "PURE_SYMBOLIC_KEY_IGNORED"
39
+ # Placeholder Key for testing or when user key is invalid
40
+ PLACEHOLDER_KEY = "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"
43
41
 
44
42
  # ------------------------------------------------------
45
43
  def __init__(self, key=None, model=None):
46
- # --- KEY/MODEL SETUP (for consistency, though ignored) ---
47
- self.key = self.PLACEHOLDER_KEY
48
- self.model = self.DEFAULT_MODEL
44
+ # --- KEY VALIDATION ---
45
+ if not key or len(key) < 20 or " " in key:
46
+ print("[CrystalAI] Warning: Invalid or missing key → using placeholder. To get a Fixed Key go to 'console.groq.com/keys'")
47
+ self.key = self.PLACEHOLDER_KEY
48
+ else:
49
+ self.key = key
50
+
51
+ # --- MODEL VALIDATION ---
52
+ if not model or not isinstance(model, str) or len(model) < 3:
53
+ print(f"[CrystalAI] Unknown model → using default: {self.DEFAULT_MODEL}.")
54
+ self.model = self.DEFAULT_MODEL
55
+ else:
56
+ self.model = model
57
+
58
+ # Persona
49
59
  self.personality = self.DEFAULT_PERSONALITY
50
60
 
51
- # --- PURE AI KNOWLEDGE BASE (The 'Thinking' Core) ---
61
+ # Pure AI knowledge (used in symbolic fallback)
52
62
  self.knowledge_graph: Dict[str, Any] = self._build_knowledge_graph()
53
63
 
54
- # v0.4 memory system (optional)
64
+ # Library knowledge (loaded .py files)
65
+ self.library_context = ""
66
+
67
+ # Memory system
55
68
  self.memory: List[Dict[str, str]] = []
56
69
  self.use_memory = True
57
70
 
58
- # v0.4 toggle for forcing local engine
59
- self.force_local = True # Always True for a self-contained AI
60
-
61
- # Library knowledge (loaded .py files)
62
- self.library_context = ""
71
+ # Force local toggle (currently not used as logic is based on Groq success)
72
+ self.force_local = False
63
73
 
64
74
  # ==========================================================
65
75
  # PERSONALITY
66
76
  # ==========================================================
67
77
  def set_personality(self, txt):
68
78
  if not isinstance(txt, str) or len(txt.strip()) < 10:
69
- print("Oops! thats not how to use it—reverting to default.")
79
+ print("Oops! that's not how to use it—reverting to default.")
70
80
  self.personality = self.DEFAULT_PERSONALITY
71
81
  return
72
82
 
@@ -83,7 +93,6 @@ class AI:
83
93
  def index_library(self, folder):
84
94
  """
85
95
  Load all Python files as context for smarter answers.
86
- (Context is used in prompt but processed by local engine's rules)
87
96
  """
88
97
  out = []
89
98
  if not os.path.exists(folder):
@@ -97,7 +106,7 @@ class AI:
97
106
  path = os.path.join(root, f)
98
107
  with open(path, "r", encoding="utf8") as fp:
99
108
  out.append(f"# FILE: {path}\n" + fp.read() + "\n\n")
100
- except:
109
+ except Exception:
101
110
  pass
102
111
 
103
112
  self.library_context = "\n".join(out)
@@ -113,8 +122,8 @@ class AI:
113
122
  try:
114
123
  with open(path, "r", encoding="utf8") as f:
115
124
  return f.read()
116
- except:
117
- return "[CrystalAI] couldnt read file."
125
+ except Exception:
126
+ return "[CrystalAI] couldn't read file."
118
127
 
119
128
  # ==========================================================
120
129
  # PROMPT BUILDER (Unified)
@@ -145,11 +154,11 @@ class AI:
145
154
  self.memory.pop(0)
146
155
 
147
156
  # ==========================================================
148
- # PURE AI KNOWLEDGE BASE (Self-Contained 'Knowledge')
157
+ # PURE AI KNOWLEDGE BASE (Symbolic Core)
149
158
  # ==========================================================
150
159
  def _build_knowledge_graph(self) -> Dict[str, Any]:
151
160
  """
152
- Defines the internal knowledge the pure AI can reason with.
161
+ Defines the internal knowledge the symbolic AI can reason with.
153
162
  """
154
163
  return {
155
164
  "python": {
@@ -169,15 +178,15 @@ class AI:
169
178
  "keywords": ["fix", "error", "bug", "syntax"]
170
179
  }
171
180
  }
172
-
181
+
173
182
  # ==========================================================
174
- # PURE AI 'THINKING' ENGINE (Replaces _local_ai)
183
+ # PURE AI 'THINKING' ENGINE (Symbolic Fallback)
175
184
  # ==========================================================
176
185
  def _symbolic_engine(self, prompt: str, file_data: Optional[str]) -> str:
177
186
  """
178
- Simulates 'thinking' using only internal rules and the knowledge graph.
187
+ Fallback logic: Simulates 'thinking' using only internal rules and AST.
179
188
  """
180
- output = ["[SymbolicEngine] Processing request..."]
189
+ output = ["[Local/SymbolicEngine] Processing request..."]
181
190
  lower_prompt = prompt.lower()
182
191
 
183
192
  # --- Stage 1: File Analysis (Real Python AST) ---
@@ -189,15 +198,12 @@ class AI:
189
198
  output.append("The code is structurally sound. Ask for refactoring or explanation.")
190
199
  return "\n".join(output)
191
200
  except SyntaxError as se:
192
- # Use the built-in fix rule from the knowledge graph
193
201
  fix_rule = self.knowledge_graph["fix_code"]["rule"]
194
202
  lineno = se.lineno or 0
195
- offset = se.offset or 0
196
203
  msg = (
197
204
  f"❌ **SyntaxError Detected** (via AST):\n"
198
205
  f"• Message: {se.msg}\n"
199
206
  f"• Line: {lineno}\n"
200
- f"• Column: {offset}\n"
201
207
  f"• Rule suggestion: {fix_rule}"
202
208
  )
203
209
  output.append(msg)
@@ -207,16 +213,14 @@ class AI:
207
213
  # --- Stage 2: Knowledge Graph Lookup (Rule-Based Reasoning) ---
208
214
  output.append("\n[Stage 2: Symbolic Lookup]")
209
215
 
210
- # Check for concepts the AI 'knows'
211
216
  found_concept = False
212
217
  for key, knowledge in self.knowledge_graph.items():
213
218
  if key in lower_prompt or any(k in lower_prompt for k in knowledge.get("keywords", [])):
214
- if key == "fix_code": continue # Already handled in Stage 1
219
+ if key == "fix_code": continue
215
220
 
216
221
  output.append(f"🧠 Found Concept: **{key.upper()}**")
217
222
  output.append(f"Description: {knowledge.get('desc', 'No detailed description.')}")
218
223
 
219
- # Simple reasoning about related syntax
220
224
  if 'syntax' in knowledge:
221
225
  output.append("Related Syntax:")
222
226
  for syn, code in knowledge['syntax'].items():
@@ -226,21 +230,55 @@ class AI:
226
230
  break
227
231
 
228
232
  if not found_concept:
229
- output.append("❓ Concept Unknown: I am limited to my internal knowledge base (Python, AST, Fix Code).")
230
- output.append("Please ask about a defined concept or provide a file for AST analysis.")
231
-
233
+ output.append("❓ Concept Unknown: I am currently offline and limited to my internal knowledge base (Python, AST, Fix Code).")
234
+ output.append("Please provide a file for AST analysis or try again later for a full Groq response.")
232
235
 
233
236
  return "\n".join(output)
234
237
 
238
+
235
239
  # ==========================================================
236
- # ASK (Pure Symbolic Engine)
240
+ # ASK (Groq Symbolic Fallback)
237
241
  # ==========================================================
238
242
  def ask(self, text, file=None):
239
243
  file_data = self._read_file(file)
240
244
  prompt = self._build_prompt(text, file_data)
241
245
 
242
- # We skip the external API call entirely, relying on the symbolic engine
243
- resp = self._symbolic_engine(prompt, file_data)
246
+ resp = None
247
+
248
+ # --- Attempt 1: Call External API (Groq) ---
249
+ try:
250
+ url = "https://api.groq.com/openai/v1/chat/completions"
251
+ headers = {
252
+ "Authorization": f"Bearer {self.key}",
253
+ "Content-Type": "application/json"
254
+ }
255
+
256
+ payload = {
257
+ "model": self.model,
258
+ "messages": [
259
+ {"role": "system", "content": self.personality},
260
+ {"role": "user", "content": prompt}
261
+ ],
262
+ "temperature": 0.3
263
+ }
264
+
265
+ r = requests.post(url, json=payload, headers=headers, timeout=10)
266
+ data = r.json()
267
+
268
+ if "error" in data:
269
+ # If the API returns an error (e.g., bad key), we force the fallback
270
+ raise RuntimeError(f"API Error: {data['error'].get('message', 'Unknown API Error')}")
271
+
272
+ resp = data["choices"][0]["message"]["content"]
273
+
274
+ except Exception as e:
275
+ # This block handles API connection failures (timeout) or API-side errors (bad key)
276
+ print(f"[CrystalAI] Groq connection failed or returned error: {e.__class__.__name__}")
277
+ print("[CrystalAI] Falling back to self-contained Symbolic Engine...")
278
+
279
+ # --- Attempt 2: Fallback to Symbolic Engine ---
280
+ resp = self._symbolic_engine(prompt, file_data)
281
+
244
282
 
245
283
  self._save_memory(text, resp)
246
284
  return CrystalAIResponse(resp)
@@ -278,7 +316,7 @@ class AI:
278
316
  msg = getattr(syntax_error, "msg", "")
279
317
  lineno = syntax_error.lineno or 0
280
318
 
281
- # missing colon
319
+ # missing colon fix
282
320
  if "expected" in msg and ":" in msg:
283
321
  if 1 <= lineno <= len(lines):
284
322
  line = lines[lineno - 1].rstrip()
@@ -289,7 +327,7 @@ class AI:
289
327
  try:
290
328
  ast.parse(candidate)
291
329
  return candidate, notes
292
- except:
330
+ except Exception:
293
331
  pass
294
332
 
295
333
  # fallback
@@ -320,10 +358,9 @@ class AI:
320
358
  out = []
321
359
  for i in range(start, end):
322
360
  mark = "->" if (i + 1) == lineno else " "
323
- # Adjusted line formatting for clarity
324
361
  out.append(f"{mark} {i+1:<4}: {lines[i]}")
325
362
  return "\n".join(out)
326
363
 
327
364
  # ==========================================================
328
- # END OF PURE SYMBOLIC ENGINE
365
+ # END OF HYBRID ENGINE
329
366
  # ==========================================================
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: crystalwindow
3
- Version: 4.3
3
+ Version: 4.4
4
4
  Summary: A Tkinter powered window + GUI toolkit made by Crystal (MEEEEEE)! Easier apps, smoother UI and all-in-one helpers!
5
5
  Home-page: https://pypi.org/project/crystalwindow/
6
6
  Author: CrystalBallyHereXD
@@ -1,6 +1,6 @@
1
1
  crystalwindow/FileHelper.py,sha256=aUnnRG7UwvzJt-idjWjmpwy3RM6nqLlC3-7Bae6Yb94,5471
2
2
  crystalwindow/__init__.py,sha256=a2kdMZ29QZ4kSQ3M8jLvCR6g3OUQxNhdaT3ycxoames,2264
3
- crystalwindow/ai.py,sha256=k-3MVOKZwAIMLB6ifyR0Gcuo4Fh0-JvoDVA9_j-PJSQ,13293
3
+ crystalwindow/ai.py,sha256=4CN3_Fi5zERxyqoHcUyXPvm4MVWaicA7JGK0lRb-ybQ,14661
4
4
  crystalwindow/animation.py,sha256=zHjrdBXQeyNaLAuaGPldJueX05OZ5j31YR8NizmR0uQ,427
5
5
  crystalwindow/assets.py,sha256=2Cj0zdhMWo3mWjdr9KU5n-9_8iKj_fJ9uShMFA-27HU,5193
6
6
  crystalwindow/camera.py,sha256=tbn4X-jxMIszAUg3Iu-89gJN5nij0mjPMEzGotcLbJI,712
@@ -33,8 +33,8 @@ crystalwindow/gametests/guitesting.py,sha256=SrOssY5peCQEV6TQ1AiOWtjb9phVGdRzW-Q
33
33
  crystalwindow/gametests/sandbox.py,sha256=Oo2tU2N0y3BPVa6T5vs_h9N6islhQrjSrr_78XLut5I,1007
34
34
  crystalwindow/gametests/squaremove.py,sha256=poP2Zjl2oc2HVvIAgIK34H2jVj6otL4jEdvAOR6L9sI,572
35
35
  crystalwindow/gametests/windowtesting.py,sha256=_9X6wnV1-_X_PtNS-0zu-k209NtFIwAc4vpxLPp7V2o,97
36
- crystalwindow-4.3.dist-info/licenses/LICENSE,sha256=Gt5cJRchdNt0guxyQMHKsATN5PM5mjuDhdO6Gzs9qQc,1096
37
- crystalwindow-4.3.dist-info/METADATA,sha256=gYbWholGWviaRGXpVc1zaOZ0wQ6KWB9lvw2ZdWbBDrY,7338
38
- crystalwindow-4.3.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
39
- crystalwindow-4.3.dist-info/top_level.txt,sha256=PeQSld4b19XWT-zvbYkvE2Xg8sakIMbDzSzSdOSRN8o,14
40
- crystalwindow-4.3.dist-info/RECORD,,
36
+ crystalwindow-4.4.dist-info/licenses/LICENSE,sha256=Gt5cJRchdNt0guxyQMHKsATN5PM5mjuDhdO6Gzs9qQc,1096
37
+ crystalwindow-4.4.dist-info/METADATA,sha256=8j8_X2PPPAo6OmZELS5-KpjARhkwJcnYcC4r2qur-7M,7338
38
+ crystalwindow-4.4.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
39
+ crystalwindow-4.4.dist-info/top_level.txt,sha256=PeQSld4b19XWT-zvbYkvE2Xg8sakIMbDzSzSdOSRN8o,14
40
+ crystalwindow-4.4.dist-info/RECORD,,