Skip to content

messages in a session without consent #136

@ColdByDefault

Description

@ColdByDefault

When a user gives consent after already having messages in a session without consent, the session update doesn't retroactively log the previous messages. Only new messages after consent is granted will be logged. This could result in incomplete conversation logs where the beginning of the conversation is missing. Consider documenting this behavior or implementing logic to log previous messages when consent is granted if they're still in memory.

      // If consent is being granted for the first time, attempt to retroactively
      // log any previous in-memory messages for this session (if provided in
      // the current request body) so that the conversation log is complete.
      let additionalMessages = 2; // current user + assistant messages
      let retroactiveMessagesData:
        | Array<{
            id: string;
            sessionId: string;
            role: string;
            content: string;
            timestamp: Date;
            status: string | null;
            pageContext: string | null;
            errorDetails: string | null;
          }>
        | null = null;

      if (!existingSession.consentGiven && Array.isArray((requestBody as any).messages)) {
        const allMessages = (requestBody as any).messages as ChatMessage[];

        // Exclude the two messages that are about to be logged below
        const retroactiveMessages = allMessages.filter(
          (m) => m.id !== userMessage.id && m.id !== assistantMessage.id,
        );

        if (retroactiveMessages.length > 0) {
          additionalMessages += retroactiveMessages.length;
          retroactiveMessagesData = retroactiveMessages.map((m) => ({
            id: m.id,
            sessionId,
            role: m.role,
            content: m.content,
            timestamp: m.timestamp,
            status: m.status || null,
            pageContext: requestBody.context?.page || null,
            errorDetails: null,
          }));
        }
      }

      await prisma.$transaction(async (tx) => {
        await tx.chatSession.update({
          where: { id: sessionId },
          data: {
            lastActivityAt: new Date(),
            totalMessages: existingSession.totalMessages + additionalMessages,
            // Update consent if not already recorded
            ...(!existingSession.consentGiven && {
              consentGiven: true,
              consentTimestamp: new Date(),
            }),
          },
        });

        // If consent has just been granted and there are prior messages in memory,
        // persist them now so the log is complete from the start of the session.
        if (retroactiveMessagesData && retroactiveMessagesData.length > 0) {
          await tx.chatMessage.createMany({
            data: retroactiveMessagesData,
          });
        }

Originally posted by @Copilot in #135 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions