Class: SwarmSDK::Snapshot
- Inherits:
-
Object
- Object
- SwarmSDK::Snapshot
- 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.
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
Class Method Summary collapse
-
.from_file(path) ⇒ Snapshot
Create snapshot from JSON file.
-
.from_hash(hash) ⇒ Snapshot
Create snapshot from hash.
-
.from_json(json_string) ⇒ Snapshot
Create snapshot from JSON string.
Instance Method Summary collapse
-
#agent_names ⇒ Array<String>
Get agent names from snapshot.
-
#delegation_instance_names ⇒ Array<String>
Get delegation instance names from snapshot.
-
#initialize(data) ⇒ Snapshot
constructor
Initialize snapshot with data.
-
#snapshot_at ⇒ String
Get timestamp when snapshot was created.
-
#swarm? ⇒ Boolean
Check if snapshot is for a swarm (vs workflow).
-
#swarm_sdk_version ⇒ String
Get SwarmSDK version that created this snapshot.
-
#to_hash ⇒ Hash
Convert snapshot to hash.
-
#to_json(pretty: true) ⇒ String
Convert snapshot to JSON string.
-
#type ⇒ String
Get snapshot type (swarm or workflow).
-
#version ⇒ String
Get snapshot version.
-
#workflow? ⇒ Boolean
Check if snapshot is for a workflow.
-
#write_to_file(path, pretty: true) ⇒ void
Write snapshot to file as JSON.
Constructor Details
#initialize(data) ⇒ Snapshot
Initialize snapshot with data
26 27 28 |
# File 'lib/swarm_sdk/snapshot.rb', line 26 def initialize(data) @data = data end |
Instance Attribute Details
#data ⇒ Object (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
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
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
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_names ⇒ Array<String>
Get agent names from snapshot
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_names ⇒ Array<String>
Get delegation instance names from snapshot
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_at ⇒ String
Get timestamp when snapshot was created
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)
145 146 147 |
# File 'lib/swarm_sdk/snapshot.rb', line 145 def swarm? type == "swarm" end |
#swarm_sdk_version ⇒ String
Get SwarmSDK version that created this snapshot
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_hash ⇒ Hash
Convert snapshot to 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
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 |
#type ⇒ String
Get snapshot type (swarm or workflow)
108 109 110 |
# File 'lib/swarm_sdk/snapshot.rb', line 108 def type @data[:type] || @data["type"] end |
#version ⇒ String
Get 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
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.
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 |