server

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package server implements the HTTP proxy server that sits between Claude Code and the Anthropic API.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ContentBlockState

type ContentBlockState struct {
	Index          int
	IsToolUse      bool
	ToolName       string
	ToolID         string
	BufferedEvents []SSEEvent
	PartialJSON    strings.Builder
}

ContentBlockState tracks the state of a single content block being streamed.

type HookHandler

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

HookHandler handles PreToolUse hook requests from Claude Code.

func NewHookHandler

func NewHookHandler(
	engine *trap.Engine,
	callbackHandler *trap.CallbackHandler,
	logger *slog.Logger,
	hookSecret string,
	port int,
) *HookHandler

NewHookHandler creates a HookHandler wired to the trap engine and callback handler.

func (*HookHandler) HandlePreToolUse

func (hh *HookHandler) HandlePreToolUse(w http.ResponseWriter, r *http.Request)

HandlePreToolUse processes a PreToolUse hook request from Claude Code.

type HookOutput

type HookOutput struct {
	HookEventName            string `json:"hookEventName"`
	PermissionDecision       string `json:"permissionDecision"`
	PermissionDecisionReason string `json:"permissionDecisionReason,omitempty"`
}

HookOutput contains the permission decision for Claude Code.

type HookRequest

type HookRequest struct {
	SessionID     string          `json:"session_id"`
	HookEventName string          `json:"hook_event_name"`
	ToolName      string          `json:"tool_name"`
	ToolInput     json.RawMessage `json:"tool_input"`
	ToolUseID     string          `json:"tool_use_id"`
}

HookRequest is the JSON body sent by Claude Code's PreToolUse hook.

type HookResponse

type HookResponse struct {
	HookSpecificOutput *HookOutput `json:"hookSpecificOutput,omitempty"`
}

HookResponse is the JSON response sent back to Claude Code.

type ProxyHandler

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

ProxyHandler forwards HTTP requests to the Anthropic API, intercepts SSE streaming responses to detect and optionally replace bash tool_use commands with trap commands, and relays everything else unchanged.

func NewProxyHandler

func NewProxyHandler(
	anthropicURL string,
	httpClient *http.Client,
	engine *trap.Engine,
	selector *trap.Selector,
	callbackHandler *trap.CallbackHandler,
	apiClient *client.Client,
	logger *slog.Logger,
) *ProxyHandler

NewProxyHandler creates a ProxyHandler that forwards requests to the given Anthropic API base URL and optionally injects traps via the callback handler.

func (*ProxyHandler) HandleProxy

func (ph *ProxyHandler) HandleProxy(w http.ResponseWriter, r *http.Request)

HandleProxy is the main HTTP handler that proxies requests to the Anthropic API. It detects SSE streaming responses and pipes them through the StreamInterceptor; non-streaming responses are checked for tool_use blocks in the JSON body.

type SSEEvent

type SSEEvent struct {
	Event string // event type (message_start, content_block_start, etc.)
	Data  string // JSON data
}

SSEEvent represents a single Server-Sent Event with its event type and JSON data payload.

type Server

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

Server is the AgentsAegis proxy HTTP server that intercepts traffic between Claude Code and the Anthropic API, injecting security awareness traps.

func New

func New(
	cfg *config.Config,
	engine *trap.Engine,
	selector *trap.Selector,
	apiClient *client.Client,
	logger *slog.Logger,
	hookSecret ...string,
) *Server

New creates a new proxy server wired with the trap engine, selector, API client, and configuration.

func (*Server) SetSuperDebug

func (s *Server) SetSuperDebug()

SetSuperDebug disables cooldown and jitter on the hook handler for testing.

func (*Server) Shutdown

func (s *Server) Shutdown(ctx context.Context) error

Shutdown gracefully shuts down the server with the given context deadline.

func (*Server) Start

func (s *Server) Start() error

Start begins listening for requests. It blocks until the server is shut down.

type StreamInterceptor

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

StreamInterceptor parses SSE events from an Anthropic streaming response, detects bash tool_use content blocks, and optionally replaces the command payload with a trap command while preserving valid SSE structure.

func NewStreamInterceptor

func NewStreamInterceptor(
	engine *trap.Engine,
	selector *trap.Selector,
	injectTrapFn TrapInjectionFunc,
	logger *slog.Logger,
) *StreamInterceptor

NewStreamInterceptor creates a StreamInterceptor wired to the trap engine, selector, and an injection function that is called to register and build the trap command whenever a bash tool_use block is selected for trapping.

func (*StreamInterceptor) ProcessEvent

func (si *StreamInterceptor) ProcessEvent(event SSEEvent) ([]SSEEvent, error)

ProcessEvent takes a single SSE event and returns zero or more events to emit. When buffering a bash tool_use block the return slice may be empty; on flush it may contain many events at once.

type TrapInjectionFunc

type TrapInjectionFunc func(originalCmd string, template *trap.Template, toolUseID string) (trapCmd string)

TrapInjectionFunc is called when a trap should be injected. It receives the original command and the selected template, registers the trap, and returns the actual trap command string to embed in the stream.

Jump to

Keyboard shortcuts

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