|
4 | 4 |
|
5 | 5 | from typing import TYPE_CHECKING, cast |
6 | 6 |
|
| 7 | +from fast_agent.commands.handlers import session_export as session_export_handlers |
7 | 8 | from fast_agent.commands.handlers import sessions as sessions_handlers |
8 | 9 | from fast_agent.commands.handlers.shared import clear_agent_histories |
9 | 10 | from fast_agent.commands.renderers.session_markdown import render_session_list_markdown |
| 11 | +from fast_agent.commands.results import CommandOutcome |
| 12 | +from fast_agent.commands.session_export_help import render_session_export_help_markdown |
10 | 13 | from fast_agent.commands.session_summaries import build_session_list_summary |
11 | | -from fast_agent.commands.shared_command_intents import parse_session_command_intent |
| 14 | +from fast_agent.commands.shared_command_intents import ( |
| 15 | + parse_session_command_intent, |
| 16 | + should_default_export_agent, |
| 17 | +) |
12 | 18 |
|
13 | 19 | if TYPE_CHECKING: |
14 | 20 | from fast_agent.acp.command_io import ACPCommandIO |
@@ -43,13 +49,15 @@ async def handle_session(handler: "SlashCommandHandler", arguments: str | None = |
43 | 49 | return await handle_session_delete(handler, intent.argument) |
44 | 50 | if intent.action == "pin": |
45 | 51 | return await handle_session_pin(handler, value=intent.pin_value, target=intent.pin_target) |
| 52 | + if intent.action == "export": |
| 53 | + return await handle_session_export(handler, intent) |
46 | 54 |
|
47 | 55 | return "\n".join( |
48 | 56 | [ |
49 | 57 | "# session", |
50 | 58 | "", |
51 | 59 | f"Unknown /session action: {intent.raw_subcommand or ''}", |
52 | | - "Usage: /session [list|new|resume|title|fork|delete|pin] [args]", |
| 60 | + "Usage: /session [list|new|resume|title|fork|delete|pin|export] [args]", |
53 | 61 | ] |
54 | 62 | ) |
55 | 63 |
|
@@ -153,3 +161,45 @@ async def handle_session_pin( |
153 | 161 | target=target, |
154 | 162 | ) |
155 | 163 | return handler._format_outcome_as_markdown(outcome, "session pin", io=io) |
| 164 | + |
| 165 | + |
| 166 | +async def handle_session_export(handler: "SlashCommandHandler", intent) -> str: |
| 167 | + if intent.export_help: |
| 168 | + return render_session_export_help_markdown() |
| 169 | + |
| 170 | + ctx = handler._build_command_context() |
| 171 | + io = cast("ACPCommandIO", ctx.io) |
| 172 | + manager = ctx.resolve_session_manager() |
| 173 | + current_session = manager.current_session |
| 174 | + current_session_id = current_session.info.name if current_session is not None else None |
| 175 | + if current_session_id != handler.session_id: |
| 176 | + try: |
| 177 | + handler_session = manager.get_session(handler.session_id) |
| 178 | + except AttributeError: |
| 179 | + handler_session = None |
| 180 | + current_session_id = handler_session.info.name if handler_session is not None else None |
| 181 | + if intent.export_target is None and current_session_id is None: |
| 182 | + outcome = CommandOutcome() |
| 183 | + outcome.add_message( |
| 184 | + "No active session to export.", |
| 185 | + channel="error", |
| 186 | + right_info="session", |
| 187 | + ) |
| 188 | + return handler._format_outcome_as_markdown(outcome, "session export", io=io) |
| 189 | + agent_name = intent.export_agent |
| 190 | + if agent_name is None and should_default_export_agent( |
| 191 | + intent.export_target, |
| 192 | + current_session_id=current_session_id, |
| 193 | + ): |
| 194 | + agent_name = handler.current_agent_name |
| 195 | + outcome = await session_export_handlers.handle_session_export( |
| 196 | + ctx, |
| 197 | + target=intent.export_target, |
| 198 | + agent_name=agent_name, |
| 199 | + output_path=intent.export_output, |
| 200 | + hf_dataset=intent.export_hf_dataset, |
| 201 | + hf_dataset_path=intent.export_hf_dataset_path, |
| 202 | + current_session_id=current_session_id, |
| 203 | + error=intent.export_error, |
| 204 | + ) |
| 205 | + return handler._format_outcome_as_markdown(outcome, "session export", io=io) |
0 commit comments