Skip to main content

GO_SDK_TEMPLATE

Constant GO_SDK_TEMPLATE 

Source
pub const GO_SDK_TEMPLATE: &str = r#"// Chasm Go SDK
// Auto-generated - Do not edit directly
// Version: {{version}}

package chasm

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io"
	"net/http"
	"net/url"
	"os"
	"time"
)

const (
	Version    = "{{version}}"
	APIVersion = "{{api_version}}"
)

// Config holds client configuration
type Config struct {
	BaseURL    string
	APIKey     string
	Timeout    time.Duration
	RetryCount int
}

// DefaultConfig returns default configuration
func DefaultConfig() *Config {
	baseURL := os.Getenv("CHASM_BASE_URL")
	if baseURL == "" {
		baseURL = "{{base_url}}"
	}
	return &Config{
		BaseURL:    baseURL,
		APIKey:     os.Getenv("CHASM_API_KEY"),
		Timeout:    30 * time.Second,
		RetryCount: 3,
	}
}

// Session represents a chat session
type Session struct {
	ID           string    `json:"id"`
	Title        string    `json:"title"`
	Provider     string    `json:"provider"`
	WorkspaceID  *string   `json:"workspace_id,omitempty"`
	MessageCount int       `json:"message_count"`
	TokenCount   int       `json:"token_count"`
	Tags         []string  `json:"tags"`
	Archived     bool      `json:"archived"`
	CreatedAt    time.Time `json:"created_at,omitempty"`
	UpdatedAt    time.Time `json:"updated_at,omitempty"`
}

// Workspace represents a workspace
type Workspace struct {
	ID           string    `json:"id"`
	Name         string    `json:"name"`
	Path         string    `json:"path"`
	Provider     string    `json:"provider"`
	SessionCount int       `json:"session_count"`
	CreatedAt    time.Time `json:"created_at,omitempty"`
}

// Error types
type ChasmError struct {
	Message    string
	StatusCode int
}

func (e *ChasmError) Error() string {
	return fmt.Sprintf("chasm: %s (status %d)", e.Message, e.StatusCode)
}

// Client is the main Chasm client
type Client struct {
	config     *Config
	httpClient *http.Client
	Sessions   *SessionsService
	Workspaces *WorkspacesService
	Harvest    *HarvestService
}

// NewClient creates a new Chasm client
func NewClient(config *Config) *Client {
	if config == nil {
		config = DefaultConfig()
	}
	
	c := &Client{
		config: config,
		httpClient: &http.Client{
			Timeout: config.Timeout,
		},
	}
	
	c.Sessions = &SessionsService{client: c}
	c.Workspaces = &WorkspacesService{client: c}
	c.Harvest = &HarvestService{client: c}
	
	return c
}

func (c *Client) request(method, path string, body interface{}, result interface{}) error {
	u, err := url.Parse(c.config.BaseURL + path)
	if err != nil {
		return err
	}

	var bodyReader io.Reader
	if body != nil {
		b, err := json.Marshal(body)
		if err != nil {
			return err
		}
		bodyReader = bytes.NewReader(b)
	}

	req, err := http.NewRequest(method, u.String(), bodyReader)
	if err != nil {
		return err
	}

	req.Header.Set("Content-Type", "application/json")
	req.Header.Set("User-Agent", fmt.Sprintf("chasm-go/%s", Version))
	if c.config.APIKey != "" {
		req.Header.Set("Authorization", "Bearer "+c.config.APIKey)
	}

	resp, err := c.httpClient.Do(req)
	if err != nil {
		return err
	}
	defer resp.Body.Close()

	if resp.StatusCode >= 400 {
		return &ChasmError{
			Message:    fmt.Sprintf("API error: %s", resp.Status),
			StatusCode: resp.StatusCode,
		}
	}

	if result != nil {
		return json.NewDecoder(resp.Body).Decode(result)
	}
	return nil
}

// Health checks API health
func (c *Client) Health() (map[string]interface{}, error) {
	var result map[string]interface{}
	err := c.request("GET", "/health", nil, &result)
	return result, err
}

// Stats gets statistics
func (c *Client) Stats() (map[string]interface{}, error) {
	var result map[string]interface{}
	err := c.request("GET", "/api/stats", nil, &result)
	return result, err
}

// SessionsService handles session operations
type SessionsService struct {
	client *Client
}

// ListOptions for listing resources
type ListOptions struct {
	Limit       int
	Offset      int
	WorkspaceID string
	Provider    string
	Archived    *bool
}

func (s *SessionsService) List(opts *ListOptions) ([]Session, error) {
	path := "/api/sessions"
	if opts != nil {
		params := url.Values{}
		if opts.Limit > 0 {
			params.Set("limit", fmt.Sprintf("%d", opts.Limit))
		}
		if opts.Offset > 0 {
			params.Set("offset", fmt.Sprintf("%d", opts.Offset))
		}
		if opts.WorkspaceID != "" {
			params.Set("workspace_id", opts.WorkspaceID)
		}
		if opts.Provider != "" {
			params.Set("provider", opts.Provider)
		}
		if opts.Archived != nil {
			params.Set("archived", fmt.Sprintf("%t", *opts.Archived))
		}
		if len(params) > 0 {
			path += "?" + params.Encode()
		}
	}
	
	var result struct {
		Sessions []Session `json:"sessions"`
	}
	err := s.client.request("GET", path, nil, &result)
	return result.Sessions, err
}

func (s *SessionsService) Get(id string) (*Session, error) {
	var session Session
	err := s.client.request("GET", "/api/sessions/"+id, nil, &session)
	return &session, err
}

func (s *SessionsService) Create(title, provider string, workspaceID *string) (*Session, error) {
	body := map[string]interface{}{
		"title":    title,
		"provider": provider,
	}
	if workspaceID != nil {
		body["workspace_id"] = *workspaceID
	}
	var session Session
	err := s.client.request("POST", "/api/sessions", body, &session)
	return &session, err
}

func (s *SessionsService) Delete(id string) error {
	return s.client.request("DELETE", "/api/sessions/"+id, nil, nil)
}

func (s *SessionsService) Search(query string, limit int) ([]Session, error) {
	path := fmt.Sprintf("/api/sessions/search?q=%s&limit=%d", url.QueryEscape(query), limit)
	var result struct {
		Sessions []Session `json:"sessions"`
	}
	err := s.client.request("GET", path, nil, &result)
	return result.Sessions, err
}

// WorkspacesService handles workspace operations
type WorkspacesService struct {
	client *Client
}

func (w *WorkspacesService) List(opts *ListOptions) ([]Workspace, error) {
	path := "/api/workspaces"
	if opts != nil && (opts.Limit > 0 || opts.Offset > 0) {
		params := url.Values{}
		if opts.Limit > 0 {
			params.Set("limit", fmt.Sprintf("%d", opts.Limit))
		}
		if opts.Offset > 0 {
			params.Set("offset", fmt.Sprintf("%d", opts.Offset))
		}
		path += "?" + params.Encode()
	}
	
	var result struct {
		Workspaces []Workspace `json:"workspaces"`
	}
	err := w.client.request("GET", path, nil, &result)
	return result.Workspaces, err
}

func (w *WorkspacesService) Get(id string) (*Workspace, error) {
	var workspace Workspace
	err := w.client.request("GET", "/api/workspaces/"+id, nil, &workspace)
	return &workspace, err
}

// HarvestService handles harvest operations
type HarvestService struct {
	client *Client
}

func (h *HarvestService) Run(providers []string) (map[string]interface{}, error) {
	body := map[string]interface{}{}
	if len(providers) > 0 {
		body["providers"] = providers
	}
	var result map[string]interface{}
	err := h.client.request("POST", "/api/harvest", body, &result)
	return result, err
}

func (h *HarvestService) Status() (map[string]interface{}, error) {
	var result map[string]interface{}
	err := h.client.request("GET", "/api/harvest/status", nil, &result)
	return result, err
}
"#;
Expand description

Go SDK template