instruction.go

Overview

The instruction.go file provides a utility function to perform session state injection into instruction templates used by AI agents. Its primary purpose is to process textual instruction templates containing placeholders and replace them dynamically with relevant runtime data from the session state or artifact contents.

This injection mechanism enables large language model (LLM) agents to receive contextually enriched prompts personalized to the current session, improving interaction quality and relevance. The function exposed here, InjectSessionState, acts as a safe wrapper that verifies the context type and delegates the actual injection work to an internal implementation.

The file is part of the Instruction Injection and Session State Injection functionality within the system, interfacing primarily with agent invocation contexts, session state management, and artifact services to resolve placeholders in templates.


Function: InjectSessionState

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

Purpose

Processes an instruction template string by injecting values from session state and artifacts, replacing placeholders of the form {key_name} or {artifact.key_name} with their corresponding runtime values.

Parameters

Returns

Usage Example

injectedInstruction, err := instructionutil.InjectSessionState(ctx, "Hello {user_name}, see report {artifact.report}.")
if err != nil {
    // handle error, e.g. missing session variable or artifact
}
fmt.Println(injectedInstruction)
// Output might be: "Hello Alice, see report <contents of report artifact>."

Behavior Details


Important Implementation Details


Interaction with Other System Components


Mermaid Diagram: Function Workflow

flowchart TD
Start[Start: Call InjectSessionState]
CheckCtx[Check context type]
FailCtx[Return error: invalid context type]
CallInternal[Call llminternal.InjectSessionState]
ReturnRes[Return injected instruction or error]
Start --> CheckCtx
CheckCtx -- Valid --> CallInternal
CheckCtx -- Invalid --> FailCtx
CallInternal --> ReturnRes

This diagram depicts the simple workflow of InjectSessionState: it verifies the context type, calls the internal injection method, and returns the injected string or an error.


Summary of File Role


References to Related Topics

This file is a critical utility that bridges session and artifact data with instruction templates, enabling AI agents to generate context-sensitive prompts dynamically.