Skip to content

Instantly share code, notes, and snippets.

@helloworld
Created October 4, 2024 00:15
Show Gist options
  • Save helloworld/031dd6c70672569d5e6dc24f30a3bf25 to your computer and use it in GitHub Desktop.
Save helloworld/031dd6c70672569d5e6dc24f30a3bf25 to your computer and use it in GitHub Desktop.

How Canvas Works

This is summary (genererated by Exponent) based on a series of experiments and a close examination of the websocket traffic between the client and the AI.

Experiment Setup

We conducted a multi-part experiment to understand Canvas's behavior:

  1. Generate a Python script for sending daily email reports.
  2. Rename a function within the script.
  3. Remove error handling from the script.
  4. Generate an HTML document.

Response Structure Analysis

General Structure

Canvas responses are structured as JSON objects with the following high-level format:

{
  "message": {
    "content": {...},
    "status": "...",
    "end_turn": boolean,
    "weight": number,
    "metadata": {...},
    "recipient": "...",
    "channel": null
  },
  "conversation_id": "...",
  "error": null
}

Key Components

  1. Content: Contains the actual response text or code modifications.
  2. Status: Indicates the progress of the response (e.g., "in_progress", "finished_successfully").
  3. Metadata: Includes crucial information about the model and the nature of the response.
  4. Recipient: Specifies the intended handler for the response.

Metadata Deep Dive

The metadata field provides insights into the AI model and processing:

"metadata": {
  "citations": [],
  "content_references": [],
  "gizmo_id": null,
  "message_type": "next",
  "model_slug": "gpt-4o-canmore",
  "default_model_slug": "gpt-4o-canmore",
  "pad": "...",
  "parent_id": "...",
  "finish_details": {...}
}
  • model_slug: Consistently "gpt-4o-canmore", suggesting a specialized version of GPT-4 for code operations.
  • message_type: Almost always "next", indicating sequential processing.
  • pad: A string of varying length, possibly used for message alignment or encryption.
  • parent_id: Links responses in a conversation thread.

Canvas's File Edit Modeling

Canvas models file edits as a series of "updates" to be applied to the existing code. Each update is structured as follows:

{
  "updates": [
    {
      "pattern": "...",
      "multiple": boolean,
      "replacement": "..."
    }
  ]
}

Components of an Update

  1. Pattern: A regex pattern to match the code to be replaced.
  2. Multiple: A boolean indicating whether the pattern should be applied multiple times.
  3. Replacement: The new code to insert in place of the matched pattern.

File Creation vs. Editing

Canvas handles file creation and editing differently:

  1. File Creation: When creating a new file, Canvas creates a new "textdoc" with a name, type, and contents.

  2. File Editing: For existing files, Canvas uses more targeted updates with specific patterns to match and replace only the relevant parts of the code. When doing more complex edits, Canvas typically uses a single update with a pattern that matches the entire file content (e.g., ".*") and provides the complete new content as the replacement.

Editing Examples

Edits are more interesting to look at than file creations, so let's look at a few examples:

1. Successful Function Renaming

In our experiment to rename a function, Canvas generated the following update:

{
  "updates": [
    {
      "pattern": "def send_email_report\\(\\):",
      "multiple": false,
      "replacement": "def create_and_send_email_report():"
    },
    {
      "pattern": "schedule\\.every\\(\\)\\.day\\.at\\(\"08:00\"\\)\\.do\\(send_email_report\\)",
      "multiple": false,
      "replacement": "schedule.every().day.at(\"08:00\").do(create_and_send_email_report)"
    }
  ]
}

This update successfully renamed the function from send_email_report to create_and_send_email_report and updated its usage in the scheduling code.

2. Removing Error Handling

When asked to remove error handling, Canvas generated:

{
  "updates": [
    {
      "pattern": ".*try:\n(\s*)server = smtplib\.SMTP\(smtp_server, smtp_port\)\n(\s*)server\.starttls\(\)  # Upgrade the connection to a secure encrypted SSL/TLS\n(\s*)server\.login\(sender_email, sender_password\)\n(\s*)server\.send_message\(msg\)\n(\s*)print\(f\"Email sent successfully to .*?\"\)\n(\s*)except Exception as e:\n(\s*)print\(f\"Failed to send email\. Error: .*?\"\)\n(\s*)finally:\n(\s*)server\.quit\(\)",
      "multiple": false,
      "replacement": "server = smtplib.SMTP(smtp_server, smtp_port)\n    server.starttls()  # Upgrade the connection to a secure encrypted SSL/TLS\n    server.login(sender_email, sender_password)\n    server.send_message(msg)\n    print(f\"Email sent successfully to {receiver_email} on {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\")\n    server.quit()"
    }
  ]
}

This update aimed to remove the try-except block but inadvertently matched and replaced a larger portion of the code due to the .* at the beginning of the pattern.

3. More complex edit to an HTML Document

In another example, Canvas generated an update to replace an entire file with new content instead of making a granular edit:

{
  "updates": [
    {
      "pattern": ".*",
      "replacement": "<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n    <meta charset=\"UTF-8\">\n    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n    <title>Josh: Engineer & Great Friend</title>\n    <!-- ... rest of the HTML ... -->\n</html>"
    }
  ]
}

This update showcases Canvas's approach to more complex edits that are not suitable for find/replace regex operations, replacing the entire content with a new HTML document.

Key Observations and Implications

  1. Regex-Based Editing: Canvas heavily relies on regex patterns for identifying and replacing code sections. This approach offers flexibility but requires precise pattern crafting to avoid unintended modifications.

  2. Contextual Understanding: The AI seems to lack a comprehensive understanding of the entire codebase, focusing instead on pattern matching within specified sections.

  3. Incremental Updates: For file editing, Canvas uses targeted, incremental updates, which can be powerful for specific changes but may struggle with complex refactoring.

  4. Complete Rewrites: For file creation or major changes, Canvas opts for complete file replacement rather than incremental edits.

  5. Specialized Model: The consistent use of "gpt-4o-canmore" suggests a model specifically tuned for code-related tasks.

  6. Real-time Interaction: The websocket-based, streaming nature of responses allows for a more interactive coding experience.

Conclusion

ChatGPT's Canvas feature represents a sophisticated approach to AI-assisted code editing. Its use of regex-based pattern matching offers flexibility for both targeted edits and complete file generations. However, it also introduces potential pitfalls, particularly when dealing with complex code structures.

The feature's strength lies in its ability to make precise, localized changes, as demonstrated in the function renaming case. However, its approach to removing error handling and generating new files shows that it can sometimes be overly broad in its modifications.

As AI continues to integrate into coding workflows, understanding these mechanisms becomes crucial. Developers must balance the benefits of AI assistance with the need for precise control and thorough understanding of the suggested changes.

Canvas, while powerful, underscores the continued importance of human oversight in the coding process. As these tools evolve, we can expect more nuanced approaches that better balance comprehensive changes with targeted precision.

This investigation into Canvas provides valuable insights for developers and researchers looking to leverage AI in coding workflows effectively and responsibly. As we continue to push the boundaries of AI-assisted development, such analyses will be crucial for shaping the future of coding practices and tools.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment