base

package
v0.1.12 Latest Latest
Warning

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

Go to latest
Published: Jun 4, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package base provides the shared runtime struct and core execution methods used by both the local and temporal runtime backends. It has no dependency on any backend-specific SDK (no Temporal, no workflow/activity imports).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyLLMSampling

func ApplyLLMSampling(s *types.LLMSampling, req *interfaces.LLMRequest)

ApplyLLMSampling copies non-zero sampling fields from s onto req. A nil sampling value is a no-op.

func CloneLLMUsage

func CloneLLMUsage(u *interfaces.LLMUsage) *interfaces.LLMUsage

CloneLLMUsage returns a shallow copy of u, or nil when u is nil.

func FindToolByName

func FindToolByName(tools []interfaces.Tool, toolName string) (interfaces.Tool, bool)

FindToolByName returns the first tool whose Name() matches toolName.

func FormatRetrieverDocs

func FormatRetrieverDocs(docs []interfaces.Document) string

FormatRetrieverDocs formats a list of documents for injection into the LLM system prompt. Each entry is rendered as "[N] content\n(source: s, score: 0.XX)\n\n".

func MergeLLMUsage

func MergeLLMUsage(acc, add *interfaces.LLMUsage) *interfaces.LLMUsage

MergeLLMUsage accumulates add into acc and returns the result. Either argument may be nil; when both are nil, nil is returned.

func SubAgentQuery

func SubAgentQuery(args map[string]any) string

SubAgentQuery extracts the query string from a sub-agent tool call's args map.

Types

type AuthorizeResult

type AuthorizeResult struct {
	Allowed bool
	Reason  string
}

AuthorizeResult is the outcome of a programmatic tool authorization check. When Allowed is false, Reason carries the denial message for logging/events.

type LLMResult

type LLMResult struct {
	Content   string
	ToolCalls []ToolCallRequest
	Usage     *interfaces.LLMUsage
}

LLMResult is the result of a successful LLM call. Content holds the assistant text; ToolCalls holds any tool invocations resolved against the registered tools list (NeedsApproval pre-computed from the approval policy).

type Runtime

type Runtime struct {
	AgentSpec      runtime.AgentSpec
	AgentExecution runtime.AgentExecution
	Tracer         interfaces.Tracer
	Metrics        interfaces.Metrics
	// ToolExecutionMode controls whether tool calls in one LLM round are executed
	// in parallel or sequentially. Defaults to parallel when empty.
	ToolExecutionMode types.AgentToolExecutionMode
}

Runtime holds the execution inputs shared by all runtime backends. Local and Temporal runtimes embed this struct and call its methods directly.

func (*Runtime) AuthorizeTool

func (rt *Runtime) AuthorizeTool(ctx context.Context, log logger.Logger, toolName string, args map[string]any) (AuthorizeResult, error)

AuthorizeTool checks programmatic authorization for a tool before approval/execution. Tools that do not implement interfaces.ToolAuthorizer are allowed by default.

func (*Runtime) BuildLLMRequest

func (rt *Runtime) BuildLLMRequest(messages []interfaces.Message, skipTools bool, retrieverContext string) (*interfaces.LLMRequest, []interfaces.Tool)

BuildLLMRequest constructs an LLMRequest from the given messages and options. When retrieverContext is non-empty it is appended to the system prompt (prefetch/hybrid mode). Returns the request and the resolved tools slice for later use in response parsing.

func (*Runtime) ExecuteLLM

func (rt *Runtime) ExecuteLLM(
	ctx context.Context,
	log logger.Logger,
	agentName, messageID string,
	messages []interfaces.Message,
	skipTools bool,
	retrieverContext string,
	emit func(events.AgentEvent),
) (*LLMResult, error)

ExecuteLLM calls the LLM in non-streaming mode, records metrics and traces, emits TEXT_MESSAGE_START / TEXT_MESSAGE_CONTENT / TEXT_MESSAGE_END events, and returns LLMResult. messageID and agentName are used only for event construction; emit may be nil.

func (*Runtime) ExecuteLLMStream

func (rt *Runtime) ExecuteLLMStream(
	ctx context.Context,
	log logger.Logger,
	agentName, messageID string,
	messages []interfaces.Message,
	skipTools bool,
	retrieverContext string,
	emit func(events.AgentEvent),
) (*LLMResult, error)

ExecuteLLMStream calls the LLM in streaming mode. When the LLM client does not support streaming it falls back to Generate automatically. Delta events (text content, reasoning) are emitted via emit as chunks arrive; a final TEXT_MESSAGE_START/CONTENT/END triple is emitted for non-streaming fallback. emit may be nil.

func (*Runtime) ExecuteRetrievers

func (rt *Runtime) ExecuteRetrievers(ctx context.Context, log logger.Logger, query string) (string, error)

ExecuteRetrievers runs all configured retrievers in parallel for the given query and returns a combined document context string for injection into the LLM system prompt. Partial failures are logged and skipped; all retrievers failing returns an error.

func (*Runtime) ExecuteTool

func (rt *Runtime) ExecuteTool(ctx context.Context, log logger.Logger, toolName string, args map[string]any) (string, error)

ExecuteTool finds the named tool and executes it, recording tracing and metrics. Returns the string representation of the tool result.

func (*Runtime) FetchConversationMessages

func (rt *Runtime) FetchConversationMessages(ctx context.Context, log logger.Logger, conversationID string) ([]interfaces.Message, error)

FetchConversationMessages loads prior messages from the conversation store. Returns an error when no conversation is configured or the store call fails.

func (*Runtime) RequiresApproval

func (rt *Runtime) RequiresApproval(t interfaces.Tool) bool

RequiresApproval reports whether t requires human approval before execution. When no approval policy is configured the tool's own ApprovalRequired flag is used.

type ToolCallRequest

type ToolCallRequest struct {
	ToolCallID      string
	ToolName        string
	ToolDisplayName string
	Args            map[string]any
	NeedsApproval   bool
}

ToolCallRequest describes one tool call returned by the LLM. NeedsApproval is pre-computed from the tool approval policy so orchestration loops (local agent loop, temporal workflow) do not need to re-evaluate the policy.

Jump to

Keyboard shortcuts

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