-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstate-management.ts
106 lines (92 loc) · 3.01 KB
/
state-management.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import { Agent, AnthropicProvider } from '../';
import { InMemoryStateProvider } from '../core/InMemoryStateProvider';
import { ConversationStateData } from '../core/interfaces';
import dotenv from 'dotenv';
dotenv.config();
async function main() {
// Initialize the agent
const agent = new Agent({
domain: 'stateful-chat',
userId: 'stateful-agent',
platform: 'cli',
capabilities: ['text-generation'],
permissions: ['read', 'write']
});
// Set up providers
const aiProvider = new AnthropicProvider({
apiKey: process.env.ANTHROPIC_API_KEY!,
model: 'claude-3-opus-20240229'
});
const stateProvider = new InMemoryStateProvider();
await agent.setAIProvider(aiProvider);
await agent.setStateProvider(stateProvider);
// Initialize the agent
await agent.initialize();
// Example conversation with state tracking
const conversation: ConversationStateData = {
id: 'demo-conversation',
turnCount: 0,
lastInteraction: new Date(),
variables: {
topic: 'state management',
userPreference: 'technical'
},
userProfile: {
id: 'demo-user',
preferences: {
language: 'English',
expertise: 'developer'
},
history: {
topics: ['state management'],
interactions: 0,
lastSeen: new Date()
}
},
emotionalState: {
user: 'neutral',
agent: 'professional',
confidence: 1.0
},
flags: {
isNewUser: true,
needsHelp: false
}
};
// Save initial state
await stateProvider.saveState(conversation);
// Process some messages
const messages = [
"How does state management work?",
"Can you remember my preferences?",
"What was my first question?"
];
for (const content of messages) {
// Update conversation state
const currentState = await stateProvider.loadState('demo-conversation');
if (currentState) {
currentState.turnCount++;
currentState.lastInteraction = new Date();
currentState.userProfile.history.interactions++;
await stateProvider.saveState(currentState);
}
// Process message
console.log('\nUser:', content);
const response = await agent.processMessage({
role: 'user',
content,
metadata: {
timestamp: new Date().toISOString(),
platform: 'cli',
conversationId: 'demo-conversation'
}
});
console.log('Assistant:', response.content);
// Show current state
const updatedState = await stateProvider.loadState('demo-conversation');
console.log('\nCurrent State:', JSON.stringify(updatedState, null, 2), '\n');
}
// Cleanup
await agent.shutdown();
}
main().catch(console.error);