scripting

package
v2.2.1 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2026 License: AGPL-3.0 Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ToolInputScope  = "tool_input"
	ToolOutputScope = "tool_output"
)

Filter scopes recorded against compliance events raised by tool filters.

View Source
const (
	ToolInputBlockedEvent  = "tool_input_blocked"
	ToolOutputBlockedEvent = "tool_output_blocked"
)

Event types synthesised when a filter blocks without reporting an event of its own, so that every block is auditable.

View Source
const ToolBlockedMessage = "blocked by policy"

ToolBlockedMessage is what a caller is told when a governance filter stops a tool call, on every transport. It is deliberately generic and identical for input and output blocks: the filter's own name and reason go to the logs and to the compliance event, so a caller cannot probe the filters to map the governance configuration or learn whether the downstream tool was reached.

Variables

This section is empty.

Functions

func CountAssistantMessages

func CountAssistantMessages(messages []llms.MessageContent) int

CountAssistantMessages counts the number of assistant messages

func CountUserMessages

func CountUserMessages(messages []llms.MessageContent) int

CountUserMessages counts the number of user messages

func GetAllText

func GetAllText(messages []llms.MessageContent) string

GetAllText concatenates all text content from all messages

func GetAllUserMessages

func GetAllUserMessages(messages []llms.MessageContent) []string

GetAllUserMessages returns all user message contents

func GetLastUserMessage

func GetLastUserMessage(messages []llms.MessageContent) string

GetLastUserMessage extracts the content of the last user message

func GetMessagesByRole

func GetMessagesByRole(messages []llms.MessageContent, role llms.ChatMessageType) []string

GetMessagesByRole returns all messages with the specified role

func GetSystemPrompt

func GetSystemPrompt(messages []llms.MessageContent) string

GetSystemPrompt extracts the content of the system message

func HasImages

func HasImages(messages []llms.MessageContent) bool

HasImages checks if any message contains image content

func MessageCount

func MessageCount(messages []llms.MessageContent) int

MessageCount returns the total number of messages

func RecordComplianceEvents

func RecordComplianceEvents(ctx context.Context, output *ScriptOutput, filterName, filterScope string, appID, userID, llmID uint, vendor, modelName string)

RecordComplianceEvents converts script output compliance events to model events enriched with context from the filter execution site, then records them asynchronously.

func RunToolInputFilters

func RunToolInputFilters(
	ctx context.Context,
	filters []models.Filter,
	service services.ServiceInterface,
	args ToolCallArgs,
	id ToolFilterIdentity,
) (ToolCallArgs, *ToolBlock, error)

RunToolInputFilters executes the request-side filters attached to a tool against the arguments bound for it, returning the arguments to actually call the tool with. A non-nil ToolBlock means the tool must not be called.

Types

type ComplianceEventOutput

type ComplianceEventOutput = engine.ComplianceEventOutput

ComplianceEventOutput represents a compliance event reported by a filter script.

type FilterRunner

type FilterRunner interface {
	RunScript(input *ScriptInput, serviceRef services.ServiceInterface) (*ScriptOutput, error)
}

FilterRunner executes one filter, whatever its kind, against the same input and with the same output contract as a script. Every call site that runs filters goes through NewFilterRunner, so a guardrail filter is attached, scoped, streamed, audited and synced to edges exactly like a script filter.

func NewFilterRunner

func NewFilterRunner(filter *models.Filter) FilterRunner

NewFilterRunner returns the runner for a filter: the Tengo script runner for a script filter, the guardrail runner for a guardrail filter.

type ScriptInput

type ScriptInput = engine.ScriptInput

ScriptInput provides rich context to scripts including messages, metadata, and vendor info

type ScriptOutput

type ScriptOutput = engine.ScriptOutput

ScriptOutput represents the result of script execution

type ScriptRunner

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

ScriptRunner manages script execution with thread safety

func NewScriptRunner

func NewScriptRunner(source []byte) *ScriptRunner

NewScriptRunner creates a new script runner

func (*ScriptRunner) RunScript

func (sr *ScriptRunner) RunScript(input *ScriptInput, serviceRef services.ServiceInterface) (output *ScriptOutput, err error)

RunScript executes a filter script and converts any panic raised by the script engine into an ordinary error.

Tengo panics instead of returning an error for faults it does not model — integer divide by zero being the common one. Filter scripts run on the caller's own goroutine, so an unrecovered panic unwinds through the HTTP handler and takes down the whole process: the AI Studio API server, or a gateway serving every other tenant's traffic. Recovering here means each call site's existing error handling applies instead, so a malformed script costs one failed request rather than the process.

The build-tagged runScript implementations hold the actual behaviour: a pass-through in CE, the Tengo VM in enterprise builds.

type ServiceInterface

type ServiceInterface = services.ServiceInterface

ServiceInterface allows scripts to interact with system services

type ToolBlock

type ToolBlock struct {
	FilterName string
	Reason     string
}

ToolBlock describes a tool call halted by a filter. The reason is for logs and compliance records only - it is deliberately never returned to the caller, which sees a generic refusal.

func RunToolOutputFilters

func RunToolOutputFilters(
	ctx context.Context,
	filters []models.Filter,
	service services.ServiceInterface,
	body string,
	id ToolFilterIdentity,
) (string, *ToolBlock, error)

RunToolOutputFilters executes the response-side filters attached to a tool against the body it returned, returning the body to hand back to the caller. A non-nil ToolBlock means the body must not be returned.

func (*ToolBlock) Error

func (b *ToolBlock) Error() string

type ToolCallArgs

type ToolCallArgs struct {
	OperationID string                 `json:"operation_id"`
	Parameters  map[string][]string    `json:"parameters"`
	Payload     map[string]interface{} `json:"payload"`
	Headers     map[string][]string    `json:"headers"`
}

ToolCallArgs is the request shape a tool input filter sees, and may rewrite. It mirrors the body of the REST tool endpoint so that REST, MCP and chat tool calls all present scripts with a single contract.

type ToolFilterIdentity

type ToolFilterIdentity struct {
	AppID     uint
	UserID    uint
	ToolID    uint
	ToolName  string
	SessionID string
	CallID    string
	IsChat    bool
}

ToolFilterIdentity carries the call context a script can inspect and that compliance events are attributed to. Every field is best effort: the MCP path has no app when the caller authenticated with an OAuth bearer token, and the REST path has no chat session.

Directories

Path Synopsis
Package engine is the hook through which the enterprise edition supplies the filter script runtime, and the types scripts exchange with it.
Package engine is the hook through which the enterprise edition supplies the filter script runtime, and the types scripts exchange with it.

Jump to

Keyboard shortcuts

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