Kakashiix26 commited on
Commit
11dec38
Β·
verified Β·
1 Parent(s): 238042d

wave2: UX Master vision + coder streaming (agent)

Browse files
agent/__pycache__/__init__.cpython-314.pyc ADDED
Binary file (154 Bytes). View file
 
agent/__pycache__/events.cpython-314.pyc ADDED
Binary file (8.23 kB). View file
 
agent/__pycache__/graph.cpython-314.pyc ADDED
Binary file (5.81 kB). View file
 
agent/__pycache__/llm.cpython-314.pyc ADDED
Binary file (16.8 kB). View file
 
agent/__pycache__/prompts.cpython-314.pyc ADDED
Binary file (24.6 kB). View file
 
agent/__pycache__/scaffold.cpython-314.pyc ADDED
Binary file (16.2 kB). View file
 
agent/__pycache__/states.cpython-314.pyc ADDED
Binary file (4.64 kB). View file
 
agent/__pycache__/tools.cpython-314.pyc ADDED
Binary file (5.22 kB). View file
 
agent/events.py CHANGED
@@ -63,6 +63,11 @@ class AgentEventEmitter:
63
  """Emit the full topology so the FE can render the graph skeleton immediately."""
64
  self._push(type="graph_init", topology=self._topology)
65
 
 
 
 
 
 
66
  def start_next(self) -> None:
67
  """Advance to the next agent and emit agent_start. No-op past the last agent."""
68
  self._current_idx += 1
 
63
  """Emit the full topology so the FE can render the graph skeleton immediately."""
64
  self._push(type="graph_init", topology=self._topology)
65
 
66
+ def chunk(self, text: str) -> None:
67
+ """Push a live coder output delta so the FE can show files being written in real time."""
68
+ if text:
69
+ self._push(type="coder_chunk", text=text)
70
+
71
  def start_next(self) -> None:
72
  """Advance to the next agent and emit agent_start. No-op past the last agent."""
73
  self._current_idx += 1
agent/graph.py CHANGED
@@ -3,8 +3,8 @@ import logging
3
  from dotenv import load_dotenv
4
  from langgraph.graph import END, START, StateGraph
5
 
6
- from agent.llm import (CODER_CHAIN, CODER_CHAIN_PREMIUM, PLANNER_CHAIN, chat,
7
- chat_structured, strip_code_fences)
8
  from agent.prompts import (CODER_SYSTEM_PROMPT, architect_prompt,
9
  coder_prompt, planner_prompt)
10
  from agent.scaffold import normalize_files, parse_multi_file_response
@@ -53,8 +53,13 @@ def _coder_node(emitter, premium=False):
53
  # Premium: 20000 tokens + CODER_CHAIN_PREMIUM (no Groq/OpenRouter) for production-grade apps.
54
  chain = CODER_CHAIN_PREMIUM if premium else CODER_CHAIN
55
  max_tokens = 20000 if premium else 14000
56
- raw = chat(coder_prompt(task_dicts, plan_json, premium=premium),
57
- chain=chain, system=CODER_SYSTEM_PROMPT, max_tokens=max_tokens)
 
 
 
 
 
58
  files = parse_multi_file_response(raw, strip_code_fences)
59
  for path in files:
60
  logger.info("Coder: wrote %s (%d chars)", path, len(files[path]))
 
3
  from dotenv import load_dotenv
4
  from langgraph.graph import END, START, StateGraph
5
 
6
+ from agent.llm import (CODER_CHAIN, CODER_CHAIN_PREMIUM, PLANNER_CHAIN,
7
+ chat_stream, chat_structured, strip_code_fences)
8
  from agent.prompts import (CODER_SYSTEM_PROMPT, architect_prompt,
9
  coder_prompt, planner_prompt)
10
  from agent.scaffold import normalize_files, parse_multi_file_response
 
53
  # Premium: 20000 tokens + CODER_CHAIN_PREMIUM (no Groq/OpenRouter) for production-grade apps.
54
  chain = CODER_CHAIN_PREMIUM if premium else CODER_CHAIN
55
  max_tokens = 20000 if premium else 14000
56
+ # ponytail: stream the first provider so the FE can show files being written live
57
+ # (coder_chunk events); chat_stream falls back to non-streaming chat() on any hiccup,
58
+ # and we parse the FULL accumulated text exactly as before (final result is authoritative).
59
+ on_delta = emitter.chunk if emitter else None
60
+ raw = chat_stream(coder_prompt(task_dicts, plan_json, premium=premium),
61
+ chain=chain, system=CODER_SYSTEM_PROMPT,
62
+ max_tokens=max_tokens, on_delta=on_delta)
63
  files = parse_multi_file_response(raw, strip_code_fences)
64
  for path in files:
65
  logger.info("Coder: wrote %s (%d chars)", path, len(files[path]))
agent/llm.py CHANGED
@@ -15,7 +15,7 @@ import logging
15
  import os
16
  import re
17
  import time
18
- from typing import Type, TypeVar
19
 
20
  from dotenv import load_dotenv
21
  from openai import APIConnectionError, APIError, OpenAI, RateLimitError
@@ -86,6 +86,14 @@ CODER_CHAIN_PREMIUM: list[tuple[str, str]] = [
86
  ("gemini", "gemini-2.5-flash"),
87
  ("sambanova", "Meta-Llama-3.3-70B-Instruct"),
88
  ]
 
 
 
 
 
 
 
 
89
 
90
  _RETRYABLE = (RateLimitError, APIError, APIConnectionError)
91
  _clients: dict[str, OpenAI] = {}
@@ -155,6 +163,106 @@ def chat(
155
  raise AllProvidersFailed(f"All providers failed: {last_err}")
156
 
157
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
158
  _FENCE_OPEN_RE = re.compile(r"^```[a-zA-Z0-9_+-]*\s*\n?")
159
 
160
 
 
15
  import os
16
  import re
17
  import time
18
+ from typing import Callable, Type, TypeVar
19
 
20
  from dotenv import load_dotenv
21
  from openai import APIConnectionError, APIError, OpenAI, RateLimitError
 
86
  ("gemini", "gemini-2.5-flash"),
87
  ("sambanova", "Meta-Llama-3.3-70B-Instruct"),
88
  ]
89
+ # ponytail: Gemini 2.5 Flash is free + MULTIMODAL β€” the only vision-capable model in the pool,
90
+ # so the vision chain is gemini-only (no second fallback that's verified to accept image_url).
91
+ VISION_CHAIN: list[tuple[str, str]] = [("gemini", "gemini-2.5-flash")]
92
+
93
+ # ponytail: providers whose OpenAI-compatible SSE streaming we trust to behave (clean deltas).
94
+ # Cloudflare GLM is excluded β€” slow 60s+ cold starts make streaming fragile, so we never lead
95
+ # the live stream with it (it falls through to non-streaming chat() instead).
96
+ _STREAM_TRUSTED: set[str] = {"cerebras", "gemini", "groq", "nvidia", "sambanova", "openrouter"}
97
 
98
  _RETRYABLE = (RateLimitError, APIError, APIConnectionError)
99
  _clients: dict[str, OpenAI] = {}
 
163
  raise AllProvidersFailed(f"All providers failed: {last_err}")
164
 
165
 
166
+ def chat_vision(
167
+ prompt: str,
168
+ image_data_url: str,
169
+ *,
170
+ chain: list[tuple[str, str]] | None = None,
171
+ system: str | None = None,
172
+ max_tokens: int = 4096,
173
+ temperature: float = 0.2,
174
+ ) -> str:
175
+ """Multimodal chat β€” sends a text prompt + an image (base64 data URL) to a vision model.
176
+
177
+ Builds OpenAI-compatible multimodal `content` (text part + image_url part) and reuses the
178
+ same provider-fallback + error handling as chat(). Defaults to VISION_CHAIN (Gemini 2.5
179
+ Flash β€” free + multimodal).
180
+ """
181
+ chain = usable_chain(chain or VISION_CHAIN)
182
+ if not chain:
183
+ raise AllProvidersFailed("No vision provider has an API key configured")
184
+
185
+ messages: list = []
186
+ if system:
187
+ messages.append({"role": "system", "content": system})
188
+ messages.append({
189
+ "role": "user",
190
+ "content": [
191
+ {"type": "text", "text": prompt},
192
+ {"type": "image_url", "image_url": {"url": image_data_url}},
193
+ ],
194
+ })
195
+
196
+ last_err: Exception | None = None
197
+ for provider, model in chain:
198
+ try:
199
+ resp = get_client(provider).chat.completions.create(
200
+ model=model, messages=messages, # type: ignore[arg-type]
201
+ max_tokens=max_tokens, temperature=temperature,
202
+ )
203
+ return (resp.choices[0].message.content or "").strip()
204
+ except _RETRYABLE as exc:
205
+ last_err = exc
206
+ logger.warning("Vision provider %s/%s failed: %s β€” falling back", provider, model, exc)
207
+ continue
208
+ raise AllProvidersFailed(f"All vision providers failed: {last_err}")
209
+
210
+
211
+ def chat_stream(
212
+ prompt: str,
213
+ *,
214
+ chain: list[tuple[str, str]] | None = None,
215
+ system: str | None = None,
216
+ max_tokens: int = 2048,
217
+ temperature: float = 0.2,
218
+ on_delta: Callable[[str], None] | None = None,
219
+ ) -> str:
220
+ """Stream the FIRST usable provider, invoking on_delta per text chunk, and return the
221
+ full accumulated text.
222
+
223
+ ponytail: only the first provider is streamed (live coder output for the FE). If it isn't
224
+ a trusted streamer, OR streaming raises/returns nothing, we fall back to the existing
225
+ non-streaming chat() over the rest of the chain β€” the result is always authoritative.
226
+ """
227
+ chain = usable_chain(chain or CODER_CHAIN)
228
+ if not chain:
229
+ raise AllProvidersFailed("No provider has an API key configured")
230
+
231
+ messages: list = []
232
+ if system:
233
+ messages.append({"role": "system", "content": system})
234
+ messages.append({"role": "user", "content": prompt})
235
+
236
+ first_provider, first_model = chain[0]
237
+ fallback_chain = chain
238
+ if first_provider in _STREAM_TRUSTED:
239
+ try:
240
+ stream = get_client(first_provider).chat.completions.create(
241
+ model=first_model, messages=messages, # type: ignore[arg-type]
242
+ max_tokens=max_tokens, temperature=temperature, stream=True,
243
+ )
244
+ parts: list[str] = []
245
+ for chunk in stream:
246
+ choices = getattr(chunk, "choices", None) or []
247
+ delta = (getattr(choices[0].delta, "content", None) or "") if choices else ""
248
+ if delta:
249
+ parts.append(delta)
250
+ if on_delta:
251
+ on_delta(delta)
252
+ full = "".join(parts).strip()
253
+ if full:
254
+ return full
255
+ logger.warning("Streaming %s/%s yielded empty output β€” falling back", first_provider, first_model)
256
+ except Exception as exc: # noqa: BLE001 ponytail: any SDK/stream hiccup β†’ non-stream fallback
257
+ logger.warning("Streaming %s/%s failed: %s β€” falling back to non-streaming", first_provider, first_model, exc)
258
+ fallback_chain = chain[1:] # first provider already attempted (and failed)
259
+
260
+ if not fallback_chain:
261
+ raise AllProvidersFailed("Streaming provider failed and no fallback provider available")
262
+ return chat(prompt, chain=fallback_chain, system=system,
263
+ max_tokens=max_tokens, temperature=temperature)
264
+
265
+
266
  _FENCE_OPEN_RE = re.compile(r"^```[a-zA-Z0-9_+-]*\s*\n?")
267
 
268
 
agent/prompts.py CHANGED
@@ -270,6 +270,68 @@ def oncall_prompt(files: dict[str, str], error: str) -> str:
270
  return vibe_prompt(files, instruction)
271
 
272
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
273
  def vibe_prompt(files: dict[str, str], instruction: str, premium: bool = False) -> str:
274
  """Build the single-call VibeEngineer prompt.
275
 
 
270
  return vibe_prompt(files, instruction)
271
 
272
 
273
+ def uxmaster_prompt(files: dict[str, str], instruction: str, image_provided: bool = True) -> str:
274
+ """Build the single-call UX MASTER prompt.
275
+
276
+ A vision review: the model sees a SCREENSHOT of the running app (sent alongside this
277
+ prompt as an image) plus the current code, critiques the ACTUAL rendered UI, and returns
278
+ the COMPLETE improved file set. Output adds a `=== VERIFY ===` boolean block (does UX
279
+ Master want a fresh screenshot to confirm the change landed?) between REPLY and the files.
280
+ """
281
+ current_files_text = "\n".join(
282
+ f"=== FILE: {path} ===\n{content}" for path, content in files.items()
283
+ )
284
+ file_list = ", ".join(files.keys())
285
+ screenshot_line = (
286
+ "A SCREENSHOT of the running app is attached to this message β€” review what it ACTUALLY renders."
287
+ if image_provided
288
+ else "No screenshot was provided β€” review the code and infer the rendered UI."
289
+ )
290
+ user_focus = instruction.strip() if instruction and instruction.strip() else (
291
+ "Do a full design pass and raise the visual quality."
292
+ )
293
+ return f"""You are UX MASTER, a senior product designer reviewing a SCREENSHOT of the running app plus its code.
294
+ {screenshot_line}
295
+
296
+ Critique the ACTUAL rendered UI β€” visual hierarchy, spacing/rhythm, contrast/legibility,
297
+ alignment, polish, responsiveness cues β€” and IMPROVE the code accordingly (follow DESIGN_GUIDELINES).
298
+ Don't just restyle blindly: tie each change to something you can see in the screenshot.
299
+
300
+ User's focus for this pass:
301
+ {user_focus}
302
+
303
+ Current app files:
304
+ {current_files_text}
305
+
306
+ {DESIGN_GUIDELINES}
307
+
308
+ RULES:
309
+ - Keep it a working CodeSandbox Sandpack 'react' app (entry /App.js, /App.js imports "./styles.css").
310
+ - Apply real, substantial design improvements β€” not cosmetic tweaks. Keep ALL behavior intact;
311
+ no missing imports, no broken references. The app must mount with NO console errors.
312
+ - Emit EVERY file (modified or unchanged) so the full file set is complete.
313
+
314
+ Output your response in this EXACT format β€” FIRST a REPLY block, THEN a VERIFY block, THEN ALL files.
315
+ No prose before/after, no markdown fences:
316
+
317
+ === REPLY ===
318
+ A first-person, conversational message AS "UX MASTER": what you saw in the screenshot, what you
319
+ changed, and WHY. Talk like a design partner, not a changelog.
320
+ === VERIFY ===
321
+ true
322
+ (this block contains EXACTLY `true` or `false` β€” `true` if you want the user to send a NEW
323
+ screenshot so you can verify your change landed; `false` if you're confident. YOU decide.)
324
+ === FILE: /styles.css ===
325
+ (full file body here)
326
+ === FILE: /App.js ===
327
+ (full file body here)
328
+
329
+ Files to emit (all of them, in dependency order): {file_list}
330
+
331
+ Use EXACTLY the delimiter format above (one REPLY block, one VERIFY block, then each file).
332
+ No other text outside the delimiters."""
333
+
334
+
335
  def vibe_prompt(files: dict[str, str], instruction: str, premium: bool = False) -> str:
336
  """Build the single-call VibeEngineer prompt.
337
 
agent/scaffold.py CHANGED
@@ -5,6 +5,7 @@ SANDPACK_TEMPLATE = "react"
5
 
6
  _FILE_HEADER_RE = re.compile(r"^=== FILE: (.+?) ===$", re.MULTILINE)
7
  _REPLY_HEADER_RE = re.compile(r"^=== REPLY ===\s*$", re.MULTILINE)
 
8
 
9
 
10
  def parse_multi_file_response(raw: str, strip_fences_fn) -> dict[str, str]:
@@ -48,6 +49,31 @@ def parse_reply_and_files(raw: str, strip_fences_fn) -> tuple[str, dict[str, str
48
  return reply, files
49
 
50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  BASE_DEPENDENCIES: dict[str, str] = {
52
  "react": "^18.2.0",
53
  "react-dom": "^18.2.0",
 
5
 
6
  _FILE_HEADER_RE = re.compile(r"^=== FILE: (.+?) ===$", re.MULTILINE)
7
  _REPLY_HEADER_RE = re.compile(r"^=== REPLY ===\s*$", re.MULTILINE)
8
+ _VERIFY_HEADER_RE = re.compile(r"^=== VERIFY ===\s*$", re.MULTILINE)
9
 
10
 
11
  def parse_multi_file_response(raw: str, strip_fences_fn) -> dict[str, str]:
 
49
  return reply, files
50
 
51
 
52
+ def parse_uxmaster_response(
53
+ raw: str, strip_fences_fn
54
+ ) -> tuple[str, bool, dict[str, str]]:
55
+ """Split a UX MASTER response into (reply, needs_verification, files).
56
+
57
+ Same shape as parse_reply_and_files but with an extra `=== VERIFY ===` block (containing
58
+ `true`/`false`) between the REPLY block and the first FILE block. Reuses
59
+ parse_reply_and_files for reply + files, then narrows the reply to end at the VERIFY header
60
+ and reads the boolean. needs_verification defaults False when the VERIFY block is absent.
61
+ """
62
+ reply, files = parse_reply_and_files(raw, strip_fences_fn)
63
+ needs_verification = False
64
+ verify_match = _VERIFY_HEADER_RE.search(raw)
65
+ if verify_match:
66
+ reply_match = _REPLY_HEADER_RE.search(raw)
67
+ if reply_match:
68
+ # parse_reply_and_files swept the VERIFY block into reply β€” trim it back out.
69
+ reply = raw[reply_match.end():verify_match.start()].strip()
70
+ file_match = _FILE_HEADER_RE.search(raw, verify_match.end())
71
+ end = file_match.start() if file_match else len(raw)
72
+ verify_text = raw[verify_match.end():end].strip().lower()
73
+ needs_verification = verify_text.startswith("true")
74
+ return reply, needs_verification, files
75
+
76
+
77
  BASE_DEPENDENCIES: dict[str, str] = {
78
  "react": "^18.2.0",
79
  "react-dom": "^18.2.0",