internal

package
v0.0.0-...-9be4777 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2026 License: MIT Imports: 30 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrSessionBusy = errors.New("session already has an active run")

ErrSessionBusy is returned when Run is called on a session that already has an active run.

Functions

func MapADKSessionEventSeq

func MapADKSessionEventSeq(seq iter.Seq2[*session.Event, error]) iter.Seq2[*SessionEvent, error]

MapADKSessionEventSeq wraps an ADK event iterator, projecting each event with MapADKSessionEvent. Stream errors are forwarded unchanged.

func RootTestLogger

func RootTestLogger() *slog.Logger

func RunExecutorFactoryFromRunner

func RunExecutorFactoryFromRunner(
	cfg runner.Config,
) (*runner.Runner, error)

RunExecutorFactoryFromRunner adapts runner.New to LLMAgentRunnerRunFactory.

func WrapAgentExecError

func WrapAgentExecError(kind AgentExecErrorKind, op string, err error) error

WrapAgentExecError wraps err with a stable kind and operation. Returns nil when err is nil.

Types

type ACPProfileExecutor

type ACPProfileExecutor interface {
	RunACPProfile(ctx context.Context, request ACPRunRequest) (*RunResult, error)
}

ACPProfileExecutor executes ACP stdio profile runs behind an internal boundary.

type ACPRunRequest

type ACPRunRequest struct {
	ProfileName string
	Profile     *ap.AgentProfile
	Model       string
	UserID      string
	SessionID   string
	Message     *MessageContent
}

ACPRunRequest describes a resolved profile run for ACP stdio execution.

type AgentExecError

type AgentExecError struct {
	Kind AgentExecErrorKind
	Op   string
	Err  error
}

AgentExecError wraps agent execution dispatch failures with a stable kind and operation.

func (*AgentExecError) Error

func (e *AgentExecError) Error() string

func (*AgentExecError) Unwrap

func (e *AgentExecError) Unwrap() error

type AgentExecErrorKind

type AgentExecErrorKind string

AgentExecErrorKind classifies agent execution dispatch failures.

const (
	// AgentExecErrorKindValidation indicates invalid input for a dispatch operation.
	AgentExecErrorKindValidation AgentExecErrorKind = "validation"
	// AgentExecErrorKindNotFound indicates the requested profile does not exist.
	AgentExecErrorKindNotFound AgentExecErrorKind = "not-found"
	// AgentExecErrorKindUnsupported indicates the selected execution mode is not supported.
	AgentExecErrorKindUnsupported AgentExecErrorKind = "unsupported"
	// AgentExecErrorKindExecution indicates a lower-level dependency failed during dispatch.
	AgentExecErrorKindExecution AgentExecErrorKind = "execution"
)

type AgentRunner

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

func (*AgentRunner) ListSessions

ListSessions returns a page of session metadata for this runner's app name. When no metadata store is configured, it returns an empty page.

func (*AgentRunner) ReadSession

func (a *AgentRunner) ReadSession(ctx context.Context, params ReadSessionParams) (*ReadSessionResult, error)

ReadSession reads session events from the configured session service and maps them to SessionEvent. It uses the runner's own session service and app name (same as Run), so it does not require a factory.

func (*AgentRunner) Run

func (a *AgentRunner) Run(ctx context.Context, params RunParams) (*RunResult, error)

Run dispatches the run according to profile selection and execution mode. Direct runs (no ProfileName) and regular profile runs go through the standard built-in agent run path. ACP stdio profiles are delegated to ACPProfileExecutor.

type AgentRunnerFactory

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

func NewAgentRunnerFactory

func NewAgentRunnerFactory(deps AgentRunnerFactoryDeps) *AgentRunnerFactory

func (*AgentRunnerFactory) ListSessions

ListSessions returns a page of session metadata from the configured metadata store.

func (*AgentRunnerFactory) NewAgentRunner

func (f *AgentRunnerFactory) NewAgentRunner(ctx context.Context, params NewAgentRunnerParams) (*AgentRunner, error)

NewAgentRunner builds an AgentRunner. When params.ModelName is non-empty the LLM is resolved and wired immediately (used for per-request child runners). When ModelName is empty no LLM is resolved and the runner acts as a profile dispatcher only — direct runs on such a runner must not be attempted.

func (*AgentRunnerFactory) ReadSession

ReadSession reads session events from the configured session service and maps them to SessionEvent.

type AgentRunnerFactoryDeps

type AgentRunnerFactoryDeps struct {
	LLMAdapterFactory     LLMAdapterFactory
	LLMAgentFactory       LLMAgentFactory
	LLMAgentRunnerFactory LLMAgentRunnerRunFactory
	SessionStorage        sessions.SessionsStorage
	RootLogger            *slog.Logger
}

type BackgroundRunner

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

BackgroundRunner wraps an underlying runner and executes runs in background goroutines decoupled from the caller's context. It provides fan-out via EventBus and supports reconnection via ReadSession.

func NewBackgroundRunner

func NewBackgroundRunner(deps BackgroundRunnerDeps) *BackgroundRunner

NewBackgroundRunner creates a new BackgroundRunner.

func (*BackgroundRunner) ListSessions

ListSessions delegates to the underlying runner (read-only; no background fan-out).

func (*BackgroundRunner) ReadSession

func (br *BackgroundRunner) ReadSession(
	ctx context.Context,
	params ReadSessionParams,
) (*ReadSessionResult, error)

ReadSession returns a ReadSessionResult for the given session. The Events() iterator yields a unified stream: pre-run history first, then (if active) current-run events from the EventBus (replay + live). IsActive() indicates whether a run is in progress.

func (*BackgroundRunner) Run

func (br *BackgroundRunner) Run(ctx context.Context, params RunParams) (*RunResult, error)

Run starts the agent run in a background goroutine with a server-scoped context (not derived from the caller's ctx). It returns a *RunResult whose Events() iterator reads from an EventBus, so the caller can stream events to the client. Cancelling the caller's ctx stops event delivery but does NOT cancel the background run.

Returns an error if a run is already active for params.SessionID.

func (*BackgroundRunner) Shutdown

func (br *BackgroundRunner) Shutdown()

Shutdown cancels all active runs.

type BackgroundRunnerDeps

type BackgroundRunnerDeps struct {
	Runner backgroundRunnerDep
	Logger *slog.Logger
}

BackgroundRunnerDeps holds the dependencies for NewBackgroundRunner.

type ConsumeOption

type ConsumeOption func(*consumeOptions)

ConsumeOption is a functional option for ConsumeEventsAsString.

func WithAgentName

func WithAgentName(name string) ConsumeOption

WithAgentName sets the agent name for error messages.

func WithEmptyResultBehavior

func WithEmptyResultBehavior(b EmptyResultBehavior) ConsumeOption

WithEmptyResultBehavior sets what happens when no content is found.

func WithResultSelection

func WithResultSelection(s ResultSelection) ConsumeOption

WithResultSelection sets how multiple events with content are combined.

type EmptyResultBehavior

type EmptyResultBehavior int

EmptyResultBehavior controls what happens when no content is found in the event stream.

const (
	EmptyResultBehaviorReturnError EmptyResultBehavior = iota
	EmptyResultBehaviorReturnEmpty
)

type EventBus

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

EventBus is a replay-capable broadcast mechanism for a single run's events. Every event published is stored in an internal buffer AND forwarded to all live subscriber channels. Late subscribers can replay all buffered events without any gap, because the buffer copy and channel registration happen atomically under the same lock.

func NewEventBus

func NewEventBus() *EventBus

NewEventBus creates a new, open EventBus ready to accept events.

func (*EventBus) Close

func (b *EventBus) Close(err error)

Close marks the bus as done, records the final error (may be nil), closes all subscriber channels, and closes the Done() channel. Any subsequent Publish calls are no-ops.

func (*EventBus) Done

func (b *EventBus) Done() <-chan struct{}

Done returns a channel that is closed when the bus is closed.

func (*EventBus) Publish

func (b *EventBus) Publish(event *SessionEvent)

Publish appends the event to the replay buffer and sends it to all current subscribers. Publish after Close is a no-op.

func (*EventBus) ReplayAndSubscribe

func (b *EventBus) ReplayAndSubscribe(ctx context.Context) iter.Seq2[*SessionEvent, error]

ReplayAndSubscribe is the key entry point for late subscribers. Under a single lock it atomically copies the current replay buffer AND registers a new subscriber channel, so there is no gap between buffered and live events.

The returned iterator first yields all buffered events, then yields live events from the subscriber channel. Iteration stops when:

  • the bus is closed (yields the final error if non-nil, then stops)
  • ctx is cancelled (stops without error)

func (*EventBus) Subscribe

func (b *EventBus) Subscribe() (int, <-chan *SessionEvent)

Subscribe returns an id and a channel that receives events published from this point forward. It is intended for the initial caller that subscribes before any events are produced (so no replay is needed). The caller must call Unsubscribe when done to release resources.

func (*EventBus) Unsubscribe

func (b *EventBus) Unsubscribe(id int)

Unsubscribe removes the subscriber with the given id. Subsequent events will not be sent to its channel.

type FakeGenkitInstance

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

FakeGenkitInstance is a test helper that provides a controllable genkit init function. Each call to the returned InitFunc increments an internal counter and returns a zero *genkit.Genkit. Access InitCount() to inspect how many times the init function was called.

func NewFakeGenkitInstance

func NewFakeGenkitInstance() *FakeGenkitInstance

NewFakeGenkitInstance creates a new FakeGenkitInstance for use in tests.

func (*FakeGenkitInstance) InitCount

func (f *FakeGenkitInstance) InitCount() int

InitCount returns the number of times InitFunc was invoked.

func (*FakeGenkitInstance) InitFunc

InitFunc returns a genkitInitFuncType-compatible function that records calls.

type GenkitLLMAdapterDeps

type GenkitLLMAdapterDeps struct {
	Genkit     *genkit.Genkit
	RootLogger *slog.Logger
	// contains filtered or unexported fields
}

GenkitLLMAdapterDeps holds dependencies for constructing the adapter.

type GenkitLLMAdapterFactory

type GenkitLLMAdapterFactory func(name string) model.LLM

GenkitLLMAdapterFactory is a function that creates a GenkitLLMAdapter for a resolved model name.

func NewGenkitLLMAdapterFactory

func NewGenkitLLMAdapterFactory(deps GenkitLLMAdapterDeps) GenkitLLMAdapterFactory

type LLMAdapterFactory

type LLMAdapterFactory func(ctx context.Context, modelName string) (model.LLM, error)

LLMAdapterFactory creates a model.LLM from a model name. Callers may pass an empty name when the factory ignores it (e.g. tests); the exported agent.Runner requires a non-empty RunParams.Model and does not substitute a runner-level default. The context MUST be the same as the run (e.g. passed to NewAgentRunner) so cancellation and request-scoped values propagate to model resolution. On failure the LLM must be nil and err non-nil (never return a nil LLM without an error).

type LLMAgentFactory

type LLMAgentFactory func(cfg llmagent.Config) (agent.Agent, error)

LLMAgentFactory creates an agent.Agent from a llmagent.Config.

type LLMAgentRunnerRunFactory

type LLMAgentRunnerRunFactory func(cfg runner.Config) (*runner.Runner, error)

LLMAgentRunnerRunFactory creates a runner.Runner from runner.Config.

type LLMRunner

type LLMRunner interface {
	Run(
		ctx context.Context,
		userID, sessionID string,
		msg *genai.Content,
		cfg agent.RunConfig,
		opts ...runner.RunOption,
	) iter.Seq2[*session.Event, error]
}

LLMRunner executes an agent run and yields session events. Compatible with runner.Runner.Run; *runner.Runner implements this interface directly.

type ListSessionMetadataParams

type ListSessionMetadataParams = sessions.ListSessionMetadataParams

ListSessionMetadataParams is an alias for sessions.ListSessionMetadataParams.

type ListSessionMetadataResult

type ListSessionMetadataResult = sessions.ListSessionMetadataResult

ListSessionMetadataResult is an alias for sessions.ListSessionMetadataResult.

type MessageContent

type MessageContent struct {
	Parts []MessagePart
}

MessageContent is a minimal representation of an inbound user message.

type MessagePart

type MessagePart struct {
	Text string
}

MessagePart is a single segment of MessageContent; only text is supported for now.

type ModelInfo

type ModelInfo struct {
	// Provider is the provider name (prefix in fully-qualified model names).
	Provider string

	// Name is the technical model identifier (e.g., "gpt-4.1").
	Name string

	// DisplayName is an optional human-friendly label.
	DisplayName string
}

ModelInfo describes a single model available through a provider.

type ModelsLocator

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

ModelsLocator resolves model.LLM adapters from fully-qualified model names and caches genkit instances per provider. Cache invalidation is based on UpdatedAt.

func NewModelsLocator

func NewModelsLocator(params ModelsLocatorParams) *ModelsLocator

NewModelsLocator constructs a ModelsLocator. ToolStubRegistrar must be non-nil.

func (*ModelsLocator) ListModels

func (l *ModelsLocator) ListModels(ctx context.Context) ([]ModelInfo, error)

ListModels returns all configured models across all providers.

func (*ModelsLocator) ResolveModel

func (l *ModelsLocator) ResolveModel(
	ctx context.Context,
	fqModelName string,
) (model.LLM, error)

ResolveModel returns a model.LLM for the given fully-qualified model name. It parses provider/model, looks up the provider config, manages a per-provider genkit cache (invalidated by UpdatedAt), and creates a new genkit instance when needed.

type ModelsLocatorParams

type ModelsLocatorParams struct {
	ProvidersSvc      lp.ProvidersConfigService
	Logger            *slog.Logger
	GenkitInitFunc    genkitInitFuncType   // injectable; defaults to defaultGenkitInit
	ToolStubRegistrar func(*genkit.Genkit) // required; called on each new genkit instance
}

ModelsLocatorParams holds dependencies for constructing a ModelsLocator.

type NewAgentRunnerParams

type NewAgentRunnerParams struct {
	AppName               string
	AgentName             string
	SystemPromptFragments []SystemPromptFragment
	ToolsRegistry         ToolsProvider
	ModelName             string // from RunParams; public Runner validates non-empty before NewAgentRunner

	// Profile execution fields — required when AgentRunner.Run should handle profile dispatch.
	DefaultAgentName   string
	ProfilesService    profilesService
	ACPProfileExecutor ACPProfileExecutor
}

type ReadSessionParams

type ReadSessionParams struct {
	AppName   string
	SessionID string
	UserID    string
}

ReadSessionParams contains the parameters for reading a session.

type ReadSessionResult

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

ReadSessionResult is the result of reading a session: identifier, whether a background run is active (only when using BackgroundRunner), and a replayable event stream.

func NewReadSessionResult

func NewReadSessionResult(sessionID string, isActive bool, events iter.Seq2[*SessionEvent, error]) *ReadSessionResult

NewReadSessionResult constructs a ReadSessionResult. Storage-only reads use isActive false.

func (*ReadSessionResult) Events

func (r *ReadSessionResult) Events() iter.Seq2[*SessionEvent, error]

Events returns the session event stream (historical and, when active, live events).

func (*ReadSessionResult) IsActive

func (r *ReadSessionResult) IsActive() bool

IsActive reports whether a background run is in progress for this session (BackgroundRunner only).

func (*ReadSessionResult) SessionID

func (r *ReadSessionResult) SessionID() string

SessionID returns the session identifier.

type ResultSelection

type ResultSelection int

ResultSelection controls how multiple final (or partial) events with content are combined.

const (
	ResultSelectionConcatenateAll ResultSelection = iota
	ResultSelectionLastOnly
)

type RunParams

type RunParams struct {
	UserID    string
	SessionID string
	Message   *MessageContent
	Model     string // fully qualified: "provider/model-name"
	// ProfileName selects a saved profile for profile-backed execution.
	// Empty means direct built-in execution using Model.
	ProfileName string
}

type RunResult

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

func NewRunResult

func NewRunResult(events iter.Seq2[*SessionEvent, error], sessionID string) *RunResult

func (*RunResult) ConsumeEventsAsString

func (r *RunResult) ConsumeEventsAsString(ctx context.Context, opts ...ConsumeOption) (string, error)

func (*RunResult) ConsumeEventsAsStringSeq

func (r *RunResult) ConsumeEventsAsStringSeq(ctx context.Context) iter.Seq2[string, error]

func (*RunResult) Events

func (r *RunResult) Events() iter.Seq2[*SessionEvent, error]

Events returns the session event stream for this run—the same sequence passed to NewRunResult.

func (*RunResult) SessionID

func (r *RunResult) SessionID() string

type SessionEvent

type SessionEvent struct {
	ErrorCode    string
	ErrorMessage string
	Partial      bool
	TurnComplete bool
	Interrupted  bool
	Author       string
	Branch       string
	InvocationID string
	Content      *SessionEventContent
}

SessionEvent is a projection of ADK session.Event fields consumed by RunResult and the agent API stream mapper.

func MapADKSessionEvent

func MapADKSessionEvent(ev *session.Event) *SessionEvent

MapADKSessionEvent copies the supported subset from a non-nil ADK event. Nil input yields nil.

type SessionEventContent

type SessionEventContent struct {
	Role  string
	Parts []SessionEventPart
}

SessionEventContent is a text-only projection of model message content on session events (no genai types).

type SessionEventFunctionCall

type SessionEventFunctionCall struct {
	ID   string
	Name string
	Args map[string]any
}

SessionEventFunctionCall holds the tool invocation requested by the model.

type SessionEventFunctionResponse

type SessionEventFunctionResponse struct {
	ID       string
	Name     string
	Response map[string]any
}

SessionEventFunctionResponse holds the result returned by a tool invocation.

type SessionEventPart

type SessionEventPart struct {
	Text             string
	FunctionCall     *SessionEventFunctionCall
	FunctionResponse *SessionEventFunctionResponse
}

SessionEventPart is one segment in streamed session-event content (not agent input; see MessagePart). Exactly one of Text, FunctionCall, or FunctionResponse is set per part.

type SessionMetadata

type SessionMetadata = sessions.SessionMetadata

SessionMetadata is an alias for sessions.SessionMetadata.

type SessionMetadataStore

type SessionMetadataStore = sessions.SessionMetadataStore

SessionMetadataStore is an alias for sessions.SessionMetadataStore.

type StaticToolsProvider

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

StaticToolsProvider returns a fixed set of tools.

func StaticTools

func StaticTools(tools []tool.Tool) *StaticToolsProvider

StaticTools returns a provider that serves a fixed set of tools.

func (*StaticToolsProvider) GetTools

func (s *StaticToolsProvider) GetTools() ([]tool.Tool, error)

type SystemPromptFragment

type SystemPromptFragment struct {
	// Section is a short title rendered as a second-level heading.
	Section string
	// Content is the body; may use markdown (use third-level headings for subsections).
	Content string
}

SystemPromptFragment is a section appended after the base system prompt template. The agent package re-exports this type as SystemPromptFragment.

type SystemPromptInstructionProviderOption

type SystemPromptInstructionProviderOption func(*systemPromptInstructionProviderConfig)

SystemPromptInstructionProviderOption configures newSystemPromptInstructionProvider.

func WithSystemPromptBaseTemplate

func WithSystemPromptBaseTemplate(s string) SystemPromptInstructionProviderOption

WithSystemPromptBaseTemplate sets the Go text template used for the base system prompt. The default is the embedded system_prompt_base.tmpl. Template data includes .AppName from agent.ReadonlyContext. Intended for tests so expectations do not depend on the shipped file.

type ToolsProvider

type ToolsProvider interface {
	GetTools() ([]tool.Tool, error)
}

ToolsProvider supplies tools for an agent. Implemented by *aitools.ToolsRegistry.

Directories

Path Synopsis
Package agentapi provides primitives to interact with the openapi HTTP API.
Package agentapi provides primitives to interact with the openapi HTTP API.
Package callerid provides the CallerIdentity interface and context helpers.
Package callerid provides the CallerIdentity interface and context helpers.
Package llmproviders implements LLM provider configuration persistence (file and database backends) and defines the domain types for provider settings.
Package llmproviders implements LLM provider configuration persistence (file and database backends) and defines the domain types for provider settings.

Jump to

Keyboard shortcuts

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