|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from pathlib import Path |
| 4 | +from typing import Any |
| 5 | + |
| 6 | +from memory_migrate_plugin.utils import write_json |
| 7 | + |
| 8 | + |
| 9 | +CANONICAL_SCHEMA_ID = "https://agent-memory-bridge.dev/schema/canonical-memory-package/v1.0" |
| 10 | + |
| 11 | + |
| 12 | +def build_canonical_package_schema() -> dict[str, Any]: |
| 13 | + return { |
| 14 | + "$schema": "https://json-schema.org/draft/2020-12/schema", |
| 15 | + "$id": CANONICAL_SCHEMA_ID, |
| 16 | + "title": "CanonicalMemoryPackage", |
| 17 | + "type": "object", |
| 18 | + "additionalProperties": False, |
| 19 | + "required": [ |
| 20 | + "schema_version", |
| 21 | + "package_id", |
| 22 | + "created_at", |
| 23 | + "source_formats", |
| 24 | + "metadata", |
| 25 | + "entries", |
| 26 | + ], |
| 27 | + "properties": { |
| 28 | + "schema_version": { |
| 29 | + "type": "string", |
| 30 | + "description": "Canonical package schema version.", |
| 31 | + "default": "1.0", |
| 32 | + }, |
| 33 | + "package_id": { |
| 34 | + "type": "string", |
| 35 | + "description": "Stable identifier for the package.", |
| 36 | + "minLength": 1, |
| 37 | + }, |
| 38 | + "created_at": { |
| 39 | + "type": "string", |
| 40 | + "description": "UTC creation timestamp in ISO 8601 format.", |
| 41 | + "format": "date-time", |
| 42 | + }, |
| 43 | + "source_formats": { |
| 44 | + "type": "array", |
| 45 | + "description": "Distinct source adapters represented in the package.", |
| 46 | + "items": {"type": "string"}, |
| 47 | + "uniqueItems": True, |
| 48 | + }, |
| 49 | + "metadata": { |
| 50 | + "type": "object", |
| 51 | + "description": "Package-level metadata and provenance fields.", |
| 52 | + "additionalProperties": True, |
| 53 | + }, |
| 54 | + "entries": { |
| 55 | + "type": "array", |
| 56 | + "description": "Normalized memory entries.", |
| 57 | + "items": {"$ref": "#/$defs/memoryEntry"}, |
| 58 | + }, |
| 59 | + }, |
| 60 | + "$defs": { |
| 61 | + "memoryEntry": { |
| 62 | + "type": "object", |
| 63 | + "additionalProperties": False, |
| 64 | + "required": [ |
| 65 | + "id", |
| 66 | + "kind", |
| 67 | + "title", |
| 68 | + "content", |
| 69 | + "tags", |
| 70 | + "source_format", |
| 71 | + "metadata", |
| 72 | + ], |
| 73 | + "properties": { |
| 74 | + "id": { |
| 75 | + "type": "string", |
| 76 | + "description": "Stable entry identifier.", |
| 77 | + "minLength": 1, |
| 78 | + }, |
| 79 | + "kind": { |
| 80 | + "type": "string", |
| 81 | + "description": "Semantic entry category such as note, project, or preference.", |
| 82 | + "minLength": 1, |
| 83 | + }, |
| 84 | + "title": { |
| 85 | + "type": "string", |
| 86 | + "description": "Human-readable entry title.", |
| 87 | + "minLength": 1, |
| 88 | + }, |
| 89 | + "content": { |
| 90 | + "type": "string", |
| 91 | + "description": "Primary body content for the memory entry.", |
| 92 | + }, |
| 93 | + "tags": { |
| 94 | + "type": "array", |
| 95 | + "description": "Keyword tags used for retrieval and filtering.", |
| 96 | + "items": {"type": "string"}, |
| 97 | + }, |
| 98 | + "source_format": { |
| 99 | + "type": "string", |
| 100 | + "description": "Adapter name that produced the entry.", |
| 101 | + "minLength": 1, |
| 102 | + }, |
| 103 | + "created_at": { |
| 104 | + "type": ["string", "null"], |
| 105 | + "description": "Original creation timestamp when available.", |
| 106 | + "format": "date-time", |
| 107 | + }, |
| 108 | + "updated_at": { |
| 109 | + "type": ["string", "null"], |
| 110 | + "description": "Original update timestamp when available.", |
| 111 | + "format": "date-time", |
| 112 | + }, |
| 113 | + "metadata": { |
| 114 | + "type": "object", |
| 115 | + "description": "Entry-level adapter metadata.", |
| 116 | + "additionalProperties": True, |
| 117 | + }, |
| 118 | + }, |
| 119 | + } |
| 120 | + }, |
| 121 | + } |
| 122 | + |
| 123 | + |
| 124 | +def write_canonical_package_schema(output_path: Path) -> dict[str, Any]: |
| 125 | + schema = build_canonical_package_schema() |
| 126 | + write_json(output_path, schema) |
| 127 | + return schema |
0 commit comments