File size: 2,815 Bytes
c2ea5ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * Schema Detection Utility for Knowledge Graph Data
 *
 * Detects whether knowledge graph data uses reference-based or direct-based schema
 * and provides capability information for conditional feature rendering.
 */

export interface SchemaCapabilities {
  supportsLineReferences: boolean;
  supportsFailures: boolean;
  supportsImportanceLevels: boolean;
  supportsInteractionPrompts: boolean;
}

/**
 * Detect schema type from knowledge graph data structure
 */
export function detectSchemaType(
  data: any
): "reference_based" | "direct_based" {
  if (!data) {
    console.warn(
      "Schema detection: No data provided, defaulting to direct_based"
    );
    return "direct_based";
  }

  try {
    // Check entities for reference-based indicators
    const hasEntityRefs = data.entities?.some(
      (entity: any) =>
        Array.isArray(entity.raw_prompt_ref) && entity.raw_prompt_ref.length > 0
    );

    // Check relations for reference-based indicators
    const hasRelationRefs = data.relations?.some(
      (relation: any) =>
        Array.isArray(relation.interaction_prompt_ref) &&
        relation.interaction_prompt_ref.length > 0
    );

    // Check for failures array (reference-based only)
    const hasFailures =
      Array.isArray(data.failures) && data.failures.length > 0;

    // Check for importance levels (more common in reference-based)
    const hasImportanceLevels = data.entities?.some(
      (entity: any) =>
        entity.importance &&
        ["HIGH", "MEDIUM", "LOW"].includes(entity.importance)
    );

    const isReferenceBased =
      hasEntityRefs || hasRelationRefs || hasFailures || hasImportanceLevels;

    console.log("Schema detection results:", {
      hasEntityRefs,
      hasRelationRefs,
      hasFailures,
      hasImportanceLevels,
      schemaType: isReferenceBased ? "reference_based" : "direct_based",
    });

    return isReferenceBased ? "reference_based" : "direct_based";
  } catch (error) {
    console.warn("Schema detection failed, defaulting to direct_based:", error);
    return "direct_based";
  }
}

/**
 * Check if data has reference-based features
 */
export function hasReferenceBasedFeatures(data: any): boolean {
  return detectSchemaType(data) === "reference_based";
}

/**
 * Get schema capabilities based on schema type
 */
export function getSchemaCapabilities(
  schemaType: "reference_based" | "direct_based"
): SchemaCapabilities {
  if (schemaType === "reference_based") {
    return {
      supportsLineReferences: true,
      supportsFailures: true,
      supportsImportanceLevels: true,
      supportsInteractionPrompts: true,
    };
  } else {
    return {
      supportsLineReferences: false,
      supportsFailures: false,
      supportsImportanceLevels: false,
      supportsInteractionPrompts: false,
    };
  }
}