fix(postgres): handle edge cases in getMessages payload parsing

SA
5d ago
+91-12

PR Checklist

Please check if your PR fulfills the following requirements:

Bugs / Features

What is the current behavior?

In PostgreSQLMemoryAdapter.getMessages (packages/postgres/src/memory-adapter.ts):

  1. When row.parts contains "null" or a non-array JSON value (e.g. {}), JSON.parse assigns a non-array value to parts, causing a TypeError: Cannot read properties of null (reading 'length') crash or returning an invalid UIMessage.parts shape.
  2. When falling back to legacy row.content containing plain text (where JSON.parse throws), an early return bypasses the metadata.createdAt normalization at the end of the mapper.
  3. When legacy row.content contains a JSON primitive string such as "123" or "true", JSON.parse returns a number or boolean, which fails the typeof content === "string" and Array.isArray(content) checks and silently drops the message content (parts = []).

What is the new behavior?

  1. row.parts is validated with Array.isArray(...) both after JSON.parse and when already parsed by pg, falling back safely to [] if it is not an array.
  2. Legacy plain-text row.content no longer returns early in the catch block, ensuring metadata.createdAt is consistently attached as a Date instance while preserving existing metadata fields.
  3. Legacy row.content values that parse as number or boolean primitives are preserved and converted into text parts ([{ type: "text", text: String(rawContent) }]).

fixes #1560

Notes for reviewers

  • Added unit regression tests in packages/postgres/src/memory-adapter.spec.ts covering all three edge cases.
  • Verified with pnpm --filter @voltagent/postgres test:coverage, pnpm --filter @voltagent/postgres build, and pnpm lint:ci.

Summary by cubic

Fixes edge cases in PostgreSQLMemoryAdapter.getMessages where malformed or legacy payloads caused crashes or lost content. Fixes #1560.

Bug Fixes

  • Non-array parts values (like "null" or {}) now safely fall back to [].
  • Legacy plain-text content no longer returns early, so metadata.createdAt is always normalized to a Date.
  • Legacy content storing JSON primitives (numbers, booleans) is preserved as text parts instead of being dropped.

Summary by CodeRabbit

  • Bug Fixes
    • Fixed message retrieval so invalid or non-array message parts are returned as empty arrays.
    • Preserved legacy plain-text content—including text that resembles a number or boolean—and populated its creation-time metadata.