lua

package
v0.0.1-alpha Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package lua exposes hex/ai's default agent to Lua scripts via a gopher-lua module named "agent".

The module is loaded by a service provider that resolves the shared *hex/lua.Environment from the container (bound by hex/lua/provider), resolves the default ai.Agent, and calls env.PreloadModule("agent", ...). Consumers add the provider to their boot.go alongside the base Lua provider:

provider.Lua(),        // hex/lua/provider
provider.LuaAI(),      // hex/ai/lua/provider — this package

After boot, any Lua script running in the environment can:

local agent = require("agent")

-- Simple ask, no conversation history:
local response, err = agent.ask("session-1", "Summarise this incident.")

-- With options — subset of tools, temperature, max tokens:
local response, err = agent.ask(thread_ts, event.text, {
    tools       = { "get_incident", "post_slack" },
    temperature = 0.3,
    max_tokens  = 500,
})

-- Introspection:
for _, name in ipairs(agent.tools()) do
    print("available:", name)
end

-- Reset a conversation:
agent.forget(thread_ts)

Response tables include:

response.text         string        -- flattened assistant text
response.usage.input_tokens        int
response.usage.output_tokens       int
response.usage.total_tokens        int
response.model        string        -- (best-effort from steps)

Errors return (nil, "message").

Concurrency:

*lua.LState is not thread-safe. When multiple goroutines may call
agent.ask() against the same environment (event bus handlers,
web request handlers, etc.), pass Bindings.Mutex so this package
guards the Generate call. See PLAT-3545 notes.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bindings

type Bindings struct {
	// Agent is the fantasy.Agent to call. Required.
	Agent ai.Agent

	// Registry is consulted for tool introspection (agent.tools()) and
	// for validating ActiveTools passed from Lua. Optional; when nil
	// the module trusts whatever tool names Lua supplies.
	Registry ai.ToolRegistry

	// Store persists multi-turn conversation history keyed by the
	// first argument to agent.ask. Optional; when nil each call is a
	// fresh single-turn interaction (Prompt only, no Messages).
	Store ai.ConversationStore

	// Mutex, when non-nil, is locked around every Generate call. Use
	// this to serialise concurrent agent.ask invocations that share
	// the same *lua.LState (event bus subscribers, web handlers).
	Mutex *sync.Mutex

	// Context, when non-nil, is used for every agent call. Defaults
	// to context.Background(). Consumers who need per-call ctx (e.g.
	// from an HTTP request) install a fresh Bindings per request or
	// stash the ctx in the LState registry themselves.
	Context context.Context
}

Bindings configures the 'agent' module. Constructed and installed by hex/ai/lua/provider; callers who want to wire the module manually (outside the provider lifecycle) build one directly and call Loader.

func (*Bindings) Loader

func (b *Bindings) Loader(L *glua.LState) int

Loader is the gopher-lua LGFunction registered against env.PreloadModule("agent", b.Loader).

Directories

Path Synopsis
Package provider is the service provider that installs the hex/ai 'agent' Lua module into the shared hex/lua environment.
Package provider is the service provider that installs the hex/ai 'agent' Lua module into the shared hex/lua environment.

Jump to

Keyboard shortcuts

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