agentnet 0.1.19 → 0.1.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/examples/previous-conversation-example.js +77 -0
- package/package.json +1 -1
- package/src/agent/runtime.js +7 -0
- package/src/index.js +14 -3
- package/src/store/store.js +13 -0
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { Message } from '../src/index.js';
|
|
2
|
+
|
|
3
|
+
// Example of using the new previousConversation capability
|
|
4
|
+
console.log('=== Previous Conversation Example ===\n');
|
|
5
|
+
|
|
6
|
+
// Example 1: Generic format (works with all LLM providers)
|
|
7
|
+
const genericPreviousConversation = [
|
|
8
|
+
{ role: 'user', content: 'Hello, I need help with my React project.' },
|
|
9
|
+
{ role: 'assistant', content: 'I\'d be happy to help! What specific aspect of your React project do you need assistance with?' },
|
|
10
|
+
{ role: 'user', content: 'I\'m having trouble with state management.' },
|
|
11
|
+
{ role: 'assistant', content: 'State management can be tricky. Are you using useState, useReducer, or a library like Redux?' }
|
|
12
|
+
];
|
|
13
|
+
|
|
14
|
+
const messageWithGeneric = new Message({
|
|
15
|
+
content: 'Can you help me implement a shopping cart?',
|
|
16
|
+
session: { id: 'session-123', userId: 'user-456' },
|
|
17
|
+
previousConversation: genericPreviousConversation
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
console.log('Generic Format Message:');
|
|
21
|
+
console.log('- Content:', messageWithGeneric.getContent());
|
|
22
|
+
console.log('- Session ID:', messageWithGeneric.getSessionId());
|
|
23
|
+
console.log('- Previous Conversation Length:', messageWithGeneric.getPreviousConversation()?.length || 0);
|
|
24
|
+
console.log('- First Previous Message:', messageWithGeneric.getPreviousConversation()?.[0]);
|
|
25
|
+
console.log();
|
|
26
|
+
|
|
27
|
+
// Example 2: Mixed format (Gemini + OpenAI formats)
|
|
28
|
+
const mixedPreviousConversation = [
|
|
29
|
+
{ role: 'user', content: 'What is machine learning?' },
|
|
30
|
+
{ role: 'model', parts: [{ text: 'Machine learning is a subset of AI...' }] }, // Gemini format
|
|
31
|
+
{ role: 'user', content: 'Can you give me an example?' },
|
|
32
|
+
{ role: 'assistant', content: 'Sure! A common example is email spam detection...' } // OpenAI format
|
|
33
|
+
];
|
|
34
|
+
|
|
35
|
+
const messageWithMixed = new Message({
|
|
36
|
+
content: 'How does neural networks work?',
|
|
37
|
+
session: { id: 'session-456' },
|
|
38
|
+
previousConversation: mixedPreviousConversation
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
console.log('Mixed Format Message:');
|
|
42
|
+
console.log('- Content:', messageWithMixed.getContent());
|
|
43
|
+
console.log('- Previous Conversation Length:', messageWithMixed.getPreviousConversation()?.length || 0);
|
|
44
|
+
console.log('- Mixed formats detected:', {
|
|
45
|
+
geminiFormat: messageWithMixed.getPreviousConversation()?.some(msg => msg.role === 'model'),
|
|
46
|
+
openaiFormat: messageWithMixed.getPreviousConversation()?.some(msg => msg.role === 'assistant')
|
|
47
|
+
});
|
|
48
|
+
console.log();
|
|
49
|
+
|
|
50
|
+
// Example 3: Serialization/Deserialization
|
|
51
|
+
console.log('Serialization Example:');
|
|
52
|
+
const serialized = messageWithGeneric.serialize();
|
|
53
|
+
console.log('- Serialized length:', serialized.length, 'characters');
|
|
54
|
+
|
|
55
|
+
const newMessage = new Message({ content: '' });
|
|
56
|
+
newMessage.deserialize(serialized);
|
|
57
|
+
console.log('- Deserialized content matches:', newMessage.getContent() === messageWithGeneric.getContent());
|
|
58
|
+
console.log('- Deserialized previous conversation matches:',
|
|
59
|
+
JSON.stringify(newMessage.getPreviousConversation()) === JSON.stringify(messageWithGeneric.getPreviousConversation()));
|
|
60
|
+
console.log();
|
|
61
|
+
|
|
62
|
+
// Example 4: Backward compatibility (no previous conversation)
|
|
63
|
+
const simpleMessage = new Message({
|
|
64
|
+
content: 'Simple question without previous context',
|
|
65
|
+
session: { id: 'session-789' }
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
console.log('Backward Compatibility:');
|
|
69
|
+
console.log('- Content:', simpleMessage.getContent());
|
|
70
|
+
console.log('- Previous Conversation:', simpleMessage.getPreviousConversation()); // Should be null
|
|
71
|
+
console.log('- String constructor still works:', new Message('Just a string').getContent());
|
|
72
|
+
|
|
73
|
+
console.log('\n=== Implementation Complete ===');
|
|
74
|
+
console.log('✅ Message class extended with previousConversation field');
|
|
75
|
+
console.log('✅ Runtime integration handles conversation prefilling');
|
|
76
|
+
console.log('✅ Backward compatibility maintained');
|
|
77
|
+
console.log('✅ Auto-detection of different LLM formats supported');
|
package/package.json
CHANGED
package/src/agent/runtime.js
CHANGED
|
@@ -68,6 +68,7 @@ export async function AgentRuntime(agentConfig) {
|
|
|
68
68
|
const content = message.getContent()
|
|
69
69
|
const session = message.getSession()
|
|
70
70
|
const sessionId = message.getSessionId()
|
|
71
|
+
const previousConversation = message.getPreviousConversation()
|
|
71
72
|
const storeStateSessionId = namespace + "." + agentName + "." + sessionId
|
|
72
73
|
|
|
73
74
|
// Load and merge session state and session data
|
|
@@ -89,6 +90,12 @@ export async function AgentRuntime(agentConfig) {
|
|
|
89
90
|
logger.info(`Loaded session state for agent ${agentName} with session id ${storeStateSessionId}, current conversation length ${storeState.conversation.getRawConversation().length}`);
|
|
90
91
|
}
|
|
91
92
|
|
|
93
|
+
// Import previous conversation if provided and current conversation is empty
|
|
94
|
+
if (previousConversation && storeState.conversation.getMessages().length === 0) {
|
|
95
|
+
storeState.conversation.importFromArray(previousConversation)
|
|
96
|
+
logger.info(`Imported ${previousConversation.length} previous messages for session ${storeStateSessionId}`)
|
|
97
|
+
}
|
|
98
|
+
|
|
92
99
|
const formattedInput = typeof content === 'string' ? content : JSON.stringify(content);
|
|
93
100
|
|
|
94
101
|
logger.debug(`Query to agent ${agentName}`, {
|
package/src/index.js
CHANGED
|
@@ -7,7 +7,8 @@ import {
|
|
|
7
7
|
redisStore,
|
|
8
8
|
postgresStore,
|
|
9
9
|
memoryStore,
|
|
10
|
-
session
|
|
10
|
+
session,
|
|
11
|
+
noStore
|
|
11
12
|
} from "./store/store.js";
|
|
12
13
|
import { Conversation } from "./utils/conversation.js";
|
|
13
14
|
|
|
@@ -27,8 +28,10 @@ export const GPT = _GPT
|
|
|
27
28
|
export const PostgresStore = postgresStore
|
|
28
29
|
export const RedisStore = redisStore
|
|
29
30
|
export const MemoryStore = memoryStore
|
|
31
|
+
export const NoStore = noStore
|
|
30
32
|
export const SessionStore = session
|
|
31
33
|
|
|
34
|
+
|
|
32
35
|
import { connect } from "@nats-io/transport-node"
|
|
33
36
|
export const NatsIO = (config) => {
|
|
34
37
|
let connected = false
|
|
@@ -53,18 +56,21 @@ export const Bindings = {
|
|
|
53
56
|
NatsIO: 'NatsIO',
|
|
54
57
|
Postgres: 'Postgres',
|
|
55
58
|
Redis: 'Redis',
|
|
56
|
-
Memory: 'Memory'
|
|
59
|
+
Memory: 'Memory',
|
|
60
|
+
NoStore: 'NoStore'
|
|
57
61
|
}
|
|
58
62
|
|
|
59
63
|
export class Message {
|
|
60
64
|
#content
|
|
61
65
|
#session
|
|
66
|
+
#previousConversation
|
|
62
67
|
constructor(input) {
|
|
63
68
|
if (typeof input === 'string') {
|
|
64
69
|
this.#content = input
|
|
65
70
|
} else {
|
|
66
71
|
this.#content = input.content
|
|
67
72
|
this.#session = input.session || {}
|
|
73
|
+
this.#previousConversation = input.previousConversation || null
|
|
68
74
|
}
|
|
69
75
|
}
|
|
70
76
|
getContent() {
|
|
@@ -76,16 +82,21 @@ export class Message {
|
|
|
76
82
|
getSession() {
|
|
77
83
|
return this.#session
|
|
78
84
|
}
|
|
85
|
+
getPreviousConversation() {
|
|
86
|
+
return this.#previousConversation
|
|
87
|
+
}
|
|
79
88
|
serialize() {
|
|
80
89
|
return JSON.stringify({
|
|
81
90
|
content: this.#content,
|
|
82
|
-
session: this.#session
|
|
91
|
+
session: this.#session,
|
|
92
|
+
previousConversation: this.#previousConversation
|
|
83
93
|
})
|
|
84
94
|
}
|
|
85
95
|
deserialize(data) {
|
|
86
96
|
const parsed = JSON.parse(data)
|
|
87
97
|
this.#content = parsed.content
|
|
88
98
|
this.#session = parsed.session || {}
|
|
99
|
+
this.#previousConversation = parsed.previousConversation || null
|
|
89
100
|
}
|
|
90
101
|
}
|
|
91
102
|
|
package/src/store/store.js
CHANGED
|
@@ -204,4 +204,17 @@ export function memoryStore () {
|
|
|
204
204
|
return state[key] || null
|
|
205
205
|
}
|
|
206
206
|
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
export function noStore () {
|
|
210
|
+
return {
|
|
211
|
+
connect: async function () {},
|
|
212
|
+
disconnect: async function () {},
|
|
213
|
+
set: async function (key, value) {
|
|
214
|
+
return null
|
|
215
|
+
},
|
|
216
|
+
get: async function (key) {
|
|
217
|
+
return null
|
|
218
|
+
}
|
|
219
|
+
}
|
|
207
220
|
}
|