@elizaos/plugin-forms
Version:
Forms plugin for ElizaOS - structured conversational data collection
483 lines (388 loc) โข 11 kB
Markdown
# ๐๏ธ ElizaOS Forms Plugin
A powerful forms management plugin for ElizaOS that enables structured data collection through conversational interactions. This plugin allows agents to create, manage, and process multi-step forms while maintaining natural conversation flow.
## ๐จ TL;DR - Quick Setup
**Want your agent to collect structured data through forms? Here's the fastest path:**
1. **Install the plugin** โ `bun install @elizaos/plugin-forms`
2. **Add to your agent**:
```typescript
import { formsPlugin } from '@elizaos/plugin-forms';
const character = {
plugins: [formsPlugin],
// ... other config
};
```
3. **Try it** โ "I need to fill out a contact form"
4. **Done!** Your agent now handles forms naturally in conversation
## โจ Features
- โ
**Multi-Step Forms** - Create complex forms with conditional logic
- โ
**Natural Language Processing** - Extract form values from user messages using LLM
- โ
**Secret Field Handling** - Secure handling of sensitive data like API keys
- โ
**Form Templates** - Pre-built templates for common use cases
- โ
**Smart Validation** - Field-level validation with custom criteria
- โ
**Extensible Callbacks** - Step and form completion hooks
- โ
**Real-time Context** - Live form state information for agents
- โ
**Conversational Flow** - Maintains natural dialogue while collecting data
## ๐ Prerequisites
- ElizaOS agent setup
- Node.js and bun installed
- Basic understanding of TypeScript (for custom forms)
## ๐ Quick Start
### Step 1: Install the Plugin
```bash
bun install @elizaos/plugin-forms
```
### Step 2: Add to Your Agent
```typescript
import { formsPlugin } from '@elizaos/plugin-forms';
// In your character configuration
const character = {
name: "FormBot",
plugins: [formsPlugin],
// ... other configuration
};
```
### Step 3: Test Basic Forms
Start your agent and try these commands:
```
User: "I need to fill out a contact form"
Agent: "I've created a new contact form for you. Let's start with Basic Information.
Please provide the following information:
- Name: Your full name
- Email: Your email address"
User: "My name is John Doe and my email is john@example.com"
Agent: "Form completed successfully! I've recorded:
- Name: John Doe
- Email: john@example.com"
```
## ๐ฏ Common Use Cases
### Collect Contact Information
```typescript
// User says: "I want to submit my contact details"
// Agent creates a contact form automatically
```
### API Configuration Forms
```typescript
const apiConfigForm = {
name: 'api-config',
steps: [{
id: 'credentials',
name: 'API Configuration',
fields: [
{
id: 'apiKey',
label: 'API Key',
type: 'text',
secret: true, // Value will be masked
},
{
id: 'endpoint',
label: 'API Endpoint',
type: 'text',
}
]
}]
};
```
### Multi-Step Surveys
```typescript
const surveyForm = {
name: 'feedback-survey',
steps: [
{
id: 'experience',
name: 'Your Experience',
fields: [
{
id: 'rating',
label: 'Overall Rating',
type: 'number',
criteria: 'Number between 1-5',
}
]
},
{
id: 'details',
name: 'Additional Details',
fields: [
{
id: 'comments',
label: 'Comments',
type: 'textarea',
optional: true,
}
]
}
]
};
```
## ๐ Core Concepts
### Field Types
```typescript
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Available Field Types โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ text โ Single line text input โ
โ textarea โ Multi-line text input โ
โ number โ Numeric input โ
โ email โ Email address validation โ
โ choice โ Selection from options โ
โ checkbox โ Boolean true/false โ
โ date โ Date input โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
```
### Form Flow
```mermaid
graph LR
A[User Request] --> B[Create Form]
B --> C[Collect Fields]
C --> D{All Fields Complete?}
D -->|No| C
D -->|Yes| E[Execute Callbacks]
E --> F[Complete Form]
```
## ๐ ๏ธ API Reference
### FormsService
The core service managing form lifecycle:
```typescript
class FormsService {
// Create a new form
createForm(templateOrDefinition): Promise<Form>
// Update form with user input
updateForm(formId, message): Promise<UpdateResult>
// List forms by status
listForms(status?): Promise<Form[]>
// Get specific form
getForm(formId): Promise<Form>
// Cancel active form
cancelForm(formId): Promise<void>
// Register custom template
registerTemplate(template): void
}
```
### Creating Custom Forms
#### Basic Form Definition
```typescript
const customForm = {
name: 'user-profile',
description: 'Collect user profile information',
steps: [
{
id: 'basic-info',
name: 'Basic Information',
fields: [
{
id: 'username',
label: 'Username',
type: 'text',
description: 'Choose a unique username',
criteria: 'Alphanumeric, 3-20 characters',
},
{
id: 'bio',
label: 'Bio',
type: 'textarea',
description: 'Tell us about yourself',
optional: true,
}
]
}
]
};
```
#### Advanced Features
**๐ Secret Fields**
```typescript
{
id: 'password',
label: 'Password',
type: 'text',
secret: true, // Value masked in outputs
criteria: 'Minimum 8 characters',
}
```
**โ
Field Validation**
```typescript
{
id: 'age',
label: 'Age',
type: 'number',
criteria: 'Must be 18 or older',
validate: (value) => {
const age = parseInt(value);
return age >= 18;
}
}
```
**๐ Callbacks**
```typescript
{
steps: [{
id: 'step1',
name: 'Step 1',
fields: [...],
onComplete: async (form, stepId) => {
console.log(`Step ${stepId} completed`);
// Custom logic here
}
}],
onComplete: async (form) => {
console.log('Form completed:', form.id);
// Save to database, send email, etc.
}
}
```
## ๐ฎ Actions
### CREATE_FORM
Creates a new form from template or definition.
**Triggers**:
- "create form"
- "fill out"
- "questionnaire"
- "survey"
- "contact"
- "application"
### UPDATE_FORM
Updates active form with user values.
**Triggers**: Automatically when form is active
### CANCEL_FORM
Cancels the current form.
**Triggers**:
- "cancel"
- "stop"
- "abort"
- "quit"
- "exit"
- "nevermind"
## ๐ง Troubleshooting
### Form Not Creating
**Issue**: Agent doesn't create form when requested
**Solution**:
- โ
Ensure plugin is loaded: Check logs for "Forms plugin initialized"
- โ
Verify trigger words: Use exact phrases like "create form"
- โ
Check character config includes the plugin
### Values Not Extracting
**Issue**: User input not being captured
**Solution**:
- โ
Be explicit: "My email is user@example.com"
- โ
One field at a time for complex forms
- โ
Check field criteria matches input format
### Form Stuck on Step
**Issue**: Form won't progress to next step
**Solution**:
- โ
Ensure all required fields are filled
- โ
Check validation criteria
- โ
Try "skip" for optional fields
### Secret Fields Visible
**Issue**: Sensitive data showing in responses
**Solution**:
- โ
Set `secret: true` on sensitive fields
- โ
Check provider implementation
- โ
Verify latest plugin version
## ๐งช Testing
### Running Tests
```bash
# Run all tests (unit + E2E)
bun test
# Run only unit tests
bun run test:unit
# Run E2E tests directly
bun run test:e2e
# Run with coverage
bun test --coverage
```
### Test Coverage
The plugin maintains ~85% test coverage:
- โ
Service lifecycle
- โ
Form CRUD operations
- โ
Multi-step progression
- โ
Secret field handling
- โ
Provider context
- โ
Action execution
- โ
Template management
### Known Issues
โ ๏ธ The `elizaos test` command may encounter pino logger compatibility issues. Use `bun run test:e2e` directly.
## ๐ Integration Examples
### With Other Plugins
```typescript
// In another plugin
const formsService = runtime.getService<FormsService>('forms');
// Create configuration form
const form = await formsService.createForm({
name: 'plugin-config',
steps: [{
id: 'settings',
name: 'Plugin Settings',
fields: [
{
id: 'apiKey',
label: 'API Key',
type: 'text',
secret: true,
}
],
onComplete: async (form) => {
// Save configuration
const apiKey = form.steps[0].fields[0].value;
await saveConfig({ apiKey });
}
}]
});
```
### Custom Form Templates
```typescript
// Register reusable template
formsService.registerTemplate({
name: 'bug-report',
description: 'Bug report form',
steps: [
{
id: 'issue',
name: 'Issue Details',
fields: [
{
id: 'title',
label: 'Issue Title',
type: 'text',
},
{
id: 'description',
label: 'Description',
type: 'textarea',
},
{
id: 'severity',
label: 'Severity',
type: 'choice',
options: ['low', 'medium', 'high', 'critical'],
}
]
}
]
});
```
## ๐ Security Best Practices
- ๐ **Always use `secret: true`** for sensitive fields
- ๐ซ **Never log secret field values**
- โ
**Validate all inputs** before processing
- ๐ **Clear forms after completion** to prevent data leaks
- ๐ **Audit form submissions** for compliance
- ๐ก๏ธ **Implement rate limiting** for form creation
## ๐ Performance Considerations
- Forms are stored in memory by default
- Implement persistence for production use
- Consider pagination for listing many forms
- Use callbacks efficiently to avoid blocking
## ๐ค Contributing
We welcome contributions! Please:
1. Check existing issues first
2. Follow the code style guide
3. Add tests for new features
4. Update documentation
5. Submit clear PR descriptions
## ๐ Additional Resources
- [ElizaOS Documentation](https://elizaos.github.io/eliza/)
- [Plugin Development Guide](https://elizaos.github.io/eliza/docs/plugins)
- [Forms Best Practices](https://elizaos.github.io/eliza/docs/forms)
- [API Reference](https://elizaos.github.io/eliza/api/forms)
## ๐ License
MIT - Part of the ElizaOS project