Class: Aircana::Contexts::Manifest

Inherits:
Object
  • Object
show all
Defined in:
lib/aircana/contexts/manifest.rb

Class Method Summary collapse

Class Method Details

.create_manifest(kb_name, sources, kb_type: "local") ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/aircana/contexts/manifest.rb', line 10

def create_manifest(kb_name, sources, kb_type: "local")
  validate_sources(sources)
  validate_kb_type(kb_type)

  manifest_path = manifest_path_for(kb_name)
  manifest_data = build_manifest_data(kb_name, sources, kb_type)

  FileUtils.mkdir_p(File.dirname(manifest_path))
  File.write(manifest_path, JSON.pretty_generate(manifest_data))

  Aircana.human_logger.info "Created knowledge manifest for '#{kb_name}' (kb_type: #{kb_type})"
  manifest_path
end

.kb_type_from_manifest(kb_name) ⇒ Object



69
70
71
72
73
74
# File 'lib/aircana/contexts/manifest.rb', line 69

def kb_type_from_manifest(kb_name)
  manifest = read_manifest(kb_name)
  return "local" unless manifest

  manifest["kb_type"] || "local"
end

.manifest_exists?(kb_name) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/aircana/contexts/manifest.rb', line 76

def manifest_exists?(kb_name)
  File.exist?(manifest_path_for(kb_name))
end

.read_manifest(kb_name) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/aircana/contexts/manifest.rb', line 45

def read_manifest(kb_name)
  manifest_path = manifest_path_for(kb_name)
  return nil unless File.exist?(manifest_path)

  begin
    manifest_data = JSON.parse(File.read(manifest_path))
    validate_manifest(manifest_data)
    manifest_data
  rescue JSON::ParserError => e
    Aircana.human_logger.warn "Invalid manifest for KB '#{kb_name}': #{e.message}"
    nil
  rescue ManifestError => e
    Aircana.human_logger.warn "Manifest validation failed for KB '#{kb_name}': #{e.message}"
    nil
  end
end

.sources_from_manifest(kb_name) ⇒ Object



62
63
64
65
66
67
# File 'lib/aircana/contexts/manifest.rb', line 62

def sources_from_manifest(kb_name)
  manifest = read_manifest(kb_name)
  return [] unless manifest

  manifest["sources"] || []
end

.update_manifest(kb_name, sources, kb_type: nil) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/aircana/contexts/manifest.rb', line 24

def update_manifest(kb_name, sources, kb_type: nil)
  validate_sources(sources)

  manifest_path = manifest_path_for(kb_name)

  if File.exist?(manifest_path)
    existing_data = JSON.parse(File.read(manifest_path))
    # Preserve existing kb_type unless explicitly provided
    kb_type_to_use = kb_type || existing_data["kb_type"] || "local"
    manifest_data = existing_data.merge({ "sources" => sources, "kb_type" => kb_type_to_use })
  else
    kb_type_to_use = kb_type || "local"
    manifest_data = build_manifest_data(kb_name, sources, kb_type_to_use)
  end

  validate_kb_type(manifest_data["kb_type"])
  FileUtils.mkdir_p(File.dirname(manifest_path))
  File.write(manifest_path, JSON.pretty_generate(manifest_data))
  manifest_path
end