virtual

package
v0.10.4 Latest Latest
Warning

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

Go to latest
Published: May 3, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package virtual implements a agent.Provider that replays recorded responses from a dictionary. This enables deterministic testing without live LLM calls.

Ported from ddx cli/internal/agent/virtual.go with adaptation to the agent.Provider interface — responses are structured (content + tool calls + token usage) rather than raw text.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NormalizePrompt

func NormalizePrompt(prompt string, patterns []NormalizePattern) string

NormalizePrompt applies regex→replacement patterns before hashing. This allows prompts with dynamic content (temp paths, timestamps, IDs) to produce stable hashes across runs.

func PromptHash

func PromptHash(prompt string) string

PromptHash computes a truncated SHA-256 hash of a prompt string. Returns 16 hex characters (64 bits).

func RecordEntry

func RecordEntry(dictDir string, messages []agent.Message, response agent.Response, patterns []NormalizePattern) error

RecordEntry saves a message→response pair to the dictionary directory.

Types

type Config

type Config struct {
	// DictDir is the directory containing recorded dictionary entries.
	DictDir string

	// InlineResponses are checked before file-based lookup.
	InlineResponses []InlineResponse

	// NormalizePatterns are applied to message content before hashing.
	NormalizePatterns []NormalizePattern
}

Config configures the virtual provider.

type Entry

type Entry struct {
	PromptHash string          `json:"prompt_hash"`
	Messages   []agent.Message `json:"messages,omitempty"`
	Response   agent.Response  `json:"response"`
	DelayMS    int             `json:"delay_ms,omitempty"`
	RecordedAt string          `json:"recorded_at,omitempty"`
}

Entry is a recorded message→response pair stored on disk.

type InlineResponse

type InlineResponse struct {
	PromptMatch string         `json:"prompt_match"` // substring or /regex/
	Response    agent.Response `json:"response"`
	DelayMS     int            `json:"delay_ms,omitempty"`
}

InlineResponse matches prompts by pattern and returns a fixed response.

type NormalizePattern

type NormalizePattern struct {
	Pattern string `json:"pattern" yaml:"pattern"`
	Replace string `json:"replace" yaml:"replace"`
}

NormalizePattern is a regex→replacement pair for prompt normalization.

type Provider

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

Provider replays recorded responses from a dictionary.

func New

func New(cfg Config) *Provider

New creates a virtual provider.

func (*Provider) Chat

func (p *Provider) Chat(ctx context.Context, messages []agent.Message, tools []agent.ToolDef, opts agent.Options) (agent.Response, error)

Chat looks up a recorded response matching the messages. It checks inline responses first, then falls back to file-based dictionary lookup by hash.

func (*Provider) SessionStartMetadata

func (p *Provider) SessionStartMetadata() (string, string)

SessionStartMetadata reports a stable identity for replay-driven runs.

Jump to

Keyboard shortcuts

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