agent

package
v0.0.0-...-6a25a8b Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2026 License: MIT Imports: 48 Imported by: 0

README

SOP Agent Framework

The SOP Agent Framework (ai/agent) is a powerful system for building autonomous agents that can execute complex workflows, interact with databases, and orchestrate multi-step tasks.

Core Concepts

1. Scripts ("Natural Language Programming")

Scripts (formerly Scripts) are the building blocks of agent behavior. They are JSON-based functional scripts that define a sequence of steps.

  • Compiled Instructions: Scripts are parsed and executed by the Service.
  • Inspectable: Tools and steps are defined in a registry, allowing for validation and introspection.
2. Swarm Computing (Async Execution)

The framework supports "Swarm Computing" by allowing steps to run in parallel.

  • Async Steps: Any step can be marked as "is_async": true.
  • Transaction Isolation: Async steps are detached from the parent transaction. They must start their own transaction if they need to write to the database. This prevents race conditions and allows for "In-Flight Data Merging" at the storage layer.
  • Nested Swarms: A step of type call_script can be run asynchronously even if the parent script is in a transaction. The nested script will start with a detached context and can establish its own transaction (e.g., on a different database), enabling powerful hierarchical agent swarms.
  • Safeguards: For non-script steps (like command or set), the system enforces synchronous execution if an active transaction exists to prevent accidental data corruption.
  • Error Propagation: By default, if an async step fails, it cancels the entire group. You can override this with "continue_on_error": true.
3. Transaction Inheritance (Subroutines)

If a nested script is run synchronously ("is_async": false) and does not specify a different database, it acts as a Subroutine.

  • Inheritance: It inherits the parent's active transaction.
  • Atomicity: Operations in the subroutine are part of the parent's atomic unit of work. If the subroutine fails, the parent transaction rolls back.
4. Saga Pattern (Multi-Database Workflows)

SOP encourages the Saga Pattern for workflows that span multiple databases. Instead of a single distributed transaction (which is complex and brittle), you chain scripts where each script operates on a specific database.

  • Database-Scoped Scripts: A script can specify a "database" field.
  • Automatic Transaction Wrapping: When a script specifies a database, the system automatically:
    1. Switches the context to that database.
    2. Starts a new transaction (ForWriting).
    3. Commits the transaction if the script succeeds.
    4. Rolls back if it fails.
4. Tool Registry

Tools are no longer hardcoded strings. They are defined in a structured Registry.

  • Definition: Tools have a Name, Description, Argument Schema, and a Go Handler function.
  • Discovery: Agents can dynamically list and inspect available tools to generate their system prompts.
6. Explicit Transaction Management

While automatic transaction wrapping (Saga Pattern) is recommended for most cases, you can still manage transactions explicitly using the manage_transaction tool. This is useful when:

  • Minimizing Lock Time: You want to perform heavy AI processing or external API calls outside of a transaction, and only wrap the database writes.
  • Multiple Transactions: A single script needs to perform multiple independent commits.
  • Inherited Context: You are running a script without a specific database field and want to control the transaction on the inherited database connection.
7. Dual-Layer Memory Architecture

The agent is designed with a cognitive memory model that separates transient context from persistent knowledge.

Short-Term Memory (Context)
  • Implementation: RunnerSession (ai/agent/memory_shortterm.go).
  • Behavior: Stores the active conversation history (History), temporary variables (Variables), and the current database transaction (Transaction).
  • Scope: Bound to the WebSocket session. It is volatile and disappears when the user disconnects.
Long-Term Memory (Knowledge)
  • V1 Implementation: Originally implemented as KnowledgeBase in ai/agent/memory_longterm.go. Used a dedicated B-Tree named memory (via agent.KnowledgeStore) to explicitly persist learned instructions, domain terms, and tool overrides.
  • V2 Implementation (Current): Omni Protocol introduces a generic blueprint approach (Expertise KB + Memory (STM/LTM)) to managing knowledge:
    • Omni: Driven by the SOP KB (static system architectural rules) + Memory System. Omni can also be configured with custom KBs to act as supplementary data lookups.
    • Avatars: Can have their own Custom KB (domain-specific rules) + Memory System for discrete user interactions.
  • The generic architecture divides into two tiers:
    1. Expertise KB (e.g. SOP KB): A static Vector Space compiled from text files that teaches the agent how to operate.
    2. Memory System (LTM KB / MRU / STM): An active layer embedding live feedback, transient constraints, and conversational context automatically.
  • Storage: Dynamic Vector Spaces embedded inside the System Database.
  • Behavior: Stores foundational frameworks in the SOP KB, and conversational adjustments natively inside the LTM KB (powering self-correction, which is just one of Omni's many advanced capabilities).
  • Architecture & Flexibility:
    • Omni / Butler Architecture: The LLM does not ingest all rules statically at boot. Instead, it dynamically switches "Vector KBs" (like a Butler accessing different rooms) to retrieve knowledge natively via vector queries.
    • Contextual Tool Injection: The orchestrator searches the SOP KB (e.g., searching for <ToolName> Tool Operations) to load instructions natively, while consulting the active Memory System (LTM KB) for session-specific behavior edits.
    • User-Driven Privacy Scopes: The scoping of the knowledge base heavily relies on the Omni protocol's separation of memory tiers. The SOP KB provides globally shared expertise, while the user's Memory System (STM and LTM KB) remain strictly private and do not cross-pollinate between users.
    • Domain-Specific KBs: Alternatively, you can provision separate Vector KBs for different domains (e.g., finance_kb, legal_kb). This creates isolated "Expert Copilots" with deep, specialized knowledge that doesn't leak into other domains.
    • Environment Isolation: Each environment (Dev, Staging, Prod) can have its own SOP KB. This allows developers to test or over-ride rules or vocabulary in Dev without affecting Production knowledge.

Script Schema

A Script consists of metadata and a list of Steps.

{
  "name": "example_script",
  "description": "Demonstrates async and db features",
  "parameters": ["user_id"],
  "database": "users_db", // Optional: Target DB for this script
  "steps": [ ... ]
}
Common Step Fields

All steps support the following optional fields to improve readability and control:

  • name: A unique identifier for the step (useful for logs and future jumps).
  • description: A human-readable explanation of the step's intent.
Step Types
Type Description Key Fields
ask Ask the LLM a question prompt, output_variable
set Set a variable variable, value
if Conditional logic condition, then, else
loop Iterate over a list list, iterator, steps
fetch Retrieve data from B-Tree source, resource, variable
command Execute a registered tool command, args
script Run a nested script script_name, script_args
Async & Error Handling Fields
  • "is_async": true: Runs the step in a background goroutine.
  • "continue_on_error": true: If this step fails, do not stop the rest of the script.

System Tools for Script Authoring

The Agent can manage its own scripts using the following registered tools:

  • create_script: Initialize a new empty script with a description.
  • save_script: Save (overwrite) a complete script structure.
  • save_step: Append a single step to the end of a script. This supports a "stream of thought" authoring process where the agent builds a program incrementally.
  • insert_step / update_step: Edit existing logic in place.

Examples

1. Async Execution (Swarm)

Run two heavy tasks in parallel.

{
  "name": "parallel_processing",
  "steps": [
    {
      "type": "command",
      "command": "heavy_computation",
      "args": {"id": "1"},
      "is_async": true
    },
    {
      "type": "command",
      "command": "heavy_computation",
      "args": {"id": "2"},
      "is_async": true
    }
  ]
}
2. Saga Pattern (Multi-DB)

Orchestrate a workflow across two databases using nested scripts.

Script 1: Update User (on users_db)

{
  "name": "update_user",
  "database": "users_db",
  "parameters": ["uid"],
  "steps": [
    { "type": "command", "command": "update_record", "args": {"id": "{{.uid}}"} }
  ]
}

Script 2: Log Audit (on audit_db)

{
  "name": "log_audit",
  "database": "audit_db",
  "parameters": ["action"],
  "steps": [
    { "type": "command", "command": "insert_log", "args": {"msg": "{{.action}}"} }
  ]
}

Script 3: Orchestrator (Saga)

{
  "name": "user_update_saga",
  "steps": [
    {
      "type": "script",
      "script_name": "update_user",
      "script_args": {"uid": "123"}
    },
    {
      "type": "script",
      "script_name": "log_audit",
      "script_args": {"action": "User 123 updated"}
    }
  ]
}

In this example:

  1. update_user runs in a transaction on users_db.
  2. If it succeeds, log_audit runs in a transaction on audit_db.
  3. If update_user fails, log_audit never runs.
  4. If log_audit fails, update_user is already committed (Saga pattern). You could add a compensation step to undo it if needed.
3. Batch Processing (Explicit Transactions)

Process a large list of items in batches, committing every few steps to avoid long-held locks or massive transactions.

{
  "name": "batch_processing",
  "steps": [
    // Batch 1
    { "type": "command", "command": "process_item", "args": {"id": "1"} },
    { "type": "command", "command": "process_item", "args": {"id": "2"} },
    { "type": "command", "command": "manage_transaction", "args": {"action": "commit"} },
    
    // Batch 2 (Transaction automatically renewed after commit)
    { "type": "command", "command": "process_item", "args": {"id": "3"} },
    { "type": "command", "command": "process_item", "args": {"id": "4"} },
    { "type": "command", "command": "manage_transaction", "args": {"action": "commit"} }
  ]
}

This pattern ensures that if Batch 2 fails, Batch 1 remains committed.


Data Engine & MOAT Features

The SOP Agent Framework includes a specialized Data Admin Agent that provides "bare metal" data manipulation capabilities. This implementation represents a significant competitive advantage (MOAT) due to its unique architecture.

1. Direct B-Tree Navigation (No Abstraction Layers)

Unlike traditional SQL engines that rely on thick layers of abstraction (SQL parsers, query optimizers, execution plans, storage engine APIs), SOP's tools interact directly with the B-Tree cursors.

  • Efficiency: Eliminates the overhead of query planning and intermediate object allocation.
  • Control: The agent has fine-grained control over navigation (Next, Previous, FindOne), allowing for optimizations that generic SQL optimizers often miss.
2. Zero-Copy Result Streaming (Cursor-to-Socket)

The select and join tools implement a rare and highly efficient Zero-Copy Streaming architecture.

  • No Buffering: Unlike standard ORMs or database drivers that load results into a massive slice or memory buffer before returning, SOP streams data directly from the B-Tree cursor to the output stream.
  • Cursor-Driven: As the B-Tree iterator advances (Next()), the current item is immediately serialized and flushed to the response (e.g., HTTP socket or console).
  • Constant Memory: This allows the agent to process, join, or select millions of records with a constant, minimal memory footprint (O(1) space complexity), regardless of the result set size.
3. 3-Way Merge Join Optimization

The join tool implements a sophisticated Merge Join strategy when joining on Primary Keys.

  • Synchronized Scanning: Iterates through both the Left and Right B-Trees simultaneously.
  • Smart Seeking: If one cursor falls behind, it doesn't just "scan" to catch up. It uses the B-Tree's FindOne capability to jump directly to the target key, skipping vast ranges of irrelevant data.
  • Full Join Support: Efficiently handles Inner, Left, Right, and Full Outer joins in a single pass, a capability rarely found in embedded or lightweight engines.
4. Smart Index Utilization

The select tool bridges the gap between "Point Lookups" and "Table Scans".

  • Operator Awareness: It analyzes filter criteria (e.g., {"age": {"$gt": 25}}).
  • Index Seeking: Instead of starting at the beginning of the table, it uses the B-Tree index to seek directly to the first matching record (e.g., the first user with age > 25) before beginning the scan.
5. Actionable Queries (Bulk Operations)

The engine supports performing actions directly within the query execution pipeline, enabling high-performance, bare-metal bulk operations without the need for "Select-then-Update" round trips.

  • Bulk Delete: The select tool accepts action="delete". It iterates through the result set and removes items in-place.
    • Optimality: Uses direct B-Tree cursor manipulation. It employs a "Peek-Next-Delete" strategy to ensure the cursor remains valid, achieving O(N) performance where N is the number of items to delete, with zero memory overhead for buffering.
  • Bulk Update: The select tool accepts action="update" and an update_values map. It merges the new values into the existing record and updates it in a single pass.
    • Efficiency: Updates happen in-place. If the update doesn't change the key, it avoids expensive re-balancing.
  • Join-Based Actions: The join tool supports action="delete_left" and action="update_left".
    • Complex Logic: You can delete or update records in the Left table based on complex join conditions with the Right table (e.g., "Delete all Users who have no Orders" via a Left Join).
    • Performance: These actions benefit from the same Merge Join and Index Seeking optimizations as standard queries. This allows for massive bulk updates across related tables with optimal I/O patterns.
6. Transaction Safety & Auto-Rollback

To ensure data integrity even when using powerful bulk operations or complex multi-step scripts:

  • Auto-Rollback: The Agent Runner enforces a "Clean Slate" policy. If a top-level script finishes execution (successfully or with an error) and leaves a transaction open (e.g., a user forgot to call commit), the runner automatically rolls back the transaction.
  • Warning: A warning is logged to the output, alerting the user that their uncommitted changes were discarded.
  • Benefit: This prevents "dangling transactions" from locking resources or leaking uncommitted state into subsequent agent interactions.

Testing & Development

Pipeline Integration Test Harness

We have a dedicated integration test harness for verifying the full RAG pipeline (Service + DataAdminAgent) end-to-end without running the full HTTP server. This is useful for debugging agent logic, tool execution, and session history recording.

File: ai/agent/pipeline_integration_test.go

Features:

  • Real LLM Integration: Uses the configured Gemini model to generate real plans.
  • Stub Mode: Runs with StubMode: true, so it simulates DB operations without side effects.
  • Full Pipeline: Tests the Service -> Pipeline -> DataAdminAgent flow.
  • History Verification: Verifies that last-tool correctly captures complex tool arguments (like scripts).

How to Run:

# Run the specific test with your API key configured in config
go test -v ./ai/agent -run TestServiceIntegration_LastTool

Documentation

Index

Constants

View Source
const (
	StrategyUnset     = 0
	StrategyIndexSeek = 1
	StrategyInMemory  = 2
	StrategyFullScan  = 3
)

Join Strategy Constants

View Source
const (
	StoresDomain = "Stores"
	SpacesDomain = "Spaces"

	RoutingGateFocused    = "focused"
	RoutingGateContinuity = "continuity"
	RoutingGateDiscovery  = "discovery"
)
View Source
const (
	// SystemDBName is the name of the system database used for internal agent storage (scripts, history, etc).
	SystemDBName = "system"

	// DefaultHost is the default host for local AI providers.
	DefaultHost = "http://localhost:11434"

	// Environment Variables
	EnvOllamaHost = "OLLAMA_HOST"

	// Providers
	ProviderGemini    = "gemini"
	ProviderChatGPT   = "chatgpt"
	ProviderAnthropic = "anthropic"
	ProviderOllama    = "ollama"
	ProviderLocal     = "local"

	// Default Models
	DefaultModelOpenAI    = ai.DefaultModelOpenAI
	DefaultModelGemini    = ai.DefaultModelGemini
	DefaultModelAnthropic = "claude-3-5-sonnet-20241022"
	DefaultModelOllama    = ai.DefaultModelOllama

	// Session Keys
	SessionPayloadKey = "session_payload"
	RunnerSessionKey  = "runner_session"
)
View Source
const (
	MaxMRUSize            = 20
	MaxExchangesInHistory = 20
)
View Source
const (
	MRUSourceUnknown     = ""
	MRUSourcePersona     = "persona"
	MRUSourceSystemTools = "system_tools"
	MRUSourceAskOutcome  = "ask_outcome"
	MRUSourceAskProgress = "ask_progress"
	MRUSourcePlaybook    = "playbook"
)
View Source
const (
	MRUScopeSession = "session"
	MRUScopeAsk     = "ask"
)
View Source
const (
	ExecuteScriptInstruction = "Execute a full ordered JSON AST under script for multi-step store operations. Each step should be an object such as {op, args?, input_var?, result_var?}. " +
		"Focus on orchestration semantics: begin a transaction, read or mutate stores, then commit or rollback. begin_tx defines the durability boundary for the workflow, so use it when related mutations must persist or roll back together. " +
		"For larger mutation runs, batch deliberately under explicit commits, with a practical default of about 50 to 250 CRUD operations per transaction unless business atomicity requires a different boundary. Chain multi-step reads with result_var/input_var. " +
		"Use list_stores to research stores before multi-store joins or whenever schema is uncertain. Prefer scoped calls such as stores:[\"users\",\"users_orders\",\"orders\"] so research stays compact on large databases. list_stores returns stores:[{name,schema,key_fields,value_fields,description,relations,empty}]. " +
		"When you fill args.condition or any predicate object, write the condition expression the engine should execute and assign the concrete comparison value directly. " +
		"Predicate format: for single-store operations (select, scan with filter), use bare field names like \"first_name\". After joins, use store-qualified paths like \"orders.total_amount\". " +
		"Read store.schema for field names and types (e.g., {\"key\": \"string\", \"first_name\": \"string\", \"age\": \"number\"}). Match types exactly: string values in quotes, numbers as numbers. " +
		"Relations map joins using schema field names. source_fields and target_fields reference fields from the schema. " +
		"Example: Find orders for users with first_name 'John' where total_amount > 500. Call list_stores([\"users\",\"users_orders\",\"orders\"]). Read schemas: users has first_name field, orders has total_amount field. Build: {\"script\":[{\"op\":\"begin_tx\",\"args\":{\"mode\":\"read\"},\"result_var\":\"tx\"},{\"op\":\"open_store\",\"args\":{\"transaction\":\"tx\",\"name\":\"users\"},\"result_var\":\"users_store\"},{\"op\":\"select\",\"args\":{\"store\":\"users_store\",\"condition\":{\"first_name\":{\"$eq\":\"John\"}}},\"result_var\":\"matched_users\"},{\"op\":\"join\",\"input_var\":\"matched_users\",\"args\":{\"target\":\"users_orders_store\",\"relation\":\"users_orders\"},\"result_var\":\"user_order_links\"},{\"op\":\"join\",\"input_var\":\"user_order_links\",\"args\":{\"target\":\"orders_store\",\"relation\":\"orders\"},\"result_var\":\"joined_orders\"},{\"op\":\"filter\",\"input_var\":\"joined_orders\",\"args\":{\"condition\":{\"orders.total_amount\":{\"$gt\":500}}},\"result_var\":\"filtered_orders\"},{\"op\":\"return\",\"input_var\":\"filtered_orders\"}]}. Do not emit booleans like {\"first_name\":true}. " +
		"Prefer relation + target for join repair instead of inventing a fresh on mapping; if on is still needed, rewrite only the invalid join slice by translating the confirmed relation into the exact concrete field mapping the join op expects, and never use store names where field paths are required. join and join_right emit a combined flat record by default, so reuse dotted store-qualified field paths unless a later project step intentionally reshapes the output. If the AST shape is ambiguous, call gettoolinfo('execute_script') and continue with concrete predicate objects, concrete join mappings, and boolean placeholders removed."
	ListStoresInstruction = "Research store structure before writing multi-store reads or repairs. Pass stores:[...] to scope the response to likely targets, and infer likely store names from the user's ask instead of leaving stores empty when obvious candidates are available. " +
		"The tool can narrow close singular/plural matches internally, but you should still pass the most likely store names you can infer. The result is a JSON object with stores:[{name,schema,key_fields,value_fields,description,relations,empty}]. " +
		"Each store.schema maps field names to types (e.g., {\"key\": \"string\", \"first_name\": \"string\", \"age\": \"number\"}). " +
		"When building predicates: for single-store operations, use bare field names like \"first_name\". After joins, use store-qualified names like \"orders.total_amount\". " +
		"Relations map store-to-store joins using schema field names. source_fields and target_fields reference fields from the schema. " +
		"Example: Find orders for users with first_name 'John' where total_amount > 500. Call list_stores with [\"users\", \"users_orders\", \"orders\"]. Read schemas: users has first_name, orders has total_amount. Build predicates: {\"first_name\": {\"$eq\": \"John\"}} for single-store filter, {\"orders.total_amount\": {\"$gt\": 500}} after joins."
)
View Source
const (
	SelectInstruction                   = "" /* 673-byte string literal not displayed */
	JoinInstruction                     = "" /* 362-byte string literal not displayed */
	ExplainJoinInstruction              = "" /* 403-byte string literal not displayed */
	AddInstruction                      = "" /* 322-byte string literal not displayed */
	UpdateInstruction                   = "" /* 330-byte string literal not displayed */
	DeleteInstruction                   = "" /* 309-byte string literal not displayed */
	ManageTransactionInstruction        = "" /* 749-byte string literal not displayed */
	MintToSpaceInstruction              = "" /* 447-byte string literal not displayed */
	DeleteSpaceInstruction              = "" /* 327-byte string literal not displayed */
	EnrichSpaceInstruction              = "" /* 306-byte string literal not displayed */
	UpdateSpaceConfigInstruction        = "" /* 338-byte string literal not displayed */
	ReadSpaceConfigInstruction          = "" /* 254-byte string literal not displayed */
	VectorizeSpaceInstruction           = "" /* 265-byte string literal not displayed */
	VectorizeSpaceCategoriesInstruction = "" /* 264-byte string literal not displayed */
	VectorizeSpaceItemsInstruction      = "" /* 262-byte string literal not displayed */
)
View Source
const (
	StrategyLookup    = "lookup"
	StrategyMerge     = "merge"
	StrategyHashRight = "hash_right" // Scan Left, Probe Right (Buffered)
	StrategyHashLeft  = "hash_left"  // Scan Right, Probe Left (Buffered)
)

Constants for Join Strategies

View Source
const (
	CtxKeyAtomicScriptContext = "_atomic_script_context"
	CtxKeyAtomicLastResult    = "_atomic_last_result"
)
View Source
const (
	RecipeKindExplicit = "explicit"
	RecipeKindImplicit = "implicit"
)
View Source
const (
	RecipeScopeMicro  = "micro"
	RecipeScopeMacro  = "macro"
	RecipeScopeSpace  = "space"
	RecipeScopeGlobal = "global"
)
View Source
const (
	// KnowledgeStore is the name of the B-tree store used to persist learned rules, vocabulary, and corrections.
	// This store resides in the System Database configured for the agent.
	KnowledgeStore = "memory"
	// MRUKnowledgeStore is the name of the store that tracks "active" or "relevant" knowledge categories/items.
	// This acts as a Working Memory Index, telling the agent what to pre-load from Long Term Memory.
	// Keys are "{Category}/{Name}" or just "{Category}", Values are Timestamps/Scores.
	MRUKnowledgeStore = "mru_knowledge"
	// KnowledgeRefreshDuration is the interval at which the agent refreshes its local view of the persistent knowledge.
	KnowledgeRefreshDuration = 5 * time.Minute
)
View Source
const CtxKeyCurrentScriptCategory contextKey = "current_script_category"
View Source
const CtxKeyJSONStreamer contextKey = "json_streamer"
View Source
const CtxKeySuppressInternalStepStart contextKey = "suppress_internal_step_start"
View Source
const CtxKeyUseNDJSON contextKey = "use_ndjson"
View Source
const MaxConsecutiveRowsWithoutMatch = 100000

MaxConsecutiveRowsWithoutMatch is the safety limit for consecutive rows scanned without finding a match in filter or join operations. This prevents runaway scans from malformed queries while still allowing unlimited streaming of matching data.

View Source
const MaxConversationThreads = 20
View Source
const (
	SYSTEM_TOOLS = "System_Tools"
)

Variables

This section is empty.

Functions

func CleanArgs

func CleanArgs(args map[string]any, reserved ...string) map[string]any

CleanArgs extracts non-reserved arguments from a map. It skips keys starting with "_" and any key in the reserved list.

func CompareLoose

func CompareLoose(a any, b any) int

CompareLoose performs a loose comparison of two values, handling mixed numeric types. It leverages btree.Compare for strict same-type comparisons but promotes mixed numeric types to float64 for correct relational ordering (e.g., 9 < 10.0).

func ExportSplitCategoryPathInstruction

func ExportSplitCategoryPathInstruction(query string) (string, string)

ExportSplitCategoryPathInstruction exports the private function for testing

func GetField

func GetField(source any, field string) any

GetField extracts a field from a given source object (Map, OrderedMap, or JSON String). It supports case-insensitive lookups, dot-notation stripping, Key/Value recursion, and suffix matching.

func HashString

func HashString(s string) string

HashString generates a deterministic hash for the given string.

func NewFromConfig

func NewFromConfig(ctx context.Context, cfg Config, deps Dependencies) (ai.Agent[map[string]any], error)

NewFromConfig creates and initializes a new Agent Service based on the provided configuration. It handles infrastructure setup (Embedder, VectorDB).

func RefineScriptSteps

func RefineScriptSteps(steps []ai.ScriptStep) []ai.ScriptStep

RefineScriptSteps applies automatic refinements to script steps to improve readability and explicitness. This ensures that scripts stored in the system follow conventions (like explicit variable names) even if the creator (LLM or user) relied on implicit behaviors.

func RunLoop

func RunLoop(ctx context.Context, agent ai.Agent[map[string]any], r io.Reader, w io.Writer, prompt string, assistantName string, cfg *ai.ConfigMap) error

RunLoop starts an interactive Read-Eval-Print Loop (REPL) for the agent. It reads user input from r, processes it using the agent's Ask method, and writes the response to w. The loop continues until the user enters "exit" or the input stream ends.

func SetupInfrastructure

func SetupInfrastructure(ctx context.Context, cfg Config, deps Dependencies) (ai.Embeddings, *database.Database, string, vector.Config, error)

SetupInfrastructure initializes the Embedder and Vector Index based on the configuration.

func TranslateASTToCEL

func TranslateASTToCEL(ast any) (string, bool)

func ValidateScriptGrammar

func ValidateScriptGrammar(script []ScriptInstruction) error

ValidateScriptGrammar exposes the execution-path grammar validation pass for reuse.

func ValidateScriptSteps

func ValidateScriptSteps(steps []ai.ScriptStep) error

ValidateScriptSteps converts []ai.ScriptStep to the internal execution form, applies the same sanitize-before-grammar path used by execute_script, and returns grammar validation errors.

Types

type AdHocAgent

type AdHocAgent struct {
	Name    string
	Handler func(ctx context.Context, args map[string]interface{}) (string, error)
}

AdHocAgent implements ai.Agent for simple function wrappers

func (*AdHocAgent) Ask

func (a *AdHocAgent) Ask(ctx context.Context, query string, cfg *ai.ConfigMap) (string, error)

func (*AdHocAgent) Close

func (a *AdHocAgent) Close(ctx context.Context) error

func (*AdHocAgent) Execute

func (a *AdHocAgent) Execute(ctx context.Context, toolName string, args map[string]any) (string, error)

Implement ToolProvider interface

func (*AdHocAgent) Open

func (a *AdHocAgent) Open(ctx context.Context) error

Implement ai.Agent[map[string]any] interface

func (*AdHocAgent) Search

func (a *AdHocAgent) Search(ctx context.Context, query string, limit int) ([]ai.Hit[map[string]any], error)

type AddArgs

type AddArgs struct {
	Database string         `json:"database,omitempty" example:"dev_db"`
	Store    string         `json:"store" binding:"required" example:"users"`
	Key      any            `json:"key" binding:"required" example:"user_123"`
	Value    map[string]any `json:"value" binding:"required"`
}

AddArgs represents arguments for adding a single item

type AskRequest

type AskRequest struct {
	// Query is the user's input question or command
	Query string

	// Session holds the current session state including transactions, variables, and database context
	Session *ai.SessionPayload

	// Executor is the tool executor for running tool calls during ReAct loops
	Executor ai.ToolExecutor

	// Generator optionally overrides the default LLM provider
	Generator ai.Generator

	// ProviderOverride optionally provides runtime provider configuration (provider, model, API key, base URL)
	ProviderOverride *ProviderDetails

	// Database optionally overrides the session's current database
	Database *database.Database

	// Writer is the output destination for streaming responses
	Writer io.Writer

	// EventStreamer receives structured events during reasoning (tool calls, progress, etc.)
	EventStreamer func(eventType string, data any)

	// ProgressSink receives progress messages during execution
	ProgressSink func(message string)

	// ScriptRecorder captures executed script steps for playback or audit
	ScriptRecorder ai.ScriptRecorder

	// DefaultFormat sets the default output format for tools (csv, json, etc.)
	DefaultFormat string

	// Options carries additional configuration from ConfigMap
	Options *ai.ConfigMap
}

AskRequest contains all dependencies for an Ask operation, making the data flow explicit. This replaces the previous pattern of hiding dependencies in context.Context.

type AskResponse

type AskResponse struct {
	// FinalText is the assistant's answer to the user
	FinalText string

	// UpdatedSession contains any session state changes (updated transactions, variables, etc.)
	UpdatedSession *ai.SessionPayload

	// CarryoverState holds provider-specific continuation state for next Ask
	CarryoverState *ai.CarryoverState

	// ToolCalls lists all tools executed during the Ask for audit/logging
	ToolCalls []ai.ToolCall

	// OutcomeFacts contains compact grounded facts safe to carry into MRU continuity
	OutcomeFacts []string

	// OutcomeRecipes contains learned patterns distilled from this Ask
	OutcomeRecipes []ai.LearnedRecipe
}

AskResponse contains the result and any state changes from an Ask operation.

type BaselineReActEngine

type BaselineReActEngine struct {
	Agent *CopilotAgent
}

func (*BaselineReActEngine) Run

type BloomFilter

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

BloomFilter is a probabilistic data structure for set membership.

func NewBloomFilter

func NewBloomFilter(n uint, p float64) *BloomFilter

NewBloomFilter creates a new Bloom Filter optimized for n elements with false positive rate p.

func (*BloomFilter) Add

func (bf *BloomFilter) Add(key string)

Add adds a string key to the Bloom Filter.

func (*BloomFilter) Test

func (bf *BloomFilter) Test(key string) bool

Test checks if a key might vary well be in the set. Returns true if the key MAY be present. Returns false if the key is DEFINITELY NOT present.

type BulkAddArgs

type BulkAddArgs struct {
	Database        string          `json:"database,omitempty" example:"dev_db"`
	Store           string          `json:"store" binding:"required" example:"users"`
	Items           []BulkItem      `json:"items" binding:"required"`
	TransactionID   string          `json:"transaction_id,omitempty"` // Use existing transaction
	TransactionMode TransactionMode `json:"transaction_mode,omitempty" default:"auto_batch" enums:"auto_batch,explicit,single"`
	BatchSize       int             `json:"batch_size,omitempty" default:"100" example:"250"`
}

BulkAddArgs represents arguments for bulk insert operations @Description Bulk insert multiple items with automatic transaction batching

type BulkDeleteArgs

type BulkDeleteArgs struct {
	Database        string          `json:"database,omitempty" example:"dev_db"`
	Store           string          `json:"store" binding:"required" example:"users"`
	Keys            []any           `json:"keys" binding:"required"`
	TransactionID   string          `json:"transaction_id,omitempty"`
	TransactionMode TransactionMode `json:"transaction_mode,omitempty" default:"auto_batch" enums:"auto_batch,explicit,single"`
	BatchSize       int             `json:"batch_size,omitempty" default:"100" example:"250"`
}

BulkDeleteArgs represents arguments for bulk delete operations

type BulkDeleteCategoriesArgs

type BulkDeleteCategoriesArgs struct {
	Database        string          `json:"database,omitempty" example:"dev_db"`
	KBName          string          `json:"kb_name" binding:"required" example:"Notes"`
	CategoryIDs     []sop.UUID      `json:"category_ids" binding:"required"`
	TransactionID   string          `json:"transaction_id,omitempty"`
	TransactionMode TransactionMode `json:"transaction_mode,omitempty" default:"single" enums:"single,explicit"`
}

BulkDeleteCategoriesArgs represents arguments for deleting multiple categories

type BulkDeleteItemsArgs

type BulkDeleteItemsArgs struct {
	Database        string          `json:"database,omitempty" example:"dev_db"`
	KBName          string          `json:"kb_name" binding:"required" example:"Notes"`
	CategoryID      sop.UUID        `json:"category_id" binding:"required"`
	ItemIDs         []sop.UUID      `json:"item_ids" binding:"required"`
	TransactionID   string          `json:"transaction_id,omitempty"`
	TransactionMode TransactionMode `json:"transaction_mode,omitempty" default:"single" enums:"single,explicit"`
}

BulkDeleteItemsArgs represents arguments for deleting multiple items

type BulkItem

type BulkItem struct {
	Key   any            `json:"key" binding:"required" example:"user_123"`
	Value map[string]any `json:"value" binding:"required"`
}

BulkItem represents a single item in a bulk operation

type BulkOperationError

type BulkOperationError struct {
	Index   int    `json:"index"`
	Key     any    `json:"key,omitempty"`
	Message string `json:"message"`
}

BulkOperationError represents an error that occurred during a bulk operation

type BulkOperationMetrics

type BulkOperationMetrics struct {
	TotalItems      int           `json:"total_items"`
	BatchesExecuted int           `json:"batches_executed"`
	AvgBatchTime    time.Duration `json:"avg_batch_time"`
	ItemsPerSecond  float64       `json:"items_per_second"`
}

BulkOperationMetrics contains performance metrics for bulk operations

type BulkOperationResult

type BulkOperationResult struct {
	Success                bool                 `json:"success"`
	Processed              int                  `json:"processed"`
	Failed                 int                  `json:"failed"`
	Duration               time.Duration        `json:"duration"`
	Errors                 []BulkOperationError `json:"errors,omitempty"`
	Metrics                BulkOperationMetrics `json:"metrics"`
	TransactionsCreated    int                  `json:"transactions_created"`     // For auto_batch mode
	TransactionsCommitted  int                  `json:"transactions_committed"`   // For auto_batch mode
	TransactionsRolledBack int                  `json:"transactions_rolled_back"` // On error
}

BulkOperationResult represents the result of a bulk operation

type BulkUpdateArgs

type BulkUpdateArgs struct {
	Database        string          `json:"database,omitempty" example:"dev_db"`
	Store           string          `json:"store" binding:"required" example:"users"`
	Items           []BulkItem      `json:"items" binding:"required"`
	TransactionID   string          `json:"transaction_id,omitempty"`
	TransactionMode TransactionMode `json:"transaction_mode,omitempty" default:"auto_batch" enums:"auto_batch,explicit,single"`
	BatchSize       int             `json:"batch_size,omitempty" default:"100" example:"250"`
}

BulkUpdateArgs represents arguments for bulk update operations

type BulkUpsertCategoriesArgs

type BulkUpsertCategoriesArgs struct {
	Database        string                       `json:"database,omitempty" example:"dev_db"`
	KBName          string                       `json:"kb_name" binding:"required" example:"Notes"`
	Parameters      []memory.UpsertCategoryParam `json:"parameters" binding:"required"`
	TransactionID   string                       `json:"transaction_id,omitempty"`
	TransactionMode TransactionMode              `json:"transaction_mode,omitempty" default:"single" enums:"single,explicit"`
}

BulkUpsertCategoriesArgs represents arguments for upserting multiple categories

type BulkUpsertItemsArgs

type BulkUpsertItemsArgs struct {
	Database        string                                   `json:"database,omitempty" example:"dev_db"`
	KBName          string                                   `json:"kb_name" binding:"required" example:"Notes"`
	Parameters      []memory.UpsertItemParam[map[string]any] `json:"parameters" binding:"required"`
	TransactionID   string                                   `json:"transaction_id,omitempty"`
	TransactionMode TransactionMode                          `json:"transaction_mode,omitempty" default:"single" enums:"single,explicit"`
}

BulkUpsertItemsArgs represents arguments for upserting multiple items

type BulkVectorizeCategoriesArgs

type BulkVectorizeCategoriesArgs struct {
	Database    string     `json:"database,omitempty" example:"dev_db"`
	KBName      string     `json:"kb_name" binding:"required" example:"Notes"`
	CategoryIDs []sop.UUID `json:"category_ids" binding:"required"`
	BatchSize   int        `json:"batch_size,omitempty" default:"100" example:"250"`
}

BulkVectorizeCategoriesArgs represents arguments for vectorizing multiple categories

type BulkVectorizeItemsArgs

type BulkVectorizeItemsArgs struct {
	Database   string     `json:"database,omitempty" example:"dev_db"`
	KBName     string     `json:"kb_name" binding:"required" example:"Notes"`
	CategoryID *sop.UUID  `json:"category_id,omitempty"`
	ItemIDs    []sop.UUID `json:"item_ids,omitempty"`
	BatchSize  int        `json:"batch_size,omitempty" default:"100" example:"250"`
}

BulkVectorizeItemsArgs represents arguments for vectorizing multiple items

type CachedScript

type CachedScript struct {
	Script CompiledScript
	Hash   string
}

type CategoryListResult

type CategoryListResult struct {
	Categories []memory.Category `json:"categories"`
	Total      int               `json:"total"`
}

CategoryListResult represents the result of listing categories

type CompiledScript

type CompiledScript func(ctx context.Context, e *ScriptEngine) error

CompiledScript is a function that executes the compiled script against an engine.

func CompileScript

func CompileScript(script []ScriptInstruction) (CompiledScript, error)

type Config

type Config struct {
	ID                    string            `json:"id"`
	Type                  string            `json:"type,omitempty"` // "standard" (default), "copilot", "policy", etc.
	Name                  string            `json:"name"`
	Description           string            `json:"description"`
	UserPrompt            string            `json:"user_prompt,omitempty"`    // Optional: Custom prompt for the interactive loop (e.g. "Patient> ")
	AssistantName         string            `json:"assistant_name,omitempty"` // Optional: Custom name for the assistant (e.g. "AI Doctor")
	Synonyms              map[string]string `json:"synonyms"`
	SystemPrompt          string            `json:"system_prompt"`
	Policies              []PolicyConfig    `json:"policies"`
	ETL                   ETLConfig         `json:"etl,omitempty"`                     // Configuration for the ETL/Curator pipeline
	Embedder              EmbedderConfig    `json:"embedder,omitempty"`                // Configuration for the embedder
	Generator             GeneratorConfig   `json:"generator,omitempty"`               // Configuration for the LLM generator
	Data                  []DataItem        `json:"data"`                              // For seeding (MVP)
	StoragePath           string            `json:"storage_path,omitempty"`            // Optional: Override default storage path. Will be converted to absolute path.
	DBType                string            `json:"db_type,omitempty"`                 // Optional: "standalone" (default) or "clustered"
	ContentSize           string            `json:"content_size,omitempty"`            // Optional: "small", "medium", "big". Defaults to "medium".
	SkipDeduplication     bool              `json:"skip_deduplication,omitempty"`      // Optional: Skip deduplication phase
	EnableIngestionBuffer bool              `json:"enable_ingestion_buffer,omitempty"` // Optional: Enable Stage 0 buffering for faster ingestion
	Verbose               bool              `json:"verbose,omitempty"`                 // Optional: Enable verbose output (e.g. tool instructions)
	// Cognitive Memory Consolidation (STM to LTM)
	SleepCycleIntervalHours int            `json:"sleep_cycle_interval_hours,omitempty"` // e.g. 1 (run every X hours on the clock). 0 disables hourly.
	IdleSleepTimeoutMinutes int            `json:"idle_sleep_timeout_minutes,omitempty"` // e.g. 5 (run cycle after X minutes of avatar inactivity).
	StubMode                bool           `json:"stub_mode,omitempty"`                  // Optional: If true, tools will return success without executing (for debugging LLM output)
	Agents                  []Config       `json:"agents,omitempty"`                     // Optional: Define agents locally to be referenced by ID
	Pipeline                []PipelineStep `json:"pipeline,omitempty"`                   // Optional: Define a chain of agents
	Params                  map[string]any `json:"params,omitempty"`                     // Type-specific configuration parameters
}

Config defines the structure of the JSON configuration file for an agent.

func LoadConfigFromFile

func LoadConfigFromFile(path string) (*Config, error)

LoadConfigFromFile reads and parses a JSON configuration file.

type ConversationRole

type ConversationRole string

ConversationRole enum

const (
	RoleUser      ConversationRole = "user"
	RoleAssistant ConversationRole = "assistant"
	RoleSystem    ConversationRole = "system"
)

type ConversationThread

type ConversationThread struct {
	ID         sop.UUID `json:"id"`
	RootPrompt string   `json:"root_prompt"` // The seed sentence that started this thread

	// Transcribed Context (Managed by LLM)
	Label        string `json:"label"`         // Short Topic Name (e.g. "Defining Client")
	Category     string `json:"category"`      // e.g. "Business Logic", "Clarification"
	ContextNotes string `json:"context_notes"` // Important notes/context for the LLM

	// The linear exchange of Q&A within this topic
	Exchanges []Interaction `json:"exchanges"`

	// Termination
	Conclusion string `json:"conclusion"` // Summary or Agreement
	Status     string `json:"status"`     // "active", "concluded"
}

ConversationThread represents a single conversational thread. It starts with a root prompt and spins up a Q&A exchange, eventually leading to a conclusion.

type CopilotAgent

type CopilotAgent struct {
	Config Config

	// Encapsulated memory and context boundaries
	Memory *memory.MemoryUnit

	// StoreOpener allows mocking the store creation (e.g. for testing)
	StoreOpener func(ctx context.Context, dbOpts sop.DatabaseOptions, storeName string, tx sop.Transaction) (jsondb.StoreAccessor, error)
	// contains filtered or unexported fields
}

CopilotAgent is a specialized agent for database administration tasks. It implements the ai.Agent interface.

func NewCopilotAgent

func NewCopilotAgent(cfg Config, databases map[string]sop.DatabaseOptions, systemDB *database.Database) *CopilotAgent

NewCopilotAgent creates a new instance of CopilotAgent.

func (*CopilotAgent) Add

func (a *CopilotAgent) Add(ctx context.Context, args AddArgs) (string, error)

Add inserts a single item into a store (first-class API)

func (*CopilotAgent) Ask

func (a *CopilotAgent) Ask(ctx context.Context, query string, cfg *ai.ConfigMap) (string, error)

Ask is the primary entry point for processing a user's conversational query to the AI Copilot.

The functional flow follows a multi-layered ReAct architecture:

  1. Generator Resolution: Dynamically selects the appropriate LLM provider (e.g., Gemini, OpenAI, Claude) based on the active session context or falling back to the system default configuration.

  2. Direct Invocation Handling: Checks if the query is a direct tool invocation (e.g., "/help" or "/clear_memory"). If so, it bypasses the LLM reasoning loop to immediately execute the deterministic command.

  3. Intent Classification (Router): Evaluates if the query should be routed to a specific sub-agent (Avatar/Persona). If the intent indicates a specialized Avatar (e.g., a "Legal Auditor" persona), it delegates execution entirely to that sub-agent.

  4. Context Classification: For generic queries (intent == "OMNI"), it performs a lightweight three-gate classification to identify the semantic domain (e.g., "Spaces" or "Stores"), likely artifacts, and CRUD layer. This stage is classification-only: it updates routing state, but does not assemble prompt slices or mutate System_Tools context.

  5. Episode Metadata Tracking (MRU Cache): Analyzes the user's prior chat exchange inside the short-term episodic memory. If the user remains engaged in the same topic and database context, it pulls the Most-Recently-Used (MRU) semantic boundaries so the LLM retains coherent situational context across turns.

  6. System Prompt Construction: Assembles the multi-part context prompt (using SystemPromptBuilder) linking the Core Persona, Active Playbooks/KBs, stable System Tools context, targeted focused execution/tool guidance derived from the current classification, semantic memory boundaries, and conversation history.

  7. Reasoning Engine Delegation: Packages the assembled context and delegates execution to the ReAct engine. The engine loops autonomously over the LLM generation and local tool executions (API-level tool calling) until it produces a final answer.

  8. Epilogue & Cleanup: Records the completed dialogue and active track-state into the short-term memory transcript, clears the volatile MRU buffer, and returns the final text response to the client.

func (*CopilotAgent) BeginTransaction

func (a *CopilotAgent) BeginTransaction(ctx context.Context, args TransactionArgs) (*TransactionHandle, error)

BeginTransaction starts a new transaction and returns a handle

func (*CopilotAgent) BulkAdd

BulkAdd inserts multiple items with transaction control (first-class API)

func (*CopilotAgent) BulkDelete

func (a *CopilotAgent) BulkDelete(ctx context.Context, args BulkDeleteArgs) (*BulkOperationResult, error)

BulkDelete deletes multiple items with transaction control (first-class API)

func (*CopilotAgent) BulkDeleteCategories

func (a *CopilotAgent) BulkDeleteCategories(ctx context.Context, args BulkDeleteCategoriesArgs) (*SpaceBulkOperationResult, error)

BulkDeleteCategories deletes multiple categories (first-class bulk API)

func (*CopilotAgent) BulkDeleteItems

BulkDeleteItems deletes multiple items (first-class bulk API)

func (*CopilotAgent) BulkUpdate

func (a *CopilotAgent) BulkUpdate(ctx context.Context, args BulkUpdateArgs) (*BulkOperationResult, error)

BulkUpdate updates multiple items with transaction control (first-class API)

func (*CopilotAgent) BulkUpsertCategories

func (a *CopilotAgent) BulkUpsertCategories(ctx context.Context, args BulkUpsertCategoriesArgs) (*SpaceBulkOperationResult, error)

BulkUpsertCategories upserts multiple categories (first-class bulk API)

func (*CopilotAgent) BulkUpsertItems

BulkUpsertItems upserts multiple items (first-class bulk API)

func (*CopilotAgent) BulkVectorizeCategories

func (a *CopilotAgent) BulkVectorizeCategories(ctx context.Context, args BulkVectorizeCategoriesArgs) (*SpaceBulkOperationResult, error)

BulkVectorizeCategories vectorizes multiple categories (first-class bulk API)

func (*CopilotAgent) BulkVectorizeItems

BulkVectorizeItems vectorizes multiple items (first-class bulk API)

func (*CopilotAgent) ClassifyContinuityTaskContext

func (a *CopilotAgent) ClassifyContinuityTaskContext(ctx context.Context, query string, mru *TaskContextClassification, anchor *TaskContextClassification, gen ai.Generator) (*TaskContextClassification, bool, error)

ClassifyContinuityTaskContext is Gate 2: The "Continuity/Switch" Classifier. Used to check if the user is maintaining MRU context or switching topics.

func (*CopilotAgent) ClassifyFocusedTaskContext

func (a *CopilotAgent) ClassifyFocusedTaskContext(ctx context.Context, query, entity, domain, artifact string, gen ai.Generator) (*TaskContextClassification, error)

ClassifyFocusedTaskContext is Gate 1: The "Focused" Classifier. Used when the user explicitly provides Hard Constraints via prefix (e.g. omni:stores:users:). Skips building the Context Outline entirely.

func (*CopilotAgent) ClassifyTaskContext

func (a *CopilotAgent) ClassifyTaskContext(ctx context.Context, query string, gen ai.Generator) (*TaskContextClassification, error)

func (*CopilotAgent) Clone

func (a *CopilotAgent) Clone() ai.Agent[map[string]any]

Clone creates a new isolated instance of the agent sharing read-only components.

func (*CopilotAgent) Close

func (a *CopilotAgent) Close(ctx context.Context) error

Close cleans up the agent's resources.

func (*CopilotAgent) CommitTransaction

func (a *CopilotAgent) CommitTransaction(ctx context.Context, args TransactionCommitArgs) error

CommitTransaction commits an active transaction

func (*CopilotAgent) CreateSpace

func (a *CopilotAgent) CreateSpace(ctx context.Context, args CreateSpaceArgs) (string, error)

CreateSpace creates or opens a Space/KnowledgeBase (first-class API)

func (*CopilotAgent) Delete

func (a *CopilotAgent) Delete(ctx context.Context, args DeleteArgs) (string, error)

Delete deletes a single item from a store (first-class API)

func (*CopilotAgent) DeleteCategory

func (a *CopilotAgent) DeleteCategory(ctx context.Context, args DeleteCategoryArgs) (string, error)

DeleteCategory deletes a single category (first-class API)

func (*CopilotAgent) DeleteItem

func (a *CopilotAgent) DeleteItem(ctx context.Context, args DeleteItemArgs) (string, error)

DeleteItem deletes a single item (first-class API)

func (*CopilotAgent) DeleteSpace

func (a *CopilotAgent) DeleteSpace(ctx context.Context, args DeleteSpaceArgs) (string, error)

DeleteSpace deletes a Space/KnowledgeBase (first-class API)

func (*CopilotAgent) Execute

func (a *CopilotAgent) Execute(ctx context.Context, toolName string, args map[string]any) (string, error)

Execute executes the requested tool against the session payload.

func (*CopilotAgent) ExecuteScript

func (a *CopilotAgent) ExecuteScript(ctx context.Context, args ExecuteScriptArgs) (string, error)

ExecuteScript executes a multi-step script (first-class API)

func (*CopilotAgent) ExportSearchKnowledgeBase

func (a *CopilotAgent) ExportSearchKnowledgeBase(ctx context.Context, db *database.Database, kbName string, query string, catPath string, category string, limit int) (string, error)

ExportSearchKnowledgeBase exports searchKnowledgeBase for testing

func (*CopilotAgent) GetMRUCategory

func (a *CopilotAgent) GetMRUCategory(category string) (string, bool)

GetMRUCategory retrieves a category from the global working memory MRU

func (*CopilotAgent) GetSampleArtifacts

func (a *CopilotAgent) GetSampleArtifacts(ctx context.Context) ([]string, []string, error)

func (*CopilotAgent) InitializePhysicalMemory

func (a *CopilotAgent) InitializePhysicalMemory(ctx context.Context) error

InitializePhysicalMemory creates strictly isolated B-Tree (STM) and Vector (LTM) structures for this Agent ID

func (*CopilotAgent) Join

func (a *CopilotAgent) Join(ctx context.Context, args JoinArgs) (string, error)

Join performs a join operation between two stores (first-class API)

func (*CopilotAgent) ListCategories

func (a *CopilotAgent) ListCategories(ctx context.Context, args ListCategoriesArgs) (*CategoryListResult, error)

ListCategories lists categories with pagination (first-class API)

func (*CopilotAgent) ListItems

func (a *CopilotAgent) ListItems(ctx context.Context, args ListItemsArgs) (*ItemListResult, error)

ListItems lists items with pagination (first-class API)

func (*CopilotAgent) ListTools

func (a *CopilotAgent) ListTools(ctx context.Context) ([]ai.ToolDefinition, error)

ListTools returns the list of available tools.

func (*CopilotAgent) MarkMRUCategory

func (a *CopilotAgent) MarkMRUCategory(category string, context string)

MarkMRUCategory adds or updates a category in the global working memory MRU

func (*CopilotAgent) Open

func (a *CopilotAgent) Open(ctx context.Context) error

Open initializes the agent's resources.

func (*CopilotAgent) ReadSpaceConfig

ReadSpaceConfig reads the configuration of a Space (first-class API)

func (*CopilotAgent) RollbackTransaction

func (a *CopilotAgent) RollbackTransaction(ctx context.Context, args TransactionRollbackArgs) error

RollbackTransaction rolls back an active transaction

func (*CopilotAgent) Search

func (a *CopilotAgent) Search(ctx context.Context, query string, limit int) ([]ai.Hit[map[string]any], error)

Search performs a search using the agent's capabilities.

func (*CopilotAgent) SearchItemsByPath

func (a *CopilotAgent) SearchItemsByPath(ctx context.Context, args SearchItemsByPathArgs) ([]memory.Item[map[string]any], error)

SearchItemsByPath searches items by path (first-class API)

func (*CopilotAgent) Select

func (a *CopilotAgent) Select(ctx context.Context, args SelectArgs) (string, error)

Select retrieves data from a store (first-class API)

func (*CopilotAgent) SetGenerator

func (a *CopilotAgent) SetGenerator(gen ai.Generator)

SetGenerator sets the generator for the agent.

func (*CopilotAgent) SetService

func (a *CopilotAgent) SetService(s *Service)

SetService sets the reference to the main service (for cache invalidation).

func (*CopilotAgent) SetVerbose

func (a *CopilotAgent) SetVerbose(v bool)

SetVerbose enables or disables verbose output.

func (*CopilotAgent) StartSleepCycle

func (a *CopilotAgent) StartSleepCycle(ctx context.Context, hourlyInterval int, idleTimeoutMinutes int, nowFn func() time.Time)

StartSleepCycle launches background consolidators over the isolated Avatar STM migrating Short-Term episodic memories into Semantic Long-Term memory.

func (*CopilotAgent) Update

func (a *CopilotAgent) Update(ctx context.Context, args UpdateArgs) (string, error)

Update updates a single item in a store (first-class API)

func (*CopilotAgent) UpdateSpaceConfig

func (a *CopilotAgent) UpdateSpaceConfig(ctx context.Context, args UpdateSpaceConfigArgs) (string, error)

UpdateSpaceConfig updates the configuration of a Space (first-class API)

func (*CopilotAgent) UpsertCategory

func (a *CopilotAgent) UpsertCategory(ctx context.Context, args UpsertCategoryArgs) (string, error)

UpsertCategory upserts a single category (first-class API)

func (*CopilotAgent) UpsertItem

func (a *CopilotAgent) UpsertItem(ctx context.Context, args UpsertItemArgs) (string, error)

UpsertItem upserts a single item (first-class API)

func (*CopilotAgent) VectorizeSpace

func (a *CopilotAgent) VectorizeSpace(ctx context.Context, args VectorizeSpaceArgs) (string, error)

VectorizeSpace vectorizes an entire Space (first-class API)

type CreateSpaceArgs

type CreateSpaceArgs struct {
	Database string `json:"database,omitempty" example:"dev_db"`
	KBName   string `json:"kb_name" binding:"required" example:"Notes"`
}

CreateSpaceArgs represents arguments for creating/opening a Space

type CuratorConfig

type CuratorConfig struct {
	Type    string         `json:"type"`              // "ollama", "agent"
	ID      string         `json:"id,omitempty"`      // ID of the agent to use (if type="agent")
	Options map[string]any `json:"options,omitempty"` // e.g. {"model": "llama3"}
	Prompt  string         `json:"prompt,omitempty"`  // The instruction for curation
}

type DataItem

type DataItem struct {
	ID          string         `json:"id"`
	Text        string         `json:"text"`
	Description string         `json:"description"`
	Category    string         `json:"category,omitempty"`
	Payload     map[string]any `json:"payload,omitempty"` // Generic payload for structured data (e.g. JSON objects)
}

type Database

type Database interface {
	BeginTransaction(ctx context.Context, mode sop.TransactionMode, maxTime ...time.Duration) (sop.Transaction, error)
	Config() sop.DatabaseOptions
}

Database interface for script execution

type DeferredCleanupCursor

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

DeferredCleanupCursor wraps a ScriptCursor and executes deferred functions on Close.

func (*DeferredCleanupCursor) Close

func (d *DeferredCleanupCursor) Close() error

func (*DeferredCleanupCursor) GetIndexSpecs

func (d *DeferredCleanupCursor) GetIndexSpecs() map[string]*jsondb.IndexSpecification

func (*DeferredCleanupCursor) GetOrderedFields

func (d *DeferredCleanupCursor) GetOrderedFields() []string

func (*DeferredCleanupCursor) Next

type DeleteArgs

type DeleteArgs struct {
	Database string `json:"database,omitempty" example:"dev_db"`
	Store    string `json:"store" binding:"required" example:"users"`
	Key      any    `json:"key" binding:"required" example:"user_123"`
}

DeleteArgs represents arguments for deleting a single item

type DeleteCategoryArgs

type DeleteCategoryArgs struct {
	Database      string   `json:"database,omitempty" example:"dev_db"`
	KBName        string   `json:"kb_name" binding:"required" example:"Notes"`
	CategoryID    sop.UUID `json:"category_id" binding:"required"`
	TransactionID string   `json:"transaction_id,omitempty"`
}

DeleteCategoryArgs represents arguments for deleting a single category

type DeleteItemArgs

type DeleteItemArgs struct {
	Database      string   `json:"database,omitempty" example:"dev_db"`
	KBName        string   `json:"kb_name" binding:"required" example:"Notes"`
	CategoryID    sop.UUID `json:"category_id" binding:"required"`
	ItemID        sop.UUID `json:"item_id" binding:"required"`
	TransactionID string   `json:"transaction_id,omitempty"`
}

DeleteItemArgs represents arguments for deleting a single item

type DeleteSpaceArgs

type DeleteSpaceArgs struct {
	Database string `json:"database,omitempty" example:"dev_db"`
	KBName   string `json:"kb_name" binding:"required" example:"Notes"`
}

DeleteSpaceArgs represents arguments for deleting a Space

type Dependencies

type Dependencies struct {
	AgentRegistry map[string]ai.Agent[map[string]any]
	SystemDB      *database.Database
	Databases     map[string]sop.DatabaseOptions
}

Dependencies holds external dependencies required for agent creation.

type ETLConfig

type ETLConfig struct {
	Curator CuratorConfig `json:"curator,omitempty"`
}

type EmbedderConfig

type EmbedderConfig struct {
	Type        string         `json:"type"`              // "simple" (default), "agent", "ollama", "gemini", or "local"
	AgentID     string         `json:"agent_id"`          // For "agent" type: ID of the agent to use
	Instruction string         `json:"instruction"`       // For "agent" type: Instruction for the agent
	Options     map[string]any `json:"options,omitempty"` // For "local" type: model_path, provider, gpu_layers
}

type EnrichSpaceArgs

type EnrichSpaceArgs struct {
	Database string `json:"database,omitempty" example:"dev_db"`
	KBName   string `json:"kb_name" binding:"required" example:"Notes"`
}

EnrichSpaceArgs represents arguments for enriching a Space

type ErrorResponse

type ErrorResponse struct {
	Error   string `json:"error" example:"store is required"`
	Code    string `json:"code,omitempty" example:"INVALID_REQUEST"`
	Details any    `json:"details,omitempty"`
}

ErrorResponse represents an API error

type EventResultStreamer

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

func NewEventResultStreamer

func NewEventResultStreamer(sendEvent func(string, any)) *EventResultStreamer

func (*EventResultStreamer) BeginArray

func (s *EventResultStreamer) BeginArray()

func (*EventResultStreamer) Close

func (s *EventResultStreamer) Close()

func (*EventResultStreamer) EndArray

func (s *EventResultStreamer) EndArray()

func (*EventResultStreamer) SetMetadata

func (s *EventResultStreamer) SetMetadata(meta map[string]any)

func (*EventResultStreamer) WriteItem

func (s *EventResultStreamer) WriteItem(item any)

type ExecuteScriptArgs

type ExecuteScriptArgs struct {
	Database string              `json:"database,omitempty" example:"dev_db"`
	Script   []ScriptInstruction `json:"script" binding:"required"`
}

ExecuteScriptArgs represents arguments for executing a script

type FilterCursor

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

FilterCursor filters a stream.

func (*FilterCursor) Close

func (fc *FilterCursor) Close() error

func (*FilterCursor) GetOrderedFields

func (fc *FilterCursor) GetOrderedFields() []string

func (*FilterCursor) Next

func (fc *FilterCursor) Next(ctx context.Context) (any, bool, error)

type FilteringStreamer

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

func (*FilteringStreamer) BeginArray

func (fs *FilteringStreamer) BeginArray()

func (*FilteringStreamer) EndArray

func (fs *FilteringStreamer) EndArray()

func (*FilteringStreamer) SetMetadata

func (fs *FilteringStreamer) SetMetadata(meta map[string]any)

func (*FilteringStreamer) WriteItem

func (fs *FilteringStreamer) WriteItem(item any)

type Flusher

type Flusher interface {
	Flush()
}

type GeneratorConfig

type GeneratorConfig struct {
	Type    string         `json:"type"`              // "ollama", "gemini", "chatgpt"
	Options map[string]any `json:"options,omitempty"` // Generator-specific options (model, api_key, etc.)
}

type Interaction

type Interaction struct {
	Role      ConversationRole `json:"role"`
	Content   string           `json:"content"`
	Timestamp int64            `json:"timestamp"`
	Entity    string           `json:"entity,omitempty"`    // OMNI or Avatar name
	ActiveKB  string           `json:"active_kb,omitempty"` // The target KB evaluated in this sequence
}

Interaction represents a single message in the conversation.

type ItemListResult

type ItemListResult struct {
	Items []memory.Item[map[string]any] `json:"items"`
	Total int                           `json:"total"`
}

ItemListResult represents the result of listing items

type JSONStreamer

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

JSONStreamer handles streaming JSON array elements. TODO: add a buffered batching wrapper in the future to reduce transport overhead for large result sets.

func NewJSONStreamer

func NewJSONStreamer(w io.Writer) *JSONStreamer

func NewNDJSONStreamer

func NewNDJSONStreamer(w io.Writer) *JSONStreamer

func (*JSONStreamer) SetFlush

func (s *JSONStreamer) SetFlush(flush bool)

func (*JSONStreamer) SetSuppressStepStart

func (s *JSONStreamer) SetSuppressStepStart(suppress bool)

func (*JSONStreamer) StartStreamingStep

func (s *JSONStreamer) StartStreamingStep(stepType, command, prompt string, stepIndex int) *StepStreamer

StartStreamingStep starts a new step in the JSON stream and returns a StepStreamer. Note: The header is written lazily when the first item is written.

func (*JSONStreamer) Write

func (s *JSONStreamer) Write(step StepExecutionResult)

type JoinArgs

type JoinArgs struct {
	Database        string         `json:"database,omitempty" example:"dev_db"`
	LeftStore       string         `json:"left_store" binding:"required" example:"users"`
	RightStore      string         `json:"right_store" binding:"required" example:"orders"`
	LeftJoinFields  []string       `json:"left_join_fields" binding:"required" example:"user_id"`
	RightJoinFields []string       `json:"right_join_fields" binding:"required" example:"user_id"`
	JoinType        string         `json:"join_type,omitempty" default:"inner" enums:"inner,left,right"`
	Fields          []string       `json:"fields,omitempty"`
	Limit           int            `json:"limit,omitempty" default:"10"`
	Direction       string         `json:"direction,omitempty" enums:"asc,desc"`
	Action          string         `json:"action,omitempty" enums:"delete_left,update_left"`
	UpdateValues    map[string]any `json:"update_values,omitempty"`
}

JoinArgs represents arguments for joining two stores

type JoinPlan

type JoinPlan struct {
	Strategy     int
	IndexFields  []string // Ordered list of fields in the Index
	PrefixFields []string // Fields from ON clause that match the Index Prefix
	IsComposite  bool     // True if the Store uses a Map Key (Composite)
	Ascending    bool     // True if the first prefix field is Ascending
}

func AnalyzeJoinPlan

func AnalyzeJoinPlan(info sop.StoreInfo, rightFields []string) (JoinPlan, string)

AnalyzeJoinPlan analyzes the store schema and join fields to determine the best execution strategy.

type JoinProcessor

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

JoinProcessor handles the execution of a join operation between two stores.

func (*JoinProcessor) Execute

func (jp *JoinProcessor) Execute() (string, error)

Execute runs the join operation.

type JoinRightCursor

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

JoinRightCursor performs a streaming join with probing and scanning support. It replaces both JoinCursor (Lookup) and NestedLoopJoinCursor (Scan).

func (*JoinRightCursor) Close

func (jc *JoinRightCursor) Close() error

func (*JoinRightCursor) GetIndexSpecs

func (jc *JoinRightCursor) GetIndexSpecs() map[string]*jsondb.IndexSpecification

func (*JoinRightCursor) Next

func (jc *JoinRightCursor) Next(ctx context.Context) (any, bool, error)

func (*JoinRightCursor) NextOptimized

func (jc *JoinRightCursor) NextOptimized(ctx context.Context) (any, bool, error)

NextOptimized is the "Execution Phase".

type KnowledgeChunk

type KnowledgeChunk struct {
	Category string `json:"category"`
	Title    string `json:"title"`
	Content  string `json:"content"`
}

type LayerInfo

type LayerInfo struct {
	Name string   `json:"name"`
	CRUD []string `json:"crud"`
}

type LimitCursor

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

LimitCursor limits a stream.

func (*LimitCursor) Close

func (lc *LimitCursor) Close() error

func (*LimitCursor) GetIndexSpecs

func (lc *LimitCursor) GetIndexSpecs() map[string]*jsondb.IndexSpecification

func (*LimitCursor) GetOrderedFields

func (lc *LimitCursor) GetOrderedFields() []string

func (*LimitCursor) Next

func (lc *LimitCursor) Next(ctx context.Context) (any, bool, error)

type ListCategoriesArgs

type ListCategoriesArgs struct {
	Database      string `json:"database,omitempty" example:"dev_db"`
	KBName        string `json:"kb_name" binding:"required" example:"Notes"`
	Limit         int    `json:"limit,omitempty" default:"100" example:"50"`
	Offset        int    `json:"offset,omitempty" default:"0" example:"0"`
	ParentPath    string `json:"parent_path,omitempty" example:"Root/SubCategory"`
	TransactionID string `json:"transaction_id,omitempty"`
}

ListCategoriesArgs represents arguments for listing categories

type ListCursor

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

ListCursor wraps a slice of maps.

func (*ListCursor) Close

func (lc *ListCursor) Close() error

func (*ListCursor) Next

func (lc *ListCursor) Next(ctx context.Context) (any, bool, error)

type ListItemsArgs

type ListItemsArgs struct {
	Database      string   `json:"database,omitempty" example:"dev_db"`
	KBName        string   `json:"kb_name" binding:"required" example:"Notes"`
	CategoryID    sop.UUID `json:"category_id,omitempty"`
	Limit         int      `json:"limit,omitempty" default:"100" example:"50"`
	Offset        int      `json:"offset,omitempty" default:"0" example:"0"`
	TransactionID string   `json:"transaction_id,omitempty"`
}

ListItemsArgs represents arguments for listing items

type MRUItem

type MRUItem struct {
	Category     string
	LastAccessed int64
	Context      string
	Source       string
	Scope        string
}

MRUItem represents a single category currently in working memory

type MintToSpaceArgs

type MintToSpaceArgs struct {
	KBName   string `json:"kb_name" binding:"required" example:"Notes"`
	Content  string `json:"content" binding:"required"`
	Category string `json:"category,omitempty" example:"General"`
}

MintToSpaceArgs represents arguments for minting content to a Space

type MockItem

type MockItem struct {
	Key   any
	Value any
}

type MockStore

type MockStore struct {
	Name  string
	Items []MockItem
	// contains filtered or unexported fields
}

MockStore implements jsondb.StoreAccessor using an in-memory slice. It is defined in package 'agent' to be available for internal tests.

func NewMockStore

func NewMockStore(name string, items []MockItem) *MockStore

func (*MockStore) Add

func (m *MockStore) Add(ctx context.Context, key any, value any) (bool, error)

func (*MockStore) FindInDescendingOrder

func (m *MockStore) FindInDescendingOrder(ctx context.Context, key any) (bool, error)

func (*MockStore) FindOne

func (m *MockStore) FindOne(ctx context.Context, key any, first bool) (bool, error)

func (*MockStore) First

func (m *MockStore) First(ctx context.Context) (bool, error)

func (*MockStore) GetCurrentKey

func (m *MockStore) GetCurrentKey() any

func (*MockStore) GetCurrentValue

func (m *MockStore) GetCurrentValue(ctx context.Context) (any, error)

func (*MockStore) GetCurrentValueNoLock

func (m *MockStore) GetCurrentValueNoLock(ctx context.Context) (any, error)

func (*MockStore) GetStoreInfo

func (m *MockStore) GetStoreInfo() sop.StoreInfo

func (*MockStore) ItemExists

func (m *MockStore) ItemExists(ctx context.Context, key any) (bool, error)

func (*MockStore) Last

func (m *MockStore) Last(ctx context.Context) (bool, error)

func (*MockStore) Next

func (m *MockStore) Next(ctx context.Context) (bool, error)

func (*MockStore) Previous

func (m *MockStore) Previous(ctx context.Context) (bool, error)

func (*MockStore) RLockCurrentItem

func (m *MockStore) RLockCurrentItem(ctx context.Context) error

func (*MockStore) Remove

func (m *MockStore) Remove(ctx context.Context, key any) (bool, error)

func (*MockStore) RemoveCurrentItem

func (m *MockStore) RemoveCurrentItem(ctx context.Context) error

Stubs for interface compliance

func (*MockStore) Update

func (m *MockStore) Update(ctx context.Context, key any, value any) (bool, error)

type MultiCursor

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

MultiCursor chains multiple cursors.

func (*MultiCursor) Close

func (mc *MultiCursor) Close() error

func (*MultiCursor) Next

func (mc *MultiCursor) Next(ctx context.Context) (any, bool, error)

type NativeReActEngine

type NativeReActEngine struct {
	EnableObfuscation bool
	MaxTokens         int
}

NativeReActEngine implements ReasoningEngine using native LLM API tool calling.

func (*NativeReActEngine) Run

Run executes the orchestration loop relying on native tool calls.

type OrderedFieldsProvider

type OrderedFieldsProvider interface {
	GetOrderedFields() []string
}

OrderedFieldsProvider allows cursors to expose the list of fields in order.

type OrderedKey

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

func (OrderedKey) MarshalJSON

func (o OrderedKey) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler to enforce field order.

type OrderedMap

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

func (OrderedMap) MarshalJSON

func (o OrderedMap) MarshalJSON() ([]byte, error)

type PendingUserConfirmation

type PendingUserConfirmation struct {
	Kind         string
	SpaceName    string
	DatabaseName string
}

type PipelineAgent

type PipelineAgent struct {
	ID     string
	Config *Config
}

func (PipelineAgent) MarshalJSON

func (pa PipelineAgent) MarshalJSON() ([]byte, error)

func (*PipelineAgent) UnmarshalJSON

func (pa *PipelineAgent) UnmarshalJSON(data []byte) error

type PipelineStep

type PipelineStep struct {
	Agent    PipelineAgent `json:"agent"`
	OutputTo string        `json:"output_to,omitempty"` // "context", "next_step" (default)
}

type PolicyAgent

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

PolicyAgent is a specialized agent that enforces policies. It implements the Agent interface but focuses on validation rather than generation.

func NewPolicyAgent

func NewPolicyAgent(id string, policy ai.PolicyEngine, classifier ai.Classifier) *PolicyAgent

NewPolicyAgent creates a new PolicyAgent.

func (*PolicyAgent) Ask

func (p *PolicyAgent) Ask(ctx context.Context, query string, cfg *ai.ConfigMap) (string, error)

Ask evaluates the input against the policy. If the policy passes, it returns the input (or a transformed version). If the policy fails, it returns an error.

func (*PolicyAgent) Close

func (p *PolicyAgent) Close(ctx context.Context) error

Close is a no-op for PolicyAgent.

func (*PolicyAgent) ID

func (p *PolicyAgent) ID() string

ID returns the agent ID.

func (*PolicyAgent) Open

func (p *PolicyAgent) Open(ctx context.Context) error

Open is a no-op for PolicyAgent.

func (*PolicyAgent) Search

func (p *PolicyAgent) Search(ctx context.Context, query string, limit int) ([]ai.Hit[map[string]any], error)

Search is not supported for PolicyAgent, but implemented to satisfy the interface.

type PolicyConfig

type PolicyConfig struct {
	ID         string `json:"id,omitempty"` // Optional: ID for referencing in pipeline
	Type       string `json:"type"`         // e.g. "profanity"
	MaxStrikes int    `json:"max_strikes"`  // e.g. 3
}

type ProjectCursor

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

ProjectCursor projects fields from a stream.

func (*ProjectCursor) Close

func (pc *ProjectCursor) Close() error

func (*ProjectCursor) GetOrderedFields

func (pc *ProjectCursor) GetOrderedFields() []string

func (*ProjectCursor) Next

func (pc *ProjectCursor) Next(ctx context.Context) (any, bool, error)

type ProjectionField

type ProjectionField struct {
	Src string
	Dst string
}

type PromptBudgetProfile

type PromptBudgetProfile struct {
	TotalChars            int
	ComponentCharBudgets  map[PromptComponent]int
	TrimPriorityLowToHigh []PromptComponent
}

PromptBudgetProfile caps prompt growth per component and overall.

type PromptBudgetReport

type PromptBudgetReport struct {
	OriginalTotalChars int                         `json:"original_total_chars"`
	FinalTotalChars    int                         `json:"final_total_chars"`
	ComponentStats     []PromptComponentBudgetStat `json:"component_stats"`
}

PromptBudgetReport captures the effect of prompt budgeting on the final prompt.

func (PromptBudgetReport) TrimmedComponents

func (r PromptBudgetReport) TrimmedComponents() []PromptComponentBudgetStat

TrimmedComponents returns only the components that were reduced under budget.

type PromptComponent

type PromptComponent string

PromptComponent represents the standard ingredients of our system prompt.

const (
	ComponentPersona        PromptComponent = "persona"
	ComponentSemanticMemory PromptComponent = "semantic_memory"
	ComponentSystemTools    PromptComponent = "system_tools"
	ComponentRecipes        PromptComponent = "workflow_recipes"
	ComponentPlaybooks      PromptComponent = "playbooks"
	ComponentFocusedContext PromptComponent = "focused_execution_context"
	ComponentSchema         PromptComponent = "schema"
	ComponentHistory        PromptComponent = "conversation_history"
	ComponentUserQuery      PromptComponent = "user_query"
)

type PromptComponentBudgetStat

type PromptComponentBudgetStat struct {
	Component     PromptComponent `json:"component"`
	OriginalChars int             `json:"original_chars"`
	FinalChars    int             `json:"final_chars"`
}

PromptComponentBudgetStat captures how a single component changed under budgeting.

func (PromptComponentBudgetStat) Trimmed

func (s PromptComponentBudgetStat) Trimmed() bool

Trimmed reports whether the component lost content under budgeting.

type PromptElement

type PromptElement struct {
	Component PromptComponent `json:"component"`
	Content   string          `json:"content"`
}

PromptElement represents a single block of context for the LLM.

type ProviderDetails

type ProviderDetails struct {
	// Provider specifies which LLM provider to use (e.g., "gemini", "chatgpt", "claude")
	Provider string

	// Model optionally specifies a specific model within the provider (e.g., "gemini-2.0-flash-thinking-exp")
	Model string

	// APIKey provides a transient API key override for the provider
	APIKey string

	// BaseURL provides a transient base URL override for the provider
	BaseURL string
}

ProviderDetails carries runtime provider configuration for generator selection.

type ReadSpaceConfigArgs

type ReadSpaceConfigArgs struct {
	Database string `json:"database,omitempty" example:"dev_db"`
	KBName   string `json:"kb_name" binding:"required" example:"Notes"`
}

ReadSpaceConfigArgs represents arguments for reading Space configuration

type RecipeItem

type RecipeItem struct {
	ID          string
	Kind        string
	Scope       string
	Domain      string
	Topic       string
	Trigger     string
	Protocol    []string
	Invariants  []string
	AntiPattern []string
	Tags        []string
	Confidence  float64
	Source      string
}

RecipeItem captures a reusable protocol that can be selected independently of facts. Facts tell the model what is true; recipes tell it how to proceed.

type ReducingStreamer

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

ReducingStreamer caps the buffered result payload as items are generated, which keeps the LLM-facing tool result bounded without holding the full row set in memory.

func (*ReducingStreamer) BeginArray

func (rs *ReducingStreamer) BeginArray()

func (*ReducingStreamer) EndArray

func (rs *ReducingStreamer) EndArray()

func (*ReducingStreamer) SetMetadata

func (rs *ReducingStreamer) SetMetadata(meta map[string]any)

func (*ReducingStreamer) WriteItem

func (rs *ReducingStreamer) WriteItem(item any)

type RefinementProposal

type RefinementProposal struct {
	ScriptName     string
	Category       string
	OriginalScript ai.Script
	NewScript      ai.Script
	Description    string   // The new summary description
	NewParams      []string // List of new parameters
	Replacements   []string // Human readable list of replacements
}

RefinementProposal holds the proposed changes for a script.

type Registry

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

Registry manages the available tools.

func NewRegistry

func NewRegistry() *Registry

NewRegistry creates a new tool registry.

func (*Registry) GeneratePrompt

func (r *Registry) GeneratePrompt() string

GeneratePrompt generates the tools description for the LLM prompt.

func (*Registry) Get

func (r *Registry) Get(name string) (ToolDefinition, bool)

Get retrieves a tool definition by name.

func (*Registry) List

func (r *Registry) List() []ToolDefinition

List returns all registered tools.

func (*Registry) Register

func (r *Registry) Register(name, description, argsSchema string, handler ToolHandler)

Register adds a tool to the registry.

func (*Registry) RegisterHidden

func (r *Registry) RegisterHidden(name, description, argsSchema string, handler ToolHandler)

RegisterHidden adds a hidden tool to the registry (not shown in prompt).

func (*Registry) RegisterWithUI

func (r *Registry) RegisterWithUI(name, shortDesc, description, argsSchema string, handler ToolHandler)

RegisterWithUI adds a tool to the registry with a short, user-friendly UI description.

type ReproMockTransaction

type ReproMockTransaction struct {
	sop.Transaction
	Stores map[string]jsondb.StoreAccessor
}

func NewReproMockTransaction

func NewReproMockTransaction() *ReproMockTransaction

func (*ReproMockTransaction) RegisterStore

func (mx *ReproMockTransaction) RegisterStore(name string, store jsondb.StoreAccessor)

Helper to register stores for the transaction to find

type ResultEmitter

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

ResultEmitter helps stream results to the UI or reduce them if no live streamer is present.

func NewResultEmitter

func NewResultEmitter(ctx context.Context) *ResultEmitter

func (*ResultEmitter) Emit

func (re *ResultEmitter) Emit(item any)

func (*ResultEmitter) Finalize

func (re *ResultEmitter) Finalize() string

func (*ResultEmitter) SetColumns

func (re *ResultEmitter) SetColumns(cols []string)

func (*ResultEmitter) Start

func (re *ResultEmitter) Start()

type RightOuterJoinStoreCursor

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

RightOuterJoinStoreCursor implements Right Outer Join where the Right side is a Store. It iterates the Right Store (Driver) and looks up matches in the Left List (Lookup).

func (*RightOuterJoinStoreCursor) Close

func (c *RightOuterJoinStoreCursor) Close() error

func (*RightOuterJoinStoreCursor) Next

type RunnerSession

type RunnerSession struct {
	Playback              bool // True if a script is currently being executed
	AutoSave              bool // If true, the draft is saved to DB after every step
	CurrentScript         *ai.Script
	CurrentScriptName     string // Name of the script being drafted
	CurrentScriptCategory string // Category for the script being drafted
	// TODO: Transaction has to be revisited, specially the interplay to SessionPayload.Transaction.
	Transaction sop.Transaction
	CurrentDB   string         // The database the transaction is bound to
	Variables   map[string]any // Session-scoped variables (e.g. cached stores)
	// Verbose is a temporary bridge for carried runtime state between asks when the caller does not
	// provide a hydrated SessionPayload. The durable preference should move through LTM -> MRU ->
	// SessionPayload instead of living here as a storage authority.
	Verbose  bool
	LastStep *ai.ScriptStep
	// LastInteractionSteps tracks the number of steps added/executed in the last user interaction.
	LastInteractionSteps int
	// LastInteractionToolCalls buffers the tool calls from the last interaction for refactoring.
	LastInteractionToolCalls []ai.ScriptStep

	// Contextual Working Memory for the Session
	MRU                 []MRUItem
	MRUMu               sync.RWMutex
	ConversationHistory string

	// PendingRefinement holds the proposed changes for a script from /script refine
	PendingRefinement *RefinementProposal
	// PendingConfirmation holds a user confirmation gate for destructive actions.
	PendingConfirmation *PendingUserConfirmation

	// Memory holds the structured Short-Term Memory of the session.
	// It replaces the flat History slice with threaded topics.
	Memory *ShortTermMemory
	// contains filtered or unexported fields
}

RunnerSession holds the state for the current agent execution session, including script drafting and transaction management. This represents the Short-Term / Working Memory of the Agent.

func NewRunnerSession

func NewRunnerSession() *RunnerSession

NewRunnerSession creates a new runner session.

func (*RunnerSession) GetCurrentDB

func (rs *RunnerSession) GetCurrentDB() string

func (*RunnerSession) IsVerbose

func (rs *RunnerSession) IsVerbose() bool

func (*RunnerSession) SetCurrentDB

func (rs *RunnerSession) SetCurrentDB(db string)

func (*RunnerSession) SetVerbose

func (rs *RunnerSession) SetVerbose(enabled bool)

type ScriptContext

type ScriptContext struct {
	Variables      map[string]any
	Transactions   map[string]sop.Transaction
	TxToDB         map[sop.Transaction]Database // Mapping from Transaction to its Database
	Stores         map[string]jsondb.StoreAccessor
	Databases      map[string]Database
	LastUpdatedVar string // helper to track the prioritization of variable draining
}

ScriptContext holds the state of the script execution.

func NewScriptContext

func NewScriptContext() *ScriptContext

type ScriptCursor

type ScriptCursor interface {
	Next(ctx context.Context) (any, bool, error)
	Close() error
}

ScriptCursor represents a streaming iterator for script operations.

type ScriptEngine

type ScriptEngine struct {
	Context         *ScriptContext
	ResolveDatabase func(name string) (Database, error)
	FunctionHandler func(ctx context.Context, name string, args map[string]any) (any, error)
	LastResult      any
	ReturnValue     any
	HasReturned     bool
	StoreOpener     func(ctx context.Context, dbOpts sop.DatabaseOptions, storeName string, tx sop.Transaction) (jsondb.StoreAccessor, error)
	Deferred        []func(context.Context, *ScriptEngine) error
}

ScriptEngine executes scripts.

func NewScriptEngine

func NewScriptEngine(ctx *ScriptContext, dbResolver func(string) (Database, error)) *ScriptEngine

func (*ScriptEngine) Add

func (e *ScriptEngine) Add(ctx context.Context, args map[string]any) (any, error)

func (*ScriptEngine) BeginTx

func (e *ScriptEngine) BeginTx(ctx context.Context, args map[string]any) (sop.Transaction, error)

func (*ScriptEngine) CallFunction

func (e *ScriptEngine) CallFunction(ctx context.Context, args map[string]any) (any, error)

func (*ScriptEngine) CallScript

func (e *ScriptEngine) CallScript(ctx context.Context, args map[string]any) (any, error)

func (*ScriptEngine) CommitTx

func (e *ScriptEngine) CommitTx(ctx context.Context, args map[string]any) (err error)

func (*ScriptEngine) Compile

func (e *ScriptEngine) Compile(script []ScriptInstruction) (CompiledScript, error)

func (*ScriptEngine) Delete

func (e *ScriptEngine) Delete(ctx context.Context, input any, args map[string]any) ([]any, error)

func (*ScriptEngine) Dispatch deprecated

func (e *ScriptEngine) Dispatch(ctx context.Context, instr ScriptInstruction) error

Deprecated: Use Compile/Execute instead

func (*ScriptEngine) Execute

func (e *ScriptEngine) Execute(ctx context.Context, script []ScriptInstruction) error

func (*ScriptEngine) ExecuteKBManagement

func (e *ScriptEngine) ExecuteKBManagement(ctx context.Context, op string, args map[string]any, input any) (any, error)

func (*ScriptEngine) Filter

func (e *ScriptEngine) Filter(ctx context.Context, input any, args map[string]any) (any, error)

func (*ScriptEngine) Find

func (e *ScriptEngine) Find(ctx context.Context, args map[string]any) (bool, error)

func (*ScriptEngine) First

func (e *ScriptEngine) First(ctx context.Context, args map[string]any) (bool, error)

func (*ScriptEngine) GetCurrentKey

func (e *ScriptEngine) GetCurrentKey(ctx context.Context, args map[string]any) (any, error)

func (*ScriptEngine) GetCurrentValue

func (e *ScriptEngine) GetCurrentValue(ctx context.Context, args map[string]any) (any, error)

func (*ScriptEngine) If

func (e *ScriptEngine) If(ctx context.Context, args map[string]any) error

func (*ScriptEngine) Inspect

func (e *ScriptEngine) Inspect(ctx context.Context, args map[string]any) (any, error)

func (*ScriptEngine) Join

func (e *ScriptEngine) Join(ctx context.Context, input any, args map[string]any) (any, error)

func (*ScriptEngine) JoinRight

func (e *ScriptEngine) JoinRight(ctx context.Context, input any, args map[string]any) (any, error)

JoinRight is a pipeline-friendly alias for Join. It expects the input to be the Left stream, and the 'store' argument to be the Right store.

func (*ScriptEngine) Last

func (e *ScriptEngine) Last(ctx context.Context, args map[string]any) (bool, error)

func (*ScriptEngine) Limit

func (e *ScriptEngine) Limit(ctx context.Context, input any, args map[string]any) (any, error)

func (*ScriptEngine) ListAppend

func (e *ScriptEngine) ListAppend(ctx context.Context, input any, args map[string]any) (any, error)

func (*ScriptEngine) Loop

func (e *ScriptEngine) Loop(ctx context.Context, args map[string]any) error

func (*ScriptEngine) MapMerge

func (e *ScriptEngine) MapMerge(ctx context.Context, args map[string]any) (map[string]any, error)

func (*ScriptEngine) Next

func (e *ScriptEngine) Next(ctx context.Context, args map[string]any) (bool, error)

func (*ScriptEngine) OpenDB

func (e *ScriptEngine) OpenDB(args map[string]any) (Database, error)

func (*ScriptEngine) OpenStore

func (e *ScriptEngine) OpenStore(ctx context.Context, args map[string]any) (jsondb.StoreAccessor, error)

func (*ScriptEngine) Previous

func (e *ScriptEngine) Previous(ctx context.Context, args map[string]any) (bool, error)

func (*ScriptEngine) Project

func (e *ScriptEngine) Project(ctx context.Context, input any, args map[string]any) (any, error)

func (*ScriptEngine) RollbackTx

func (e *ScriptEngine) RollbackTx(ctx context.Context, args map[string]any) error

func (*ScriptEngine) Scan

func (e *ScriptEngine) Scan(ctx context.Context, args map[string]any, input any) (any, error)

func (*ScriptEngine) Sort

func (e *ScriptEngine) Sort(ctx context.Context, input any, args map[string]any) (any, error)

func (*ScriptEngine) Update

func (e *ScriptEngine) Update(ctx context.Context, input any, args map[string]any) ([]any, error)

handleInto handles the 'into' argument for JoinResult.

type ScriptInstruction

type ScriptInstruction struct {
	Name      string         `json:"name"`       // User-defined name for the step
	Op        string         `json:"op"`         // Operation name
	Args      map[string]any `json:"args"`       // Arguments
	InputVar  string         `json:"input_var"`  // Variable to use as input (optional)
	ResultVar string         `json:"result_var"` // Variable to store result (optional)
}

ScriptInstruction represents a single operation in the script.

func SanitizeScript

func SanitizeScript(script []ScriptInstruction) []ScriptInstruction

SanitizeScript exposes the execution-path sanitization pass for reuse.

type ScriptRunContext

type ScriptRunContext struct {
	// JSONStreamer handles streaming JSON array elements for script execution output
	JSONStreamer *JSONStreamer

	// SuppressInternalStepStart suppresses step_start events in streamed output
	SuppressInternalStepStart bool

	// StepIndex tracks the current step number in script execution
	StepIndex int

	// Verbose enables detailed logging and progress reporting for script steps
	Verbose bool

	// UseNDJSON indicates whether to use newline-delimited JSON format instead of JSON array
	UseNDJSON bool

	// CurrentScriptCategory tracks the category of the currently executing script
	CurrentScriptCategory string

	// StringBuilderMutex protects concurrent writes to the script output string builder
	StringBuilderMutex *sync.Mutex
}

ScriptRunContext carries all dependencies needed for script execution orchestration. This replaces the pattern of hiding script orchestration state in context.Context. Affects Phase 3 context keys: CtxKeyJSONStreamer, CtxKeySuppressInternalStepStart, "step_index", "verbose", CtxKeyUseNDJSON, CtxKeyCurrentScriptCategory

type SearchItemsByPathArgs

type SearchItemsByPathArgs struct {
	Database      string                   `json:"database,omitempty" example:"dev_db"`
	KBName        string                   `json:"kb_name" binding:"required" example:"Notes"`
	Parameters    []memory.PathSearchParam `json:"parameters" binding:"required"`
	TransactionID string                   `json:"transaction_id,omitempty"`
}

SearchItemsByPathArgs represents arguments for searching items by path

type SelectArgs

type SelectArgs struct {
	Database     string         `json:"database,omitempty" example:"dev_db"`
	Store        string         `json:"store" binding:"required" example:"users"`
	Key          any            `json:"key,omitempty"`
	KeyMatch     any            `json:"key_match,omitempty"`
	Value        map[string]any `json:"value,omitempty"`
	Filter       map[string]any `json:"filter,omitempty"`
	Fields       []string       `json:"fields,omitempty"`
	Limit        int            `json:"limit,omitempty" default:"10"`
	OrderBy      string         `json:"order_by,omitempty"`
	Direction    string         `json:"direction,omitempty" enums:"asc,desc"`
	Action       string         `json:"action,omitempty" enums:"delete,update"`
	UpdateValues map[string]any `json:"update_values,omitempty"`
}

SelectArgs represents arguments for selecting data from a store

type Service

type Service struct {
	EnableObfuscation bool
	// Feature Flags
	EnableHistoryInjection bool
	EnableShortTermMemory  bool
	// contains filtered or unexported fields
}

Service is a generic agent service that operates on any Domain.

func NewService

func NewService(domain ai.Domain[map[string]any], systemDB *database.Database, databases map[string]sop.DatabaseOptions, generator ai.Generator, pipeline []PipelineStep, registry map[string]ai.Agent[map[string]any], enableObfuscation bool) *Service

NewService creates a new agent service for a specific domain.

func (*Service) Ask

func (s *Service) Ask(ctx context.Context, query string, cfg *ai.ConfigMap) (string, error)

Ask is the public interface method that maintains backward compatibility with ai.Agent interface. It extracts configuration from ConfigMap (not context!), constructs an AskRequest, and delegates to ask().

Proper separation of concerns: - context.Context: Only for cancellation, deadlines, request-scoped tracing - cfg *ConfigMap: For all parameter passing (session, executor, writer, etc.)

func (*Service) Clone

func (s *Service) Clone() ai.Agent[map[string]any]

func (*Service) Close

func (s *Service) Close(ctx context.Context) error

Close cleans up the agent service.

func (*Service) Domain

func (s *Service) Domain() ai.Domain[map[string]any]

Domain returns the underlying domain of the service.

func (*Service) GetLastToolInstructions

func (s *Service) GetLastToolInstructions() string

GetLastToolInstructions returns the JSON instructions of the last executed tool.

func (*Service) GetSessionMode

func (s *Service) GetSessionMode() SessionMode

GetSessionMode returns the current mode of the session.

func (*Service) InitializeShortTermMemory

func (s *Service) InitializeShortTermMemory(ctx context.Context) error

InitializeShortTermMemory buffers the DDL creation of the active scratchpad B-Tree store. This MUST be called sequentially during Service setup before DML (logEpisode) operations begin.

func (*Service) InitializeUserSession

func (s *Service) InitializeUserSession(ctx context.Context, userID string) error

InitializeUserSession explicitly creates the DDL components required for a user's workflow. This creates the User's Long-Term Memory (LTM) Vector Knowledge Base and any other dependencies that must exist before the ReAct loop starts querying with ForReading transactions.

func (*Service) LastInteractionToolCallsSnapshot

func (s *Service) LastInteractionToolCallsSnapshot() []ai.ScriptStep

func (*Service) ListTools

func (s *Service) ListTools(ctx context.Context) ([]ai.ToolDefinition, error)

ListTools exposes the registered tool definitions for the service-backed runtime path.

func (*Service) Open

func (s *Service) Open(ctx context.Context) error

Open initializes the agent service.

func (*Service) PlayScript

func (s *Service) PlayScript(ctx context.Context, name string, category string, args map[string]any, w io.Writer) error

PlayScript executes a script by name with provided arguments and streams the output to the writer.

func (*Service) RecordStep

func (s *Service) RecordStep(ctx context.Context, step ai.ScriptStep)

RecordStep implements the ScriptRecorder interface.

func (*Service) RefactorLastSteps

func (s *Service) RefactorLastSteps(count int, mode string, name string) error

RefactorLastSteps implements the ScriptRecorder interface

func (*Service) RunPipeline

func (s *Service) RunPipeline(ctx context.Context, input string, cfg *ai.ConfigMap) (string, error)

RunPipeline executes the configured chain of agents.

func (*Service) RunScript

func (s *Service) RunScript(ctx context.Context, name string, category string, args map[string]any) (string, error)

RunScript executes a script by name with provided arguments and returns the output as a string. This is a convenience wrapper around PlayScript for non-streaming use cases.

func (*Service) RunnerSession

func (s *Service) RunnerSession() *RunnerSession

Clone creates a new isolated instance of the agent sharing read-only components.

func (*Service) Search

func (s *Service) Search(ctx context.Context, query string, limit int) ([]ai.Hit[map[string]any], error)

Search performs a semantic search in the domain's knowledge base. It enforces policies and uses the domain's embedder. This implements HYBRID SEARCH:

  • Semantic/Vector search (embedding similarity)
  • Text/BM25 search (keyword matching) when text index is configured
  • Results merged using Reciprocal Rank Fusion (RRF)
  • CategoryPath search (when explicit path detected in query)

Used by retrieveKnowledge to automatically enrich LLM context in Ask flow.

func (*Service) SeedSemanticBaseKnowledge

func (s *Service) SeedSemanticBaseKnowledge(jsonFilePath string)

func (*Service) SetFeature

func (s *Service) SetFeature(feature string, enabled bool)

SetFeature allows toggling of agent features at runtime.

type ServiceAskOptions

type ServiceAskOptions struct {
	// Database to use for this query (overrides session default)
	Database *database.Database

	// Payload contains session state: current DB, selected KBs, transaction, variables, etc.
	Payload *ai.SessionPayload

	// Executor for running tool calls during ReAct loops (commands, queries, scripts)
	Executor ai.ToolExecutor

	// Writer for streaming output in real-time (optional)
	Writer io.Writer

	// Recorder captures script steps for playback or audit (optional)
	Recorder ai.ScriptRecorder

	// DefaultFormat for query results: "csv", "json", "table", etc.
	DefaultFormat string

	// EventStreamer receives structured events: tool calls, progress, errors (optional)
	EventStreamer func(string, any)

	// ProgressSink receives progress messages during long operations (optional)
	ProgressSink func(string)

	// Generator optionally overrides the default LLM provider
	Generator ai.Generator

	// ProviderDetails for runtime provider configuration (API key, base URL, model)
	ProviderDetails *ProviderDetails

	// IsNewTopic indicates this query starts a new conversation topic
	IsNewTopic bool

	// ForcedDBName overrides database selection logic (internal use)
	ForcedDBName string
}

ServiceAskOptions contains explicit, typed parameters for Service.Ask(). This is the user-facing struct with IDE support, type safety, and clear documentation. It gets converted from generic ConfigMap at the Agent interface boundary.

Example usage:

// Working with the Agent interface (generic):
cfg := ai.NewConfigMap()
cfg.Set("database", myDB)
cfg.Set("payload", sessionPayload)
response, err := agent.Ask(ctx, query, cfg)

// Working with Service directly (explicit, typed):
opts := &ServiceAskOptions{
    Database: myDB,
    Payload:  sessionPayload,
    Verbose:  true,
}
// Convert to ConfigMap for Agent interface
cfg := optsToConfigMap(opts)
response, err := service.Ask(ctx, query, cfg)

type ServiceToolExecutor

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

ServiceToolExecutor delegates tool execution to registered agents. It carries explicit ToolExecutionContext to make dependencies visible.

func (*ServiceToolExecutor) Execute

func (e *ServiceToolExecutor) Execute(ctx context.Context, toolName string, args map[string]any) (string, error)

func (*ServiceToolExecutor) ListTools

func (e *ServiceToolExecutor) ListTools(ctx context.Context) ([]ai.ToolDefinition, error)

type SessionMode

type SessionMode int

SessionMode defines the operating mode of the agent session.

const (
	// SessionModeInteractive is the default mode where each request is independent.
	SessionModeInteractive SessionMode = iota
	// SessionModePlayback executes a script with state preservation.
	SessionModePlayback
)

func (SessionMode) String

func (m SessionMode) String() string

type ShortTermMemory

type ShortTermMemory struct {
	Threads            map[sop.UUID]*ConversationThread
	Order              []sop.UUID // Maintains the sequence of threads
	CurrentThreadID    sop.UUID   // The currently active thread
	LastRoutingState   *TaskContextClassification
	LastMRUSnapshot    []MRUItem
	LastRecipeSnapshot []RecipeItem
	LastCarryoverState *ai.CarryoverState
}

ShortTermMemory manages the history of conversation threads.

func NewShortTermMemory

func NewShortTermMemory() *ShortTermMemory

NewShortTermMemory initializes the memory structure.

func (*ShortTermMemory) AddThread

func (stm *ShortTermMemory) AddThread(thread *ConversationThread)

AddThread adds a new thread to memory, enforcing the LRU limit.

func (*ShortTermMemory) GetCarryoverState

func (stm *ShortTermMemory) GetCarryoverState() *ai.CarryoverState

func (*ShortTermMemory) GetCurrentThread

func (stm *ShortTermMemory) GetCurrentThread() *ConversationThread

GetCurrentThread returns the active conversation thread or nil.

func (*ShortTermMemory) GetMRUSnapshot

func (stm *ShortTermMemory) GetMRUSnapshot() []MRUItem

func (*ShortTermMemory) GetRecipeSnapshot

func (stm *ShortTermMemory) GetRecipeSnapshot() []RecipeItem

func (*ShortTermMemory) GetRoutingState

func (stm *ShortTermMemory) GetRoutingState() *TaskContextClassification

func (*ShortTermMemory) PromoteThread

func (stm *ShortTermMemory) PromoteThread(id sop.UUID)

PromoteThread moves the specified thread ID to the end of the order (most recent).

func (*ShortTermMemory) ResetProjectionForTopicSwitch

func (stm *ShortTermMemory) ResetProjectionForTopicSwitch()

func (*ShortTermMemory) SetCarryoverState

func (stm *ShortTermMemory) SetCarryoverState(state *ai.CarryoverState)

func (*ShortTermMemory) SetMRUSnapshot

func (stm *ShortTermMemory) SetMRUSnapshot(items []MRUItem)

func (*ShortTermMemory) SetRecipeSnapshot

func (stm *ShortTermMemory) SetRecipeSnapshot(items []RecipeItem)

func (*ShortTermMemory) SetRoutingState

func (stm *ShortTermMemory) SetRoutingState(taskCtx *TaskContextClassification)

type SpaceBulkOperationError

type SpaceBulkOperationError struct {
	Index   int      `json:"index"`
	ID      sop.UUID `json:"id,omitempty"`
	Message string   `json:"message"`
}

SpaceBulkOperationError represents an error in a bulk Space operation

type SpaceBulkOperationMetrics

type SpaceBulkOperationMetrics struct {
	TotalItems     int           `json:"total_items"`
	ItemsPerSecond float64       `json:"items_per_second"`
	AvgItemTime    time.Duration `json:"avg_item_time"`
}

SpaceBulkOperationMetrics contains performance metrics for bulk Space operations

type SpaceBulkOperationResult

type SpaceBulkOperationResult struct {
	Success   bool                      `json:"success"`
	Processed int                       `json:"processed"`
	Failed    int                       `json:"failed"`
	Duration  time.Duration             `json:"duration"`
	Errors    []SpaceBulkOperationError `json:"errors,omitempty"`
	Metrics   SpaceBulkOperationMetrics `json:"metrics"`
}

SpaceBulkOperationResult represents the result of a bulk Space operation

type SpecProvider

type SpecProvider interface {
	GetIndexSpecs() map[string]*jsondb.IndexSpecification
}

SpecProvider allows cursors to expose IndexSpecifications for field ordering.

type StepExecutionResult

type StepExecutionResult struct {
	Type    string `json:"type"`
	Command string `json:"command,omitempty"`
	Prompt  string `json:"prompt,omitempty"`
	// Use Payload for generic message/label if needed, but Command/Prompt cover most.
	// We might want to add "Name" explicitly if strictly needed,
	// but currently the code equates Command field to the label.
	StepIndex int    `json:"step_index"`
	Result    any    `json:"result,omitempty"`
	Record    any    `json:"record,omitempty"`
	Error     string `json:"error,omitempty"`
}

type StepStreamer

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

StepStreamer implements ai.ResultStreamer to stream result items.

func (*StepStreamer) BeginArray

func (ss *StepStreamer) BeginArray()

func (*StepStreamer) Close

func (ss *StepStreamer) Close()

func (*StepStreamer) EndArray

func (ss *StepStreamer) EndArray()

func (*StepStreamer) SetMetadata

func (ss *StepStreamer) SetMetadata(meta map[string]any)

func (*StepStreamer) WriteItem

func (ss *StepStreamer) WriteItem(item any)

type StoreCursor

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

StoreCursor wraps a StoreAccessor to provide a ScriptCursor.

func (*StoreCursor) Close

func (sc *StoreCursor) Close() error

func (*StoreCursor) GetIndexSpecs

func (sc *StoreCursor) GetIndexSpecs() map[string]*jsondb.IndexSpecification

func (*StoreCursor) Next

func (sc *StoreCursor) Next(ctx context.Context) (any, bool, error)

type SystemPromptBuilder

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

SystemPromptBuilder helps construct robust, structured prompts declaratively.

func NewSystemPromptBuilder

func NewSystemPromptBuilder() *SystemPromptBuilder

NewSystemPromptBuilder initializes a new declarative builder.

func (*SystemPromptBuilder) ToJSON

func (b *SystemPromptBuilder) ToJSON() string

ToJSON serializes the structured prompt into a highly readable JSON array string. This is optimal for modern LLMs (Gemini, GPT-4) to parse distinct sections clearly.

func (*SystemPromptBuilder) ToJSONWithBudget

func (b *SystemPromptBuilder) ToJSONWithBudget(profile PromptBudgetProfile) string

ToJSONWithBudget serializes the prompt after applying component and total-size budgets.

func (*SystemPromptBuilder) ToJSONWithBudgetReport

func (b *SystemPromptBuilder) ToJSONWithBudgetReport(profile PromptBudgetProfile) (string, PromptBudgetReport)

ToJSONWithBudgetReport serializes the prompt after applying budgets and returns diagnostics.

func (*SystemPromptBuilder) ToXML

func (b *SystemPromptBuilder) ToXML() string

ToXML serializes the structured prompt into XML-style tags. This is optimal for Claude and older Anthropic models.

func (*SystemPromptBuilder) With

func (b *SystemPromptBuilder) With(component PromptComponent, content string) *SystemPromptBuilder

With adds a new ingredient to the prompt if the content is not empty.

type TaskContextClassification

type TaskContextClassification struct {
	Entity          string      `json:"entity"`
	Domain          string      `json:"domain"`
	DBArtifacts     []string    `json:"db_artifacts"`
	StoresArtifacts []string    `json:"stores_artifacts,omitempty"`
	SpacesArtifacts []string    `json:"spaces_artifacts,omitempty"`
	Layers          []LayerInfo `json:"layers"`
	ScriptAuthoring bool        `json:"-"`
	RoutingGate     string      `json:"-"`
	// KB Search Results (for specialized KB routing)
	KBSearchResults string `json:"-"`
	KBMatchCount    int    `json:"-"`
	LLMInstruction  string `json:"-"`
	CleanQuery      string `json:"-"` // Query without :llm meta-token
	DirectDisplay   bool   `json:"-"`
}

type ToolDefinition

type ToolDefinition struct {
	Name             string
	Description      string
	ShortDescription string
	ArgsSchema       string // JSON schema or description of args
	Handler          ToolHandler
	Hidden           bool
}

ToolDefinition defines a tool's metadata and handler.

type ToolExecutionContext

type ToolExecutionContext struct {
	// Session holds the current session state for tools that need access to variables or state
	Session *ai.SessionPayload

	// Executor is the tool executor (may be nested/chained)
	Executor ai.ToolExecutor

	// Recorder captures script steps during execution for playback/audit
	Recorder ai.ScriptRecorder

	// Writer is the output destination for streaming tool results
	Writer io.Writer

	// ResultStreamer enables structured streaming output for tools (BeginArray, WriteItem, EndArray)
	ResultStreamer ai.ResultStreamer

	// NativeToolHints indicates this is native Ask-loop tool execution that can consume structured hints
	NativeToolHints bool

	// Database is the target database for script/tool execution
	Database *database.Database

	// EventStreamer receives structured events during tool execution
	EventStreamer func(eventType string, data any)

	// ProgressSink receives progress messages
	ProgressSink func(message string)
}

ToolExecutionContext carries all dependencies needed for tool execution. This replaces the pattern of hiding tool dependencies in context.Context via multiple context keys. Affects Phase 2 context keys: CtxKeyExecutor, CtxKeyScriptRecorder, CtxKeyWriter, CtxKeyResultStreamer, CtxKeyNativeToolHints

type ToolHandler

type ToolHandler func(ctx context.Context, args map[string]any) (any, error)

ToolHandler is the function signature for a tool execution.

type ToolProvider

type ToolProvider interface {
	Execute(ctx context.Context, toolName string, args map[string]any) (string, error)
}

ToolProvider interface for agents that can execute tools

type TopicAssessment

type TopicAssessment struct {
	IsNewTopic    bool   `json:"is_new_topic"`
	TopicUUID     string `json:"topic_uuid,omitempty"` // If not new, the UUID of the existing graph
	NewTopicLabel string `json:"new_topic_label,omitempty"`
	Reasoning     string `json:"reasoning"`
}

TopicAssessment is the structure returned by the generic router.

type TransactionArgs

type TransactionArgs struct {
	Database string `json:"database,omitempty" example:"dev_db"`
	Mode     string `json:"mode" binding:"required" enums:"read,write" default:"read"`
}

TransactionArgs for beginning a transaction

type TransactionCommitArgs

type TransactionCommitArgs struct {
	TransactionID string `json:"transaction_id" binding:"required"`
}

TransactionCommitArgs for committing a transaction

type TransactionHandle

type TransactionHandle struct {
	ID       string    `json:"id"`       // Unique transaction ID
	Database string    `json:"database"` // Database name
	Mode     string    `json:"mode"`     // "read" or "write"
	Started  time.Time `json:"started"`  // When transaction began
}

TransactionHandle represents an active transaction

type TransactionMode

type TransactionMode string

TransactionMode controls how transactions are managed in bulk operations

const (
	// TransactionModeAutoBatch creates transactions automatically, commits per batch
	// Use for: Large single-operation bulk inserts (10K+ items)
	TransactionModeAutoBatch TransactionMode = "auto_batch"

	// TransactionModeExplicit uses provided transaction, never auto-commits
	// Use for: Multi-operation atomicity (all-or-nothing across operations)
	TransactionModeExplicit TransactionMode = "explicit"

	// TransactionModeSingle creates ONE transaction for ALL items, single commit at end
	// Use for: Moderate datasets (< 10K items) requiring atomicity
	TransactionModeSingle TransactionMode = "single"
)

type TransactionRollbackArgs

type TransactionRollbackArgs struct {
	TransactionID string `json:"transaction_id" binding:"required"`
}

TransactionRollbackArgs for rolling back a transaction

type UpdateArgs

type UpdateArgs struct {
	Database string         `json:"database,omitempty" example:"dev_db"`
	Store    string         `json:"store" binding:"required" example:"users"`
	Key      any            `json:"key" binding:"required" example:"user_123"`
	Value    map[string]any `json:"value" binding:"required"`
}

UpdateArgs represents arguments for updating a single item

type UpdateSpaceConfigArgs

type UpdateSpaceConfigArgs struct {
	Database string                     `json:"database,omitempty" example:"dev_db"`
	KBName   string                     `json:"kb_name" binding:"required" example:"Notes"`
	Config   memory.KnowledgeBaseConfig `json:"config" binding:"required"`
}

UpdateSpaceConfigArgs represents arguments for updating Space configuration

type UpsertCategoryArgs

type UpsertCategoryArgs struct {
	Database      string                     `json:"database,omitempty" example:"dev_db"`
	KBName        string                     `json:"kb_name" binding:"required" example:"Notes"`
	Parameter     memory.UpsertCategoryParam `json:"parameter" binding:"required"`
	TransactionID string                     `json:"transaction_id,omitempty"`
}

UpsertCategoryArgs represents arguments for upserting a single category

type UpsertItemArgs

type UpsertItemArgs struct {
	Database      string                                 `json:"database,omitempty" example:"dev_db"`
	KBName        string                                 `json:"kb_name" binding:"required" example:"Notes"`
	Parameter     memory.UpsertItemParam[map[string]any] `json:"parameter" binding:"required"`
	TransactionID string                                 `json:"transaction_id,omitempty"`
}

UpsertItemArgs represents arguments for upserting a single item

type VectorizeSpaceArgs

type VectorizeSpaceArgs struct {
	Database  string `json:"database,omitempty" example:"dev_db"`
	KBName    string `json:"kb_name" binding:"required" example:"Notes"`
	BatchSize int    `json:"batch_size,omitempty" default:"100" example:"250"`
}

VectorizeSpaceArgs represents arguments for vectorizing an entire Space

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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