serve

package
v0.4.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 6, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// AnthropicAPIURL is the default Anthropic models endpoint.
	AnthropicAPIURL = "https://api.anthropic.com/v1/models"
)
View Source
const DefaultPort = 8484

DefaultPort is the default listen port for devcell serve.

Variables

This section is empty.

Functions

func AuthMiddleware

func AuthMiddleware(secret string, next http.Handler) http.Handler

AuthMiddleware returns a handler that checks the Authorization: Bearer <secret> header. If secret is empty, all requests are allowed (no auth).

func DefaultCredentialsPath

func DefaultCredentialsPath() string

DefaultCredentialsPath returns the default path to Claude's credentials.

func GenerateAPIKey

func GenerateAPIKey() string

GenerateAPIKey creates a random API key for use when none is configured.

func NewChatHandler

func NewChatHandler(exec Executor) http.Handler

NewChatHandler returns an http.Handler for POST /v1/chat/completions.

func NewModelsHandler

func NewModelsHandler(lookPath LookPathFunc, ac AnthropicClient) http.Handler

NewModelsHandler returns an http.Handler for GET /v1/models.

func ReadClaudeCredentials

func ReadClaudeCredentials(path string) string

ReadClaudeCredentials reads the OAuth access token from Claude's credentials file.

Types

type AnthropicClient

type AnthropicClient interface {
	FetchModels() ([]ModelInfo, error)
}

AnthropicClient abstracts Anthropic API calls for testability.

type ChatChoice

type ChatChoice struct {
	Index        int         `json:"index"`
	Message      ChatMessage `json:"message"`
	FinishReason string      `json:"finish_reason"`
}

ChatChoice is a single choice in the response.

type ChatMessage

type ChatMessage struct {
	Role    string `json:"role"`
	Content string `json:"content"`
}

ChatMessage is an OpenAI-compatible message.

type ChatRequest

type ChatRequest struct {
	Model    string        `json:"model"`
	Messages []ChatMessage `json:"messages"`
}

ChatRequest is the OpenAI-compatible chat completions request.

type ChatResponse

type ChatResponse struct {
	ID      string       `json:"id"`
	Object  string       `json:"object"`
	Created int64        `json:"created"`
	Model   string       `json:"model"`
	Choices []ChatChoice `json:"choices"`
	Usage   ChatUsage    `json:"usage"`
}

ChatResponse is the OpenAI-compatible chat completions response.

type ChatUsage

type ChatUsage struct {
	PromptTokens     int `json:"prompt_tokens"`
	CompletionTokens int `json:"completion_tokens"`
	TotalTokens      int `json:"total_tokens"`
}

ChatUsage tracks token usage (stubbed for now).

type ExecResult

type ExecResult struct {
	Stdout   string
	Stderr   string
	ExitCode int
}

ExecResult holds the output of an agent execution.

type Executor

type Executor interface {
	Run(agent, prompt, model string) ExecResult
}

Executor runs an agent command and returns the result.

type LookPathFunc

type LookPathFunc func(name string) (string, error)

LookPathFunc matches exec.LookPath signature.

type ModelInfo

type ModelInfo struct {
	ID      string `json:"id"`
	Object  string `json:"object"`
	Created int64  `json:"created"`
	OwnedBy string `json:"owned_by"`
}

ModelInfo represents a single model in the OpenAI /v1/models response.

func DiscoverModels

func DiscoverModels(lookPath LookPathFunc, ac AnthropicClient) []ModelInfo

DiscoverModels probes for installed agent binaries and returns available models. When claude is found, tries the Anthropic API first (via credentials), falls back to hardcoded aliases.

func FetchAnthropicModels

func FetchAnthropicModels(baseURL, token string) ([]ModelInfo, error)

FetchAnthropicModels hits the Anthropic API to get available models. Returns nil, nil if token is empty (no-op).

type ModelsResponse

type ModelsResponse struct {
	Object string      `json:"object"`
	Data   []ModelInfo `json:"data"`
}

ModelsResponse is the OpenAI-compatible /v1/models response.

type RealAnthropicClient

type RealAnthropicClient struct {
	CredentialsPath string // path to .credentials.json
	APIURL          string // override for testing; defaults to AnthropicAPIURL
}

RealAnthropicClient reads credentials and hits the Anthropic API.

func (*RealAnthropicClient) FetchModels

func (c *RealAnthropicClient) FetchModels() ([]ModelInfo, error)

FetchModels reads the Claude OAuth token and fetches models from the Anthropic API.

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server is the devcell HTTP API server.

func NewServer

func NewServer(exec Executor, port int) *Server

NewServer creates a Server. Use port=0 to let the OS pick a free port. Uses exec.LookPath for model discovery and RealAnthropicClient by default.

func (*Server) APIKey

func (s *Server) APIKey() string

APIKey returns the configured API key.

func (*Server) SetAPIKey

func (s *Server) SetAPIKey(key string)

SetAPIKey sets the API key for bearer auth. Empty disables auth.

func (*Server) SetAnthropicClient

func (s *Server) SetAnthropicClient(ac AnthropicClient)

SetAnthropicClient overrides the Anthropic API client (for testing).

func (*Server) SetLookPath

func (s *Server) SetLookPath(fn LookPathFunc)

SetLookPath overrides the binary discovery function (for testing).

func (*Server) Start

func (s *Server) Start(ctx context.Context) (addr string, errCh chan error)

Start begins listening and returns the address and an error channel. The server shuts down when ctx is cancelled.

type ShellExecutor

type ShellExecutor struct{}

ShellExecutor runs agent binaries as subprocesses.

func (*ShellExecutor) Run

func (e *ShellExecutor) Run(agent, prompt, model string) ExecResult

Run executes the agent binary with the given prompt and optional model.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL