Module: CodexSDK::ConfigSerializer

Defined in:
lib/codex_sdk/config_serializer.rb

Overview

Serializes Ruby hashes into –config CLI flags using TOML value syntax. Mirrors the TypeScript SDK’s toTomlValue() and flattenConfig() logic.

Constant Summary collapse

BARE_KEY_PATTERN =
/\A[A-Za-z0-9_-]+\z/

Class Method Summary collapse

Class Method Details

.flatten(hash, prefix: nil) ⇒ Object

Flattens a nested hash into dotted key paths.

flatten({ a: { b: 1, c: { d: 2 } } })
# => { "a.b" => 1, "a.c.d" => 2 }


23
24
25
26
27
28
29
30
31
32
# File 'lib/codex_sdk/config_serializer.rb', line 23

def flatten(hash, prefix: nil)
  hash.each_with_object({}) do |(key, value), result|
    full_key = prefix ? "#{prefix}.#{key}" : key.to_s
    if value.is_a?(Hash)
      result.merge!(flatten(value, prefix: full_key))
    else
      result[full_key] = value
    end
  end
end

.format_key(key) ⇒ Object



57
58
59
60
# File 'lib/codex_sdk/config_serializer.rb', line 57

def format_key(key)
  str = key.to_s
  str.match?(BARE_KEY_PATTERN) ? str : str.to_json
end

.to_flags(config) ⇒ Object

Converts a nested hash into an array of [“–config”, “key=value”] pairs.

to_flags({ sandbox_workspace_write: { network_access: true } })
# => ["--config", "sandbox_workspace_write.network_access=true"]


15
16
17
# File 'lib/codex_sdk/config_serializer.rb', line 15

def to_flags(config)
  flatten(config).flat_map { |key, value| ["--config", "#{key}=#{to_toml_value(value)}"] }
end

.to_toml_value(value) ⇒ Object

Converts a Ruby value to a TOML literal string.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/codex_sdk/config_serializer.rb', line 35

def to_toml_value(value)
  case value
  when String
    value.to_json
  when Integer, Float
    raise ArgumentError, "cannot serialize non-finite number" unless value.to_f.finite?

    value.to_s
  when true, false
    value.to_s
  when Array
    "[#{value.map { |v| to_toml_value(v) }.join(", ")}]"
  when Hash
    inner = value.map { |k, v| "#{format_key(k)} = #{to_toml_value(v)}" }.join(", ")
    "{#{inner}}"
  when nil
    raise ArgumentError, "cannot serialize nil to TOML"
  else
    raise ArgumentError, "unsupported type: #{value.class}"
  end
end