- Preview
- Code
- Doc
Copy
/**
* Agentic chat using Mastra
*/
import { Agent } from '@mastra/core/agent';
import { createOpenAICompatible } from '@ai-sdk/openai-compatible';
import { Memory } from '@mastra/memory'
import { LibSQLStore } from "@mastra/libsql";
const memory = new Memory({
storage: new LibSQLStore({
url: ':memory:',
}),
});
/**
* Create agentic chat agent
*/
export function createAgenticChatAgent(): Agent {
const openaiCompatible = createOpenAICompatible({
name: 'custom',
baseURL: process.env.OPENAI_BASE_URL!,
apiKey: process.env.OPENAI_API_KEY!,
includeUsage: true, // Include usage information in streaming responses
});
const agent = new Agent({
name: 'agentic-chat-agent',
description: 'A helpful AI assistant',
model: openaiCompatible(process.env.OPENAI_MODEL!),
instructions: 'You are a helpful assistant.',
memory
});
return agent;
}
Agentic Chat - Mastra (TypeScript)
What This Demo Shows
This demo showcases AG-Kit’s integration with Mastra framework:- Mastra Framework: Uses Mastra’s agent system with built-in capabilities
- Simplified Agent Creation: Mastra’s declarative agent configuration
- Tool Integration: Seamless tool binding and execution
- State Management: Built-in conversation state handling
- OpenAI Integration: Native OpenAI model support
How to Interact
Try these suggestions or ask your own questions:- “Alert user about typhoon” (triggers alert tool)
- “Change background color to blue/red/green/yellow/purple/orange/pink/brown/gray/black/white” (triggers color change)
- “Get my current location” (triggers geolocation tool)
- “Hello, how are you today?”
- “Can you help me write a short story about a robot?”
Technical Implementation
Backend (Mastra):- Mastra’s
Agentclass with declarative configuration - Built-in OpenAI model integration
- Automatic tool registration and handling
- Simplified state management
- Native streaming support
- Same
useChathook andAgKitChatcomponent - Three built-in tools:
alert,change-background-color, andget-current-location
Key Features
- Declarative Configuration: Simple agent setup with minimal boilerplate
- Built-in Best Practices: Mastra handles common patterns automatically
- Framework Flexibility: Easy to switch between different agent frameworks
- Unified Interface: Consistent API across AG-Kit integrations