feat: add IdentityDB core memory graph APIs
This commit is contained in:
64
src/core/utils.ts
Normal file
64
src/core/utils.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
import { randomUUID } from 'node:crypto';
|
||||
|
||||
import type { Fact, FactTopic, Topic } from '../types/api';
|
||||
import type { FactRecord, TopicRecord } from '../types/domain';
|
||||
|
||||
export function normalizeTopicName(name: string): string {
|
||||
return name.trim().replace(/\s+/g, ' ').toLowerCase();
|
||||
}
|
||||
|
||||
export function canonicalizeTopicName(name: string): string {
|
||||
return name.trim().replace(/\s+/g, ' ');
|
||||
}
|
||||
|
||||
export function nowIsoString(): string {
|
||||
return new Date().toISOString();
|
||||
}
|
||||
|
||||
export function createId(): string {
|
||||
return randomUUID();
|
||||
}
|
||||
|
||||
export function serializeMetadata(metadata: unknown): string | null {
|
||||
if (metadata === undefined || metadata === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return JSON.stringify(metadata);
|
||||
}
|
||||
|
||||
export function deserializeMetadata(metadata: string | null): unknown | null {
|
||||
if (metadata === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return JSON.parse(metadata);
|
||||
}
|
||||
|
||||
export function mapTopicRow(record: TopicRecord): Topic {
|
||||
return {
|
||||
id: record.id,
|
||||
name: record.name,
|
||||
normalizedName: record.normalized_name,
|
||||
category: record.category,
|
||||
granularity: record.granularity,
|
||||
description: record.description,
|
||||
metadata: deserializeMetadata(record.metadata) as Topic['metadata'],
|
||||
createdAt: record.created_at,
|
||||
updatedAt: record.updated_at,
|
||||
};
|
||||
}
|
||||
|
||||
export function mapFactRow(record: FactRecord, topics: FactTopic[]): Fact {
|
||||
return {
|
||||
id: record.id,
|
||||
statement: record.statement,
|
||||
summary: record.summary,
|
||||
source: record.source,
|
||||
confidence: record.confidence,
|
||||
metadata: deserializeMetadata(record.metadata) as Fact['metadata'],
|
||||
createdAt: record.created_at,
|
||||
updatedAt: record.updated_at,
|
||||
topics,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user