Skip to content

Commit 58ec91f

Browse files
jerelvelardeclaude
andcommitted
fix: edit existing HTML instead of regenerating from scratch
Add edit-vs-regenerate decision logic so the agent modifies previous HTML in place when the user requests changes, instead of always regenerating the entire visualization from scratch. Closes #77 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent f97d1e8 commit 58ec91f

3 files changed

Lines changed: 50 additions & 6 deletions

File tree

apps/agent/main.py

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,15 @@
5353
- Pre-styled form elements (buttons, inputs, sliders look native automatically)
5454
- Pre-built SVG CSS classes for color ramps (.c-purple, .c-teal, .c-blue, etc.)
5555
56-
## Visualization Workflow (MANDATORY)
56+
## New Visualization Workflow (MANDATORY for new widgets)
5757
58-
When producing ANY visual response (widgetRenderer, pieChart, barChart), you MUST
59-
follow this exact sequence:
58+
When producing a NEW visual response (widgetRenderer, pieChart, barChart), you
59+
MUST follow this exact sequence:
6060
6161
1. **Acknowledge** — Reply with 1-2 sentences of plain text acknowledging the
6262
request and setting context for what the visualization will show.
63-
2. **Plan** — Call `plan_visualization` with your approach, technology choice,
64-
and 2-4 key elements. Keep it concise.
63+
2. **Plan** — Call `plan_visualization` with `mode="new"`, your approach,
64+
technology choice, and 2-4 key elements. Keep it concise.
6565
3. **Build** — Call the appropriate visualization tool (widgetRenderer, pieChart,
6666
or barChart).
6767
4. **Narrate** — After the visualization, add 2-3 sentences walking through
@@ -70,6 +70,32 @@
7070
NEVER skip the plan_visualization step. NEVER call widgetRenderer, pieChart, or
7171
barChart without calling plan_visualization first.
7272
73+
If editing an existing widget, follow the Edit workflow below instead.
74+
75+
## Editing Existing Visualizations
76+
77+
When the user asks to modify, tweak, update, or build on top of an existing
78+
visualization, you should EDIT the previous HTML rather than regenerating
79+
from scratch. Look at the `html` parameter from your most recent
80+
widgetRenderer tool call in the conversation history — that is the current
81+
widget.
82+
83+
**Decision rule:**
84+
- If the user's request builds on, modifies, or refines the current widget
85+
→ EDIT mode: use the existing HTML as your starting point
86+
- If the user asks for something conceptually different or unrelated
87+
→ NEW mode: generate fresh HTML from scratch
88+
89+
**Edit workflow:**
90+
1. **Acknowledge** — 1 sentence confirming what you'll change.
91+
2. **Plan** — Call `plan_visualization` with `mode="edit"`, a brief
92+
`approach` describing only the targeted changes, and `key_elements`
93+
listing just the parts being modified (not the entire widget).
94+
3. **Build** — Call widgetRenderer with the FULL updated HTML. Start from
95+
the previous HTML and apply only the requested changes. Do NOT strip
96+
out or rewrite parts that aren't changing.
97+
4. **Narrate** — 1-2 sentences describing what changed.
98+
7399
## Visualization Quality Standards
74100
75101
The iframe has an import map with these ES module libraries — use `<script type="module">` and bare import specifiers:

apps/agent/skills/master-playbook/SKILL.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ User asks a question
4444
+- Is it emotional/personal? -> Warm text response. No visuals needed.
4545
```
4646

47+
### Editing Existing Visuals
48+
49+
When a user asks to tweak, adjust, or build on a previous visualization:
50+
- Start from the previous HTML (from your last widgetRenderer call)
51+
- Make surgical changes — don't rewrite CSS or structure that isn't changing
52+
- Keep the same technology, libraries, and overall architecture
53+
- The edit plan should be 1-2 bullet points, not a full spec
54+
- Narration after an edit should be shorter (1-2 sentences)
55+
4756
### The 3-Layer Response Pattern
4857

4958
Great responses layer information:

apps/agent/src/plan.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,26 @@
55

66
@tool
77
def plan_visualization(
8-
approach: str, technology: str, key_elements: list[str]
8+
approach: str,
9+
technology: str,
10+
key_elements: list[str],
11+
mode: str = "new",
912
) -> str:
1013
"""Plan a visualization before building it. MUST be called before
1114
widgetRenderer, pieChart, or barChart. Outlines the approach, technology
1215
choice, and key elements.
1316
1417
Args:
1518
approach: One sentence describing the visualization strategy.
19+
For edits, describe only the targeted changes.
1620
technology: The primary technology (e.g. "inline SVG", "Chart.js",
1721
"HTML + Canvas", "Three.js", "Mermaid", "D3.js").
1822
key_elements: 2-4 concise bullet points describing what will be built.
23+
For edits, list only the elements being modified.
24+
mode: "new" for a fresh visualization, "edit" for modifying an
25+
existing one.
1926
"""
2027
elements = "\n".join(f" - {e}" for e in key_elements)
28+
if mode == "edit":
29+
return f"Edit Plan: {approach}\nTech: {technology}\nChanges:\n{elements}"
2130
return f"Plan: {approach}\nTech: {technology}\n{elements}"

0 commit comments

Comments
 (0)