Class: SwarmSDK::Snapshot

Inherits:
Object
  • Object
show all
Defined in:
lib/swarm_sdk/snapshot.rb

Overview

Snapshot of swarm conversation state

Encapsulates snapshot data with methods for serialization and deserialization. Provides a clean API for saving/loading snapshots in various formats.

Examples:

Create and save snapshot

snapshot = swarm.snapshot
snapshot.write_to_file("session.json")

Load and restore snapshot

snapshot = SwarmSDK::Snapshot.from_file("session.json")
result = swarm.restore(snapshot)

Convert to/from hash

hash = snapshot.to_hash
snapshot = SwarmSDK::Snapshot.from_hash(hash)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Snapshot

Initialize snapshot with data

Parameters:

  • data (Hash)

    Snapshot data hash



26
27
28
# File 'lib/swarm_sdk/snapshot.rb', line 26

def initialize(data)
  @data = data
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



21
22
23
# File 'lib/swarm_sdk/snapshot.rb', line 21

def data
  @data
end

Class Method Details

.from_file(path) ⇒ Snapshot

Create snapshot from JSON file

Parameters:

  • path (String)

    File path to read from

Returns:



92
93
94
95
# File 'lib/swarm_sdk/snapshot.rb', line 92

def from_file(path)
  json_string = File.read(path)
  from_json(json_string)
end

.from_hash(hash) ⇒ Snapshot

Create snapshot from hash

Parameters:

  • hash (Hash)

    Snapshot data hash

Returns:



75
76
77
# File 'lib/swarm_sdk/snapshot.rb', line 75

def from_hash(hash)
  new(hash)
end

.from_json(json_string) ⇒ Snapshot

Create snapshot from JSON string

Parameters:

  • json_string (String)

    JSON string

Returns:



83
84
85
86
# File 'lib/swarm_sdk/snapshot.rb', line 83

def from_json(json_string)
  hash = JSON.parse(json_string, symbolize_names: true)
  new(hash)
end

Instance Method Details

#agent_namesArray<String>

Get agent names from snapshot

Returns:

  • (Array<String>)

    Agent names



129
130
131
132
# File 'lib/swarm_sdk/snapshot.rb', line 129

def agent_names
  agents = @data[:agents] || @data["agents"]
  agents ? agents.keys.map(&:to_s) : []
end

#delegation_instance_namesArray<String>

Get delegation instance names from snapshot

Returns:

  • (Array<String>)

    Delegation instance names



137
138
139
140
# File 'lib/swarm_sdk/snapshot.rb', line 137

def delegation_instance_names
  delegations = @data[:delegation_instances] || @data["delegation_instances"]
  delegations ? delegations.keys.map(&:to_s) : []
end

#snapshot_atString

Get timestamp when snapshot was created

Returns:

  • (String)

    ISO8601 timestamp



115
116
117
# File 'lib/swarm_sdk/snapshot.rb', line 115

def snapshot_at
  @data[:snapshot_at] || @data["snapshot_at"]
end

#swarm?Boolean

Check if snapshot is for a swarm (vs workflow)

Returns:

  • (Boolean)

    true if swarm snapshot



145
146
147
# File 'lib/swarm_sdk/snapshot.rb', line 145

def swarm?
  type == "swarm"
end

#swarm_sdk_versionString

Get SwarmSDK version that created this snapshot

Returns:

  • (String)

    SwarmSDK version



122
123
124
# File 'lib/swarm_sdk/snapshot.rb', line 122

def swarm_sdk_version
  @data[:swarm_sdk_version] || @data["swarm_sdk_version"]
end

#to_hashHash

Convert snapshot to hash

Returns:

  • (Hash)

    Snapshot data as hash



33
34
35
# File 'lib/swarm_sdk/snapshot.rb', line 33

def to_hash
  @data
end

#to_json(pretty: true) ⇒ String

Convert snapshot to JSON string

Parameters:

  • pretty (Boolean) (defaults to: true)

    Whether to pretty-print JSON (default: true)

Returns:

  • (String)

    JSON string



41
42
43
44
45
46
47
# File 'lib/swarm_sdk/snapshot.rb', line 41

def to_json(pretty: true)
  if pretty
    JSON.pretty_generate(@data)
  else
    JSON.generate(@data)
  end
end

#typeString

Get snapshot type (swarm or workflow)

Returns:

  • (String)

    Snapshot type



108
109
110
# File 'lib/swarm_sdk/snapshot.rb', line 108

def type
  @data[:type] || @data["type"]
end

#versionString

Get snapshot version

Returns:

  • (String)

    Snapshot version



101
102
103
# File 'lib/swarm_sdk/snapshot.rb', line 101

def version
  @data[:version] || @data["version"]
end

#workflow?Boolean

Check if snapshot is for a workflow

Returns:

  • (Boolean)

    true if workflow snapshot



152
153
154
# File 'lib/swarm_sdk/snapshot.rb', line 152

def workflow?
  type == "workflow"
end

#write_to_file(path, pretty: true) ⇒ void

This method returns an undefined value.

Write snapshot to file as JSON

Uses atomic write pattern (write to temp file, then rename) to prevent corruption if process crashes during write.

Parameters:

  • path (String)

    File path to write to

  • pretty (Boolean) (defaults to: true)

    Whether to pretty-print JSON (default: true)



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/swarm_sdk/snapshot.rb', line 57

def write_to_file(path, pretty: true)
  # Atomic write: write to temp file, then rename
  # This ensures the snapshot file is never corrupted even if process crashes
  temp_path = "#{path}.tmp.#{Process.pid}.#{Time.now.to_i}.#{SecureRandom.hex(4)}"

  File.write(temp_path, to_json(pretty: pretty))
  File.rename(temp_path, path)
rescue
  # Clean up temp file if it exists
  File.delete(temp_path) if File.exist?(temp_path)
  raise
end