Skip to main content

genja_core/settings/
runner.rs

1use super::env_defaults::{
2    get_runner_max_connection_attempts_default, get_runner_max_task_depth_default,
3    get_runner_options_default, get_runner_plugin_default,
4};
5use serde::{Deserialize, Serialize};
6
7/// Task runner configuration.
8///
9/// The plugin name defaults from `GENJA_RUNNER_PLUGIN`. `worker_count`,
10/// `max_task_depth`, and `max_connection_attempts` control built-in runner
11/// behavior; `options` carries plugin-specific JSON for custom runners.
12#[derive(Deserialize, Serialize, Clone, Debug)]
13#[serde(default)]
14pub struct RunnerConfig {
15    plugin: String,
16    options: serde_json::Value,
17    worker_count: Option<usize>,
18    max_task_depth: usize,
19    max_connection_attempts: usize,
20}
21
22impl Default for RunnerConfig {
23    fn default() -> Self {
24        Self {
25            plugin: get_runner_plugin_default(),
26            options: get_runner_options_default(),
27            worker_count: None,
28            max_task_depth: get_runner_max_task_depth_default(),
29            max_connection_attempts: get_runner_max_connection_attempts_default(),
30        }
31    }
32}
33
34impl RunnerConfig {
35    pub fn builder() -> RunnerConfigBuilder {
36        RunnerConfigBuilder::default()
37    }
38
39    pub fn plugin(&self) -> &str {
40        &self.plugin
41    }
42
43    pub fn options(&self) -> &serde_json::Value {
44        &self.options
45    }
46
47    pub fn worker_count(&self) -> Option<usize> {
48        self.worker_count
49    }
50
51    pub fn max_task_depth(&self) -> usize {
52        self.max_task_depth
53    }
54
55    pub fn max_connection_attempts(&self) -> usize {
56        self.max_connection_attempts
57    }
58}
59
60/// Builder for `RunnerConfig`.
61#[derive(Default)]
62pub struct RunnerConfigBuilder {
63    plugin: Option<String>,
64    options: Option<serde_json::Value>,
65    worker_count: Option<usize>,
66    max_task_depth: Option<usize>,
67    max_connection_attempts: Option<usize>,
68}
69
70impl RunnerConfigBuilder {
71    pub fn plugin(mut self, plugin: impl Into<String>) -> Self {
72        self.plugin = Some(plugin.into());
73        self
74    }
75
76    pub fn options(mut self, options: serde_json::Value) -> Self {
77        self.options = Some(options);
78        self
79    }
80
81    pub fn worker_count(mut self, worker_count: usize) -> Self {
82        self.worker_count = Some(worker_count);
83        self
84    }
85
86    pub fn max_task_depth(mut self, max_task_depth: usize) -> Self {
87        self.max_task_depth = Some(max_task_depth);
88        self
89    }
90
91    pub fn max_connection_attempts(mut self, max_connection_attempts: usize) -> Self {
92        self.max_connection_attempts = Some(max_connection_attempts);
93        self
94    }
95
96    pub fn build(self) -> RunnerConfig {
97        RunnerConfig {
98            plugin: self.plugin.unwrap_or_else(get_runner_plugin_default),
99            options: self.options.unwrap_or_else(get_runner_options_default),
100            worker_count: self.worker_count,
101            max_task_depth: self
102                .max_task_depth
103                .unwrap_or_else(get_runner_max_task_depth_default),
104            max_connection_attempts: self
105                .max_connection_attempts
106                .unwrap_or_else(get_runner_max_connection_attempts_default),
107        }
108    }
109}