Session Debug
Session ID: {sessionId}
Active Step: {orchestrationState.activeStep || 'None'}
Tools Used: {orchestrationState.recentlyUsedTools.join(', ')}
Sequence Position: {orchestrationState.sequenceIndex}
);
}
```
This helps with:
- Verifying state persistence
- Checking sequence progression
- Identifying orchestration issues
## Best Practices
1. **Initialize Only When Needed**
- Check `template.orchestration` before calling orchestration-specific functions like `getOrchestrationState`.
2. **Configure for Environment**
- Set appropriate cleanup options based on your deployment environment
- Disable cleanup timers in serverless environments
3. **Client-Side Caching**
- Store state in client-side cache
- Update from response headers
- Handle expired or invalid state gracefully
4. **Clean Session IDs**
- Use a consistent format with sufficient entropy
- Never expose sensitive information in session IDs
- Include timestamps for debugging
5. **Error Handling**
- Have fallbacks for orchestration failures
- Parse state carefully with try/catch
- Log orchestration errors but continue if possible
### Environment-Based TTL Configuration
The `src/lib/orchestration-adapter.ts` file handles reading environment variables to configure the `agentdock-core` `OrchestrationManager` instance. It ensures a single instance (singleton) is used per server process.
```typescript
// Simplified logic from src/lib/orchestration-adapter.ts
import {
createOrchestrationManager,
getStorageFactory,
OrchestrationManager,
// ... other imports
} from 'agentdock-core';
declare global {
var __orchestrationManagerInstance: OrchestrationManager | null | undefined;
}
export function getOrchestrationManagerInstance() {
// Use global singleton
if (globalThis.__orchestrationManagerInstance) {
return globalThis.__orchestrationManagerInstance;
}
// Determine storage provider based on ENV (KV_STORE_PROVIDER, etc.)
const storageProvider = getConfiguredStorageProvider(); // Internal helper function
// Read and calculate TTL from ENV
const sessionTtlSeconds = process.env.SESSION_TTL_SECONDS ? parseInt(process.env.SESSION_TTL_SECONDS, 10) : undefined;
let sessionTtlMs: number | undefined = undefined;
if (sessionTtlSeconds && sessionTtlSeconds > 0) {
sessionTtlMs = sessionTtlSeconds * 1000; // Convert to ms
}
// Create the manager instance
const newInstance = createOrchestrationManager({
storageProvider: storageProvider,
// Pass configured TTL (undefined lets core use its 24h default)
cleanup: {
enabled: false, // Cleanup timer managed within core if needed
ttlMs: sessionTtlMs
}
});
// Store globally and return
globalThis.__orchestrationManagerInstance = newInstance;
return newInstance;
}
```
This ensures that the session TTL configured in the environment dictates the actual session lifespan managed by `agentdock-core`.
## Session Management
This directory contains documentation about AgentDock's session management system.
## Overview
Sessions in AgentDock provide isolation between concurrent conversations and maintain state across multiple requests. They are a critical part of the architecture that enables:
- Stateful conversations with LLM agents
- Tool context persistence
- Orchestration state management
- Memory efficiency and resource optimization
## Documentation Files
- [session-overview.md](./session-overview.md) - Core concepts and architecture of the session system
- [session-implementation.md](./session-implementation.md) - Technical implementation details and API
- [session-optimization.md](./session-optimization.md) - Performance optimizations and memory management
- [nextjs-integration.md](./nextjs-integration.md) - How sessions integrate with Next.js applications
## Session Management
Sessions in AgentDock provide the foundation for stateful, continuous interactions between users and AI agents. This document covers the core concepts, architecture, implementation details, and optimization strategies for the session management system.
## Core Concepts
- **Session:** Represents a single conversation, identified by a unique `SessionId`. It maintains state across multiple interactions (messages, tool calls) to preserve context.
- **Session Isolation:** Ensures that concurrent conversations do not interfere with each other. Critical for multi-user environments.
- **Single Source of Truth:** Session IDs are generated centrally (typically by the entry point, like an API route handler), and state is managed through a single `SessionManager` instance for a given state type, ensuring consistency.
## Architecture & Implementation
The session system revolves around the generic `SessionManager` class found in `agentdock-core/src/session/index.ts`.
### `SessionManager