Skip to content
reaatechREAATECH

@reaatech/agent-memory-core

npm v0.1.0

Provides the canonical TypeScript interfaces, enums, and utility functions for the agent-memory ecosystem, including vector similarity calculations and retry logic. It serves as the shared type definition layer for building custom memory adapters and agent integrations.

@reaatech/agent-memory-core

npm version License: MIT CI

Status: Pre-1.0 — APIs may change in minor versions. Pin to a specific version in production.

Canonical TypeScript types, enums, and utilities for the agent-memory system. This package is the single source of truth for the Memory data structure, lifecycle states, importance levels, contradiction strategies, and shared helpers used throughout the @reaatech/agent-memory-* ecosystem.

Installation

terminal
npm install @reaatech/agent-memory-core
# or
pnpm add @reaatech/agent-memory-core

Feature Overview

  • 5 enums, 6 interfaces — every domain concept has a first-class type
  • Retry with backoff — exponential backoff with jitter for transient failures
  • Cosine similarity — vector comparison for semantic search
  • Fetch with timeoutAbortController-based HTTP helper
  • Pluggable loggingLogger interface with default console implementation
  • Zero runtime dependencies — lightweight and tree-shakeable
  • Dual ESM/CJS output — works with import and require

Quick Start

typescript
import {
  Memory,
  MemoryType,
  MemoryImportance,
  MemorySource,
  MemoryLifecycle,
  cosineSimilarity,
  withRetry,
} from '@reaatech/agent-memory-core';
 
const memory: Memory = {
  id: 'abc-123',
  tenantId: 'default',
  ownerId: 'user-1',
  content: 'User prefers dark mode',
  type: MemoryType.PREFERENCE,
  category: 'ui',
  source: MemorySource.USER_STATEMENT,
  importance: MemoryImportance.HIGH,
  confidence: 0.95,
  tags: ['ui', 'preference'],
  lifecycle: MemoryLifecycle.ACTIVE,
  createdAt: new Date(),
  updatedAt: new Date(),
  lastAccessedAt: new Date(),
  embeddings: {
    vector: [0.1, 0.2, 0.3],
    model: 'text-embedding-3-small',
    dimensions: 1536,
  },
  version: 1,
  history: [],
};
 
const similarity = cosineSimilarity([0.1, 0.2], [0.3, 0.4]);
 
const result = await withRetry(async () => {
  const response = await fetch('https://api.example.com/data');
  if (!response.ok) throw new Error(`HTTP ${response.status}`);
  return response.json();
});

API Reference

Domain Enums

EnumValues
MemoryTypeFACT, PREFERENCE, DECISION, CORRECTION, CONTEXT, EPISODIC
MemoryImportanceCRITICAL, HIGH, MEDIUM, LOW, TRANSIENT
MemorySourceUSER_STATEMENT, AGENT_INFERENCE, SYSTEM_EVENT
MemoryLifecycleACTIVE, ARCHIVED, PENDING_REVIEW, FORGOTTEN
ContradictionStrategyNEWEST_WINS, OLDEST_WINS, HIGHEST_CONFIDENCE, MANUAL_REVIEW

Core Interfaces

InterfaceDescription
MemoryThe central data structure — 21 fields covering identity, content, metadata, lifecycle, embeddings, and relationships
MemoryCandidateA proposed memory before full processing and storage
MemoryVersionA single entry in the memory audit trail
EmbeddingMetadataVector metadata: model, dimensions, vector
ConversationTurnA single message in a conversation: speaker, content, timestamp
HealthStatusReported by adapters: status, timestamp, latencyMs?, error?

Type Aliases

TypeDefinition
MemoryIdstring (UUID v4)
TenantIdstring
OwnerIdstring

Functions

cosineSimilarity(a: number[], b: number[]): number

Computes the cosine similarity between two dense vectors. Returns a value between -1 and 1.

typescript
const score = cosineSimilarity(queryVector, memoryVector);
// 0.87 — highly similar; -0.1 — unrelated

withRetry<T>(fn: () => Promise<T>, config?: RetryConfig): Promise<T>

Retries a function with exponential backoff and jitter on failure.

typescript
const data = await withRetry(
  () => fetchData(),
  { maxAttempts: 5, baseDelayMs: 100, maxDelayMs: 5000 },
);

RetryConfig

PropertyTypeDefaultDescription
maxAttemptsnumber3Maximum retry attempts
baseDelayMsnumber1000Initial delay in milliseconds
maxDelayMsnumber30000Maximum delay cap
shouldRetry(error: Error) => boolean() => trueCondition for retrying

fetchWithTimeout(url: string, init?: RequestInit, timeoutMs?: number): Promise<Response>

Wraps fetch with an AbortController timeout. Handles racing signals if the caller supplies its own.

typescript
const res = await fetchWithTimeout('https://api.example.com', {}, 5000);

setLogger(logger: Logger): void / getLogger(): Logger

Pluggable logging. Set a custom logger (Pino, Winston, etc.) via setLogger. Defaults to console.

typescript
import { setLogger, getLogger, type Logger } from '@reaatech/agent-memory-core';
import pino from 'pino';
 
const logger: Logger = pino({ name: 'agent-memory' });
setLogger(logger);
 
const log = getLogger();
log.info('Memory system initialized');

License

MIT