@x12i/xtivix-schema 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 x12
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,19 @@
1
+ # @x12i/xtivix-schema
2
+
3
+ Canonical AI call record schema (JSON Schema v1.0), TypeScript types, metadata rules and model aliases for xtivix.
4
+
5
+ Part of [xtivix](https://www.npmjs.com/package/@x12i/xtivix), which collects provider-side AI logs, normalizes them to one canonical record, stores them in MongoDB and forwards them to observability tools.
6
+
7
+ ## Install
8
+
9
+ ```bash
10
+ npm install @x12i/xtivix-schema
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ```ts
16
+ import { SCHEMA_VERSION, SCHEMA_MAJOR, canonicalRecordSchema } from '@x12i/xtivix-schema';
17
+ ```
18
+
19
+ ESM only, Node.js 22 or newer. Type declarations are included.
@@ -0,0 +1,170 @@
1
+ {
2
+ "$schema": "http://json-schema.org/draft-07/schema#",
3
+ "$id": "https://xtivix.dev/schemas/canonical-record.v1.json",
4
+ "title": "CanonicalRecord",
5
+ "description": "One provider-side AI call, normalized. Schema version 1.x. Changes within 1.x are additive; readers ignore unknown fields.",
6
+ "type": "object",
7
+ "required": ["schema_version", "id", "source", "provider_record_id", "started_at", "operation", "model", "status", "metadata", "content_state"],
8
+ "additionalProperties": true,
9
+ "definitions": {
10
+ "Timestamp": {
11
+ "type": "string",
12
+ "pattern": "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(\\.\\d{1,9})?Z$"
13
+ },
14
+ "Part": {
15
+ "title": "Part",
16
+ "type": "object",
17
+ "required": ["type"],
18
+ "additionalProperties": true,
19
+ "properties": {
20
+ "type": { "type": "string", "enum": ["text", "tool_call", "tool_result", "media_ref"] },
21
+ "text": { "type": "string" },
22
+ "tool_call_id": { "type": "string" },
23
+ "name": { "type": "string" },
24
+ "arguments": { "type": "string", "description": "Tool call arguments as a JSON string" },
25
+ "content": { "type": "string", "description": "Tool result content" },
26
+ "is_error": { "type": "boolean" },
27
+ "media_type": { "type": "string", "enum": ["image", "audio", "video", "file", "other"] },
28
+ "mime_type": { "type": "string" },
29
+ "size_bytes": { "type": "number" },
30
+ "uri": { "type": "string" }
31
+ }
32
+ },
33
+ "Message": {
34
+ "title": "Message",
35
+ "type": "object",
36
+ "required": ["role", "parts"],
37
+ "additionalProperties": true,
38
+ "properties": {
39
+ "role": { "type": "string", "enum": ["system", "user", "assistant", "tool"] },
40
+ "name": { "type": "string" },
41
+ "parts": { "type": "array", "items": { "$ref": "#/definitions/Part" } }
42
+ }
43
+ },
44
+ "MetadataValue": {
45
+ "type": ["string", "number", "boolean"]
46
+ }
47
+ },
48
+ "properties": {
49
+ "schema_version": { "type": "string", "pattern": "^1\\.\\d+$" },
50
+ "id": { "type": "string", "minLength": 3, "maxLength": 512 },
51
+ "tenant_id": { "type": "string", "description": "Reserved for multi-tenancy (SEC-12). Unused in v1." },
52
+ "source": {
53
+ "title": "RecordSource",
54
+ "type": "object",
55
+ "required": ["id", "provider"],
56
+ "additionalProperties": true,
57
+ "properties": {
58
+ "id": { "type": "string", "pattern": "^[a-z0-9_-]{1,64}$" },
59
+ "provider": { "type": "string", "enum": ["openrouter", "cloudflare_ai_gateway", "aws_bedrock", "custom"] },
60
+ "scope": { "type": "string" }
61
+ }
62
+ },
63
+ "provider_record_id": { "type": "string", "minLength": 1, "maxLength": 440 },
64
+ "started_at": { "$ref": "#/definitions/Timestamp" },
65
+ "ended_at": { "$ref": "#/definitions/Timestamp" },
66
+ "received_at": { "$ref": "#/definitions/Timestamp" },
67
+ "ingested_at": { "$ref": "#/definitions/Timestamp" },
68
+ "operation": { "type": "string", "enum": ["chat", "completion", "embedding", "image", "other"] },
69
+ "model": {
70
+ "title": "RecordModel",
71
+ "type": "object",
72
+ "required": ["raw"],
73
+ "additionalProperties": true,
74
+ "properties": {
75
+ "raw": { "type": "string" },
76
+ "canonical": { "type": "string" },
77
+ "upstream_provider": { "type": "string" }
78
+ }
79
+ },
80
+ "request": {
81
+ "title": "RecordRequest",
82
+ "type": "object",
83
+ "additionalProperties": true,
84
+ "properties": {
85
+ "messages": { "type": "array", "items": { "$ref": "#/definitions/Message" } },
86
+ "params": {
87
+ "title": "RequestParams",
88
+ "type": "object",
89
+ "additionalProperties": true,
90
+ "properties": {
91
+ "temperature": { "type": "number" },
92
+ "top_p": { "type": "number" },
93
+ "max_tokens": { "type": "number" },
94
+ "stream": { "type": "boolean" },
95
+ "tools": { "type": "array" },
96
+ "extra": { "type": "object" }
97
+ }
98
+ }
99
+ }
100
+ },
101
+ "response": {
102
+ "title": "RecordResponse",
103
+ "type": "object",
104
+ "additionalProperties": true,
105
+ "properties": {
106
+ "messages": { "type": "array", "items": { "$ref": "#/definitions/Message" } },
107
+ "finish_reason": { "type": "string" }
108
+ }
109
+ },
110
+ "status": { "type": "string", "enum": ["ok", "error", "cached", "filtered"] },
111
+ "error": {
112
+ "title": "RecordError",
113
+ "type": "object",
114
+ "additionalProperties": true,
115
+ "properties": {
116
+ "code": { "type": "string" },
117
+ "message": { "type": "string" },
118
+ "http_status": { "type": "number" }
119
+ }
120
+ },
121
+ "usage": {
122
+ "title": "RecordUsage",
123
+ "type": "object",
124
+ "additionalProperties": true,
125
+ "properties": {
126
+ "input_tokens": { "type": "number" },
127
+ "output_tokens": { "type": "number" },
128
+ "total_tokens": { "type": "number" },
129
+ "cached_tokens": { "type": "number" },
130
+ "reasoning_tokens": { "type": "number" }
131
+ }
132
+ },
133
+ "cost": {
134
+ "title": "RecordCost",
135
+ "type": "object",
136
+ "required": ["source"],
137
+ "additionalProperties": true,
138
+ "properties": {
139
+ "amount": { "type": "number" },
140
+ "currency": { "type": "string" },
141
+ "source": { "type": "string", "enum": ["provider", "none"] }
142
+ }
143
+ },
144
+ "latency_ms": { "type": "number" },
145
+ "ttft_ms": { "type": "number" },
146
+ "metadata": {
147
+ "type": "object",
148
+ "maxProperties": 50,
149
+ "propertyNames": { "pattern": "^[a-z0-9_.-]{1,64}$" },
150
+ "additionalProperties": { "$ref": "#/definitions/MetadataValue" }
151
+ },
152
+ "tags": { "type": "array", "items": { "type": "string" } },
153
+ "provider_metadata": { "type": "object", "additionalProperties": true },
154
+ "content_state": { "type": "string", "enum": ["full", "redacted", "truncated", "absent"] },
155
+ "content_omitted_reason": {
156
+ "type": "string",
157
+ "enum": ["store_content_off", "provider", "retention", "scope", "not_requested", "body_unavailable"]
158
+ },
159
+ "redaction": {
160
+ "title": "RecordRedaction",
161
+ "type": "object",
162
+ "additionalProperties": true,
163
+ "properties": {
164
+ "policy": { "type": "string" },
165
+ "version": { "type": "number" }
166
+ }
167
+ },
168
+ "raw": { "description": "Original provider payload, kept only when enabled for the source" }
169
+ }
170
+ }
@@ -0,0 +1,156 @@
1
+ type Timestamp = string;
2
+ type MetadataValue = string | number | boolean;
3
+ /**
4
+ * One provider-side AI call, normalized. Schema version 1.x. Changes within 1.x are additive; readers ignore unknown fields.
5
+ */
6
+ interface CanonicalRecord {
7
+ schema_version: string;
8
+ id: string;
9
+ /**
10
+ * Reserved for multi-tenancy (SEC-12). Unused in v1.
11
+ */
12
+ tenant_id?: string;
13
+ source: RecordSource;
14
+ provider_record_id: string;
15
+ started_at: Timestamp;
16
+ ended_at?: Timestamp;
17
+ received_at?: Timestamp;
18
+ ingested_at?: Timestamp;
19
+ operation: 'chat' | 'completion' | 'embedding' | 'image' | 'other';
20
+ model: RecordModel;
21
+ request?: RecordRequest;
22
+ response?: RecordResponse;
23
+ status: 'ok' | 'error' | 'cached' | 'filtered';
24
+ error?: RecordError;
25
+ usage?: RecordUsage;
26
+ cost?: RecordCost;
27
+ latency_ms?: number;
28
+ ttft_ms?: number;
29
+ metadata: {
30
+ [k: string]: MetadataValue;
31
+ };
32
+ tags?: string[];
33
+ provider_metadata?: {
34
+ [k: string]: unknown;
35
+ };
36
+ content_state: 'full' | 'redacted' | 'truncated' | 'absent';
37
+ content_omitted_reason?: 'store_content_off' | 'provider' | 'retention' | 'scope' | 'not_requested' | 'body_unavailable';
38
+ redaction?: RecordRedaction;
39
+ /**
40
+ * Original provider payload, kept only when enabled for the source
41
+ */
42
+ raw?: {
43
+ [k: string]: unknown;
44
+ };
45
+ [k: string]: unknown;
46
+ }
47
+ interface RecordSource {
48
+ id: string;
49
+ provider: 'openrouter' | 'cloudflare_ai_gateway' | 'aws_bedrock' | 'custom';
50
+ scope?: string;
51
+ [k: string]: unknown;
52
+ }
53
+ interface RecordModel {
54
+ raw: string;
55
+ canonical?: string;
56
+ upstream_provider?: string;
57
+ [k: string]: unknown;
58
+ }
59
+ interface RecordRequest {
60
+ messages?: Message[];
61
+ params?: RequestParams;
62
+ [k: string]: unknown;
63
+ }
64
+ interface Message {
65
+ role: 'system' | 'user' | 'assistant' | 'tool';
66
+ name?: string;
67
+ parts: Part[];
68
+ [k: string]: unknown;
69
+ }
70
+ interface Part {
71
+ type: 'text' | 'tool_call' | 'tool_result' | 'media_ref';
72
+ text?: string;
73
+ tool_call_id?: string;
74
+ name?: string;
75
+ /**
76
+ * Tool call arguments as a JSON string
77
+ */
78
+ arguments?: string;
79
+ /**
80
+ * Tool result content
81
+ */
82
+ content?: string;
83
+ is_error?: boolean;
84
+ media_type?: 'image' | 'audio' | 'video' | 'file' | 'other';
85
+ mime_type?: string;
86
+ size_bytes?: number;
87
+ uri?: string;
88
+ [k: string]: unknown;
89
+ }
90
+ interface RequestParams {
91
+ temperature?: number;
92
+ top_p?: number;
93
+ max_tokens?: number;
94
+ stream?: boolean;
95
+ tools?: unknown[];
96
+ extra?: {};
97
+ [k: string]: unknown;
98
+ }
99
+ interface RecordResponse {
100
+ messages?: Message[];
101
+ finish_reason?: string;
102
+ [k: string]: unknown;
103
+ }
104
+ interface RecordError {
105
+ code?: string;
106
+ message?: string;
107
+ http_status?: number;
108
+ [k: string]: unknown;
109
+ }
110
+ interface RecordUsage {
111
+ input_tokens?: number;
112
+ output_tokens?: number;
113
+ total_tokens?: number;
114
+ cached_tokens?: number;
115
+ reasoning_tokens?: number;
116
+ [k: string]: unknown;
117
+ }
118
+ interface RecordCost {
119
+ amount?: number;
120
+ currency?: string;
121
+ source: 'provider' | 'none';
122
+ [k: string]: unknown;
123
+ }
124
+ interface RecordRedaction {
125
+ policy?: string;
126
+ version?: number;
127
+ [k: string]: unknown;
128
+ }
129
+
130
+ declare const SCHEMA_VERSION = "1.0";
131
+ declare const SCHEMA_MAJOR = 1;
132
+ declare const canonicalRecordSchema: Record<string, unknown>;
133
+ interface SchemaValidationResult {
134
+ valid: boolean;
135
+ errors: string[];
136
+ }
137
+ declare function validateRecord(record: unknown): SchemaValidationResult;
138
+ declare const METADATA_KEY_RE: RegExp;
139
+ declare const METADATA_MAX_KEYS = 50;
140
+ declare const CONVENTIONAL_METADATA_KEYS: readonly ["user_id", "session_id", "trace_id"];
141
+ interface MetadataNormalization {
142
+ metadata: Record<string, MetadataValue>;
143
+ rejected: Record<string, unknown>;
144
+ }
145
+ /**
146
+ * Applies REC-5: keys must match the pattern, values must be scalars, at most 50 keys.
147
+ * Anything that breaks the rules is returned in `rejected` so the caller can keep it in
148
+ * `provider_metadata.rejected_metadata` rather than dropping it.
149
+ */
150
+ declare function normalizeMetadata(input: Record<string, unknown> | undefined | null): MetadataNormalization;
151
+ /** Flattens nested JSON into dotted keys, e.g. `{a:{b:1}}` -> `{"a.b":1}`. Arrays are JSON-encoded. */
152
+ declare function flattenMetadata(input: unknown, prefix?: string, out?: Record<string, unknown>): Record<string, unknown>;
153
+ /** Best-effort canonical alias for a raw provider model name. Returns undefined when unknown. */
154
+ declare function resolveModelAlias(raw: string | undefined | null): string | undefined;
155
+
156
+ export { CONVENTIONAL_METADATA_KEYS, type CanonicalRecord, METADATA_KEY_RE, METADATA_MAX_KEYS, type Message, type MetadataNormalization, type MetadataValue, type Part, type RecordCost, type RecordError, type RecordModel, type RecordRedaction, type RecordRequest, type RecordResponse, type RecordSource, type RecordUsage, type RequestParams, SCHEMA_MAJOR, SCHEMA_VERSION, type SchemaValidationResult, type Timestamp, canonicalRecordSchema, flattenMetadata, normalizeMetadata, resolveModelAlias, validateRecord };
package/dist/index.js ADDED
@@ -0,0 +1,279 @@
1
+ // src/index.ts
2
+ import { readFileSync } from "fs";
3
+ import { fileURLToPath } from "url";
4
+ import path from "path";
5
+ import AjvModule from "ajv";
6
+ import { parse as parseYaml } from "yaml";
7
+
8
+ // canonical-record.v1.json
9
+ var canonical_record_v1_default = {
10
+ $schema: "http://json-schema.org/draft-07/schema#",
11
+ $id: "https://xtivix.dev/schemas/canonical-record.v1.json",
12
+ title: "CanonicalRecord",
13
+ description: "One provider-side AI call, normalized. Schema version 1.x. Changes within 1.x are additive; readers ignore unknown fields.",
14
+ type: "object",
15
+ required: ["schema_version", "id", "source", "provider_record_id", "started_at", "operation", "model", "status", "metadata", "content_state"],
16
+ additionalProperties: true,
17
+ definitions: {
18
+ Timestamp: {
19
+ type: "string",
20
+ pattern: "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(\\.\\d{1,9})?Z$"
21
+ },
22
+ Part: {
23
+ title: "Part",
24
+ type: "object",
25
+ required: ["type"],
26
+ additionalProperties: true,
27
+ properties: {
28
+ type: { type: "string", enum: ["text", "tool_call", "tool_result", "media_ref"] },
29
+ text: { type: "string" },
30
+ tool_call_id: { type: "string" },
31
+ name: { type: "string" },
32
+ arguments: { type: "string", description: "Tool call arguments as a JSON string" },
33
+ content: { type: "string", description: "Tool result content" },
34
+ is_error: { type: "boolean" },
35
+ media_type: { type: "string", enum: ["image", "audio", "video", "file", "other"] },
36
+ mime_type: { type: "string" },
37
+ size_bytes: { type: "number" },
38
+ uri: { type: "string" }
39
+ }
40
+ },
41
+ Message: {
42
+ title: "Message",
43
+ type: "object",
44
+ required: ["role", "parts"],
45
+ additionalProperties: true,
46
+ properties: {
47
+ role: { type: "string", enum: ["system", "user", "assistant", "tool"] },
48
+ name: { type: "string" },
49
+ parts: { type: "array", items: { $ref: "#/definitions/Part" } }
50
+ }
51
+ },
52
+ MetadataValue: {
53
+ type: ["string", "number", "boolean"]
54
+ }
55
+ },
56
+ properties: {
57
+ schema_version: { type: "string", pattern: "^1\\.\\d+$" },
58
+ id: { type: "string", minLength: 3, maxLength: 512 },
59
+ tenant_id: { type: "string", description: "Reserved for multi-tenancy (SEC-12). Unused in v1." },
60
+ source: {
61
+ title: "RecordSource",
62
+ type: "object",
63
+ required: ["id", "provider"],
64
+ additionalProperties: true,
65
+ properties: {
66
+ id: { type: "string", pattern: "^[a-z0-9_-]{1,64}$" },
67
+ provider: { type: "string", enum: ["openrouter", "cloudflare_ai_gateway", "aws_bedrock", "custom"] },
68
+ scope: { type: "string" }
69
+ }
70
+ },
71
+ provider_record_id: { type: "string", minLength: 1, maxLength: 440 },
72
+ started_at: { $ref: "#/definitions/Timestamp" },
73
+ ended_at: { $ref: "#/definitions/Timestamp" },
74
+ received_at: { $ref: "#/definitions/Timestamp" },
75
+ ingested_at: { $ref: "#/definitions/Timestamp" },
76
+ operation: { type: "string", enum: ["chat", "completion", "embedding", "image", "other"] },
77
+ model: {
78
+ title: "RecordModel",
79
+ type: "object",
80
+ required: ["raw"],
81
+ additionalProperties: true,
82
+ properties: {
83
+ raw: { type: "string" },
84
+ canonical: { type: "string" },
85
+ upstream_provider: { type: "string" }
86
+ }
87
+ },
88
+ request: {
89
+ title: "RecordRequest",
90
+ type: "object",
91
+ additionalProperties: true,
92
+ properties: {
93
+ messages: { type: "array", items: { $ref: "#/definitions/Message" } },
94
+ params: {
95
+ title: "RequestParams",
96
+ type: "object",
97
+ additionalProperties: true,
98
+ properties: {
99
+ temperature: { type: "number" },
100
+ top_p: { type: "number" },
101
+ max_tokens: { type: "number" },
102
+ stream: { type: "boolean" },
103
+ tools: { type: "array" },
104
+ extra: { type: "object" }
105
+ }
106
+ }
107
+ }
108
+ },
109
+ response: {
110
+ title: "RecordResponse",
111
+ type: "object",
112
+ additionalProperties: true,
113
+ properties: {
114
+ messages: { type: "array", items: { $ref: "#/definitions/Message" } },
115
+ finish_reason: { type: "string" }
116
+ }
117
+ },
118
+ status: { type: "string", enum: ["ok", "error", "cached", "filtered"] },
119
+ error: {
120
+ title: "RecordError",
121
+ type: "object",
122
+ additionalProperties: true,
123
+ properties: {
124
+ code: { type: "string" },
125
+ message: { type: "string" },
126
+ http_status: { type: "number" }
127
+ }
128
+ },
129
+ usage: {
130
+ title: "RecordUsage",
131
+ type: "object",
132
+ additionalProperties: true,
133
+ properties: {
134
+ input_tokens: { type: "number" },
135
+ output_tokens: { type: "number" },
136
+ total_tokens: { type: "number" },
137
+ cached_tokens: { type: "number" },
138
+ reasoning_tokens: { type: "number" }
139
+ }
140
+ },
141
+ cost: {
142
+ title: "RecordCost",
143
+ type: "object",
144
+ required: ["source"],
145
+ additionalProperties: true,
146
+ properties: {
147
+ amount: { type: "number" },
148
+ currency: { type: "string" },
149
+ source: { type: "string", enum: ["provider", "none"] }
150
+ }
151
+ },
152
+ latency_ms: { type: "number" },
153
+ ttft_ms: { type: "number" },
154
+ metadata: {
155
+ type: "object",
156
+ maxProperties: 50,
157
+ propertyNames: { pattern: "^[a-z0-9_.-]{1,64}$" },
158
+ additionalProperties: { $ref: "#/definitions/MetadataValue" }
159
+ },
160
+ tags: { type: "array", items: { type: "string" } },
161
+ provider_metadata: { type: "object", additionalProperties: true },
162
+ content_state: { type: "string", enum: ["full", "redacted", "truncated", "absent"] },
163
+ content_omitted_reason: {
164
+ type: "string",
165
+ enum: ["store_content_off", "provider", "retention", "scope", "not_requested", "body_unavailable"]
166
+ },
167
+ redaction: {
168
+ title: "RecordRedaction",
169
+ type: "object",
170
+ additionalProperties: true,
171
+ properties: {
172
+ policy: { type: "string" },
173
+ version: { type: "number" }
174
+ }
175
+ },
176
+ raw: { description: "Original provider payload, kept only when enabled for the source" }
177
+ }
178
+ };
179
+
180
+ // src/index.ts
181
+ var SCHEMA_VERSION = "1.0";
182
+ var SCHEMA_MAJOR = 1;
183
+ var canonicalRecordSchema = canonical_record_v1_default;
184
+ function aliasFilePath() {
185
+ if (process.env.XTIVIX_MODEL_ALIASES) return process.env.XTIVIX_MODEL_ALIASES;
186
+ const here = path.dirname(fileURLToPath(import.meta.url));
187
+ return path.join(here, "..", "model-aliases.yaml");
188
+ }
189
+ var Ajv = AjvModule.default ?? AjvModule;
190
+ var ajv = new Ajv({ allErrors: true, strict: false });
191
+ var validateFn = ajv.compile(canonicalRecordSchema);
192
+ function formatErrors(errors) {
193
+ return (errors ?? []).map((e) => `${e.instancePath || "/"} ${e.message ?? "invalid"}`.trim());
194
+ }
195
+ function validateRecord(record) {
196
+ const valid = validateFn(record);
197
+ return { valid, errors: valid ? [] : formatErrors(validateFn.errors) };
198
+ }
199
+ var METADATA_KEY_RE = /^[a-z0-9_.-]{1,64}$/;
200
+ var METADATA_MAX_KEYS = 50;
201
+ var CONVENTIONAL_METADATA_KEYS = ["user_id", "session_id", "trace_id"];
202
+ function normalizeMetadata(input) {
203
+ const metadata = {};
204
+ const rejected = {};
205
+ if (!input) return { metadata, rejected };
206
+ const entries = Object.entries(input).sort(([a], [b]) => {
207
+ const ia = CONVENTIONAL_METADATA_KEYS.indexOf(a);
208
+ const ib = CONVENTIONAL_METADATA_KEYS.indexOf(b);
209
+ return (ia === -1 ? 99 : ia) - (ib === -1 ? 99 : ib);
210
+ });
211
+ for (const [key, value] of entries) {
212
+ const scalar = typeof value === "string" || typeof value === "boolean" || typeof value === "number" && Number.isFinite(value);
213
+ if (!METADATA_KEY_RE.test(key) || !scalar || Object.keys(metadata).length >= METADATA_MAX_KEYS) {
214
+ rejected[key] = value;
215
+ continue;
216
+ }
217
+ metadata[key] = value;
218
+ }
219
+ return { metadata, rejected };
220
+ }
221
+ function flattenMetadata(input, prefix = "", out = {}) {
222
+ if (input === null || input === void 0) return out;
223
+ if (typeof input !== "object" || Array.isArray(input)) {
224
+ if (prefix) out[prefix] = Array.isArray(input) ? JSON.stringify(input) : input;
225
+ return out;
226
+ }
227
+ for (const [k, v] of Object.entries(input)) {
228
+ const key = prefix ? `${prefix}.${k}` : k;
229
+ if (v && typeof v === "object" && !Array.isArray(v)) flattenMetadata(v, key, out);
230
+ else if (Array.isArray(v)) out[key] = JSON.stringify(v);
231
+ else out[key] = v;
232
+ }
233
+ return out;
234
+ }
235
+ var aliasCache;
236
+ function loadAliases() {
237
+ if (aliasCache) return aliasCache;
238
+ const file = parseYaml(readFileSync(aliasFilePath(), "utf8"));
239
+ aliasCache = {
240
+ exact: new Map(Object.entries(file.exact ?? {}).map(([k, v]) => [k.toLowerCase(), v])),
241
+ rules: (file.rules ?? []).map((r) => ({ re: new RegExp(r.match, "i"), replace: r.replace, reapply: !!r.reapply }))
242
+ };
243
+ return aliasCache;
244
+ }
245
+ function resolveModelAlias(raw) {
246
+ if (!raw) return void 0;
247
+ const { exact, rules } = loadAliases();
248
+ let current = raw.trim().toLowerCase();
249
+ for (let depth = 0; depth < 4; depth++) {
250
+ const hit = exact.get(current);
251
+ if (hit) return hit;
252
+ let rewritten;
253
+ let reapply = false;
254
+ for (const rule of rules) {
255
+ if (rule.re.test(current)) {
256
+ rewritten = current.replace(rule.re, rule.replace);
257
+ reapply = rule.reapply;
258
+ break;
259
+ }
260
+ }
261
+ if (rewritten === void 0) return void 0;
262
+ if (!reapply) return exact.get(rewritten) ?? rewritten;
263
+ current = rewritten;
264
+ }
265
+ return void 0;
266
+ }
267
+ export {
268
+ CONVENTIONAL_METADATA_KEYS,
269
+ METADATA_KEY_RE,
270
+ METADATA_MAX_KEYS,
271
+ SCHEMA_MAJOR,
272
+ SCHEMA_VERSION,
273
+ canonicalRecordSchema,
274
+ flattenMetadata,
275
+ normalizeMetadata,
276
+ resolveModelAlias,
277
+ validateRecord
278
+ };
279
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts","../canonical-record.v1.json"],"sourcesContent":["import { readFileSync } from 'node:fs';\r\nimport { fileURLToPath } from 'node:url';\r\nimport path from 'node:path';\r\nimport AjvModule, { type ErrorObject, type ValidateFunction } from 'ajv';\r\nimport { parse as parseYaml } from 'yaml';\r\nimport schemaJson from '../canonical-record.v1.json' with { type: 'json' };\r\nimport type { CanonicalRecord, MetadataValue } from './types.gen.js';\r\n\r\nexport * from './types.gen.js';\r\n\r\nexport const SCHEMA_VERSION = '1.0';\r\nexport const SCHEMA_MAJOR = 1;\r\n\r\nexport const canonicalRecordSchema: Record<string, unknown> = schemaJson as Record<string, unknown>;\r\n\r\nfunction aliasFilePath(): string {\r\n if (process.env.XTIVIX_MODEL_ALIASES) return process.env.XTIVIX_MODEL_ALIASES;\r\n const here = path.dirname(fileURLToPath(import.meta.url));\r\n return path.join(here, '..', 'model-aliases.yaml');\r\n}\r\n\r\nconst Ajv = (AjvModule as unknown as { default: typeof AjvModule }).default ?? AjvModule;\r\nconst ajv = new Ajv({ allErrors: true, strict: false });\r\nconst validateFn: ValidateFunction = ajv.compile(canonicalRecordSchema);\r\n\r\nexport interface SchemaValidationResult {\r\n valid: boolean;\r\n errors: string[];\r\n}\r\n\r\nfunction formatErrors(errors: ErrorObject[] | null | undefined): string[] {\r\n return (errors ?? []).map((e) => `${e.instancePath || '/'} ${e.message ?? 'invalid'}`.trim());\r\n}\r\n\r\nexport function validateRecord(record: unknown): SchemaValidationResult {\r\n const valid = validateFn(record) as boolean;\r\n return { valid, errors: valid ? [] : formatErrors(validateFn.errors) };\r\n}\r\n\r\n// ---------------------------------------------------------------------------\r\n// Metadata rules (REC-5)\r\n// ---------------------------------------------------------------------------\r\n\r\nexport const METADATA_KEY_RE = /^[a-z0-9_.-]{1,64}$/;\r\nexport const METADATA_MAX_KEYS = 50;\r\nexport const CONVENTIONAL_METADATA_KEYS = ['user_id', 'session_id', 'trace_id'] as const;\r\n\r\nexport interface MetadataNormalization {\r\n metadata: Record<string, MetadataValue>;\r\n rejected: Record<string, unknown>;\r\n}\r\n\r\n/**\r\n * Applies REC-5: keys must match the pattern, values must be scalars, at most 50 keys.\r\n * Anything that breaks the rules is returned in `rejected` so the caller can keep it in\r\n * `provider_metadata.rejected_metadata` rather than dropping it.\r\n */\r\nexport function normalizeMetadata(input: Record<string, unknown> | undefined | null): MetadataNormalization {\r\n const metadata: Record<string, MetadataValue> = {};\r\n const rejected: Record<string, unknown> = {};\r\n if (!input) return { metadata, rejected };\r\n // Conventional keys first so they survive the key cap.\r\n const entries = Object.entries(input).sort(([a], [b]) => {\r\n const ia = (CONVENTIONAL_METADATA_KEYS as readonly string[]).indexOf(a);\r\n const ib = (CONVENTIONAL_METADATA_KEYS as readonly string[]).indexOf(b);\r\n return (ia === -1 ? 99 : ia) - (ib === -1 ? 99 : ib);\r\n });\r\n for (const [key, value] of entries) {\r\n const scalar =\r\n typeof value === 'string' ||\r\n typeof value === 'boolean' ||\r\n (typeof value === 'number' && Number.isFinite(value));\r\n if (!METADATA_KEY_RE.test(key) || !scalar || Object.keys(metadata).length >= METADATA_MAX_KEYS) {\r\n rejected[key] = value;\r\n continue;\r\n }\r\n metadata[key] = value as MetadataValue;\r\n }\r\n return { metadata, rejected };\r\n}\r\n\r\n/** Flattens nested JSON into dotted keys, e.g. `{a:{b:1}}` -> `{\"a.b\":1}`. Arrays are JSON-encoded. */\r\nexport function flattenMetadata(input: unknown, prefix = '', out: Record<string, unknown> = {}): Record<string, unknown> {\r\n if (input === null || input === undefined) return out;\r\n if (typeof input !== 'object' || Array.isArray(input)) {\r\n if (prefix) out[prefix] = Array.isArray(input) ? JSON.stringify(input) : input;\r\n return out;\r\n }\r\n for (const [k, v] of Object.entries(input as Record<string, unknown>)) {\r\n const key = prefix ? `${prefix}.${k}` : k;\r\n if (v && typeof v === 'object' && !Array.isArray(v)) flattenMetadata(v, key, out);\r\n else if (Array.isArray(v)) out[key] = JSON.stringify(v);\r\n else out[key] = v;\r\n }\r\n return out;\r\n}\r\n\r\n// ---------------------------------------------------------------------------\r\n// Model aliases (EXT-2)\r\n// ---------------------------------------------------------------------------\r\n\r\ninterface AliasRule {\r\n match: string;\r\n replace: string;\r\n reapply?: boolean;\r\n}\r\n\r\ninterface AliasFile {\r\n version: number;\r\n exact: Record<string, string>;\r\n rules: AliasRule[];\r\n}\r\n\r\nlet aliasCache: { exact: Map<string, string>; rules: Array<{ re: RegExp; replace: string; reapply: boolean }> } | undefined;\r\n\r\nfunction loadAliases() {\r\n if (aliasCache) return aliasCache;\r\n const file = parseYaml(readFileSync(aliasFilePath(), 'utf8')) as AliasFile;\r\n aliasCache = {\r\n exact: new Map(Object.entries(file.exact ?? {}).map(([k, v]) => [k.toLowerCase(), v])),\r\n rules: (file.rules ?? []).map((r) => ({ re: new RegExp(r.match, 'i'), replace: r.replace, reapply: !!r.reapply })),\r\n };\r\n return aliasCache;\r\n}\r\n\r\n/** Best-effort canonical alias for a raw provider model name. Returns undefined when unknown. */\r\nexport function resolveModelAlias(raw: string | undefined | null): string | undefined {\r\n if (!raw) return undefined;\r\n const { exact, rules } = loadAliases();\r\n let current = raw.trim().toLowerCase();\r\n for (let depth = 0; depth < 4; depth++) {\r\n const hit = exact.get(current);\r\n if (hit) return hit;\r\n let rewritten: string | undefined;\r\n let reapply = false;\r\n for (const rule of rules) {\r\n if (rule.re.test(current)) {\r\n rewritten = current.replace(rule.re, rule.replace);\r\n reapply = rule.reapply;\r\n break;\r\n }\r\n }\r\n if (rewritten === undefined) return undefined;\r\n if (!reapply) return exact.get(rewritten) ?? rewritten;\r\n current = rewritten;\r\n }\r\n return undefined;\r\n}\r\n\r\nexport type { CanonicalRecord };\r\n","{\r\n \"$schema\": \"http://json-schema.org/draft-07/schema#\",\r\n \"$id\": \"https://xtivix.dev/schemas/canonical-record.v1.json\",\r\n \"title\": \"CanonicalRecord\",\r\n \"description\": \"One provider-side AI call, normalized. Schema version 1.x. Changes within 1.x are additive; readers ignore unknown fields.\",\r\n \"type\": \"object\",\r\n \"required\": [\"schema_version\", \"id\", \"source\", \"provider_record_id\", \"started_at\", \"operation\", \"model\", \"status\", \"metadata\", \"content_state\"],\r\n \"additionalProperties\": true,\r\n \"definitions\": {\r\n \"Timestamp\": {\r\n \"type\": \"string\",\r\n \"pattern\": \"^\\\\d{4}-\\\\d{2}-\\\\d{2}T\\\\d{2}:\\\\d{2}:\\\\d{2}(\\\\.\\\\d{1,9})?Z$\"\r\n },\r\n \"Part\": {\r\n \"title\": \"Part\",\r\n \"type\": \"object\",\r\n \"required\": [\"type\"],\r\n \"additionalProperties\": true,\r\n \"properties\": {\r\n \"type\": { \"type\": \"string\", \"enum\": [\"text\", \"tool_call\", \"tool_result\", \"media_ref\"] },\r\n \"text\": { \"type\": \"string\" },\r\n \"tool_call_id\": { \"type\": \"string\" },\r\n \"name\": { \"type\": \"string\" },\r\n \"arguments\": { \"type\": \"string\", \"description\": \"Tool call arguments as a JSON string\" },\r\n \"content\": { \"type\": \"string\", \"description\": \"Tool result content\" },\r\n \"is_error\": { \"type\": \"boolean\" },\r\n \"media_type\": { \"type\": \"string\", \"enum\": [\"image\", \"audio\", \"video\", \"file\", \"other\"] },\r\n \"mime_type\": { \"type\": \"string\" },\r\n \"size_bytes\": { \"type\": \"number\" },\r\n \"uri\": { \"type\": \"string\" }\r\n }\r\n },\r\n \"Message\": {\r\n \"title\": \"Message\",\r\n \"type\": \"object\",\r\n \"required\": [\"role\", \"parts\"],\r\n \"additionalProperties\": true,\r\n \"properties\": {\r\n \"role\": { \"type\": \"string\", \"enum\": [\"system\", \"user\", \"assistant\", \"tool\"] },\r\n \"name\": { \"type\": \"string\" },\r\n \"parts\": { \"type\": \"array\", \"items\": { \"$ref\": \"#/definitions/Part\" } }\r\n }\r\n },\r\n \"MetadataValue\": {\r\n \"type\": [\"string\", \"number\", \"boolean\"]\r\n }\r\n },\r\n \"properties\": {\r\n \"schema_version\": { \"type\": \"string\", \"pattern\": \"^1\\\\.\\\\d+$\" },\r\n \"id\": { \"type\": \"string\", \"minLength\": 3, \"maxLength\": 512 },\r\n \"tenant_id\": { \"type\": \"string\", \"description\": \"Reserved for multi-tenancy (SEC-12). Unused in v1.\" },\r\n \"source\": {\r\n \"title\": \"RecordSource\",\r\n \"type\": \"object\",\r\n \"required\": [\"id\", \"provider\"],\r\n \"additionalProperties\": true,\r\n \"properties\": {\r\n \"id\": { \"type\": \"string\", \"pattern\": \"^[a-z0-9_-]{1,64}$\" },\r\n \"provider\": { \"type\": \"string\", \"enum\": [\"openrouter\", \"cloudflare_ai_gateway\", \"aws_bedrock\", \"custom\"] },\r\n \"scope\": { \"type\": \"string\" }\r\n }\r\n },\r\n \"provider_record_id\": { \"type\": \"string\", \"minLength\": 1, \"maxLength\": 440 },\r\n \"started_at\": { \"$ref\": \"#/definitions/Timestamp\" },\r\n \"ended_at\": { \"$ref\": \"#/definitions/Timestamp\" },\r\n \"received_at\": { \"$ref\": \"#/definitions/Timestamp\" },\r\n \"ingested_at\": { \"$ref\": \"#/definitions/Timestamp\" },\r\n \"operation\": { \"type\": \"string\", \"enum\": [\"chat\", \"completion\", \"embedding\", \"image\", \"other\"] },\r\n \"model\": {\r\n \"title\": \"RecordModel\",\r\n \"type\": \"object\",\r\n \"required\": [\"raw\"],\r\n \"additionalProperties\": true,\r\n \"properties\": {\r\n \"raw\": { \"type\": \"string\" },\r\n \"canonical\": { \"type\": \"string\" },\r\n \"upstream_provider\": { \"type\": \"string\" }\r\n }\r\n },\r\n \"request\": {\r\n \"title\": \"RecordRequest\",\r\n \"type\": \"object\",\r\n \"additionalProperties\": true,\r\n \"properties\": {\r\n \"messages\": { \"type\": \"array\", \"items\": { \"$ref\": \"#/definitions/Message\" } },\r\n \"params\": {\r\n \"title\": \"RequestParams\",\r\n \"type\": \"object\",\r\n \"additionalProperties\": true,\r\n \"properties\": {\r\n \"temperature\": { \"type\": \"number\" },\r\n \"top_p\": { \"type\": \"number\" },\r\n \"max_tokens\": { \"type\": \"number\" },\r\n \"stream\": { \"type\": \"boolean\" },\r\n \"tools\": { \"type\": \"array\" },\r\n \"extra\": { \"type\": \"object\" }\r\n }\r\n }\r\n }\r\n },\r\n \"response\": {\r\n \"title\": \"RecordResponse\",\r\n \"type\": \"object\",\r\n \"additionalProperties\": true,\r\n \"properties\": {\r\n \"messages\": { \"type\": \"array\", \"items\": { \"$ref\": \"#/definitions/Message\" } },\r\n \"finish_reason\": { \"type\": \"string\" }\r\n }\r\n },\r\n \"status\": { \"type\": \"string\", \"enum\": [\"ok\", \"error\", \"cached\", \"filtered\"] },\r\n \"error\": {\r\n \"title\": \"RecordError\",\r\n \"type\": \"object\",\r\n \"additionalProperties\": true,\r\n \"properties\": {\r\n \"code\": { \"type\": \"string\" },\r\n \"message\": { \"type\": \"string\" },\r\n \"http_status\": { \"type\": \"number\" }\r\n }\r\n },\r\n \"usage\": {\r\n \"title\": \"RecordUsage\",\r\n \"type\": \"object\",\r\n \"additionalProperties\": true,\r\n \"properties\": {\r\n \"input_tokens\": { \"type\": \"number\" },\r\n \"output_tokens\": { \"type\": \"number\" },\r\n \"total_tokens\": { \"type\": \"number\" },\r\n \"cached_tokens\": { \"type\": \"number\" },\r\n \"reasoning_tokens\": { \"type\": \"number\" }\r\n }\r\n },\r\n \"cost\": {\r\n \"title\": \"RecordCost\",\r\n \"type\": \"object\",\r\n \"required\": [\"source\"],\r\n \"additionalProperties\": true,\r\n \"properties\": {\r\n \"amount\": { \"type\": \"number\" },\r\n \"currency\": { \"type\": \"string\" },\r\n \"source\": { \"type\": \"string\", \"enum\": [\"provider\", \"none\"] }\r\n }\r\n },\r\n \"latency_ms\": { \"type\": \"number\" },\r\n \"ttft_ms\": { \"type\": \"number\" },\r\n \"metadata\": {\r\n \"type\": \"object\",\r\n \"maxProperties\": 50,\r\n \"propertyNames\": { \"pattern\": \"^[a-z0-9_.-]{1,64}$\" },\r\n \"additionalProperties\": { \"$ref\": \"#/definitions/MetadataValue\" }\r\n },\r\n \"tags\": { \"type\": \"array\", \"items\": { \"type\": \"string\" } },\r\n \"provider_metadata\": { \"type\": \"object\", \"additionalProperties\": true },\r\n \"content_state\": { \"type\": \"string\", \"enum\": [\"full\", \"redacted\", \"truncated\", \"absent\"] },\r\n \"content_omitted_reason\": {\r\n \"type\": \"string\",\r\n \"enum\": [\"store_content_off\", \"provider\", \"retention\", \"scope\", \"not_requested\", \"body_unavailable\"]\r\n },\r\n \"redaction\": {\r\n \"title\": \"RecordRedaction\",\r\n \"type\": \"object\",\r\n \"additionalProperties\": true,\r\n \"properties\": {\r\n \"policy\": { \"type\": \"string\" },\r\n \"version\": { \"type\": \"number\" }\r\n }\r\n },\r\n \"raw\": { \"description\": \"Original provider payload, kept only when enabled for the source\" }\r\n }\r\n}\r\n"],"mappings":";AAAA,SAAS,oBAAoB;AAC7B,SAAS,qBAAqB;AAC9B,OAAO,UAAU;AACjB,OAAO,eAA4D;AACnE,SAAS,SAAS,iBAAiB;;;ACJnC;AAAA,EACE,SAAW;AAAA,EACX,KAAO;AAAA,EACP,OAAS;AAAA,EACT,aAAe;AAA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;AACT;AAGO,SAAS,kBAAkB,KAAoD;AACpF,MAAI,CAAC,IAAK,QAAO;AACjB,QAAM,EAAE,OAAO,MAAM,IAAI,YAAY;AACrC,MAAI,UAAU,IAAI,KAAK,EAAE,YAAY;AACrC,WAAS,QAAQ,GAAG,QAAQ,GAAG,SAAS;AACtC,UAAM,MAAM,MAAM,IAAI,OAAO;AAC7B,QAAI,IAAK,QAAO;AAChB,QAAI;AACJ,QAAI,UAAU;AACd,eAAW,QAAQ,OAAO;AACxB,UAAI,KAAK,GAAG,KAAK,OAAO,GAAG;AACzB,oBAAY,QAAQ,QAAQ,KAAK,IAAI,KAAK,OAAO;AACjD,kBAAU,KAAK;AACf;AAAA,MACF;AAAA,IACF;AACA,QAAI,cAAc,OAAW,QAAO;AACpC,QAAI,CAAC,QAAS,QAAO,MAAM,IAAI,SAAS,KAAK;AAC7C,cAAU;AAAA,EACZ;AACA,SAAO;AACT;","names":[]}
@@ -0,0 +1,33 @@
1
+ # Maps provider model names to canonical aliases (EXT-2).
2
+ # Adding an alias is a data change; no code change needed.
3
+ #
4
+ # exact: raw model string (lowercased) -> canonical alias
5
+ # rules: ordered regex rewrites applied when no exact match exists; first match wins.
6
+ # `replace` may reference capture groups as $1, $2, ...
7
+ version: 1
8
+ exact:
9
+ anthropic.claude-sonnet-4-5-20250929-v1:0: anthropic/claude-sonnet-4.5
10
+ us.anthropic.claude-sonnet-4-5-20250929-v1:0: anthropic/claude-sonnet-4.5
11
+ eu.anthropic.claude-sonnet-4-5-20250929-v1:0: anthropic/claude-sonnet-4.5
12
+ anthropic.claude-3-5-sonnet-20241022-v2:0: anthropic/claude-3.5-sonnet
13
+ anthropic.claude-3-haiku-20240307-v1:0: anthropic/claude-3-haiku
14
+ amazon.nova-pro-v1:0: amazon/nova-pro
15
+ amazon.nova-lite-v1:0: amazon/nova-lite
16
+ amazon.titan-embed-text-v2:0: amazon/titan-embed-text-v2
17
+ meta.llama3-1-70b-instruct-v1:0: meta-llama/llama-3.1-70b-instruct
18
+ claude-sonnet-4-5: anthropic/claude-sonnet-4.5
19
+ claude-sonnet-4-5-20250929: anthropic/claude-sonnet-4.5
20
+ gpt-4o: openai/gpt-4o
21
+ gpt-4o-mini: openai/gpt-4o-mini
22
+ "@cf/meta/llama-3.1-8b-instruct": meta-llama/llama-3.1-8b-instruct
23
+ rules:
24
+ # Bedrock cross-region inference profile prefixes: us./eu./apac./global.
25
+ - match: '^(?:us|eu|apac|global)\.(.+)$'
26
+ replace: '$1'
27
+ reapply: true
28
+ # Bedrock "<vendor>.<model>-vN:M" -> "<vendor>/<model>"
29
+ - match: '^(anthropic|amazon|meta|mistral|cohere|ai21|deepseek)\.([a-z0-9.-]+?)(?:-\d{8})?(?:-v\d+)?(?::\d+)?$'
30
+ replace: '$1/$2'
31
+ # Already "<vendor>/<model>" (OpenRouter style): keep as is, dropping ":variant" suffixes like ":free"
32
+ - match: '^([a-z0-9-]+/[a-z0-9._-]+)(?::[a-z]+)?$'
33
+ replace: '$1'
package/package.json ADDED
@@ -0,0 +1,49 @@
1
+ {
2
+ "name": "@x12i/xtivix-schema",
3
+ "version": "0.1.0",
4
+ "description": "Canonical AI call record schema (JSON Schema v1.0), TypeScript types, metadata rules and model aliases for xtivix",
5
+ "keywords": [
6
+ "xtivix",
7
+ "llm",
8
+ "observability",
9
+ "schema",
10
+ "json-schema",
11
+ "genai"
12
+ ],
13
+ "license": "MIT",
14
+ "author": "x12",
15
+ "engines": {
16
+ "node": ">=22"
17
+ },
18
+ "publishConfig": {
19
+ "access": "public"
20
+ },
21
+ "type": "module",
22
+ "main": "./dist/index.js",
23
+ "types": "./dist/index.d.ts",
24
+ "exports": {
25
+ ".": {
26
+ "xtivix-source": "./src/index.ts",
27
+ "types": "./dist/index.d.ts",
28
+ "import": "./dist/index.js"
29
+ },
30
+ "./canonical-record.v1.json": "./canonical-record.v1.json",
31
+ "./model-aliases.yaml": "./model-aliases.yaml",
32
+ "./package.json": "./package.json"
33
+ },
34
+ "files": [
35
+ "dist",
36
+ "canonical-record.v1.json",
37
+ "model-aliases.yaml",
38
+ "README.md"
39
+ ],
40
+ "sideEffects": false,
41
+ "scripts": {
42
+ "build": "tsup"
43
+ },
44
+ "dependencies": {
45
+ "ajv": "^8.20.0",
46
+ "ajv-formats": "^3.0.1",
47
+ "yaml": "^2.9.1"
48
+ }
49
+ }