Service Contexts

Provide service-specific memory and context to AI assistants

Overview

Service Contexts allow services to provide additional memory and contextual information to AI assistants. This helps the assistant make better decisions by understanding user history, preferences, and state specific to your service.

Understanding Service Contexts

While AI assistants maintain their own memory, Service Contexts allow your service to:

  • Inject service-specific history
  • Provide user preferences
  • Share interaction patterns
  • Include relevant state information
  • Add specialized knowledge

Contexts are refreshed between every tool call and interaction with the assistant.

Creating a Context

A service context is defined using the ServiceContext type:

interface ServiceContext {
  id: string;              // Unique identifier
  name: string;           // Display name
  description: string;    // Description of the context
  getContextData: (agentInfo: AgentInfo) => Promise<string>;  // Context generator
}

Usage

Add contexts to your service configuration:

const dainService = defineDAINService({
  // ... other config
  contexts: [orderHistoryContext, userPreferencesContext]
});

Example Implementations

Shopping History Context

const orderHistoryContext: ServiceContext = {
  id: "orderHistory",
  name: "Shopping History",
  description: "User's previous orders and preferences",
  getContextData: async (agentInfo) => {
    const orders = await db.orders.findRecent(agentInfo.id);
    const preferences = await db.preferences.get(agentInfo.id);
    
    return `
User has placed ${orders.length} orders in the last 30 days.
Frequently ordered items: ${orders.getTopItems().join(", ")}
Dietary preferences: ${preferences.dietary.join(", ")}
Preferred delivery times: ${preferences.deliveryTimes}
Last order: ${orders[0]?.summary || "No recent orders"}
    `.trim();
  }
};

User Preferences Context

const userPreferencesContext: ServiceContext = {
  id: "userPreferences",
  name: "User Preferences",
  description: "User's service preferences and settings",
  getContextData: async (agentInfo) => {
    const prefs = await getUserPreferences(agentInfo.id);
    
    return `
User preferred units: ${prefs.units}
Language preference: ${prefs.language}
Notification settings: ${prefs.notifications}
Account type: ${prefs.accountType}
Feature access: ${Object.keys(prefs.enabledFeatures).join(", ")}
    `.trim();
  }
};

Common Use Cases

Service Contexts are valuable for providing:

  1. Historical Information
const searchHistoryContext: ServiceContext = {
  id: "searchHistory",
  name: "Search History",
  description: "Recent search patterns",
  getContextData: async (agentInfo) => {
    const searches = await getRecentSearches(agentInfo.id);
    return `Recent searches: ${searches.map(s => s.query).join(", ")}`;
  }
};
  1. User Behavior Patterns
const userBehaviorContext: ServiceContext = {
  id: "userBehavior",
  name: "Usage Patterns",
  description: "User interaction patterns",
  getContextData: async (agentInfo) => {
    const patterns = await analyzeBehavior(agentInfo.id);
    return `User typically active: ${patterns.activeHours}
Preferred categories: ${patterns.topCategories}`;
  }
};
  1. State Information
const cartContext: ServiceContext = {
  id: "cartState",
  name: "Shopping Cart State",
  description: "Current cart contents and history",
  getContextData: async (agentInfo) => {
    const cart = await getCart(agentInfo.id);
    return `Cart has ${cart.items.length} items worth $${cart.total}
Abandoned items: ${cart.recentlyRemoved.join(", ")}`;
  }
};
  1. Service Status
const serviceStatusContext: ServiceContext = {
  id: "serviceStatus",
  name: "Service Status",
  description: "Current service availability",
  getContextData: async (agentInfo) => {
    const status = await getServiceStatus();
    return `Service load: ${status.load}
Available features: ${status.availableFeatures}
Maintenance mode: ${status.maintenance}`;
  }
};