diff --git a/honcho-cli/src/honcho_cli/commands/message.py b/honcho-cli/src/honcho_cli/commands/message.py index 5b561374..13240503 100644 --- a/honcho-cli/src/honcho_cli/commands/message.py +++ b/honcho-cli/src/honcho_cli/commands/message.py @@ -38,11 +38,13 @@ def list_messages( sess = client.session(sid) try: - msgs = list(sess.messages()) + # SDK returns most-recent-first. Default case (newest N) only needs + # the first page. --reverse (oldest N) still walks all pages until + # the SDK accepts order=asc on session.messages(). if not reverse: - msgs = msgs[-last:] + msgs = sess.messages().items[:last] else: - msgs = msgs[:last] + msgs = list(sess.messages())[-last:] # Detect duplicate content content_hashes: dict[str, list[str]] = {} diff --git a/honcho-cli/src/honcho_cli/commands/session.py b/honcho-cli/src/honcho_cli/commands/session.py index efecc467..9df08db3 100644 --- a/honcho-cli/src/honcho_cli/commands/session.py +++ b/honcho-cli/src/honcho_cli/commands/session.py @@ -125,11 +125,13 @@ def messages( sess = client.session(sid) try: - msgs = list(sess.messages()) + # SDK returns most-recent-first. Default case (newest N) only needs + # the first page. --reverse (oldest N) still walks all pages until + # the SDK accepts order=asc on session.messages(). if not reverse: - msgs = msgs[-last:] + msgs = sess.messages().items[:last] else: - msgs = msgs[:last] + msgs = list(sess.messages())[-last:] items = [ { diff --git a/honcho-cli/src/honcho_cli/commands/workspace.py b/honcho-cli/src/honcho_cli/commands/workspace.py index 53cf95cc..a5ad7a29 100644 --- a/honcho-cli/src/honcho_cli/commands/workspace.py +++ b/honcho-cli/src/honcho_cli/commands/workspace.py @@ -53,7 +53,7 @@ def list_workspaces( from honcho_cli.main import get_client handle_cmd_flags(json_output=json_output) - client, config = get_client() + client, config = get_client(require_workspace=False) try: workspaces = list(client.workspaces()) diff --git a/honcho-cli/src/honcho_cli/main.py b/honcho-cli/src/honcho_cli/main.py index 687c714b..bf70f560 100644 --- a/honcho-cli/src/honcho_cli/main.py +++ b/honcho-cli/src/honcho_cli/main.py @@ -89,13 +89,28 @@ def get_resolved_config(): return config -def get_client(): - """Create a Honcho client from resolved config.""" +def get_client(*, require_workspace: bool = True): + """Create a Honcho client from resolved config. + + By default, refuses to build a client when no workspace is scoped — the + SDK's get-or-create semantics would otherwise silently operate on an empty + workspace. Commands that legitimately run without a workspace (e.g. + ``workspace list``) pass ``require_workspace=False``. + """ + import typer + from honcho import Honcho from honcho_cli.config import get_client_kwargs + from honcho_cli.output import print_error config = get_resolved_config() + if require_workspace and not config.workspace_id: + print_error( + "NO_WORKSPACE", + "No workspace scoped. Pass --workspace/-w or set HONCHO_WORKSPACE_ID.", + ) + raise typer.Exit(1) return Honcho(**get_client_kwargs(config)), config