Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Module Resolution Error when triying to save a message automatically when passing the resourceId in the streamed response #2492

Open
mundume opened this issue Feb 24, 2025 · 1 comment
Labels
bug Something isn't working dependencies Pull requests that update a dependency file Engine Issue with Mastra's DB engine

Comments

@mundume
Copy link

mundume commented Feb 24, 2025

Module Resolution Error when triying to save a message automatically when passing the resourceId in the streamed response

Description

When passing the resourceId to the stream response for automatically saving of thread conversations in a Next.js API route, I encounter a module resolution error. The error suggests that the expression is too dynamic for the module resolver to handle.

Error Message

  27 |
  28 |   try {
> 29 |     const res = await myAgent.stream(messages, {
     |                ^
  30 |       threadId,
  31 |       resourceId,
  32 |     });
 ⨯ Error: Cannot find module as expression is too dynamic
at at async POST (src/app/api/chat/route.ts:29:16)

API Route (src/app/api/chat/route.ts):

import { mastra } from "@/mastra";
import { CoreUserMessage } from "@mastra/core";
import { memory } from "@/mastra/memory";

export async function POST(req: Request) {
  const { messages, resourceId, threadId } = await req.json();
  console.log({ resourceId });

  if (!mastra.memory) throw new Error("Mastra memory not set up");
  const myAgent = mastra.getAgent("myAgent");

  if (messages.length === 1) {
    const thread = await mastra.memory?.getThreadById({ threadId });

    if (!thread?.title || thread?.title === "New Thread") {
      const agent = mastra.getAgent("myAgent");
      const title = await agent.generateTitleFromUserMessage({
        message: messages.filter((m: CoreUserMessage) => m.role === "user")[0],
      });
      await memory.updateThread({
        id: threadId,
        title,
        metadata: { project: "mastraaa", topic: "architectureee" },
      });
    }
  }

  try {
    const res = await myAgent.stream(messages, {
      threadId,
      resourceId,
    });
    return res.toDataStreamResponse();
  } catch (error) {
    console.error(error);
    throw error;
  }
}

Agent Configuration:

import { Agent } from "@mastra/core/agent";
import { groq } from "@ai-sdk/groq";
import { memory } from "../memory";

export const myAgent = new Agent({
  name: "My Agent",
  instructions: "You are a helpful assistant.",
  //   @ts-expect-error
  memory,
  model: groq("llama-3.3-70b-versatile"),
});

Memory Configuration:

import { UpstashStore } from "@mastra/upstash";
import { Memory } from "@mastra/memory";

const storage = new UpstashStore({
  url: process.env.UPSTASH_URL!,
  token: process.env.UPSTASH_TOKEN!,
});

export const memory = new Memory({
  storage,
  options: { lastMessages: 10 },
});

Package Versions

{
  "dependencies": {
    "@ai-sdk/groq": "^1.1.10",
    "@ai-sdk/react": "^1.1.17",
    "@mastra/core": "0.4.0-alpha.1",
    "@mastra/memory": "^0.1.3",
    "@mastra/upstash": "^0.1.3",
    "next": "15.1.7"
  }
}

Environment

  • Framework: Next.js 15.1.7
  • Bundler: Using next dev --turbopack
  • Node.js Type: v23.7.0

Expected Behavior

The API route should successfully stream responses while handling the resource ID parameter.

Actual Behavior

The application throws a module resolution error, indicating that the expression is too dynamic to resolve.

Additional Context

  • The error occurs specifically when trying to automatically save responses with a resource ID
  • Memory storage is configured using Upstash(also doesnt work with postgres too)
  • There's a TypeScript expect-error comment in the agent configuration
  • Using Turbopack for development
@github-actions github-actions bot added bug Something isn't working dependencies Pull requests that update a dependency file Engine Issue with Mastra's DB engine labels Feb 24, 2025
@mundume mundume changed the title Module Resolution Error with Resource ID in Stream Response Module Resolution Error when triying to save a message automatically when passing the resourceId in the streamed response Feb 24, 2025
@GnaneshK24
Copy link

I think using dynamic import of @mastra/core can solve this issue. Assign this to me i can solve this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working dependencies Pull requests that update a dependency file Engine Issue with Mastra's DB engine
Projects
None yet
Development

No branches or pull requests

2 participants