Files
humanlayer/claudecode-go
Sundeep Malladi 88da1b7076 Sundeep/eng 1672 seeing situations where taskgroup loaders remain displayed (#381)
* fix: Handle Task tool array content format in claudecode-go (ENG-1672)

Task tools send content as an array format while regular tools use string format,
causing unmarshal failures that silently drop Task tool results. This left
spinners running indefinitely in the WUI.

Changes:
- Add custom ContentField type with UnmarshalJSON to handle both formats
- Update Content struct to use ContentField instead of string
- Add error logging for unmarshal failures in parseStreamingJSON
- Update hld references to use content.Content.Value
- Add comprehensive tests for both content formats
- Update WUI TaskGroup styling for better focus indication

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

* mct

---------

Co-authored-by: Claude <noreply@anthropic.com>
2025-07-30 09:06:50 -07:00
..
2025-06-06 15:49:19 -07:00
2025-05-29 10:07:26 -07:00

Claude Code Go SDK (Experimental)

A Go SDK for programmatically interacting with Claude Code (Anthropic's AI coding assistant).

Installation

go get github.com/humanlayer/humanlayer/claudecode-go

Prerequisites

  • Claude Code CLI must be installed and available in your PATH
  • Valid Anthropic API key configured

Quick Start

package main

import (
    "fmt"
    "log"

    claudecode "github.com/humanlayer/humanlayer/claudecode-go"
)

func main() {
    // Create client
    client, err := claudecode.NewClient()
    if err != nil {
        log.Fatal(err)
    }

    // Run a simple query
    result, err := client.LaunchAndWait(claudecode.SessionConfig{
        Query: "Write a hello world function in Go",
    })
    if err != nil {
        log.Fatal(err)
    }

    fmt.Println(result.Result)
}

Streaming Example

// Launch with streaming output
session, err := client.Launch(claudecode.SessionConfig{
    Query:        "Build a REST API",
    Model:        claudecode.ModelSonnet,
    OutputFormat: claudecode.OutputStreamJSON,
})

// Process events as they arrive
for event := range session.Events {
    switch event.Type {
    case "assistant":
        fmt.Println("Claude:", event.Message.Content[0].Text)
    case "result":
        fmt.Printf("Done! Cost: $%.4f\n", event.Result.CostUSD)
    }
}

MCP Integration

// Configure MCP servers
mcpConfig := &claudecode.MCPConfig{
    MCPServers: map[string]claudecode.MCPServer{
        "approvals": {
            Command: "npx",
            Args:    []string{"humanlayer", "mcp", "claude_approvals"},
        },
    },
}

// Launch with approval handling
session, err := client.Launch(claudecode.SessionConfig{
    Query:                "Deploy to production",
    MCPConfig:            mcpConfig,
    PermissionPromptTool: "mcp__approvals__request_permission",
    AllowedTools:         []string{"mcp__approvals__*"},
})

Features

  • Type-safe configuration - Build configurations with Go structs
  • Streaming support - Real-time event processing
  • MCP integration - Add approval workflows and custom tools
  • Session management - Resume previous conversations
  • Process control - Full control over Claude subprocess

Output Formats

  • OutputText - Plain text output (default)
  • OutputJSON - Structured JSON with metadata
  • OutputStreamJSON - Real-time streaming JSON events

Configuration Options

type SessionConfig struct {
    // Core
    Query     string
    SessionID string // Resume existing session

    // Model
    Model Model // ModelOpus or ModelSonnet

    // Output
    OutputFormat OutputFormat

    // MCP
    MCPConfig            *MCPConfig
    PermissionPromptTool string

    // Control
    MaxTurns           int
    WorkingDir         string
    SystemPrompt       string
    AppendSystemPrompt string
    AllowedTools       []string
    DisallowedTools    []string
    Verbose            bool
}

Error Handling

The SDK provides detailed error information:

result, err := client.LaunchAndWait(config)
if err != nil {
    // Handle launch/execution errors
    log.Fatal(err)
}

if result.IsError {
    // Handle Claude-reported errors
    fmt.Printf("Error: %s\n", result.Error)
}

Integration with HumanLayer

This SDK integrates seamlessly with HumanLayer for approval workflows:

  1. Configure the HumanLayer MCP server in your MCPConfig
  2. Set appropriate permission prompt tool
  3. Handle approvals through HumanLayer's TUI or API

License

MIT