load_artifacts_tool.go

Overview

The load_artifacts_tool.go file implements the Artifact Loading Tool, a specialized tool within the tooling framework that enables AI agents or large language models (LLMs) to dynamically access and incorporate stored artifacts (e.g., files or blobs) during an agent session. This tool informs the model about available artifacts and provides their content on demand through function calls, allowing agents to respond to queries involving external data stored as artifacts.

Key capabilities include:

This tool operates within the session context (tool.Context) and interacts closely with the artifact service (agent.Artifacts), integrating tightly with the overall agent lifecycle and tooling system as described in Tooling System and Artifact Loading Tool.


Types

artifactsTool

Implements the tool.Tool interface representing the artifact loading tool.

Fields:

Name

Type

Description

name

string

Tool name identifier (load_artifacts)

description

string

Human-readable tool description


Functions and Methods

New() tool.Tool

Creates and returns a new instance of artifactsTool.

Usage Example:

tool := loadartifactstool.New()
fmt.Println(tool.Name())        // Output: "load_artifacts"
fmt.Println(tool.Description()) // Output: "Loads the artifacts and adds them to the session."

(t *artifactsTool) Name() string

Returns the tool's name identifier.


(t *artifactsTool) Description() string

Returns the tool's human-readable description.


(t *artifactsTool) IsLongRunning() bool

Indicates whether the tool performs long-running operations.

Returns: false — the artifact loading operations are designed to be quick and non-blocking.


(t *artifactsTool) Declaration() *genai.FunctionDeclaration

Returns a GenAI function declaration describing the tool's callable interface.

The declaration specifies:

This declaration enables the LLM to understand the function signature for making function calls during generation.


(t *artifactsTool) Run(ctx tool.Context, args any) (map[string]any, error)

Executes the tool's core logic when invoked via a function call.

Parameters:

Behavior:

Returns:

Usage Example:

result, err := tool.Run(ctx, map[string]any{
    "artifact_names": []string{"file1.txt", "config.json"},
})
if err != nil {
    // handle error
}
// result["artifact_names"] is []string{"file1.txt", "config.json"}

(t *artifactsTool) ProcessRequest(ctx tool.Context, req *model.LLMRequest) error

Integrates the tool into the agent request lifecycle by:

  1. Packing the tool into the LLM request tools list.

  2. Appending initial instructions listing available artifacts.

  3. Detecting and processing function calls to load_artifacts in the request contents.

Parameters:

Returns:


(t *artifactsTool) appendInitialInstructions(ctx tool.Context, req *model.LLMRequest) error

Queries the artifact service to list all available artifact filenames and appends an instruction to the LLM request.

Instruction Content Highlights:

Returns: Error on failure to list or marshal artifact names.


(t *artifactsTool) processLoadArtifactsFunctionCall(ctx tool.Context, req *model.LLMRequest) error

Detects if the last LLM request content contains a function call to load_artifacts. If so:

Returns: An error if loading fails or input types are invalid.


(t *artifactsTool) loadIndividualArtifact(ctx context.Context, artifactsService agent.Artifacts, artifactName string) (*genai.Content, error)

Loads the content of a single artifact from the artifact service.

Parameters:

Returns:


Important Implementation Details


Interaction with Other Components


Usage Workflow

sequenceDiagram
participant Agent
participant LoadArtifactsTool as Tool
participant LLMModel as LLM
participant ArtifactService as Artifacts
Agent->>Tool: ProcessRequest (LLMRequest)
Tool->>ArtifactService: List available artifacts
ArtifactService-->>Tool: Return artifact name list
Tool->>LLMModel: Append instructions with artifact list
LLMModel->>Agent: Generate function call "load_artifacts" with artifact_names
Agent->>Tool: Detect function call in LLMRequest
Tool->>ArtifactService: Concurrently load requested artifacts
ArtifactService-->>Tool: Return artifact contents
Tool->>Agent: Append artifact contents to LLMRequest
Agent->>LLMModel: Continue generation using artifact contents

Summary

The loadartifactstool provides an essential interface bridging stored artifact data with AI agents' reasoning processes via structured function calls. By integrating with artifact management and the tooling framework, it enables dynamic, on-demand retrieval and inclusion of artifact contents, enhancing the agent's contextual awareness and response capability in a safe, efficient manner. This closely aligns with the goals and architecture discussed in Artifact Loading Tool and the broader Tooling System.