Class: Insika::PackImporter

Inherits:
Object
  • Object
show all
Defined in:
lib/insika/pack_importer.rb

Overview

Pack importer (Phase 6/D4/F6, task 7): reads a Pack and emits the ALREADY existing authoring Commands — create_agent/update_agent + write_agent_file + write_skill + write_data_tool — making an agent provisionable at runtime from a standardized pack. It's the piece that achei-b2b's GatewayClient/ ProvisionStore triggers (via the provisioning API, task 8).

GENERIC (NF1): nothing here mentions achei-b2b — the pack is the contract. Tool names come from the PACK, so prompts<->tools stay consistent by construction (NF2). It doesn't write to the store directly: it only dispatches Commands on the bus (the same transport discipline) + READS the ProfileSource to decide create vs update.

IDEMPOTENT (upsert): re-importing the same pack reconciles — files/skills/ tools rewritten; the allowlists (prompt_files/skills/tools_allow) are AUTHORITATIVE from the pack, so whatever left the pack leaves the agent (isolation and no drift). An in-flight turn keeps the profile it captured.

Instance Method Summary collapse

Constructor Details

#initialize(bus:, profiles:) ⇒ PackImporter

Returns a new instance of PackImporter.



21
22
23
24
# File 'lib/insika/pack_importer.rb', line 21

def initialize(bus:, profiles:)
  @bus = bus
  @profiles = profiles
end

Instance Method Details

#delete(id) ⇒ Object

Removes the agent (delete_agent). Does NOT delete skills/tools/files: they may be shared and the SkillStore/ToolStore are global — selective removal is operator work. NotFoundError (missing agent) propagates -> 404.



49
50
51
52
# File 'lib/insika/pack_importer.rb', line 49

def delete(id)
  dispatch(:delete_agent, { id: id })
  { agent_id: id, deleted: true }
end

#import(pack) ⇒ Object

-> { agent_id:, created:, files: [names], skills: [names], tools: [names] }. Validation/lookup errors from the Commands (missing id/model, etc.) propagate (the transport maps them to 422/404).



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/insika/pack_importer.rb', line 29

def import(pack)
  id = presence(pack.config[:id]) ||
       (raise Insika::ValidationError, "pack missing config.id")

  created = @profiles[id].nil?
  # create_agent rejects an already-existing id; update_agent requires it to
  # exist — the choice by presence makes the import an upsert.
  dispatch(created ? :create_agent : :update_agent, agent_attrs(pack, id))

  pack.files.each { |name, body| dispatch(:write_agent_file, { agent_id: id, file: name, content: body }) }
  pack.skills.each { |name, body| dispatch(:write_skill, { name: name, content: body }) }
  pack.tools.each { |defn| dispatch(:write_data_tool, defn) }

  { agent_id: id, created: created,
    files: pack.files.keys, skills: pack.skills.keys, tools: pack.tools.map { |t| tool_name(t) } }
end