chasm-cli 2.0.0

Universal chat session manager - harvest, merge, and analyze AI chat history from VS Code, Cursor, and other editors
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
// Copyright (c) 2024-2026 Nervosys LLC
// SPDX-License-Identifier: AGPL-3.0-only
//! Ontology layer for AI agent discoverability.
//!
//! Provides a structured, machine-readable description of:
//! - Entity types across all providers (session, message, tool_call, etc.)
//! - Semantic tags that map provider-specific field names to universal concepts
//! - Cross-provider field mappings (field A in provider X ≡ field B in provider Y)
//! - Migration paths between schema versions
//! - Relationship graphs between entities
//!
//! AI agents can query the ontology to understand what data exists,
//! where it lives, how to access it, and how to translate between providers.

use crate::schema::types::*;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

// ============================================================================
// Semantic Tags
// ============================================================================

/// A semantic tag represents a universal concept that may appear under
/// different field names in different providers.
///
/// Example: The concept "session creation time" is stored as:
///   - `creationDate` in Copilot Chat
///   - `dateCreated` in Continue.dev
///   - `timestamp` (of first message) in Claude Code
///
/// The semantic tag `created_at` unifies all of these.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SemanticTag {
    /// Canonical tag name (e.g., "created_at", "session_id", "message_text")
    pub tag: String,
    /// Human-readable description
    pub description: String,
    /// The universal data type for this concept
    pub canonical_type: DataType,
    /// Entity type this tag belongs to
    pub entity: EntityType,
    /// Related tags (e.g., "created_at" is related to "updated_at")
    #[serde(default)]
    pub related_tags: Vec<String>,
}

// ============================================================================
// Entity Types
// ============================================================================

/// High-level entity types that appear across providers
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum EntityType {
    /// A chat session / conversation
    Session,
    /// A single message (user or assistant turn)
    Message,
    /// A request-response pair (Copilot Chat's "request" unit)
    RequestResponse,
    /// An AI model
    Model,
    /// A tool/function call
    ToolCall,
    /// An AI agent
    Agent,
    /// A workspace / project context
    Workspace,
    /// Session index / registry metadata
    SessionIndex,
    /// Cache / UI state metadata
    UiState,
}

impl std::fmt::Display for EntityType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Session => write!(f, "session"),
            Self::Message => write!(f, "message"),
            Self::RequestResponse => write!(f, "request_response"),
            Self::Model => write!(f, "model"),
            Self::ToolCall => write!(f, "tool_call"),
            Self::Agent => write!(f, "agent"),
            Self::Workspace => write!(f, "workspace"),
            Self::SessionIndex => write!(f, "session_index"),
            Self::UiState => write!(f, "ui_state"),
        }
    }
}

// ============================================================================
// Entity Relationships
// ============================================================================

/// A relationship between two entity types
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EntityRelationship {
    /// Source entity type
    pub from: EntityType,
    /// Target entity type
    pub to: EntityType,
    /// Relationship kind
    pub kind: RelationshipKind,
    /// Description of the relationship
    pub description: String,
}

/// Kinds of relationships between entities
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum RelationshipKind {
    /// One-to-many containment (session contains messages)
    Contains,
    /// Many-to-one reference (message belongs to session)
    BelongsTo,
    /// Many-to-many association (session uses models)
    References,
    /// One-to-one equivalence (session <-> index entry)
    MapsTo,
}

// ============================================================================
// Cross-Provider Mapping
// ============================================================================

/// A mapping between fields in two different provider schemas.
///
/// Used to translate data between providers (e.g., migrating sessions
/// from Copilot to Cursor, or merging sessions from multiple providers).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CrossProviderMapping {
    /// Source schema version ID
    pub source_schema: String,
    /// Source field path (dot-separated, e.g., "session.creationDate")
    pub source_field: String,
    /// Target schema version ID
    pub target_schema: String,
    /// Target field path
    pub target_field: String,
    /// Transformation required (if any)
    pub transform: Option<FieldTransform>,
    /// Confidence level (0.0 – 1.0)
    pub confidence: f64,
    /// Semantic tag that links these fields
    pub semantic_tag: String,
}

/// Transformation to apply when mapping a field between providers
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum FieldTransform {
    /// Direct copy (no transformation needed)
    #[serde(rename = "identity")]
    Identity,
    /// Rename only (same value, different key)
    #[serde(rename = "rename")]
    Rename,
    /// Type conversion (e.g., epoch ms → ISO 8601)
    #[serde(rename = "type_convert")]
    TypeConvert { from_type: String, to_type: String },
    /// Value mapping (e.g., "model" → "assistant" for Gemini roles)
    #[serde(rename = "value_map")]
    ValueMap { mapping: HashMap<String, String> },
    /// Structural transformation (e.g., flatten nested object)
    #[serde(rename = "restructure")]
    Restructure { description: String },
    /// Custom transformation requiring code
    #[serde(rename = "custom")]
    Custom { description: String },
}

// ============================================================================
// Migration Path
// ============================================================================

/// Describes how to migrate data from one schema version to another.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MigrationPath {
    /// Source schema version ID
    pub from_schema: String,
    /// Target schema version ID
    pub to_schema: String,
    /// Whether the migration is lossless
    pub lossless: bool,
    /// Ordered list of field mappings to apply
    pub mappings: Vec<CrossProviderMapping>,
    /// Fields that will be lost in migration
    #[serde(default)]
    pub data_loss: Vec<String>,
    /// Fields that will be added (with defaults)
    #[serde(default)]
    pub new_fields: Vec<String>,
    /// Human-readable migration notes
    #[serde(default)]
    pub notes: Vec<String>,
}

// ============================================================================
// Ontology (aggregate)
// ============================================================================

/// The complete ontology over all provider schemas.
///
/// AI agents can query this to:
/// 1. Discover what data is available across providers
/// 2. Understand field semantics and relationships
/// 3. Find equivalent fields across providers
/// 4. Plan data migrations and merges
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Ontology {
    /// Version of the ontology specification
    pub version: String,
    /// All known semantic tags (universal field concepts)
    pub semantic_tags: Vec<SemanticTag>,
    /// Entity type relationships
    pub relationships: Vec<EntityRelationship>,
    /// Cross-provider field mappings
    pub mappings: Vec<CrossProviderMapping>,
    /// Known migration paths between schema versions
    pub migration_paths: Vec<MigrationPath>,
    /// Provider capability matrix (provider → set of capabilities)
    pub capabilities: HashMap<String, Vec<String>>,
}

impl Ontology {
    /// Build the default ontology from all known schemas
    pub fn build() -> Self {
        Self {
            version: "1.0.0".into(),
            semantic_tags: build_semantic_tags(),
            relationships: build_relationships(),
            mappings: build_cross_provider_mappings(),
            migration_paths: build_migration_paths(),
            capabilities: build_capability_matrix(),
        }
    }

    /// Find all mappings between two schemas
    pub fn cross_provider_mappings(
        &self,
        source_schema: &str,
        target_schema: &str,
    ) -> Vec<&CrossProviderMapping> {
        self.mappings
            .iter()
            .filter(|m| m.source_schema == source_schema && m.target_schema == target_schema)
            .collect()
    }

    /// Find all fields matching a semantic tag across all schemas
    pub fn find_by_semantic_tag(&self, tag: &str) -> Vec<&CrossProviderMapping> {
        self.mappings
            .iter()
            .filter(|m| m.semantic_tag == tag)
            .collect()
    }

    /// Get migration path between two schema versions
    pub fn migration_path(&self, from: &str, to: &str) -> Option<&MigrationPath> {
        self.migration_paths
            .iter()
            .find(|p| p.from_schema == from && p.to_schema == to)
    }

    /// Get capabilities for a specific provider
    pub fn provider_capabilities(&self, provider: &str) -> Option<&Vec<String>> {
        self.capabilities.get(provider)
    }

    /// Get all entity types used in the ontology
    pub fn entity_types(&self) -> Vec<&EntityType> {
        let mut types: Vec<&EntityType> = self
            .semantic_tags
            .iter()
            .map(|t| &t.entity)
            .collect::<std::collections::HashSet<_>>()
            .into_iter()
            .collect();
        types.sort_by_key(|e| format!("{}", e));
        types
    }
}

// ============================================================================
// Default Ontology Builders
// ============================================================================

fn build_semantic_tags() -> Vec<SemanticTag> {
    vec![
        // Session-level tags
        SemanticTag {
            tag: "session_id".into(),
            description: "Unique identifier for a chat session".into(),
            canonical_type: DataType::Uuid,
            entity: EntityType::Session,
            related_tags: vec![],
        },
        SemanticTag {
            tag: "title".into(),
            description: "Human-readable session title".into(),
            canonical_type: DataType::String,
            entity: EntityType::Session,
            related_tags: vec![],
        },
        SemanticTag {
            tag: "created_at".into(),
            description: "When the session was created (timestamp)".into(),
            canonical_type: DataType::Timestamp,
            entity: EntityType::Session,
            related_tags: vec!["updated_at".into()],
        },
        SemanticTag {
            tag: "updated_at".into(),
            description: "When the session was last modified (timestamp)".into(),
            canonical_type: DataType::Timestamp,
            entity: EntityType::Session,
            related_tags: vec!["created_at".into()],
        },
        SemanticTag {
            tag: "is_imported".into(),
            description: "Whether the session was imported from another source".into(),
            canonical_type: DataType::Boolean,
            entity: EntityType::Session,
            related_tags: vec![],
        },
        SemanticTag {
            tag: "session_location".into(),
            description: "Where in the IDE the session was initiated (panel, terminal, etc.)"
                .into(),
            canonical_type: DataType::String,
            entity: EntityType::Session,
            related_tags: vec![],
        },
        // Message-level tags
        SemanticTag {
            tag: "message_role".into(),
            description: "The role of a message sender (user, assistant, system, tool)".into(),
            canonical_type: DataType::Enum(vec![
                "user".into(),
                "assistant".into(),
                "system".into(),
                "tool".into(),
            ]),
            entity: EntityType::Message,
            related_tags: vec!["message_text".into()],
        },
        SemanticTag {
            tag: "message_text".into(),
            description: "The text content of a message".into(),
            canonical_type: DataType::String,
            entity: EntityType::Message,
            related_tags: vec!["message_role".into(), "message_parts".into()],
        },
        SemanticTag {
            tag: "message_timestamp".into(),
            description: "When a message was sent/received".into(),
            canonical_type: DataType::Timestamp,
            entity: EntityType::Message,
            related_tags: vec![],
        },
        SemanticTag {
            tag: "message_parts".into(),
            description: "Multi-part message content (multimodal: text, images, code)".into(),
            canonical_type: DataType::Array(Box::new(DataType::Json)),
            entity: EntityType::Message,
            related_tags: vec!["message_text".into()],
        },
        SemanticTag {
            tag: "user_message".into(),
            description: "The user's input message in a request-response pair".into(),
            canonical_type: DataType::Json,
            entity: EntityType::RequestResponse,
            related_tags: vec!["assistant_response".into()],
        },
        SemanticTag {
            tag: "assistant_response".into(),
            description: "The AI's response in a request-response pair".into(),
            canonical_type: DataType::Json,
            entity: EntityType::RequestResponse,
            related_tags: vec!["user_message".into()],
        },
        // Model-level tags
        SemanticTag {
            tag: "model_id".into(),
            description: "Identifier of the AI model used (e.g., 'gpt-4o', 'claude-3.5-sonnet')"
                .into(),
            canonical_type: DataType::String,
            entity: EntityType::Model,
            related_tags: vec![],
        },
        // Agent/tool tags
        SemanticTag {
            tag: "agent".into(),
            description: "AI agent metadata (for agentic sessions)".into(),
            canonical_type: DataType::Json,
            entity: EntityType::Agent,
            related_tags: vec!["tools".into()],
        },
        SemanticTag {
            tag: "tools".into(),
            description: "Available tools/functions for the session".into(),
            canonical_type: DataType::Array(Box::new(DataType::Json)),
            entity: EntityType::ToolCall,
            related_tags: vec!["tool_calls".into()],
        },
        SemanticTag {
            tag: "tool_calls".into(),
            description: "Tool/function invocations made by the assistant".into(),
            canonical_type: DataType::Array(Box::new(DataType::Json)),
            entity: EntityType::ToolCall,
            related_tags: vec!["tools".into()],
        },
        // Context tags
        SemanticTag {
            tag: "context".into(),
            description: "Context data provided to the model (files, selections, terminal)".into(),
            canonical_type: DataType::Json,
            entity: EntityType::RequestResponse,
            related_tags: vec![],
        },
        // State tags
        SemanticTag {
            tag: "response_state".into(),
            description: "State of the response: Pending, Complete, Cancelled, Failed, NeedsInput"
                .into(),
            canonical_type: DataType::Enum(vec![
                "pending".into(),
                "complete".into(),
                "cancelled".into(),
                "failed".into(),
                "needs_input".into(),
            ]),
            entity: EntityType::RequestResponse,
            related_tags: vec!["is_canceled".into()],
        },
        SemanticTag {
            tag: "is_canceled".into(),
            description: "Whether a request was canceled by the user".into(),
            canonical_type: DataType::Boolean,
            entity: EntityType::RequestResponse,
            related_tags: vec!["response_state".into()],
        },
        // Cost/performance tags
        SemanticTag {
            tag: "cost".into(),
            description: "Monetary cost of the request/response".into(),
            canonical_type: DataType::Float,
            entity: EntityType::RequestResponse,
            related_tags: vec!["latency".into()],
        },
        SemanticTag {
            tag: "latency".into(),
            description: "Time taken for the request to complete (milliseconds)".into(),
            canonical_type: DataType::Integer,
            entity: EntityType::RequestResponse,
            related_tags: vec!["cost".into()],
        },
        // Schema metadata
        SemanticTag {
            tag: "schema_version".into(),
            description: "Version number of the session format schema".into(),
            canonical_type: DataType::Integer,
            entity: EntityType::Session,
            related_tags: vec![],
        },
        SemanticTag {
            tag: "event_type".into(),
            description: "Type of event in event-sourced formats (snapshot, update)".into(),
            canonical_type: DataType::String,
            entity: EntityType::Session,
            related_tags: vec!["event_data".into()],
        },
        SemanticTag {
            tag: "event_data".into(),
            description: "Payload of an event in event-sourced formats".into(),
            canonical_type: DataType::Json,
            entity: EntityType::Session,
            related_tags: vec!["event_type".into()],
        },
        // UI state tags
        SemanticTag {
            tag: "resource_uri".into(),
            description: "URI identifying a session resource in the IDE".into(),
            canonical_type: DataType::Uri,
            entity: EntityType::UiState,
            related_tags: vec![],
        },
        SemanticTag {
            tag: "is_empty".into(),
            description: "Whether a session has no messages/requests".into(),
            canonical_type: DataType::Boolean,
            entity: EntityType::SessionIndex,
            related_tags: vec![],
        },
        SemanticTag {
            tag: "timing".into(),
            description: "Session timing metadata (created, last request, last response)".into(),
            canonical_type: DataType::Object("Timing".into()),
            entity: EntityType::SessionIndex,
            related_tags: vec!["created_at".into(), "updated_at".into()],
        },
        SemanticTag {
            tag: "user_name".into(),
            description: "Display name of the human user".into(),
            canonical_type: DataType::String,
            entity: EntityType::Session,
            related_tags: vec!["assistant_name".into()],
        },
        SemanticTag {
            tag: "assistant_name".into(),
            description: "Display name of the AI assistant".into(),
            canonical_type: DataType::String,
            entity: EntityType::Session,
            related_tags: vec!["user_name".into()],
        },
        SemanticTag {
            tag: "workspace_id".into(),
            description: "Identifier of the workspace/project".into(),
            canonical_type: DataType::String,
            entity: EntityType::Workspace,
            related_tags: vec![],
        },
        SemanticTag {
            tag: "model_state".into(),
            description: "Model processing state (Pending/Complete/Cancelled)".into(),
            canonical_type: DataType::Object("ModelState".into()),
            entity: EntityType::RequestResponse,
            related_tags: vec!["response_state".into()],
        },
        SemanticTag {
            tag: "completed_at".into(),
            description: "Timestamp when model finished processing".into(),
            canonical_type: DataType::Timestamp,
            entity: EntityType::RequestResponse,
            related_tags: vec!["message_timestamp".into()],
        },
        SemanticTag {
            tag: "completion_state".into(),
            description: "Numeric completion state (0=Pending, 1=Complete, 2=Cancelled)".into(),
            canonical_type: DataType::Integer,
            entity: EntityType::RequestResponse,
            related_tags: vec!["response_state".into(), "model_state".into()],
        },
        SemanticTag {
            tag: "streaming".into(),
            description: "Whether the response should be streamed".into(),
            canonical_type: DataType::Boolean,
            entity: EntityType::RequestResponse,
            related_tags: vec![],
        },
        SemanticTag {
            tag: "temperature".into(),
            description: "Sampling temperature for model generation".into(),
            canonical_type: DataType::Float,
            entity: EntityType::RequestResponse,
            related_tags: vec![],
        },
        SemanticTag {
            tag: "request_id".into(),
            description: "Unique identifier for a request within a session".into(),
            canonical_type: DataType::Uuid,
            entity: EntityType::RequestResponse,
            related_tags: vec!["session_id".into()],
        },
        SemanticTag {
            tag: "session_index".into(),
            description: "Index/registry of all session IDs and their metadata".into(),
            canonical_type: DataType::Json,
            entity: EntityType::SessionIndex,
            related_tags: vec![],
        },
        SemanticTag {
            tag: "index_version".into(),
            description: "Version of the session index format".into(),
            canonical_type: DataType::Integer,
            entity: EntityType::SessionIndex,
            related_tags: vec!["schema_version".into()],
        },
        SemanticTag {
            tag: "last_read".into(),
            description: "Timestamp when a session was last read by the user".into(),
            canonical_type: DataType::Timestamp,
            entity: EntityType::UiState,
            related_tags: vec![],
        },
        SemanticTag {
            tag: "messages".into(),
            description: "Collection of messages/requests in a session".into(),
            canonical_type: DataType::Array(Box::new(DataType::Json)),
            entity: EntityType::Session,
            related_tags: vec!["message_text".into(), "message_role".into()],
        },
    ]
}

fn build_relationships() -> Vec<EntityRelationship> {
    vec![
        EntityRelationship {
            from: EntityType::Session,
            to: EntityType::RequestResponse,
            kind: RelationshipKind::Contains,
            description: "A session contains zero or more request-response pairs".into(),
        },
        EntityRelationship {
            from: EntityType::Session,
            to: EntityType::Message,
            kind: RelationshipKind::Contains,
            description: "A session contains an ordered sequence of messages".into(),
        },
        EntityRelationship {
            from: EntityType::RequestResponse,
            to: EntityType::Message,
            kind: RelationshipKind::Contains,
            description: "Each request-response pair contains a user message and assistant reply"
                .into(),
        },
        EntityRelationship {
            from: EntityType::RequestResponse,
            to: EntityType::ToolCall,
            kind: RelationshipKind::Contains,
            description: "A request may invoke zero or more tool calls".into(),
        },
        EntityRelationship {
            from: EntityType::Session,
            to: EntityType::Model,
            kind: RelationshipKind::References,
            description: "A session may use one or more AI models".into(),
        },
        EntityRelationship {
            from: EntityType::Session,
            to: EntityType::Agent,
            kind: RelationshipKind::References,
            description: "An agentic session references an AI agent identity".into(),
        },
        EntityRelationship {
            from: EntityType::Session,
            to: EntityType::Workspace,
            kind: RelationshipKind::BelongsTo,
            description: "A session belongs to a workspace/project".into(),
        },
        EntityRelationship {
            from: EntityType::Session,
            to: EntityType::SessionIndex,
            kind: RelationshipKind::MapsTo,
            description: "Each session has a corresponding index entry for UI display".into(),
        },
        EntityRelationship {
            from: EntityType::SessionIndex,
            to: EntityType::UiState,
            kind: RelationshipKind::MapsTo,
            description: "Index entries map to UI state (cache, read timestamps)".into(),
        },
        EntityRelationship {
            from: EntityType::Workspace,
            to: EntityType::Session,
            kind: RelationshipKind::Contains,
            description: "A workspace contains multiple sessions".into(),
        },
    ]
}

fn build_cross_provider_mappings() -> Vec<CrossProviderMapping> {
    let mut mappings = Vec::new();

    // Copilot JSON v3 → Copilot JSONL v1
    mappings.extend(copilot_json_to_jsonl_mappings());

    // Copilot JSON v3 → Cursor v1
    mappings.extend(copilot_to_cursor_mappings());

    // Copilot JSON v3 → OpenAI API v1
    mappings.extend(copilot_to_openai_mappings());

    // Copilot JSON v3 → Claude Code v1
    mappings.extend(copilot_to_claude_code_mappings());

    // Claude Code v1 → OpenAI API v1
    mappings.extend(claude_code_to_openai_mappings());

    mappings
}

// --- Copilot JSON v3 → Copilot JSONL v1 ---

fn copilot_json_to_jsonl_mappings() -> Vec<CrossProviderMapping> {
    vec![
        CrossProviderMapping {
            source_schema: "copilot-json-v3".into(),
            source_field: "session.version".into(),
            target_schema: "copilot-jsonl-v1".into(),
            target_field: "data.version".into(),
            transform: Some(FieldTransform::Restructure {
                description: "Wrapped inside kind:0 event envelope".into(),
            }),
            confidence: 1.0,
            semantic_tag: "schema_version".into(),
        },
        CrossProviderMapping {
            source_schema: "copilot-json-v3".into(),
            source_field: "session.sessionId".into(),
            target_schema: "copilot-jsonl-v1".into(),
            target_field: "data.sessionId".into(),
            transform: Some(FieldTransform::Identity),
            confidence: 1.0,
            semantic_tag: "session_id".into(),
        },
        CrossProviderMapping {
            source_schema: "copilot-json-v3".into(),
            source_field: "session.creationDate".into(),
            target_schema: "copilot-jsonl-v1".into(),
            target_field: "data.creationDate".into(),
            transform: Some(FieldTransform::Identity),
            confidence: 1.0,
            semantic_tag: "created_at".into(),
        },
        CrossProviderMapping {
            source_schema: "copilot-json-v3".into(),
            source_field: "session.requests".into(),
            target_schema: "copilot-jsonl-v1".into(),
            target_field: "data.requests".into(),
            transform: Some(FieldTransform::Restructure {
                description: "Response format changed from {value:[{value:text}]} to [{kind:\"\",value:text}]".into(),
            }),
            confidence: 0.9,
            semantic_tag: "messages".into(),
        },
    ]
}

// --- Copilot JSON v3 → Cursor v1 ---

fn copilot_to_cursor_mappings() -> Vec<CrossProviderMapping> {
    vec![
        CrossProviderMapping {
            source_schema: "copilot-json-v3".into(),
            source_field: "session.sessionId".into(),
            target_schema: "cursor-json-v1".into(),
            target_field: "session.sessionId".into(),
            transform: Some(FieldTransform::Identity),
            confidence: 1.0,
            semantic_tag: "session_id".into(),
        },
        CrossProviderMapping {
            source_schema: "copilot-json-v3".into(),
            source_field: "session.creationDate".into(),
            target_schema: "cursor-json-v1".into(),
            target_field: "session.creationDate".into(),
            transform: Some(FieldTransform::Identity),
            confidence: 1.0,
            semantic_tag: "created_at".into(),
        },
        CrossProviderMapping {
            source_schema: "copilot-json-v3".into(),
            source_field: "session.requests".into(),
            target_schema: "cursor-json-v1".into(),
            target_field: "session.requests".into(),
            transform: Some(FieldTransform::Identity),
            confidence: 0.95,
            semantic_tag: "messages".into(),
        },
    ]
}

// --- Copilot JSON v3 → OpenAI API v1 ---

fn copilot_to_openai_mappings() -> Vec<CrossProviderMapping> {
    vec![
        CrossProviderMapping {
            source_schema: "copilot-json-v3".into(),
            source_field: "request.message.text".into(),
            target_schema: "openai-api-openai-api-v1".into(),
            target_field: "messages[].content".into(),
            transform: Some(FieldTransform::Restructure {
                description: "Extract text from ChatMessage and set role='user'".into(),
            }),
            confidence: 0.9,
            semantic_tag: "message_text".into(),
        },
        CrossProviderMapping {
            source_schema: "copilot-json-v3".into(),
            source_field: "request.response".into(),
            target_schema: "openai-api-openai-api-v1".into(),
            target_field: "messages[].content".into(),
            transform: Some(FieldTransform::Custom {
                description: "Extract text from response value array and set role='assistant'"
                    .into(),
            }),
            confidence: 0.85,
            semantic_tag: "assistant_response".into(),
        },
        CrossProviderMapping {
            source_schema: "copilot-json-v3".into(),
            source_field: "request.modelId".into(),
            target_schema: "openai-api-openai-api-v1".into(),
            target_field: "model".into(),
            transform: Some(FieldTransform::Identity),
            confidence: 0.95,
            semantic_tag: "model_id".into(),
        },
    ]
}

// --- Copilot JSON v3 → Claude Code v1 ---

fn copilot_to_claude_code_mappings() -> Vec<CrossProviderMapping> {
    vec![
        CrossProviderMapping {
            source_schema: "copilot-json-v3".into(),
            source_field: "request.message.text".into(),
            target_schema: "claude-code-jsonl-v1".into(),
            target_field: "message.content".into(),
            transform: Some(FieldTransform::Restructure {
                description: "Set type='human' and wrap in message object".into(),
            }),
            confidence: 0.85,
            semantic_tag: "message_text".into(),
        },
        CrossProviderMapping {
            source_schema: "copilot-json-v3".into(),
            source_field: "request.timestamp".into(),
            target_schema: "claude-code-jsonl-v1".into(),
            target_field: "timestamp".into(),
            transform: Some(FieldTransform::TypeConvert {
                from_type: "epoch_ms".into(),
                to_type: "iso8601".into(),
            }),
            confidence: 0.9,
            semantic_tag: "message_timestamp".into(),
        },
    ]
}

// --- Claude Code v1 → OpenAI API v1 ---

fn claude_code_to_openai_mappings() -> Vec<CrossProviderMapping> {
    vec![
        CrossProviderMapping {
            source_schema: "claude-code-jsonl-v1".into(),
            source_field: "type".into(),
            target_schema: "openai-api-openai-api-v1".into(),
            target_field: "messages[].role".into(),
            transform: Some(FieldTransform::ValueMap {
                mapping: HashMap::from([
                    ("human".into(), "user".into()),
                    ("assistant".into(), "assistant".into()),
                    ("system".into(), "system".into()),
                    ("tool_use".into(), "assistant".into()),
                    ("tool_result".into(), "tool".into()),
                ]),
            }),
            confidence: 0.9,
            semantic_tag: "message_role".into(),
        },
        CrossProviderMapping {
            source_schema: "claude-code-jsonl-v1".into(),
            source_field: "message.content".into(),
            target_schema: "openai-api-openai-api-v1".into(),
            target_field: "messages[].content".into(),
            transform: Some(FieldTransform::Identity),
            confidence: 0.95,
            semantic_tag: "message_text".into(),
        },
    ]
}

// ============================================================================
// Migration Paths
// ============================================================================

fn build_migration_paths() -> Vec<MigrationPath> {
    vec![
        // Copilot JSON v3 → JSONL v1 (the big format transition)
        MigrationPath {
            from_schema: "copilot-json-v3".into(),
            to_schema: "copilot-jsonl-v1".into(),
            lossless: false,
            mappings: copilot_json_to_jsonl_mappings(),
            data_loss: vec![
                "Response format changes (legacy value array → typed parts array)".into(),
            ],
            new_fields: vec![
                "modelState (required for VS Code to show session)".into(),
                "timeSpentWaiting".into(),
                "Event envelope (kind, data)".into(),
            ],
            notes: vec![
                "Major format transition from single JSON to event-sourced JSONL".into(),
                "Response extraction logic must change".into(),
                "Index format changes from UUID array to UUID→entry map".into(),
                "Model cache (agentSessions.model.cache) must be populated".into(),
                "File extension changes from .json to .jsonl".into(),
            ],
        },
        // Copilot JSONL v1 → JSON v3 (reverse migration for backwards compat)
        MigrationPath {
            from_schema: "copilot-jsonl-v1".into(),
            to_schema: "copilot-json-v3".into(),
            lossless: false,
            mappings: vec![], // Reverse of json_to_jsonl
            data_loss: vec![
                "modelState field dropped".into(),
                "timeSpentWaiting field dropped".into(),
                "Event history lost (only kind:0 snapshot preserved)".into(),
                "Incremental updates (kind:1, kind:2) discarded".into(),
            ],
            new_fields: vec![],
            notes: vec![
                "Reverse migration for backwards compatibility with older VS Code versions".into(),
                "Compact JSONL to single snapshot first, then unwrap event envelope".into(),
                "Response parts array must be converted back to legacy format".into(),
            ],
        },
    ]
}

// ============================================================================
// Capability Matrix
// ============================================================================

fn build_capability_matrix() -> HashMap<String, Vec<String>> {
    HashMap::from([
        (
            "copilot".into(),
            vec![
                "session_storage".into(),
                "session_index".into(),
                "model_cache".into(),
                "state_cache".into(),
                "event_sourcing".into(),
                "agent_mode".into(),
                "tool_calling".into(),
                "multi_model".into(),
                "mcp".into(),
                "checkpoints".into(),
            ],
        ),
        (
            "cursor".into(),
            vec![
                "session_storage".into(),
                "multi_model".into(),
                "agent_mode".into(),
                "tool_calling".into(),
            ],
        ),
        (
            "claude-code".into(),
            vec![
                "session_storage".into(),
                "tool_calling".into(),
                "agent_mode".into(),
                "cost_tracking".into(),
                "mcp".into(),
            ],
        ),
        (
            "codex-cli".into(),
            vec![
                "session_storage".into(),
                "tool_calling".into(),
                "agent_mode".into(),
            ],
        ),
        (
            "gemini-cli".into(),
            vec![
                "session_storage".into(),
                "tool_calling".into(),
                "agent_mode".into(),
                "multi_modal".into(),
            ],
        ),
        (
            "continue-dev".into(),
            vec![
                "session_storage".into(),
                "multi_model".into(),
                "multi_provider".into(),
            ],
        ),
        (
            "openai-api".into(),
            vec![
                "chat_completions".into(),
                "tool_calling".into(),
                "streaming".into(),
                "multi_model".into(),
                "embeddings".into(),
            ],
        ),
    ])
}