Skip to content

Commit c1f037f

Browse files
author
Your Name
committed
Add task() method to main file to allow a relatively easy to use programmatic interface to invoke cecli
1 parent 222138f commit c1f037f

1 file changed

Lines changed: 72 additions & 0 deletions

File tree

cecli/main.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,6 +1314,78 @@ def load_slow_imports(swallow=True):
13141314
raise e
13151315

13161316

1317+
async def task(message, setting=None, env=None, force_git_root=None, return_coder=True):
1318+
"""
1319+
Programmatically run cecli with a message and optional settings.
1320+
1321+
Args:
1322+
message: The message/command to send to cecli (e.g., "Add a function to process data")
1323+
setting: Optional YAML string of settings to override configuration
1324+
env: Optional dict of environment variables to set
1325+
force_git_root: Optional path to force as git root
1326+
return_coder: Whether to return the coder object (default: True)
1327+
1328+
Returns:
1329+
The coder object if return_coder=True, otherwise exit code
1330+
"""
1331+
import json
1332+
import os
1333+
import tempfile
1334+
1335+
import yaml
1336+
1337+
# Set environment variables if provided
1338+
if env:
1339+
for key, value in env.items():
1340+
os.environ[key] = str(value)
1341+
1342+
# Build argv with message as --message flag
1343+
argv = ["--message", message]
1344+
1345+
# Handle settings via temporary config file
1346+
if setting:
1347+
# Parse YAML to validate
1348+
settings_dict = yaml.safe_load(setting)
1349+
1350+
# Add yes-always: True to ensure automatic confirmation
1351+
settings_dict["pretty"] = False
1352+
settings_dict["tui"] = False
1353+
settings_dict["yes-always"] = True
1354+
1355+
# Add agent-config with skip_cli_confirmations: true as JSON string
1356+
# Merge with existing agent-config if present
1357+
agent_config = {"skip_cli_confirmations": True}
1358+
if "agent-config" in settings_dict:
1359+
try:
1360+
existing_config = json.loads(settings_dict["agent-config"])
1361+
agent_config.update(existing_config)
1362+
except (json.JSONDecodeError, TypeError):
1363+
# If existing agent-config is not valid JSON, overwrite it
1364+
pass
1365+
settings_dict["agent-config"] = json.dumps(agent_config)
1366+
1367+
# Create temporary config file
1368+
with tempfile.NamedTemporaryFile(mode="w", suffix=".yml", delete=False) as f:
1369+
yaml.dump(settings_dict, f)
1370+
config_file = f.name
1371+
1372+
# Add config file argument
1373+
argv = ["--config", config_file] + argv
1374+
else:
1375+
config_file = None
1376+
1377+
try:
1378+
# Run main_async with constructed arguments
1379+
result = await main_async(
1380+
argv=argv, force_git_root=force_git_root, return_coder=return_coder
1381+
)
1382+
return result
1383+
finally:
1384+
# Clean up temporary config file
1385+
if config_file and os.path.exists(config_file):
1386+
os.unlink(config_file)
1387+
1388+
13171389
async def graceful_exit(coder=None, exit_code=0):
13181390
sys.settrace(None)
13191391
if coder:

0 commit comments

Comments
 (0)