KnowledgeSnapshot.fromMap constructor
KnowledgeSnapshot.fromMap(})
Reopens saved passages without tokenization or embedding inference.
Graph and typed metadata meanings are reconstructed by upstream OKF,
and linkFields are read from the saved sources again.
Implementation
factory KnowledgeSnapshot.fromMap(
Map<String, Object?> map, {
Iterable<OkfLinkField> linkFields = const [],
}) {
if (map['version'] != 1) {
throw const FormatException('Unsupported knowledge snapshot version.');
}
final bundleId = map['bundleId']! as String;
if (bundleId.trim().isEmpty) {
throw ArgumentError.value(bundleId, 'bundleId');
}
final sources = Map<String, String>.from(map['sources']! as Map);
final assets = List<String>.from(map['assets']! as List);
final parsed = _parseBundle(sources, assets);
final rawChunks = (map['chunks']! as List)
.map((value) => Chunk.fromMap(Map<String, Object?>.from(value as Map)))
.toList();
final rawContexts = Map<String, String>.from(map['contextTexts']! as Map);
final rawIds = rawChunks.map((chunk) => chunk.id).toSet();
if (rawIds.length != rawChunks.length ||
rawContexts.length != rawIds.length ||
rawChunks.any(
(chunk) =>
!parsed.metadata.containsKey(chunk.sourcePath) ||
!(rawContexts[chunk.id]?.endsWith(chunk.content) ?? false),
)) {
throw const FormatException('Inconsistent saved knowledge passages.');
}
final chunks = [
for (final chunk in rawChunks)
if (!parsed.excludedPaths.contains(chunk.sourcePath)) chunk,
];
final keptIds = {for (final chunk in chunks) chunk.id};
final contexts = {
for (final entry in rawContexts.entries)
if (keptIds.contains(entry.key)) entry.key: entry.value,
};
return KnowledgeSnapshot._(
bundleId,
List.unmodifiable(chunks),
OkfGraph.fromBundle(parsed.bundle),
List.unmodifiable(okfFieldEdges(parsed.bundle, linkFields)),
Map.unmodifiable(parsed.metadata),
Map.unmodifiable(contexts),
Map.unmodifiable(sources),
List.unmodifiable(assets),
List.unmodifiable(
(map['inputRecoveries'] as List? ?? const [])
.map(
(value) => _InputRecovery.fromMap(
Map<String, Object?>.from(value as Map),
),
)
.where((r) => !parsed.excludedPaths.contains(r.sourcePath)),
),
);
}