Module: AgentCliRuntime::OpenCode::Overlay

Defined in:
lib/agent_cli_runtime/opencode/overlay.rb

Constant Summary collapse

MAX_CONFIGURATION_BYTES =
1_048_576

Class Method Summary collapse

Class Method Details

.prepare!(preparation, env: ENV) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/agent_cli_runtime/opencode/overlay.rb', line 47

def prepare!(preparation, env: ENV)
  unless preparation.is_a?(OpenCodePreparationRequest)
    raise ArgumentError,
          "request must be an AgentCliRuntime::OpenCodePreparationRequest"
  end

  profile = Profiles.resolve(preparation.request.profile)
  unless profile.name == :opencode
    raise ConfigurationError,
          "OpenCode preparation requires profile :opencode"
  end

  source_config, source_label = load_configuration(preparation)
  requested_route = resolve_route(preparation.request, source_config)
  validate_provider!(source_config, requested_route.provider)
  validate_nonsecret!(source_config)
  roots = resolve_roots(preparation)
  credential_environment_keys = provider_credential_environment_keys(
    source_config, requested_route.provider,
    preparation.credential_environment_keys
  )
  plugins = selected_plugins(source_config, preparation.plugins)
  permission = permission_rules(preparation, roots, plugins: plugins)

  root = create_root!(preparation.invocation_root)
  cleanup = Cleanup.new(root)
  begin
    paths = create_directories(root)
    pure = preparation.pure && plugins.empty?
    config = generated_configuration(
      source_config, plugins, requested_route, permission
    )
    configuration_path = File.join(paths.fetch(:source), "opencode.json")
    write_private_file(configuration_path, JSON.pretty_generate(config) + "\n")
    generated_paths = [ root, *paths.values, configuration_path ]

    staged_credential = stage_credential_file(
      preparation.credential_file,
      File.join(paths.fetch(:data), "opencode", "auth.json")
    )
    generated_paths.concat(staged_credential ? staged_credential : [])

    environment = overlay_environment(
      paths, configuration_path, pure: pure
    )
    executable =
      preparation.request.executable || profile.bin(env: env)
    probe_env = env.to_h.merge(
      "AGENT_CLI_RUNTIME_OPENCODE_BIN" => executable
    )
    probe_request = ProbeRequest.new(
      profile: profile,
      route: requested_route,
      variant: preparation.request.effort,
      environment: environment,
      credential_environment_keys: credential_environment_keys,
      credential_file_staged:
        staged_credential && credential_file_supports_provider?(
          staged_credential.last, requested_route.provider
        )
    )
    probe_result = OpenCode::Probe.call!(probe_request, env: probe_env)
    invocation = compile_invocation(
      preparation, profile, requested_route, roots,
      executable:, pure: pure,
      probe_result:
    )

    PreparedInvocation.new(
      invocation: invocation,
      environment: environment,
      credential_environment_keys: credential_environment_keys,
      invocation_root: root,
      generated_paths: generated_paths.uniq,
      configuration_path: configuration_path,
      requested_route: requested_route,
      configuration_source: source_label,
      probe_result: probe_result,
      cleanup: cleanup,
      executable: executable
    )
  rescue Exception
    cleanup.call
    raise
  end
end