Skip to main content

Setting Sessions

The Session Store API provides thread-scoped key-value storage for managing dynamic data across agent iterations. Session values persist throughout a conversation thread and can be used to store state, share data between agent calls, and attach additional content to user messages.

Overview

Session values are scoped to a specific thread ID and can include:

  • Regular values: Key-value pairs for storing arbitrary data
  • User parts: Special prefixed values (__user_part_*) that automatically attach to user messages
  • Expiry: Optional expiration timestamps for temporary data

Session Management Interface

Basic Session Operations

import { Distri } from '@distri/core';

const client = new Distri({
baseUrl: 'http://localhost:8787/api/v1',
});

const threadId = 'thread-123';

// Set a session value
await client.setSessionValue(
threadId,
'user_preference',
{ theme: 'dark', language: 'en' }
);

// Get a single session value
const value = await client.getSessionValue(threadId, 'user_preference');
console.log('User preference:', value);

// Get all session values
const allValues = await client.getSessionValues(threadId);
for (const [key, value] of Object.entries(allValues)) {
console.log(`${key}:`, value);
}

// Delete a specific key
await client.deleteSessionValue(threadId, 'user_preference');

// Clear all values in a session
await client.clearSession(threadId);

User Parts

User parts automatically attach to user messages when prefixed with __user_part_:

// Set a text user part
await client.setUserPartText(
threadId,
'screenshot_description',
'Screenshot shows the login form with validation errors'
);

// Set an image user part
await client.setUserPartImage(
threadId,
'screenshot',
{
bytes: base64ImageString,
mimeType: 'image/png',
name: 'screenshot.png',
}
);

// Delete a specific user part
await client.deleteUserPart(threadId, 'screenshot_description');

// Clear all user parts
await client.clearUserParts(threadId);

Session Value Expiry

Set expiration times for temporary data:

// Set a value with 24-hour expiry
const expiry = new Date();
expiry.setHours(expiry.getHours() + 24);

await client.setSessionValue(
threadId,
'temporary_data',
{ data: 'value' },
expiry.toISOString()
);

Use Cases

  • Browser Automation: Store screenshots, DOM observations, and user interactions
  • State Management: Maintain conversation context and user preferences
  • Tool Integration: Share data between external tools and agent iterations
  • Multi-step Workflows: Persist intermediate results across agent calls
  • Rich User Input: Attach images, observations, and structured data to user messages