Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 72 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,30 +41,53 @@ Store any data type an LLM can process: text, JSON, images, and files. All data
- **Templates**: Enable dynamic value resolution with circular reference protection

### Automatic Tool Generation
Tools are automatically generated for each writable key, allowing agents to read and write memory without manual tool definitions. Tools integrate seamlessly with Vercel AI SDK.
Tools are automatically generated for each writable key, allowing agents to read and write memory without manual tool definitions. Tools integrate seamlessly with Vercel AI SDK and other AI frameworks.

### Custom Types (v3.6+)
Define structured data schemas using human-readable Markdown, then assign types to keys for consistent LLM output:

```typescript
// Define a custom type with Markdown schema
mc.registerType('Contact', `
#Contact
* name: full name of the contact
* email: email address
* phone: phone number
* notes: any additional context
`);

// Create a key with this type
mc.set_value('contact_alice', JSON.stringify({ name: 'Alice', email: 'alice@example.com' }));
mc.setType('contact_alice', 'Contact');

// LLM tools will enforce the schema
const tools = mc.create_vercel_ai_tools(); // For Vercel AI SDK
// or
const rawTools = mc.create_tools(); // For other frameworks (OpenAI, Anthropic, etc.)
```

## Quick Start

```typescript
import { mindcache } from 'mindcache';
import { MindCache } from 'mindcache';

const mc = new MindCache();

// Store values
mindcache.set_value('userName', 'Alice');
mindcache.set_value('favoriteColor', 'blue');
mc.set_value('userName', 'Alice');
mc.set_value('favoriteColor', 'blue');

// Generate system prompt for your AI agent
const systemPrompt = mindcache.get_system_prompt();
// "userName: Alice. You can rewrite \"userName\" by using the write_userName tool..."
const systemPrompt = mc.get_system_prompt();

// Generate tools for Vercel AI SDK
const tools = mindcache.get_aisdk_tools();
// { write_userName: {...}, write_favoriteColor: {...} }
const tools = mc.create_vercel_ai_tools();

// Use with AI SDK
import { generateText } from 'ai';
const { text } = await generateText({
model: openai('gpt-4'),
tools: tools,
tools,
system: systemPrompt,
prompt: 'Remember that I love green now, not blue.'
});
Expand Down Expand Up @@ -145,19 +168,40 @@ mindcache.withContext({ includeTags: ['admin'] }, () => {
### Vercel AI SDK
```typescript
import { streamText } from 'ai';
import { mindcache } from 'mindcache';
import { MindCache } from 'mindcache';

const mc = new MindCache();
mc.set_value('userName', 'Alice', { systemTags: ['SystemPrompt', 'LLMRead', 'LLMWrite'] });

const tools = mindcache.get_aisdk_tools();
const systemPrompt = mindcache.get_system_prompt();
const tools = mc.create_vercel_ai_tools();
const systemPrompt = mc.get_system_prompt();

const result = await streamText({
model: openai('gpt-4'),
tools: tools,
tools,
system: systemPrompt,
prompt: userMessage
});
```

### Other AI Frameworks (OpenAI, Anthropic, LangChain)
```typescript
import { MindCache } from 'mindcache';

const mc = new MindCache();
const tools = mc.create_tools(); // Raw JSON Schema tools

// Use with OpenAI SDK
const response = await openai.chat.completions.create({
model: 'gpt-4',
tools: Object.values(tools).map(t => ({
type: 'function',
function: { name: t.name, description: t.description, parameters: t.parameters }
})),
messages: [{ role: 'user', content: userMessage }]
});
```

### Next.js
See the [Next.js example](./examples/nextjs_demo) for a complete integration.

Expand Down Expand Up @@ -256,13 +300,24 @@ export class MyDurableObject {
### Core Methods
- `set_value(key, value, attributes?)` - Store a value with optional attributes
- `get_value(key)` - Retrieve a value (supports template processing)
- `delete(key)` - Remove a key-value pair
- `delete_key(key)` - Remove a key-value pair
- `has(key)` - Check if a key exists
- `clear()` - Clear all memory
- `keys()` - Get all key names

### Custom Types (v3.6+)
- `registerType(name, markdownSchema)` - Register a custom type with Markdown schema
- `setType(key, typeName)` - Assign a custom type to a key (also sets type to 'json')
- `getKeyType(key)` - Get the custom type name for a key
- `getTypeSchema(typeName)` - Get the parsed schema definition
- `getRegisteredTypes()` - List all registered type names

### LLM Tool Generation
- `create_vercel_ai_tools()` - Generate Zod-based tools for Vercel AI SDK v5
- `create_tools()` - Generate raw JSON Schema tools (OpenAI, Anthropic, LangChain)
- `get_system_prompt()` - Generate system prompt from visible keys

### Memory Management
- `get_system_prompt()` - Generate system prompt from visible keys
- `get_aisdk_tools()` - Generate tools for Vercel AI SDK
- `injectSTM(template)` - Inject memory values into template strings
- `getSTM()` - Get formatted string of all visible entries

Expand Down Expand Up @@ -297,6 +352,7 @@ export class MyDurableObject {
## Examples

See the [examples directory](./examples) for complete implementations:
- **[Contact Extractor](./examples/contact_extractor)** - AI-powered contact extraction using custom types
- Form management with AI assistant
- Image processing workflows
- Multi-step workflows with memory persistence
Expand Down
150 changes: 145 additions & 5 deletions docs/llms-full.md
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,140 @@ const history = mc.getGlobalHistory();

---

## Custom Types (v3.6+)

Define structured data schemas using human-readable Markdown. Custom types guide LLM output to follow consistent schemas.

### Registering a Type

```typescript
import { MindCache } from 'mindcache';

const mc = new MindCache();

// Define a Contact type with Markdown schema
mc.registerType('Contact', `
#Contact
* name: full name of the contact
* email: email address (primary)
* phone: phone number (mobile preferred)
* company: company or organization name
* role: job title or role
* notes: any additional context about this person
`);
```

### Assigning Types to Keys

```typescript
// Create a key and assign the type
mc.set_value('contact_alice', JSON.stringify({
name: 'Alice Smith',
email: 'alice@example.com',
company: 'Acme Corp',
role: 'Engineer'
}), {
systemTags: ['SystemPrompt', 'LLMRead', 'LLMWrite']
});

// Assign the custom type (also sets underlying type to 'json')
mc.setType('contact_alice', 'Contact');
```

### Querying Types

```typescript
// Get the custom type for a key
const typeName = mc.getKeyType('contact_alice');
// Returns: 'Contact'

// Get the schema definition
const schema = mc.getTypeSchema('Contact');
// Returns: { name: 'Contact', fields: [...], rawSchema: '...' }

// List all registered types
const types = mc.getRegisteredTypes();
// Returns: ['Contact']
```

### LLM Tool Integration

When a key has a custom type, the generated tools include schema guidance:

```typescript
// Generate tools - schema is embedded in tool descriptions
const tools = mc.create_vercel_ai_tools();

// The write_contact_alice tool description includes:
// - The Contact schema fields
// - Example JSON format
// - Guidance for the LLM to follow the schema
```

### System Prompt with Types

The system prompt automatically includes type information:

```typescript
const systemPrompt = mc.get_system_prompt();
// Includes:
// - Available types (Contact)
// - Keys with their types
// - Schema definitions for each typed key
```

### Framework-Agnostic Tools

```typescript
// For Vercel AI SDK v5 (uses Zod schemas + tool() helper)
const vercelTools = mc.create_vercel_ai_tools();

// For other frameworks (raw JSON Schema)
const rawTools = mc.create_tools();
// Works with: OpenAI SDK, Anthropic SDK, LangChain, etc.
```

### Complete Example: Contact Manager

```typescript
import { MindCache } from 'mindcache';
import { generateText } from 'ai';
import { openai } from '@ai-sdk/openai';

const mc = new MindCache();

// Register Contact type
mc.registerType('Contact', `
#Contact
* name: full name
* email: email address
* phone: phone number
* notes: additional context
`);

// Create initial contact
mc.set_value('contact_bob', JSON.stringify({
name: 'Bob Jones',
email: 'bob@example.com'
}), { systemTags: ['SystemPrompt', 'LLMRead', 'LLMWrite'] });
mc.setType('contact_bob', 'Contact');

// Let LLM update contact
const tools = mc.create_vercel_ai_tools();
const { text } = await generateText({
model: openai('gpt-4'),
tools,
system: mc.get_system_prompt(),
prompt: 'Add phone number 555-1234 to Bob'
});

// Contact is updated with proper schema
console.log(mc.get_value('contact_bob'));
// { name: 'Bob Jones', email: 'bob@example.com', phone: '555-1234' }
```

---

## Attributes & Metadata

### Available Attributes
Expand Down Expand Up @@ -601,13 +735,13 @@ const userEntries = Object.entries(mindcache.getAll())

## LLM Tool Generation

MindCache automatically generates tools for Vercel AI SDK.
MindCache automatically generates tools for AI frameworks.

### Generating Tools
### Generating Tools for Vercel AI SDK

```typescript
// Signature
get_aisdk_tools(): Record<string, Tool>
// For Vercel AI SDK v5 (uses Zod schemas + tool() helper)
const tools = mc.create_vercel_ai_tools();

// Setup
mindcache.set_value('userName', 'Alice');
Expand Down Expand Up @@ -1902,9 +2036,15 @@ const all = mindcache.getAll();
const result = mindcache.injectSTM('Hello {{name}}!');

// LLM tools
const tools = mindcache.get_aisdk_tools();
const tools = mindcache.create_vercel_ai_tools(); // For Vercel AI SDK
const rawTools = mindcache.create_tools(); // For other frameworks
const prompt = mindcache.get_system_prompt();

// Custom types (v3.6+)
mindcache.registerType('Contact', '#Contact\n* name: full name\n* email: email');
mindcache.setType('contact_alice', 'Contact');
const typeName = mindcache.getKeyType('contact_alice');

// Tags
mindcache.addTag('key', 'tagName');
const tagged = mindcache.getTagged('tagName');
Expand Down
Loading
Loading