-
Notifications
You must be signed in to change notification settings - Fork 17
feat: add Canvas feature for AI generated webpages #116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
esafwan
wants to merge
24
commits into
develop
Choose a base branch
from
feature/canvas
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- Add validate() method for slug sanitization - Add after_insert() hook for auto-directory creation - Add on_update() hook to ensure directory exists - Add _ensure_canvas_directory() method to copy starter template - Add reset_to_starter() API endpoint for reset functionality - Add standalone hook functions for Frappe doc events This enables automatic canvas directory creation and starter template copying when a Canvas DocType is created or updated.
- Add canvas tool injection in AgentManager when canvas_slug in context - Modify create_canvas_tools() to accept canvas_slug parameter - Update run_agent_sync() to accept context and persist_conversation params - Fix file path references: all use agents.md (lowercase) consistently - Update TODO with reset button wiring task Canvas tools (read_canvas_file, write_canvas_files, validate_canvas) are now automatically available to agents when running in canvas context.
…API calls - Fix sandbox security warning: remove allow-same-origin, keep only allow-scripts - Fix JavaScript syntax error: remove duplicate function definitions - Fix send button: add provider and model parameters to run_agent_sync call - Clean up loadCanvasMetadata() function (remove duplicate code) - Clean up sendMessage() function (remove duplicate code) - Improve error handling and loading indicators Issues resolved: 1. Sandbox warning about allow-scripts + allow-same-origin 2. SyntaxError: Unexpected token 'function' 3. Send button not working (missing provider/model params)
- Create canvas_renderer.py with get_context() function - Create canvas_renderer.html template - Add route /canvas/<slug> to hooks.py - Renderer reads index.html from canvas directory - Automatically injects separate CSS/JS files if they exist - Handles errors gracefully with proper 404 responses This enables the iframe to load canvas previews at /canvas/<slug>
- Create canvas_edit.py context handler for portal page - Include frappe-web.bundle.js script in HTML - Initialize frappe object and CSRF token from cookies - Add proper HTML structure with head/body tags - Context handler validates canvas exists and provides metadata This fixes 'frappe is not defined' error in portal page
- Add callFrappeMethod helper that works with or without frappe.call - Fallback to fetch API if frappe.call is not available - Properly handle CSRF token in both cases - Use defer attribute for script loading - Add console logging for debugging This ensures canvas editor works even if frappe-web.bundle.js doesn't load properly
- Remove frappe-web.bundle.js dependency (was 404) - Use pure fetch API with CSRF token from cookies - Better error handling with detailed error messages - Simpler, more reliable approach for portal pages This fixes the 404 bundle error and 400 BAD REQUEST API errors
- Add csrf_token to context in canvas_edit.py - Inject csrf_token into HTML template via window.csrf_token - Update getCSRFToken() to use window.csrf_token first, then cookie fallback - Add warning if CSRF token is missing This fixes the 400 BAD REQUEST error by ensuring CSRF token is available
- Remove window.csrf_token injection (was causing mismatch) - Read CSRF token directly from 'csrf_token' cookie - Add better error handling and logging - Change credentials to 'include' for proper cookie handling The issue was that template context token didn't match session token. Now reading directly from the session cookie like Frappe expects.
- Use {{ frappe.session.csrf_token }} directly in template
- This is the standard Frappe way per Jinja API docs
- Remove manual csrf_token context setting
- Simplify getCSRFToken() to just read window.csrf_token
This matches the documented Frappe Jinja API approach.
Changes: 1. Added 3 new built-in tool types to Agent Tool Function DocType: - Read Canvas File - Write Canvas Files - Validate Canvas 2. Implemented built-in handlers in sdk_tools.py: - handle_read_canvas_file() - handle_write_canvas_files() - handle_validate_canvas() - All with comprehensive debug logging 3. Added debug logging to canvas_api.py: - Logs agent tools loaded from DB - Tracks context passing 4. Tools now work like other built-in types (Get Document, etc.) instead of requiring Custom Function configuration This eliminates the need for manual Custom Function setup and ensures Canvas tools are recognized by the LLM provider.
…md+Enter shortcut
- Update huf/www/canvas/website/index.html to feature a Pacman-themed landing page. - Add huf/www/canvas/website/index.js with basic Pacman character movement logic. - Create huf/www/canvas/netflix directory with a complete Calculator implementation. - Add server-side context in huf/www/canvas/netflix/index.py. - Add developer guidelines in huf/www/canvas/netflix/AGENTS.md.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Adds the Canvas feature that enables AI agents to create and edit interactive web artifacts (HTML/CSS/JS) through a visual editor, similar to Claude's Artifacts or Gemini Canvas.
What Was Done
1. Canvas DocType
File:
huf/huf/doctype/canvas/Created Canvas DocType to manage web artifacts:
title,slug,agent,description,allow_editingwww/canvas/<slug>/2. Canvas Tools (Agent Tools)
Files:
huf/ai/canvas_tools.py,huf/ai/sdk_tools.pyThree built-in tools for agents to manipulate canvas files:
a) Read Canvas File
b) Write Canvas Files
c) Validate Canvas
3. Canvas API
File:
huf/ai/canvas_api.pyTwo API endpoints:
get_canvas_metadata(slug)- Returns canvas and agent infosend_canvas_message(slug, message)- Sends user message to agent, agent edits canvas4. Editor UI
File:
huf/www/canvas_edit/Portal page with split-pane interface:
/canvas/edit/<slug>Note: HTML/CSS structure complete (577 lines), JavaScript needs implementation.
5. Canvas Renderer
File:
huf/www/canvas_renderer.pyServes canvas HTML files:
/canvas/<slug>6. Examples
Added two example canvases:
www/canvas/netflix/) - Functional calculator with dark themewww/canvas/website/) - Vintage game landing pageHow to Use
1. Create a Canvas
This creates directory:
www/canvas/my-web-app/with starter template.2. Edit Canvas
Navigate to:
/canvas/edit/my-web-appChat with agent:
Agent uses tools to read/write files, preview updates automatically.
3. View Canvas
Navigate to:
/canvas/my-web-appSee the rendered artifact (HTML with CSS/JS injected).
How to Test
Test 1: Create Canvas
Test 2: Test Canvas Tools
Test 3: View Example Canvases
Test 4: Editor UI (Manual)
Note: Chat functionality requires JavaScript implementation (currently placeholder).
Test 5: API Endpoints
Security
sandbox="allow-scripts"allow_editingflagKnown Limitations
JavaScript Implementation: Editor UI JavaScript is placeholder (1 line)
Agent Configuration: Currently hardcoded to "canvas" agent
No Version History: Canvas edits are not versioned yet
Integration
Canvas tools work alongside:
Tools are automatically injected when
canvas_slugis in context.Routes Added
Migration
Checklist