Skip to main content

API Integration

HAI Build Code Generator exposes a public API that allows other VS Code extensions to programmatically control Cline tasks. This enables powerful integrations and automation workflows.

Overview

The HAI API provides methods to:
  • Start new coding tasks with optional images
  • Send messages to active tasks
  • Simulate button interactions
  • Control task lifecycle

Getting Started

1. Install the Type Definitions

Copy the type definition file from the HAI Build repository:
Or create it manually:

2. Add Extension Dependency

Ensure HAI Build is activated before your extension by adding it to package.json:

3. Access the API

Get the API instance in your extension’s activate function:

API Reference

startNewTask()

Starts a new task with an optional initial message and images. Signature:
Parameters:
  • task (optional): Initial task description
  • images (optional): Array of image data URIs in base64 format
Example:
Behavior:
  1. Clears any existing task
  2. Updates the webview state
  3. Initializes a new task with the provided prompt
  4. Opens the HAI Build sidebar if not already visible

sendMessage()

Sends a message to the currently active task. Signature:
Parameters:
  • message (optional): Message text to send
  • images (optional): Array of image data URIs
Example:
Error Handling: If no task is active, logs an error but does not throw:

pressPrimaryButton()

Simulates clicking the primary button in the chat interface (e.g., “Save”, “Proceed”). Signature:
Example:
Use Cases:
  • Auto-approve file changes in automation workflows
  • Proceed with task execution programmatically
  • Confirm user actions in headless scenarios

pressSecondaryButton()

Simulates clicking the secondary button (e.g., “Reject”, “Cancel”). Signature:
Example:

Usage Patterns

Pattern 1: Task Automation

Create automated workflows that run Cline tasks:

Pattern 2: Context-Aware Commands

Create VS Code commands that pass context to Cline:

Pattern 3: Screenshot-Based Tasks

Capture screenshots and send to Cline:

Pattern 4: Interactive Approval Flow

Implement custom approval logic:

Pattern 5: Git Integration

Integrate with git workflows:

Complete Integration Example

Here’s a full extension that integrates with HAI Build:
package.json:

Best Practices

Error Handling

Always check if the extension and API are available:

Image Data URIs

Ensure images are properly encoded:

Task State Management

Keep track of active tasks to avoid conflicts:

Troubleshooting

Extension Not Found

API Not Available

Task Not Responding

If sendMessage() doesn’t work, ensure a task is active:

Next Steps

CLI Usage

Use Cline from the command line

Custom Experts

Create domain-specific experts