@looopy-ai/aws 2.1.24 → 2.1.25
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.
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { BedrockAgentCoreClient } from '@aws-sdk/client-bedrock-agentcore';
|
|
2
|
-
import type
|
|
2
|
+
import { type CompactionOptions, type CompactionResult, type LLMMessage, type MessageStore } from '@looopy-ai/core';
|
|
3
3
|
export interface AgentCoreMemoryMessageStoreConfig {
|
|
4
4
|
memoryId: string;
|
|
5
5
|
agentId: string;
|
|
@@ -12,11 +12,10 @@ export interface AgentCoreMemoryMessageStoreConfig {
|
|
|
12
12
|
export declare class AgentCoreMemoryMessageStore implements MessageStore {
|
|
13
13
|
private readonly memoryId;
|
|
14
14
|
private readonly actorId;
|
|
15
|
-
private readonly includeLongTermMemories;
|
|
16
15
|
private readonly longTermMemoryNamespace?;
|
|
17
16
|
private readonly client;
|
|
18
17
|
private readonly initialFetchLimit;
|
|
19
|
-
private readonly
|
|
18
|
+
private readonly messages;
|
|
20
19
|
constructor(config: AgentCoreMemoryMessageStoreConfig);
|
|
21
20
|
append(contextId: string, messages: LLMMessage[]): Promise<void>;
|
|
22
21
|
getRecent(contextId: string, options?: {
|
|
@@ -28,14 +27,12 @@ export declare class AgentCoreMemoryMessageStore implements MessageStore {
|
|
|
28
27
|
getRange(contextId: string, startIndex: number, endIndex: number): Promise<LLMMessage[]>;
|
|
29
28
|
compact(_contextId: string, _options?: CompactionOptions): Promise<CompactionResult>;
|
|
30
29
|
clear(contextId: string): Promise<void>;
|
|
31
|
-
private ensureCache;
|
|
32
30
|
private loadCacheIfNeeded;
|
|
33
31
|
searchMemories(query: string, options?: {
|
|
34
32
|
maxResults?: number;
|
|
35
33
|
}): Promise<unknown[]>;
|
|
36
34
|
private convertEventsToMessages;
|
|
37
35
|
private retrieveLongTermMemories;
|
|
38
|
-
private formatLongTermMemories;
|
|
39
36
|
private toAgentCoreRole;
|
|
40
37
|
private fromAgentCoreRole;
|
|
41
38
|
}
|
|
@@ -1,19 +1,17 @@
|
|
|
1
1
|
import { BedrockAgentCoreClient, CreateEventCommand, DeleteEventCommand, ListEventsCommand, RetrieveMemoryRecordsCommand, } from '@aws-sdk/client-bedrock-agentcore';
|
|
2
|
-
import { trimToTokenBudget } from '@looopy-ai/core';
|
|
2
|
+
import { estimateTokens, trimToTokenBudget, } from '@looopy-ai/core';
|
|
3
3
|
export class AgentCoreMemoryMessageStore {
|
|
4
4
|
memoryId;
|
|
5
5
|
actorId;
|
|
6
|
-
includeLongTermMemories;
|
|
7
6
|
longTermMemoryNamespace;
|
|
8
7
|
client;
|
|
9
8
|
initialFetchLimit;
|
|
10
|
-
|
|
9
|
+
messages = new Map();
|
|
11
10
|
constructor(config) {
|
|
12
11
|
this.memoryId = config.memoryId;
|
|
13
12
|
this.actorId = config.agentId;
|
|
14
|
-
this.includeLongTermMemories = !!config.longTermMemoryNamespace;
|
|
15
13
|
this.longTermMemoryNamespace = config.longTermMemoryNamespace;
|
|
16
|
-
this.initialFetchLimit = config.initialFetchLimit ??
|
|
14
|
+
this.initialFetchLimit = config.initialFetchLimit ?? 50;
|
|
17
15
|
this.client =
|
|
18
16
|
config.client ||
|
|
19
17
|
new BedrockAgentCoreClient({
|
|
@@ -21,9 +19,18 @@ export class AgentCoreMemoryMessageStore {
|
|
|
21
19
|
});
|
|
22
20
|
}
|
|
23
21
|
async append(contextId, messages) {
|
|
24
|
-
const
|
|
22
|
+
const stored = this.messages.get(contextId) || [];
|
|
23
|
+
const nextIndex = stored.length;
|
|
24
|
+
const newMessages = messages.map((msg, i) => ({
|
|
25
|
+
...msg,
|
|
26
|
+
id: `msg_${contextId}_${nextIndex + i}`,
|
|
27
|
+
contextId,
|
|
28
|
+
index: nextIndex + i,
|
|
29
|
+
timestamp: new Date().toISOString(),
|
|
30
|
+
tokens: estimateTokens(msg.content),
|
|
31
|
+
}));
|
|
32
|
+
this.messages.set(contextId, [...stored, ...newMessages]);
|
|
25
33
|
for (const message of messages) {
|
|
26
|
-
cache.push(message);
|
|
27
34
|
const command = new CreateEventCommand({
|
|
28
35
|
memoryId: this.memoryId,
|
|
29
36
|
actorId: this.actorId,
|
|
@@ -55,33 +62,28 @@ export class AgentCoreMemoryMessageStore {
|
|
|
55
62
|
}
|
|
56
63
|
}
|
|
57
64
|
async getRecent(contextId, options) {
|
|
58
|
-
|
|
59
|
-
const
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
role: 'system',
|
|
65
|
-
content: this.formatLongTermMemories(longTerm),
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
if (options?.maxTokens) {
|
|
70
|
-
return trimToTokenBudget(messages, options.maxTokens);
|
|
65
|
+
await this.loadCacheIfNeeded(contextId, options?.maxMessages);
|
|
66
|
+
const all = this.messages.get(contextId) || [];
|
|
67
|
+
const { maxMessages = 50, maxTokens } = options || {};
|
|
68
|
+
let messages = all.slice(-maxMessages);
|
|
69
|
+
if (maxTokens) {
|
|
70
|
+
messages = trimToTokenBudget(messages, maxTokens);
|
|
71
71
|
}
|
|
72
72
|
return messages.slice();
|
|
73
73
|
}
|
|
74
74
|
async getAll(contextId) {
|
|
75
|
-
|
|
76
|
-
return
|
|
75
|
+
await this.loadCacheIfNeeded(contextId);
|
|
76
|
+
return (this.messages.get(contextId) || []).slice();
|
|
77
77
|
}
|
|
78
78
|
async getCount(contextId) {
|
|
79
|
-
|
|
80
|
-
|
|
79
|
+
await this.loadCacheIfNeeded(contextId);
|
|
80
|
+
const messages = this.messages.get(contextId) || [];
|
|
81
|
+
return messages.length;
|
|
81
82
|
}
|
|
82
83
|
async getRange(contextId, startIndex, endIndex) {
|
|
83
|
-
|
|
84
|
-
|
|
84
|
+
await this.loadCacheIfNeeded(contextId, endIndex);
|
|
85
|
+
const all = this.messages.get(contextId) || [];
|
|
86
|
+
return all.slice(startIndex, endIndex);
|
|
85
87
|
}
|
|
86
88
|
async compact(_contextId, _options) {
|
|
87
89
|
return {
|
|
@@ -91,7 +93,7 @@ export class AgentCoreMemoryMessageStore {
|
|
|
91
93
|
};
|
|
92
94
|
}
|
|
93
95
|
async clear(contextId) {
|
|
94
|
-
this.
|
|
96
|
+
this.messages.delete(contextId);
|
|
95
97
|
const list = await this.client.send(new ListEventsCommand({
|
|
96
98
|
memoryId: this.memoryId,
|
|
97
99
|
actorId: this.actorId,
|
|
@@ -109,14 +111,8 @@ export class AgentCoreMemoryMessageStore {
|
|
|
109
111
|
}));
|
|
110
112
|
}
|
|
111
113
|
}
|
|
112
|
-
ensureCache(contextId) {
|
|
113
|
-
if (!this.cache.has(contextId)) {
|
|
114
|
-
this.cache.set(contextId, []);
|
|
115
|
-
}
|
|
116
|
-
return this.cache.get(contextId);
|
|
117
|
-
}
|
|
118
114
|
async loadCacheIfNeeded(contextId, requested) {
|
|
119
|
-
const existing = this.
|
|
115
|
+
const existing = this.messages.get(contextId);
|
|
120
116
|
if (existing) {
|
|
121
117
|
return existing;
|
|
122
118
|
}
|
|
@@ -129,7 +125,7 @@ export class AgentCoreMemoryMessageStore {
|
|
|
129
125
|
});
|
|
130
126
|
const response = await this.client.send(command);
|
|
131
127
|
const messages = this.convertEventsToMessages(response.events ?? []);
|
|
132
|
-
this.
|
|
128
|
+
this.messages.set(contextId, messages);
|
|
133
129
|
return messages;
|
|
134
130
|
}
|
|
135
131
|
async searchMemories(query, options) {
|
|
@@ -142,7 +138,8 @@ export class AgentCoreMemoryMessageStore {
|
|
|
142
138
|
const dateB = b.eventTimestamp?.getTime() ?? 0;
|
|
143
139
|
return dateA - dateB;
|
|
144
140
|
});
|
|
145
|
-
for (
|
|
141
|
+
for (let i = 0; i < events.length; i++) {
|
|
142
|
+
const event = events[i];
|
|
146
143
|
const message = { role: 'assistant', content: '' };
|
|
147
144
|
for (const payload of event.payload ?? []) {
|
|
148
145
|
if (payload.conversational) {
|
|
@@ -157,7 +154,15 @@ export class AgentCoreMemoryMessageStore {
|
|
|
157
154
|
message.toolCalls = blob.toolCalls;
|
|
158
155
|
}
|
|
159
156
|
}
|
|
160
|
-
|
|
157
|
+
const storedMessage = {
|
|
158
|
+
...message,
|
|
159
|
+
id: event.eventId ?? `event_${i}`,
|
|
160
|
+
contextId: event.sessionId ?? '',
|
|
161
|
+
index: i,
|
|
162
|
+
timestamp: event.eventTimestamp?.toISOString() ?? new Date().toISOString(),
|
|
163
|
+
tokens: estimateTokens(message.content),
|
|
164
|
+
};
|
|
165
|
+
messages.push(storedMessage);
|
|
161
166
|
}
|
|
162
167
|
return messages;
|
|
163
168
|
}
|
|
@@ -173,16 +178,6 @@ export class AgentCoreMemoryMessageStore {
|
|
|
173
178
|
const response = await this.client.send(command);
|
|
174
179
|
return response.memoryRecordSummaries ?? [];
|
|
175
180
|
}
|
|
176
|
-
formatLongTermMemories(memories) {
|
|
177
|
-
if (memories.length === 0) {
|
|
178
|
-
return '';
|
|
179
|
-
}
|
|
180
|
-
const lines = memories.map((record) => {
|
|
181
|
-
const data = record;
|
|
182
|
-
return `- ${String(data.content || data.memory || JSON.stringify(record))}`;
|
|
183
|
-
});
|
|
184
|
-
return `Relevant context from previous sessions:\n${lines.join('\n')}`;
|
|
185
|
-
}
|
|
186
181
|
toAgentCoreRole(role) {
|
|
187
182
|
switch (role) {
|
|
188
183
|
case 'user':
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@looopy-ai/aws",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.25",
|
|
4
4
|
"description": "AWS storage and providers for Looopy AI",
|
|
5
5
|
"repository": {
|
|
6
6
|
"url": "https://github.com/looopy-ai/lib"
|
|
@@ -39,23 +39,23 @@
|
|
|
39
39
|
"author": "",
|
|
40
40
|
"license": "MIT",
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"@aws-sdk/client-bedrock-agentcore": "^3.
|
|
43
|
-
"@aws-sdk/client-dynamodb": "^3.
|
|
44
|
-
"@aws-sdk/client-secrets-manager": "^3.
|
|
45
|
-
"@aws-sdk/lib-dynamodb": "^3.
|
|
46
|
-
"@hono/node-server": "^1.19.
|
|
42
|
+
"@aws-sdk/client-bedrock-agentcore": "^3.971.0",
|
|
43
|
+
"@aws-sdk/client-dynamodb": "^3.971.0",
|
|
44
|
+
"@aws-sdk/client-secrets-manager": "^3.971.0",
|
|
45
|
+
"@aws-sdk/lib-dynamodb": "^3.971.0",
|
|
46
|
+
"@hono/node-server": "^1.19.9",
|
|
47
47
|
"@opentelemetry/exporter-metrics-otlp-http": "^0.207.0",
|
|
48
48
|
"@opentelemetry/exporter-trace-otlp-http": "^0.207.0",
|
|
49
49
|
"@opentelemetry/instrumentation": "^0.207.0",
|
|
50
|
-
"@opentelemetry/resources": "^2.
|
|
51
|
-
"@opentelemetry/sdk-metrics": "^2.
|
|
52
|
-
"@opentelemetry/sdk-trace-base": "^2.
|
|
53
|
-
"@opentelemetry/sdk-trace-node": "^2.
|
|
54
|
-
"@opentelemetry/semantic-conventions": "^1.
|
|
55
|
-
"@smithy/types": "^4.
|
|
50
|
+
"@opentelemetry/resources": "^2.4.0",
|
|
51
|
+
"@opentelemetry/sdk-metrics": "^2.4.0",
|
|
52
|
+
"@opentelemetry/sdk-trace-base": "^2.4.0",
|
|
53
|
+
"@opentelemetry/sdk-trace-node": "^2.4.0",
|
|
54
|
+
"@opentelemetry/semantic-conventions": "^1.39.0",
|
|
55
|
+
"@smithy/types": "^4.12.0",
|
|
56
56
|
"hono": "^4.10.5",
|
|
57
57
|
"pino-http": "^11.0.0",
|
|
58
|
-
"@looopy-ai/core": "2.1.
|
|
58
|
+
"@looopy-ai/core": "2.1.22"
|
|
59
59
|
},
|
|
60
60
|
"publishConfig": {
|
|
61
61
|
"access": "public"
|