@mastra/upstash 0.10.2-alpha.1 → 0.10.2-alpha.2
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/.turbo/turbo-build.log +23 -7
- package/CHANGELOG.md +9 -0
- package/dist/_tsup-dts-rollup.d.cts +16 -5
- package/dist/_tsup-dts-rollup.d.ts +16 -5
- package/dist/chunk-HSTZWXH7.js +1666 -0
- package/dist/chunk-IGKEDEDE.js +12 -0
- package/dist/chunk-N2CPQVE3.cjs +35 -0
- package/dist/chunk-U74OJRHU.cjs +1678 -0
- package/dist/getMachineId-bsd-HDZ73WR7.cjs +30 -0
- package/dist/getMachineId-bsd-KKIDU47O.js +28 -0
- package/dist/getMachineId-darwin-3PL23DL6.cjs +31 -0
- package/dist/getMachineId-darwin-UTKBTJ2U.js +29 -0
- package/dist/getMachineId-linux-K3QXQYAB.js +23 -0
- package/dist/getMachineId-linux-KYLPK3HC.cjs +25 -0
- package/dist/getMachineId-unsupported-DEDJN4ZS.cjs +17 -0
- package/dist/getMachineId-unsupported-VPWBQCK7.js +15 -0
- package/dist/getMachineId-win-L2EYIM5A.js +30 -0
- package/dist/getMachineId-win-ZTI2LRDJ.cjs +52 -0
- package/dist/index.cjs +42499 -4
- package/dist/index.js +42499 -4
- package/package.json +2 -2
- package/src/storage/index.ts +27 -9
- package/src/storage/upstash.test.ts +10 -9
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mastra/upstash",
|
|
3
|
-
"version": "0.10.2-alpha.
|
|
3
|
+
"version": "0.10.2-alpha.2",
|
|
4
4
|
"description": "Upstash provider for Mastra - includes both vector and db storage capabilities",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
"typescript": "^5.8.2",
|
|
33
33
|
"vitest": "^3.1.2",
|
|
34
34
|
"@internal/lint": "0.0.7",
|
|
35
|
-
"@mastra/core": "0.10.2-alpha.
|
|
35
|
+
"@mastra/core": "0.10.2-alpha.4"
|
|
36
36
|
},
|
|
37
37
|
"peerDependencies": {
|
|
38
38
|
"@mastra/core": "^0.10.0-alpha.0"
|
package/src/storage/index.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { MetricResult, TestInfo } from '@mastra/core/eval';
|
|
2
|
-
import type { StorageThreadType,
|
|
2
|
+
import type { StorageThreadType, MastraMessageV1, MastraMessageV2 } from '@mastra/core/memory';
|
|
3
3
|
import {
|
|
4
4
|
MastraStorage,
|
|
5
5
|
TABLE_MESSAGES,
|
|
@@ -18,6 +18,7 @@ import type {
|
|
|
18
18
|
} from '@mastra/core/storage';
|
|
19
19
|
import type { WorkflowRunState } from '@mastra/core/workflows';
|
|
20
20
|
import { Redis } from '@upstash/redis';
|
|
21
|
+
import { MessageList } from '../../../../packages/core/dist/agent/index.cjs';
|
|
21
22
|
|
|
22
23
|
export interface UpstashConfig {
|
|
23
24
|
url: string;
|
|
@@ -509,7 +510,12 @@ export class UpstashStore extends MastraStorage {
|
|
|
509
510
|
await this.redis.del(key);
|
|
510
511
|
}
|
|
511
512
|
|
|
512
|
-
async saveMessages(
|
|
513
|
+
async saveMessages(args: { messages: MastraMessageV1[]; format?: undefined | 'v1' }): Promise<MastraMessageV1[]>;
|
|
514
|
+
async saveMessages(args: { messages: MastraMessageV2[]; format: 'v2' }): Promise<MastraMessageV2[]>;
|
|
515
|
+
async saveMessages(
|
|
516
|
+
args: { messages: MastraMessageV1[]; format?: undefined | 'v1' } | { messages: MastraMessageV2[]; format: 'v2' },
|
|
517
|
+
): Promise<MastraMessageV2[] | MastraMessageV1[]> {
|
|
518
|
+
const { messages, format = 'v1' } = args;
|
|
513
519
|
if (messages.length === 0) return [];
|
|
514
520
|
|
|
515
521
|
// Add an index to each message to maintain order
|
|
@@ -523,14 +529,14 @@ export class UpstashStore extends MastraStorage {
|
|
|
523
529
|
const batch = messagesWithIndex.slice(i, i + batchSize);
|
|
524
530
|
const pipeline = this.redis.pipeline();
|
|
525
531
|
for (const message of batch) {
|
|
526
|
-
const key = this.getMessageKey(message.threadId
|
|
532
|
+
const key = this.getMessageKey(message.threadId!, message.id);
|
|
527
533
|
const score = message._index !== undefined ? message._index : new Date(message.createdAt).getTime();
|
|
528
534
|
|
|
529
535
|
// Store the message data
|
|
530
536
|
pipeline.set(key, message);
|
|
531
537
|
|
|
532
538
|
// Add to sorted set for this thread
|
|
533
|
-
pipeline.zadd(this.getThreadMessagesKey(message.threadId), {
|
|
539
|
+
pipeline.zadd(this.getThreadMessagesKey(message.threadId!), {
|
|
534
540
|
score,
|
|
535
541
|
member: message.id,
|
|
536
542
|
});
|
|
@@ -539,10 +545,18 @@ export class UpstashStore extends MastraStorage {
|
|
|
539
545
|
await pipeline.exec();
|
|
540
546
|
}
|
|
541
547
|
|
|
542
|
-
|
|
548
|
+
const list = new MessageList().add(messages, 'memory');
|
|
549
|
+
if (format === `v2`) return list.get.all.v2();
|
|
550
|
+
return list.get.all.v1();
|
|
543
551
|
}
|
|
544
552
|
|
|
545
|
-
async getMessages
|
|
553
|
+
public async getMessages(args: StorageGetMessagesArg & { format?: 'v1' }): Promise<MastraMessageV1[]>;
|
|
554
|
+
public async getMessages(args: StorageGetMessagesArg & { format: 'v2' }): Promise<MastraMessageV2[]>;
|
|
555
|
+
public async getMessages({
|
|
556
|
+
threadId,
|
|
557
|
+
selectBy,
|
|
558
|
+
format,
|
|
559
|
+
}: StorageGetMessagesArg & { format?: 'v1' | 'v2' }): Promise<MastraMessageV1[] | MastraMessageV2[]> {
|
|
546
560
|
const limit = typeof selectBy?.last === `number` ? selectBy.last : 40;
|
|
547
561
|
const messageIds = new Set<string>();
|
|
548
562
|
const threadMessagesKey = this.getThreadMessagesKey(threadId);
|
|
@@ -585,17 +599,21 @@ export class UpstashStore extends MastraStorage {
|
|
|
585
599
|
const messages = (
|
|
586
600
|
await Promise.all(
|
|
587
601
|
Array.from(messageIds).map(async id =>
|
|
588
|
-
this.redis.get<
|
|
602
|
+
this.redis.get<MastraMessageV2 & { _index?: number }>(this.getMessageKey(threadId, id)),
|
|
589
603
|
),
|
|
590
604
|
)
|
|
591
|
-
).filter(msg => msg !== null) as (
|
|
605
|
+
).filter(msg => msg !== null) as (MastraMessageV2 & { _index?: number })[];
|
|
592
606
|
|
|
593
607
|
// Sort messages by their position in the sorted set
|
|
594
608
|
const messageOrder = await this.redis.zrange(threadMessagesKey, 0, -1);
|
|
595
609
|
messages.sort((a, b) => messageOrder.indexOf(a!.id) - messageOrder.indexOf(b!.id));
|
|
596
610
|
|
|
597
611
|
// Remove _index before returning
|
|
598
|
-
|
|
612
|
+
const prepared = messages.map(({ _index, ...message }) => message as unknown as MastraMessageV1);
|
|
613
|
+
|
|
614
|
+
const list = new MessageList().add(prepared, 'memory');
|
|
615
|
+
if (format === `v2`) return list.get.all.v2();
|
|
616
|
+
return list.get.all.v1();
|
|
599
617
|
}
|
|
600
618
|
|
|
601
619
|
async persistWorkflowSnapshot(params: {
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { randomUUID } from 'crypto';
|
|
2
|
-
import type { MastraMessageV2 } from '@mastra/core
|
|
3
|
-
import type { MessageType } from '@mastra/core/memory';
|
|
2
|
+
import type { MastraMessageV2 } from '@mastra/core';
|
|
4
3
|
import type { TABLE_NAMES } from '@mastra/core/storage';
|
|
5
4
|
import {
|
|
6
5
|
TABLE_MESSAGES,
|
|
@@ -26,7 +25,7 @@ const createSampleThread = (date?: Date) => ({
|
|
|
26
25
|
metadata: { key: 'value' },
|
|
27
26
|
});
|
|
28
27
|
|
|
29
|
-
const createSampleMessage = (threadId: string, content: string = 'Hello'):
|
|
28
|
+
const createSampleMessage = (threadId: string, content: string = 'Hello'): MastraMessageV2 => ({
|
|
30
29
|
id: `msg-${randomUUID()}`,
|
|
31
30
|
role: 'user',
|
|
32
31
|
threadId,
|
|
@@ -51,6 +50,7 @@ const createSampleWorkflowSnapshot = (status: string, createdAt?: Date) => {
|
|
|
51
50
|
},
|
|
52
51
|
input: {},
|
|
53
52
|
} as WorkflowRunState['context'],
|
|
53
|
+
serializedStepGraph: [],
|
|
54
54
|
activePaths: [],
|
|
55
55
|
suspendedPaths: {},
|
|
56
56
|
runId,
|
|
@@ -319,15 +319,15 @@ describe('UpstashStore', () => {
|
|
|
319
319
|
});
|
|
320
320
|
|
|
321
321
|
it('should save and retrieve messages in order', async () => {
|
|
322
|
-
const messages:
|
|
322
|
+
const messages: MastraMessageV2[] = [
|
|
323
323
|
createSampleMessage(threadId, 'First'),
|
|
324
324
|
createSampleMessage(threadId, 'Second'),
|
|
325
325
|
createSampleMessage(threadId, 'Third'),
|
|
326
326
|
];
|
|
327
327
|
|
|
328
|
-
await store.saveMessages({ messages: messages });
|
|
328
|
+
await store.saveMessages({ messages: messages, format: 'v2' });
|
|
329
329
|
|
|
330
|
-
const retrievedMessages = await store.getMessages
|
|
330
|
+
const retrievedMessages = await store.getMessages({ threadId, format: 'v2' });
|
|
331
331
|
expect(retrievedMessages).toHaveLength(3);
|
|
332
332
|
expect(retrievedMessages.map((m: any) => m.content.parts[0].text)).toEqual(['First', 'Second', 'Third']);
|
|
333
333
|
});
|
|
@@ -353,11 +353,11 @@ describe('UpstashStore', () => {
|
|
|
353
353
|
},
|
|
354
354
|
createdAt: new Date(),
|
|
355
355
|
},
|
|
356
|
-
] as
|
|
356
|
+
] as MastraMessageV2[];
|
|
357
357
|
|
|
358
|
-
await store.saveMessages({ messages });
|
|
358
|
+
await store.saveMessages({ messages, format: 'v2' });
|
|
359
359
|
|
|
360
|
-
const retrievedMessages = await store.getMessages
|
|
360
|
+
const retrievedMessages = await store.getMessages({ threadId, format: 'v2' });
|
|
361
361
|
expect(retrievedMessages[0].content).toEqual(messages[0].content);
|
|
362
362
|
});
|
|
363
363
|
});
|
|
@@ -474,6 +474,7 @@ describe('UpstashStore', () => {
|
|
|
474
474
|
endedAt: new Date(Date.now() + 15000).getTime(),
|
|
475
475
|
},
|
|
476
476
|
} as WorkflowRunState['context'],
|
|
477
|
+
serializedStepGraph: [],
|
|
477
478
|
runId: testRunId,
|
|
478
479
|
activePaths: [],
|
|
479
480
|
suspendedPaths: {},
|