aifunc

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2026 License: MIT Imports: 5 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildRetryPrompt

func BuildRetryPrompt(code string, execError string, failedCondition string) string

BuildRetryPrompt constructs a retry prompt for the LLM after a failure.

func BuildSystemPrompt

func BuildSystemPrompt(spec *Spec, language string) string

BuildSystemPrompt constructs the system prompt for the LLM.

func BuildUserPrompt

func BuildUserPrompt(spec *Spec, args map[string]any) string

BuildUserPrompt constructs the user prompt from spec parameters and argument values.

func Execute

func Execute(ctx context.Context, runner codeact.Runner, spec *Spec, model Model, args map[string]any) (*codeact.RunResult, error)

Execute runs the full AI Function flow: prompt building, LLM call, code execution, post-condition validation, and retry. Callers must call SetupRunner before Execute if GoFuncs are used.

func ExtractCode

func ExtractCode(response string) string

ExtractCode extracts code from an LLM response, stripping markdown fences if present. Handles fences at any position in the text (e.g. preceded by explanation).

func InferParamType

func InferParamType(t reflect.Type) string

InferParamType maps a Go reflect.Type to a JSON Schema type string.

func InferSignature

func InferSignature(fn any, language ...string) string

InferSignature returns a human-readable signature string for a Go function. An optional language ("javascript" or "python") maps Go types to target language types. With no language argument, Go type names are used as-is.

func SetupRunner

func SetupRunner(runner codeact.Runner, funcs []GoFuncSpec) error

SetupRunner registers Go functions from the spec with the runner. The runner must implement RegisterAnyFunc(name string, fn any) error.

func ValidatePostConditions

func ValidatePostConditions(ctx context.Context, runner codeact.Runner, resultJSON string, conditions []string) (bool, string, error)

ValidatePostConditions validates post-conditions by running them on the runner. The result JSON is injected as a `result` variable so post-condition expressions can reference it regardless of the original code's variable scoping. Returns (true, "", nil) on success, or (false, failureMessage, nil) on validation failure. Infrastructure errors are returned as the error value.

Types

type GoFuncSpec

type GoFuncSpec struct {
	Name      string
	Fn        any
	Signature string // Human-readable signature for prompts
}

GoFuncSpec defines a Go function exposed to LLM-generated code.

type Message

type Message struct {
	Role    string // "user" or "assistant"
	Content string
}

Message represents a conversation message for the LLM.

type Model

type Model interface {
	Generate(ctx context.Context, systemPrompt string, messages []Message) (string, error)
}

Model is the interface adapters use to call the LLM for code generation.

type ModelFunc

type ModelFunc func(ctx context.Context, systemPrompt string, messages []Message) (string, error)

ModelFunc adapts a plain function to the Model interface.

func (ModelFunc) Generate

func (f ModelFunc) Generate(ctx context.Context, systemPrompt string, messages []Message) (string, error)

Generate implements Model.

type ParamSpec

type ParamSpec struct {
	Name        string
	Type        string // "string", "number", "boolean", "object", "array"
	Description string
}

ParamSpec defines a parameter for an AI Function.

type Spec

type Spec struct {
	Name           string
	Description    string
	Params         []ParamSpec
	GoFuncs        []GoFuncSpec
	PostConditions []string // Validation expressions (JS or Python)
	MaxRetries     int      // Default 3
}

Spec defines an AI Function (framework-independent).

Jump to

Keyboard shortcuts

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