|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import shlex |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +from agents.models import AgentSpec |
| 7 | +from cli.context import CliContext |
| 8 | +from cli.models import ParsedStartCommand |
| 9 | +from provider_core.caller_env import ( |
| 10 | + caller_context_env, |
| 11 | + export_env_clause, |
| 12 | + join_env_prefix, |
| 13 | + provider_user_session_env, |
| 14 | +) |
| 15 | +from provider_core.contracts import ProviderRuntimeLauncher |
| 16 | +from provider_core.runtime_shared import provider_start_parts |
| 17 | +from workspace.models import WorkspacePlan |
| 18 | + |
| 19 | + |
| 20 | +_YOLO_FLAG = '--dangerously-skip-permissions' |
| 21 | + |
| 22 | + |
| 23 | +def build_runtime_launcher() -> ProviderRuntimeLauncher: |
| 24 | + return ProviderRuntimeLauncher( |
| 25 | + provider='agy', |
| 26 | + launch_mode='simple_tmux', |
| 27 | + prepare_launch_context=prepare_launch_context, |
| 28 | + build_start_cmd=build_start_cmd, |
| 29 | + build_session_payload=build_session_payload, |
| 30 | + ) |
| 31 | + |
| 32 | + |
| 33 | +def prepare_launch_context( |
| 34 | + context: CliContext, |
| 35 | + spec: AgentSpec, |
| 36 | + plan: WorkspacePlan, |
| 37 | + runtime_dir: Path, |
| 38 | + prepared_state: dict[str, object], |
| 39 | +) -> dict[str, object]: |
| 40 | + del runtime_dir |
| 41 | + payload = dict(prepared_state or {}) |
| 42 | + payload['agent_name'] = spec.name |
| 43 | + payload['project_root'] = str(context.project.project_root) |
| 44 | + payload['workspace_path'] = str(prepared_state.get('run_cwd') or plan.workspace_path) |
| 45 | + payload['agent_events_path'] = str(context.paths.agent_events_path(spec.name)) |
| 46 | + return payload |
| 47 | + |
| 48 | + |
| 49 | +def build_start_cmd( |
| 50 | + command: ParsedStartCommand, |
| 51 | + spec: AgentSpec, |
| 52 | + runtime_dir, |
| 53 | + launch_session_id: str, |
| 54 | + *, |
| 55 | + prepared_state: dict[str, object] | None = None, |
| 56 | +) -> str: |
| 57 | + del prepared_state |
| 58 | + cmd_parts = provider_start_parts('agy') |
| 59 | + if command.auto_permission and _YOLO_FLAG not in cmd_parts and _YOLO_FLAG not in spec.startup_args: |
| 60 | + cmd_parts.append(_YOLO_FLAG) |
| 61 | + if command.restore and not _has_restore_arg(cmd_parts) and not _has_restore_arg(spec.startup_args): |
| 62 | + cmd_parts.append('--continue') |
| 63 | + cmd_parts.extend(spec.startup_args) |
| 64 | + cmd = ' '.join(shlex.quote(str(part)) for part in cmd_parts) |
| 65 | + runtime_dir = Path(runtime_dir) |
| 66 | + env_prefix = join_env_prefix( |
| 67 | + export_env_clause(provider_user_session_env()), |
| 68 | + export_env_clause(spec.env), |
| 69 | + export_env_clause( |
| 70 | + caller_context_env(actor=spec.name, runtime_dir=runtime_dir, launch_session_id=launch_session_id) |
| 71 | + ), |
| 72 | + ) |
| 73 | + if env_prefix: |
| 74 | + return f'{env_prefix}; {cmd}' |
| 75 | + return cmd |
| 76 | + |
| 77 | + |
| 78 | +def build_session_payload( |
| 79 | + context: CliContext, |
| 80 | + spec: AgentSpec, |
| 81 | + plan: WorkspacePlan, |
| 82 | + runtime_dir, |
| 83 | + run_cwd, |
| 84 | + pane_id: str, |
| 85 | + pane_title_marker: str, |
| 86 | + start_cmd: str, |
| 87 | + launch_session_id: str, |
| 88 | + prepared_state: dict[str, object], |
| 89 | +) -> dict[str, object]: |
| 90 | + del context, spec, prepared_state |
| 91 | + return { |
| 92 | + 'ccb_session_id': launch_session_id, |
| 93 | + 'runtime_dir': str(runtime_dir), |
| 94 | + 'completion_artifact_dir': str(runtime_dir / 'completion'), |
| 95 | + 'terminal': 'tmux', |
| 96 | + 'tmux_session': pane_id, |
| 97 | + 'pane_id': pane_id, |
| 98 | + 'pane_title_marker': pane_title_marker, |
| 99 | + 'workspace_path': str(plan.workspace_path), |
| 100 | + 'work_dir': str(run_cwd), |
| 101 | + 'start_cmd': start_cmd, |
| 102 | + } |
| 103 | + |
| 104 | + |
| 105 | +def _has_restore_arg(parts: tuple[str, ...] | list[str]) -> bool: |
| 106 | + normalized = {str(part).strip() for part in parts} |
| 107 | + return bool({'--continue', '-c', '--conversation'} & normalized) |
| 108 | + |
| 109 | + |
| 110 | +__all__ = ['build_runtime_launcher', 'build_start_cmd', 'prepare_launch_context'] |
0 commit comments