14
14
from rich .panel import Panel
15
15
from rich .syntax import Syntax
16
16
17
+ from fractale .agent .context import get_context
18
+ import fractale .agent .defaults as defaults
17
19
import fractale .agent .kubernetes_job .prompts as prompts
18
20
import fractale .utils as utils
19
21
from fractale .agent .base import Agent
@@ -66,7 +68,7 @@ def add_arguments(self, subparser):
66
68
dest = "agent_name" ,
67
69
)
68
70
69
- def run (self , args , extra , error_message = None , template = None , context = None , attempts = 0 ):
71
+ def run (self , context , template = None ):
70
72
"""
71
73
Run the agent.
72
74
"""
@@ -77,28 +79,44 @@ def run(self, args, extra, error_message=None, template=None, context=None, atte
77
79
except KeyError :
78
80
sys .exit ("ERROR: GEMINI_API_KEY environment variable not set." )
79
81
82
+ # Init attempts. Each agent has an internal counter for total attempts
83
+ self .attempts = self .attempts or 0
84
+
85
+ # Create or get global context
86
+ context = get_context (context )
87
+ error_message = context .get ('error_message' )
88
+
80
89
# If we have a context file, add to template
81
- context = None
82
- if args .context_file is not None :
83
- context = utils .read_file (args .context_file )
90
+ context_file = context .get ('context_file' )
91
+ build_context = context .get ('build_context' )
92
+ if context_file is not None and build_context is None :
93
+ build_context = utils .read_file (context_file )
94
+ context .build_context = build_context
95
+
96
+ # These are required
97
+ container = context .get ('container' , required = True )
98
+
99
+ # Assume most containers are built and loaded
100
+ environment = context .get ('environment' , defaults .environment )
101
+ no_pull = context .get ('no_pull' , True )
84
102
85
103
# This will either generate fresh or rebuild erroneous Job
86
104
job_crd = self .generate_crd (
87
- container = args . container ,
88
- environment = args . environment ,
105
+ container = container ,
106
+ environment = environment ,
89
107
error_message = error_message ,
90
108
template = template ,
91
- context = context ,
92
- no_pull = args . no_pull ,
109
+ context = context . build_context ,
110
+ no_pull = no_pull ,
93
111
)
94
112
print (Panel (job_crd , title = "[green]job.yaml[/green]" , border_style = "green" ))
95
113
96
114
# Make and deploy it! Success is exit code 0.
97
- return_code , output = self .deploy (job_crd , image_name = args . container , attempt = attempts )
115
+ return_code , output = self .deploy (job_crd , image_name = container )
98
116
if return_code == 0 :
99
117
print (
100
118
Panel (
101
- f"[bold green]✅ Deploy complete in { attempts } attempts[/bold green]" ,
119
+ f"[bold green]✅ Deploy complete in { self . attempts } attempts[/bold green]" ,
102
120
title = "Success" ,
103
121
border_style = "green" ,
104
122
)
@@ -111,24 +129,21 @@ def run(self, args, extra, error_message=None, template=None, context=None, atte
111
129
border_style = "red" ,
112
130
)
113
131
)
114
- attempts += 1
115
132
print ("\n [bold cyan] Requesting Correction from Kubernetes Job Agent[/bold cyan]" )
133
+ self .attempts += 1
116
134
117
135
# Trigger again, provide initial context and error message
136
+ context .error_message = output
118
137
return self .run (
119
- args ,
120
- extra ,
121
- error_message = output ,
122
138
template = job_crd ,
123
139
context = context ,
124
- attempts = attempts ,
125
140
)
126
141
127
142
# Add generation line
128
143
job_crd += "\n # Generated by fractale build agent"
129
144
self .print_crd (job_crd )
130
- if args .outfile :
131
- utils .write_file (job_crd , args .outfile )
145
+ if context .outfile :
146
+ utils .write_file (job_crd , context .outfile )
132
147
return job_crd
133
148
134
149
def print_crd (self , job_crd ):
@@ -274,7 +289,7 @@ def cleanup_job(self, job_name, namespace):
274
289
check = False ,
275
290
)
276
291
277
- def deploy (self , job_crd , image_name , attempt ):
292
+ def deploy (self , job_crd , image_name ):
278
293
"""
279
294
Deploy the Kubernetes Job.
280
295
"""
@@ -320,7 +335,7 @@ def deploy(self, job_crd, image_name, attempt):
320
335
utils .write_file (job_crd , job_manifest_path )
321
336
print (
322
337
Panel (
323
- f"Attempt { attempt } to deploy Kubernetes Job: [bold cyan]{ image_name } [/bold cyan]" ,
338
+ f"Attempt { self . attempts + 1 } to deploy Kubernetes Job: [bold cyan]{ image_name } [/bold cyan]" ,
324
339
title = "[blue]Kubernetes Job[/blue]" ,
325
340
border_style = "blue" ,
326
341
)
0 commit comments