instruction

package
v1.13.2 Latest Latest
Warning

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

Go to latest
Published: Dec 24, 2025 License: AGPL-3.0 Imports: 6 Imported by: 0

Documentation

Overview

Package instruction provides instruction templating for Hector v2 agents.

This package implements adk-go compatible instruction templating, allowing agent instructions to contain dynamic placeholders that are resolved at runtime.

Placeholder Syntax

Placeholders use curly braces and support several forms:

{variable}           - Session state variable
{app:variable}       - App-scoped state (shared across all users/sessions)
{user:variable}      - User-scoped state (shared across sessions for a user)
{temp:variable}      - Temporary state (discarded after invocation)
{artifact.filename}  - Artifact text content
{variable?}          - Optional (empty string if not found, no error)

Usage

Basic usage with InjectState:

template := "Hello {user_name}, you are working on {app:project_name}."
resolved, err := instruction.InjectState(ctx, template)
if err != nil {
    return err
}
// resolved: "Hello Alice, you are working on MyProject."

Using the Template type:

tmpl := instruction.New("Task: {task}\nContext: {artifact.context?}")
resolved, err := tmpl.Render(ctx)

Integration with LLM Agents

The instruction package is used by llmagent to resolve instruction templates before sending to the LLM:

agent, _ := llmagent.New(llmagent.Config{
    Name: "assistant",
    Instruction: `
        You are helping {user_name?} with {task}.

        Project context:
        {artifact.project_context?}
    `,
})

Error Handling

Required placeholders (without ?) return an error if not found. Optional placeholders (with ?) return an empty string if not found. Invalid placeholder names (not valid identifiers) are left as-is.

Package instruction provides instruction templating utilities for Hector v2.

Instructions can contain placeholders that are resolved at runtime:

{variable}           - resolves from session state
{app:variable}       - resolves from app-scoped state
{user:variable}      - resolves from user-scoped state
{temp:variable}      - resolves from temp-scoped state
{artifact.filename}  - resolves artifact text content
{variable?}          - optional (empty string if not found)

Example:

instruction := "Hello {user_name}, you are working on {app:project_name}."
resolved, err := instruction.InjectState(ctx, instruction)

Index

Constants

View Source
const (
	PrefixApp  = "app:"
	PrefixUser = "user:"
	PrefixTemp = "temp:"
)

State key prefixes matching adk-go and session package.

Variables

This section is empty.

Functions

func HasPlaceholders

func HasPlaceholders(template string) bool

HasPlaceholders returns true if the template contains any placeholders.

func InjectState

func InjectState(ctx agent.ReadonlyContext, template string) (string, error)

InjectState populates values in an instruction template from context. This is the main entry point for template resolution, matching adk-go's pattern.

Placeholder syntax:

  • {variable_name} - resolves from session state
  • {app:variable} - resolves from app-scoped state
  • {user:variable} - resolves from user-scoped state
  • {temp:variable} - resolves from temp-scoped state
  • {artifact.filename} - resolves artifact text content
  • {variable?} - optional (empty string if not found, no error)

If a required placeholder cannot be resolved, an error is returned. Invalid placeholder names (not matching identifier rules) are left as-is.

func ListPlaceholders

func ListPlaceholders(template string) []string

ListPlaceholders returns all placeholder names found in the template.

func MustInjectState

func MustInjectState(ctx agent.ReadonlyContext, template string) string

MustInjectState is like InjectState but panics on error. Use only when you're certain the template is valid.

Types

type Template

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

Template represents an instruction template with placeholders.

func New

func New(template string) *Template

New creates a new instruction template.

func (*Template) Raw

func (t *Template) Raw() string

Raw returns the raw template string.

func (*Template) Render

func (t *Template) Render(ctx agent.ReadonlyContext) (string, error)

Render resolves all placeholders in the template using the context.

Jump to

Keyboard shortcuts

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