2323
2424import argparse
2525import os
26- import secrets
2726from pathlib import Path
2827
2928import modal
@@ -83,7 +82,7 @@ def clone_github_repo(
8382
8483# ## Grant Modal credentials
8584
86- # Since the agent is working with Modal code, we also give it access to Modal.
85+ # Since the agent is working with Modal code, we also make it easy to provide Modal access .
8786# Examples in this repo should run with nothing more than `modal` installed --
8887# except for a few that use `fastapi`.
8988
@@ -149,10 +148,6 @@ def create_sandbox(
149148 )
150149
151150
152- # The server is secured with a temporary password
153- # generated with the `secrets` library from the Python stdlib.
154- # We pass it to the Sandbox via a [Modal Secret](https://modal.com/docs/guide/secrets).
155-
156151# OpenCode is truly open -- there are many interfaces to the underlying
157152# coding agent server.
158153# Here we print information for:
@@ -161,7 +156,7 @@ def create_sandbox(
161156# - accessing the TUI from your local terminal
162157
163158
164- def print_access_info (sandbox : modal .Sandbox , password : str ):
159+ def print_access_info (sandbox : modal .Sandbox , password_secret_name : str ):
165160 print (
166161 "🏖️ Access the sandbox directly:" ,
167162 f"modal shell { sandbox .object_id } " ,
@@ -173,16 +168,29 @@ def print_access_info(sandbox: modal.Sandbox, password: str):
173168 "🏖️ Access the WebUI:" ,
174169 tunnel .url ,
175170 "Username: opencode" ,
176- f"Password: { password } " ,
177171 sep = "\n \t " ,
178172 )
179173 print (
180174 "🏖️ Access the TUI:" ,
181- f"OPENCODE_SERVER_PASSWORD={ password } opencode attach { tunnel .url } " ,
175+ f"OPENCODE_SERVER_PASSWORD=YOUR_PASSWORD opencode attach { tunnel .url } " ,
176+ sep = "\n \t " ,
177+ )
178+ print (
179+ "🏖️ Display the password:" ,
180+ f"modal shell --secret { password_secret_name } --cmd 'env | grep OPENCODE_SERVER_PASSWORD='" ,
182181 sep = "\n \t " ,
183182 )
184183
185184
185+ # The server is secured via a password in a [Modal Secret](https://modal.com/docs/guide/secrets).
186+ # You can create one by heading to the [Secrets Dashboard](https://modal.com/secrets)
187+ # and creating a new "Custom" Secret. Use `OPENCODE_SERVER_PASSWORD` as the key
188+ # and the password as the value.
189+
190+ # The CLI will also give you a helpful one-liner you can use to recover the password
191+ # with your Modal credentials in case you forget it.
192+
193+
186194# ## Putting it all together
187195
188196
@@ -193,6 +201,7 @@ def main(
193201 github_repo : str ,
194202 github_ref : str ,
195203 github_token : str | None ,
204+ password_secret_name : str ,
196205):
197206 app = modal .App .lookup (app_name , create_if_missing = True )
198207 image = define_base_image ()
@@ -202,15 +211,14 @@ def main(
202211
203212 image = clone_github_repo (image , github_repo , github_ref , github_token )
204213
205- password = secrets .token_urlsafe (13 )
206- password_secret = modal .Secret .from_dict ({"OPENCODE_SERVER_PASSWORD" : password })
207- sandbox_secrets = [password_secret ]
214+ password_secret = modal .Secret .from_name (password_secret_name )
208215
216+ sandbox_secrets = [password_secret ]
209217 if github_token :
210218 sandbox_secrets .append (modal .Secret .from_dict ({"GH_TOKEN" : github_token }))
211219
212220 sandbox = create_sandbox (image , timeout , app , sandbox_secrets , "/root/code" )
213- print_access_info (sandbox , password )
221+ print_access_info (sandbox , password_secret_name )
214222
215223
216224# ## Command-line options
@@ -262,6 +270,12 @@ def parse_timeout(timeout_str: str) -> int:
262270 dest = "allow_modal_access" ,
263271 help = "Disable Modal credential access" ,
264272 )
273+ parser .add_argument (
274+ "--password-secret" ,
275+ dest = "password_secret_name" ,
276+ help = "Name" ,
277+ default = "opencode-secret" ,
278+ )
265279 parser .add_argument (
266280 "--github-repo" ,
267281 type = str ,
@@ -290,4 +304,5 @@ def parse_timeout(timeout_str: str) -> int:
290304 args .github_repo ,
291305 args .github_ref ,
292306 args .github_token ,
307+ args .password_secret_name ,
293308 )
0 commit comments