API Reference
Comprehensive reference for the Agentuity JavaScript SDK API
This section provides detailed documentation for the Agentuity JavaScript SDK API, including method signatures, parameters, return values, and example usage.
Table of Contents
Agent Lifecycle
The Agentuity SDK provides a structured way to define and handle agents. An agent consists of a handler function, with its configuration managed by the Agentuity CLI.
Agent Configuration
Agent configuration is managed by the Agentuity CLI and stored in the project configuration file. The AgentConfig
interface is used internally by the CLI and SDK:
Agent Handler
The AgentHandler
type defines the handler function for an agent:
Parameters
request
: AnAgentRequest
object containing the request dataresponse
: AnAgentResponse
object for creating responsescontext
: AnAgentContext
object providing access to various capabilities
Return Value
The handler function should return a Promise that resolves to an AgentResponseType
object.
Example
Storage APIs
The Agentuity SDK provides two storage APIs: Key-Value Storage and Vector Storage.
Key-Value Storage
The Key-Value Storage API provides a simple way to store and retrieve data. It is accessed through the context.kv
object.
get(name: string, key: string): Promise<ArrayBuffer | null>
Retrieves a value from the key-value storage.
Parameters
name
: The name of the key-value storagekey
: The key to retrieve the value for
Return Value
Returns a Promise that resolves to an ArrayBuffer containing the value, or null if the key does not exist.
Example
set(name: string, key: string, value: ArrayBuffer | string | Json, ttl?: number): Promise<void>
Stores a value in the key-value storage.
Parameters
name
: The name of the key-value storagekey
: The key to store the value undervalue
: The value to store (can be an ArrayBuffer, string, or JSON object)ttl
(optional): Time-to-live in seconds (minimum 60 seconds)
Return Value
Returns a Promise that resolves when the value has been stored.
Example
delete(name: string, key: string): Promise<void>
Deletes a value from the key-value storage.
Parameters
name
: The name of the key-value storagekey
: The key to delete
Return Value
Returns a Promise that resolves when the value has been deleted.
Example
Vector Storage
The Vector Storage API provides a way to store and search for data using vector embeddings. It is accessed through the context.vector
object.
upsert(name: string, ...documents: VectorUpsertParams[]): Promise<string[]>
Inserts or updates vectors in the vector storage.
Parameters
name
: The name of the vector storagedocuments
: One or more documents to upsert, each with either embeddings or text
Return Value
Returns a Promise that resolves to an array of string IDs for the upserted vectors.
Example
search(name: string, params: VectorSearchParams): Promise<VectorSearchResult[]>
Searches for vectors in the vector storage.
Parameters
name
: The name of the vector storageparams
: Search parameters including query, limit, similarity threshold, and metadata filters
Return Value
Returns a Promise that resolves to an array of search results, each containing an ID, metadata, and distance score.
Example
delete(name: string, ...ids: string[]): Promise<number>
Deletes vectors from the vector storage.
Parameters
name
: The name of the vector storageids
: One or more IDs of vectors to delete
Return Value
Returns a Promise that resolves to the number of vectors that were deleted.
Example
Agent Communication
The Agentuity SDK allows agents to communicate with each other through the context.getAgent()
method and agent redirection.
getAgent(params: GetAgentRequestParams): Promise<RemoteAgent>
Retrieves a handle to a remote agent that can be invoked.
Parameters
params
: Parameters to identify the agent, either by ID or by name and project ID
Return Value
Returns a Promise that resolves to a RemoteAgent
object that can be used to invoke the agent.
Example
Agent Handoff
The response.handoff()
method allows an agent to handoff the request to another agent.
handoff(agent: GetAgentRequestParams, payload?: Json | ArrayBuffer | string, contentType?: string, metadata?: Record<string, Json>): AgentRedirectResponse
Redirects the current request to another agent.
Handoff
Handoff is currently only supported for handoff to other agents in the same project. However, we are working on remote agent handoff and should have that working soon.
Parameters
agent
: Parameters to identify the target agentpayload
(optional): The payload to send to the target agentcontentType
(optional): The content type of the payloadmetadata
(optional): Additional metadata to include with the request
Return Value
Returns an AgentRedirectResponse
object.
Example
Response Types
The Agentuity SDK provides various methods for creating different types of responses through the response
object.
JSON Responses
json(data: Json, metadata?: Record<string, Json>): AgentResponseType
Creates a JSON response.
Parameters
data
: The JSON data to include in the responsemetadata
(optional): Additional metadata to include with the response
Return Value
Returns an AgentResponseType
object with the JSON data.
Example
Text Responses
text(data: string, metadata?: Record<string, Json>): AgentResponseType
Creates a text response.
Parameters
data
: The text to include in the responsemetadata
(optional): Additional metadata to include with the response
Return Value
Returns an AgentResponseType
object with the text data.
Example
Binary Responses
binary(data: ArrayBuffer, metadata?: Record<string, Json>): AgentResponseType
Creates a binary response.
Parameters
data
: The binary data to include in the responsemetadata
(optional): Additional metadata to include with the response
Return Value
Returns an AgentResponseType
object with the binary data.
Example
Media Type Responses
The SDK provides specialized methods for various media types:
pdf(data: ArrayBuffer, metadata?: Record<string, Json>): AgentResponseType
png(data: ArrayBuffer, metadata?: Record<string, Json>): AgentResponseType
jpeg(data: ArrayBuffer, metadata?: Record<string, Json>): AgentResponseType
gif(data: ArrayBuffer, metadata?: Record<string, Json>): AgentResponseType
webp(data: ArrayBuffer, metadata?: Record<string, Json>): AgentResponseType
mp3(data: ArrayBuffer, metadata?: Record<string, Json>): AgentResponseType
mp4(data: ArrayBuffer, metadata?: Record<string, Json>): AgentResponseType
m4a(data: ArrayBuffer, metadata?: Record<string, Json>): AgentResponseType
m4p(data: ArrayBuffer, metadata?: Record<string, Json>): AgentResponseType
webm(data: ArrayBuffer, metadata?: Record<string, Json>): AgentResponseType
wav(data: ArrayBuffer, metadata?: Record<string, Json>): AgentResponseType
ogg(data: ArrayBuffer, metadata?: Record<string, Json>): AgentResponseType
Each method works similarly to the binary()
method but sets the appropriate content type.
Example
HTML Responses
html(data: string, metadata?: Record<string, Json>): AgentResponseType
Creates an HTML response.
Parameters
data
: The HTML content to include in the responsemetadata
(optional): Additional metadata to include with the response
Return Value
Returns an AgentResponseType
object with the HTML content.
Example
Empty Responses
empty(metadata?: Record<string, Json>): AgentResponseType
Creates an empty response.
Parameters
metadata
(optional): Additional metadata to include with the response
Return Value
Returns an AgentResponseType
object with no payload.
Example
Request Handling
The Agentuity SDK provides various methods for accessing request data through the request
object.
Accessing Request Data
get trigger(): string
Gets the trigger type of the request.
Return Value
Returns a string representing the trigger type (webhook, cron, manual, agent, etc.).
Example
get(key: string, defaultValue?: Json): Json
Gets a value from the request. The available properties depend on the trigger type.
Parameters
key
: The key to retrievedefaultValue
(optional): A default value to return if the key does not exist
Return Value
Returns the value for the specified key, or the default value if the key does not exist.
Trigger-specific Properties
Different trigger types provide different properties:
- Webhook: Includes a
headers
property containing the HTTP headers from the webhook request.
Example
metadata(key: string, defaultValue?: Json): Json
Note: This method is deprecated. Use get(key, defaultValue)
instead.
Gets metadata associated with the request.
Parameters
key
: The metadata key to retrievedefaultValue
(optional): A default value to return if the key does not exist
Return Value
Returns the metadata value for the specified key, or the default value if the key does not exist.
json(): Json
Gets the payload of the request as a JSON object.
Return Value
Returns the request payload as a JSON object.
Example
text(): string
Gets the payload of the request as a string.
Return Value
Returns the request payload as a string.
Example
binary(): ArrayBuffer
Gets the payload of the request as an ArrayBuffer.
Return Value
Returns the request payload as an ArrayBuffer.
Example
Media-Specific Methods
The SDK provides specialized methods for various media types:
pdf(): ArrayBuffer
png(): ArrayBuffer
jpeg(): ArrayBuffer
gif(): ArrayBuffer
webp(): ArrayBuffer
mp3(): ArrayBuffer
mp4(): ArrayBuffer
m4a(): ArrayBuffer
m4p(): ArrayBuffer
webm(): ArrayBuffer
wav(): ArrayBuffer
ogg(): ArrayBuffer
Each method returns the request payload as an ArrayBuffer with the appropriate content type validation.
Example
Logging
The Agentuity SDK provides logging functionality through the context.logger
object.
Logger Interface
The Logger
interface defines the following methods:
Logging Methods
debug(message: string, ...args: unknown[]): void
Logs a debug message.
Parameters
message
: The message to logargs
: Additional arguments to include in the log
Example
info(message: string, ...args: unknown[]): void
Logs an informational message.
Parameters
message
: The message to logargs
: Additional arguments to include in the log
Example
warn(message: string, ...args: unknown[]): void
Logs a warning message.
Parameters
message
: The message to logargs
: Additional arguments to include in the log
Example
error(message: string, ...args: unknown[]): void
Logs an error message.
Parameters
message
: The message to logargs
: Additional arguments to include in the log
Example
Creating Child Loggers
child(opts: Record<string, unknown>): Logger
Creates a child logger with additional context.
Parameters
opts
: Additional context to include in all logs from the child logger
Return Value
Returns a new Logger
instance with the additional context.
Example
Session
The Agentuity SDK provides a Session
interface that represents the current agent execution context.
Telemetry
The Agentuity SDK integrates with OpenTelemetry for tracing and metrics.
Tracing
The SDK provides access to OpenTelemetry tracing through the context.tracer
object.