Ali MadooeiA
Convex Community12mo ago
3 replies
Ali Madooei

Convex Node runtime vs Node

Hi! I have a code that works outside of Convex runtime. It is short and simple so I put here for reference. When this runs on Convex, I get "Connection lost while action was in flight" and the logs show "Your request couldn't be completed. Try again later." The latter seems like a response from one of the external services I'm using. However, running the same code, with the same API keys, outside of Convex works perfectly fine. Has anyone came by something like this before?

"use node";

import { streamText } from "ai";
import { internalAction } from "./_generated/server";
import { internal } from "./_generated/api";
import { v } from "convex/values";
import { createMem0 } from "@mem0/vercel-ai-provider";

export const completion = internalAction({
  args: {
    chatId: v.id("chats"),
    prompt: v.string(),
    placeholderMessageId: v.id("messages"),
    temperature: v.optional(v.number()),
  },
  handler: async (ctx, args) => {

    const mem0 = createMem0({
      provider: "openai",
      mem0ApiKey: process.env.MEM0_API_KEY as string,
      apiKey: process.env.OPENAI_API_KEY as string,
    });

    const result = streamText({
      model: mem0("gpt-4-turbo", {
        user_id: args.chatId,
      }),
      prompt: args.prompt,
      temperature: args.temperature || 1,
    });

    // Update the placeholder message with the full response
    // as it comes in from the API
    let fullResponse = "";
    for await (const delta of result.textStream) {
      fullResponse += delta;
      await ctx.runMutation(internal.messages.update, {
        messageId: args.placeholderMessageId,
        content: fullResponse,
      });
    }
  },
});
Was this page helpful?