codemode

package
v2.1.17 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateTypes

func GenerateTypes(tools []Tool) string

GenerateTypes produces TypeScript type declarations from registered tools. Output is a string suitable for injecting into LLM system prompts.

func Normalize

func Normalize(code string) (string, error)

Normalize cleans LLM-generated code for execution. Handles 4 cases: 1. Markdown fences: strips them 2. Async arrow functions: passes through as-is 3. Named function definitions: appends function call 4. Bare expressions: prepends return to last line if needed

func Transpile

func Transpile(code string) (string, error)

Transpile converts modern JavaScript to ES2015 compatible code using esbuild. Returns the original code unchanged if transpilation fails (best-effort).

Types

type ExecuteResult

type ExecuteResult struct {
	Value   string           // JSON-encoded return value
	Console []string         // console.log/warn/error output
	Calls   []ToolCallRecord // tool call records
}

ExecuteResult holds the output of a codemode execution.

type Executor

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

Executor runs sandboxed JavaScript with injected tool bindings.

func NewExecutor

func NewExecutor(opts ...ExecutorOption) *Executor

NewExecutor creates a new executor with optional configuration.

func (*Executor) Execute

func (e *Executor) Execute(ctx context.Context, code string, tools []Tool) (*ExecuteResult, error)

Execute runs code in a fresh goja sandbox with injected tools.

type ExecutorOption

type ExecutorOption func(*Executor)

ExecutorOption configures an Executor.

func WithCallTimeout

func WithCallTimeout(d time.Duration) ExecutorOption

WithCallTimeout sets the timeout for individual tool calls (default 30s).

func WithMaxCodeSize

func WithMaxCodeSize(size int64) ExecutorOption

WithMaxCodeSize sets the maximum code size (default 64KB).

func WithMaxToolCalls

func WithMaxToolCalls(max int) ExecutorOption

WithMaxToolCalls sets the maximum tool calls per execution (default 20).

type Registry

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

Registry holds registered tools.

func NewRegistry

func NewRegistry() *Registry

NewRegistry returns an empty tool registry.

func (*Registry) Get

func (r *Registry) Get(name string) *Tool

Get returns a pointer to a copy of the tool, or nil if not found.

func (*Registry) List

func (r *Registry) List() []Tool

List returns all registered tools sorted by name.

func (*Registry) Register

func (r *Registry) Register(t Tool)

Register adds a tool to the registry. Panics if the tool name is already registered.

func (*Registry) Search

func (r *Registry) Search(query string) []Tool

Search returns tools matching the query (case-insensitive substring on name and description).

func (*Registry) Tools

func (r *Registry) Tools() []Tool

Tools returns a slice of all registered tools for passing to Executor.Execute().

type Tool

type Tool struct {
	Name        string         `json:"name"`
	Description string         `json:"description"`
	Func        ToolFunc       `json:"-"`      // from executor.go
	Schema      map[string]any `json:"schema"` // JSON schema for type generation
	Returns     string         `json:"-"`      // TypeScript return type (e.g. "ExploreResult"), empty = "any"
}

Tool describes a registered tool for codemode.

type ToolCallRecord

type ToolCallRecord struct {
	Tool     string
	Args     map[string]any
	Duration time.Duration
	Error    string // empty if successful
}

ToolCallRecord tracks a single tool invocation.

type ToolFunc

type ToolFunc func(args map[string]any) (any, error)

ToolFunc is the callback signature for registered tools.

Jump to

Keyboard shortcuts

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