@mastra/upstash 0.10.2-alpha.1 → 0.10.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mastra/upstash",
3
- "version": "0.10.2-alpha.1",
3
+ "version": "0.10.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",
@@ -31,11 +31,11 @@
31
31
  "tsup": "^8.4.0",
32
32
  "typescript": "^5.8.2",
33
33
  "vitest": "^3.1.2",
34
- "@internal/lint": "0.0.7",
35
- "@mastra/core": "0.10.2-alpha.2"
34
+ "@internal/lint": "0.0.8",
35
+ "@mastra/core": "0.10.2"
36
36
  },
37
37
  "peerDependencies": {
38
- "@mastra/core": "^0.10.0-alpha.0"
38
+ "@mastra/core": "^0.10.2-alpha.0"
39
39
  },
40
40
  "scripts": {
41
41
  "pretest": "docker compose up -d",
@@ -1,5 +1,5 @@
1
1
  import type { MetricResult, TestInfo } from '@mastra/core/eval';
2
- import type { StorageThreadType, MessageType } from '@mastra/core/memory';
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({ messages }: { messages: MessageType[] }): Promise<MessageType[]> {
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, message.id);
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
- return messages;
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<T = unknown>({ threadId, selectBy }: StorageGetMessagesArg): Promise<T[]> {
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<MessageType & { _index?: number }>(this.getMessageKey(threadId, id)),
602
+ this.redis.get<MastraMessageV2 & { _index?: number }>(this.getMessageKey(threadId, id)),
589
603
  ),
590
604
  )
591
- ).filter(msg => msg !== null) as (MessageType & { _index?: number })[];
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
- return messages.map(({ _index, ...message }) => message as unknown as T);
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/agent';
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'): MessageType => ({
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: MessageType[] = [
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<MastraMessageV2[]>({ threadId });
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 MessageType[];
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<MastraMessageV2>({ threadId });
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: {},