langstage-cli 0.6.53__tar.gz → 0.6.54__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {langstage_cli-0.6.53/langstage_cli.egg-info → langstage_cli-0.6.54}/PKG-INFO +5 -3
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/README.md +4 -2
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/langstage_cli/cli.py +94 -8
- {langstage_cli-0.6.53 → langstage_cli-0.6.54/langstage_cli.egg-info}/PKG-INFO +5 -3
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/langstage_cli.egg-info/SOURCES.txt +1 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/pyproject.toml +1 -1
- langstage_cli-0.6.54/tests/test_quiet_tool_result_204.py +344 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/LICENSE +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/deepagent_code/__init__.py +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/langstage_cli/__init__.py +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/langstage_cli/agui_stream.py +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/langstage_cli/config.py +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/langstage_cli/exit_codes.py +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/langstage_cli/sessions.py +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/langstage_cli.egg-info/dependency_links.txt +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/langstage_cli.egg-info/entry_points.txt +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/langstage_cli.egg-info/requires.txt +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/langstage_cli.egg-info/top_level.txt +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/setup.cfg +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/tests/test_advertised_wave4.py +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/tests/test_agui_stream.py +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/tests/test_approval_menu_abort.py +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/tests/test_async_mode.py +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/tests/test_checkpointer.py +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/tests/test_cli.py +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/tests/test_cli_help.py +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/tests/test_codeconfig.py +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/tests/test_config.py +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/tests/test_config_command.py +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/tests/test_configurable.py +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/tests/test_ctrl_c_sync_tool.py +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/tests/test_custom_decision_195.py +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/tests/test_deferred_pass.py +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/tests/test_dumb_term.py +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/tests/test_exit_codes.py +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/tests/test_generic_interrupt.py +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/tests/test_help_render.py +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/tests/test_hidden_option_suggestions.py +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/tests/test_history_persist.py +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/tests/test_hitl_approval_preview.py +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/tests/test_hitl_resume_warning.py +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/tests/test_init.py +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/tests/test_interrupt_non_tty.py +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/tests/test_legacy_config_home.py +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/tests/test_load_error.py +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/tests/test_menu_allowed_197.py +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/tests/test_message_boundaries.py +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/tests/test_no_color.py +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/tests/test_no_interactive_approve.py +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/tests/test_no_persist_resume.py +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/tests/test_paused_session_message.py +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/tests/test_persist_env.py +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/tests/test_quiet_output.py +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/tests/test_reasoning_render.py +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/tests/test_rename_shim.py +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/tests/test_render_markdown.py +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/tests/test_routine_1024.py +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/tests/test_session_lifecycle.py +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/tests/test_sessions.py +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/tests/test_show_config.py +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/tests/test_show_config_malformed.py +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/tests/test_spec_loading_core.py +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/tests/test_spec_resolution.py +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/tests/test_spinner_reply_survives.py +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/tests/test_stdin_interactive.py +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/tests/test_stdin_single_shot.py +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/tests/test_stream_mode.py +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/tests/test_streaming_marker.py +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/tests/test_terminal_width.py +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/tests/test_toml_relative_paths.py +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/tests/test_tool_card_args.py +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/tests/test_tool_output_escape.py +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/tests/test_turn_error_verbose.py +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/tests/test_turn_exit_and_render.py +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/tests/test_unicode_console.py +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/tests/test_usage_hint_186.py +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/tests/test_verbose_command.py +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/tests/test_verify_flag.py +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/tests/test_workspace.py +0 -0
- {langstage_cli-0.6.53 → langstage_cli-0.6.54}/tests/test_wrong_type_graph.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: langstage-cli
|
|
3
|
-
Version: 0.6.
|
|
3
|
+
Version: 0.6.54
|
|
4
4
|
Summary: The terminal stage for your LangGraph agent — Claude Code-style CLI for any CompiledGraph
|
|
5
5
|
Author-email: Kedar Dabhadkar <kdabhadk@gmail.com>
|
|
6
6
|
License-Expression: MIT
|
|
@@ -309,8 +309,10 @@ langstage-cli --verify -a my_agent.py:graph || { echo "agent broken" >&2; exit 1
|
|
|
309
309
|
A single-shot run (a `MESSAGE` argument or `-f/--file`) prints only the agent's
|
|
310
310
|
reply — no header, spinner, tool chatter, reasoning, timing, or color — as soon as its
|
|
311
311
|
output is **piped** (stdout isn't a TTY). Errors and diagnostics go to stderr, and so does
|
|
312
|
-
anything your agent module `print`s while it is being imported.
|
|
313
|
-
|
|
312
|
+
anything your agent module `print`s while it is being imported. When a turn ends on a
|
|
313
|
+
tool result with no assistant text after it (a `return_direct` tool), that result is the
|
|
314
|
+
reply. The process exits non-zero if the turn failed or left nothing to print, so a run
|
|
315
|
+
is safe to capture:
|
|
314
316
|
|
|
315
317
|
```bash
|
|
316
318
|
answer=$(langstage-cli --demo "say hi") || echo "run failed" >&2
|
|
@@ -271,8 +271,10 @@ langstage-cli --verify -a my_agent.py:graph || { echo "agent broken" >&2; exit 1
|
|
|
271
271
|
A single-shot run (a `MESSAGE` argument or `-f/--file`) prints only the agent's
|
|
272
272
|
reply — no header, spinner, tool chatter, reasoning, timing, or color — as soon as its
|
|
273
273
|
output is **piped** (stdout isn't a TTY). Errors and diagnostics go to stderr, and so does
|
|
274
|
-
anything your agent module `print`s while it is being imported.
|
|
275
|
-
|
|
274
|
+
anything your agent module `print`s while it is being imported. When a turn ends on a
|
|
275
|
+
tool result with no assistant text after it (a `return_direct` tool), that result is the
|
|
276
|
+
reply. The process exits non-zero if the turn failed or left nothing to print, so a run
|
|
277
|
+
is safe to capture:
|
|
276
278
|
|
|
277
279
|
```bash
|
|
278
280
|
answer=$(langstage-cli --demo "say hi") || echo "run failed" >&2
|
|
@@ -2529,7 +2529,7 @@ async def run_single_turn_agui(
|
|
|
2529
2529
|
f"{YELLOW}⏺ This session is paused at an approval from its previous turn; {note}{RESET}"
|
|
2530
2530
|
)
|
|
2531
2531
|
try:
|
|
2532
|
-
had_error, produced, saw_interrupt = await _stream_turn(
|
|
2532
|
+
had_error, produced, saw_interrupt, tool_results = await _stream_turn(
|
|
2533
2533
|
agent, message, thread_id, interactive, verbose
|
|
2534
2534
|
)
|
|
2535
2535
|
except SystemExit:
|
|
@@ -2538,12 +2538,21 @@ async def run_single_turn_agui(
|
|
|
2538
2538
|
if saw_interrupt: # the pass only answered the old approval
|
|
2539
2539
|
if produced:
|
|
2540
2540
|
print() # the previous turn's reply ends; the new message's begins
|
|
2541
|
-
more_error,
|
|
2541
|
+
more_error, more_produced, _, tool_results = await _stream_turn(
|
|
2542
2542
|
agent, message, thread_id, interactive, verbose
|
|
2543
2543
|
)
|
|
2544
2544
|
had_error = had_error or more_error
|
|
2545
|
+
# Quiet mode: the first pass's reply already reached stdout, so the run
|
|
2546
|
+
# as a whole printed something even if the new message's turn did not.
|
|
2547
|
+
produced = more_produced or (_QUIET and produced)
|
|
2545
2548
|
else:
|
|
2546
|
-
had_error, produced, _ = await _stream_turn(
|
|
2549
|
+
had_error, produced, _, tool_results = await _stream_turn(
|
|
2550
|
+
agent, message, thread_id, interactive, verbose
|
|
2551
|
+
)
|
|
2552
|
+
if _QUIET and not had_error and not produced:
|
|
2553
|
+
# Nothing reached stdout. A turn that ends on a tool result (a
|
|
2554
|
+
# `return_direct` tool) has its answer in that result: print it (gh #204).
|
|
2555
|
+
produced = await _print_final_tool_results(agent, thread_id, tool_results)
|
|
2547
2556
|
if not had_error and not produced:
|
|
2548
2557
|
had_error = not await _render_empty_turn(agent, thread_id)
|
|
2549
2558
|
return time.time() - start_time, had_error
|
|
@@ -2568,7 +2577,17 @@ async def _stream_turn(
|
|
|
2568
2577
|
) -> tuple[bool, bool, bool]:
|
|
2569
2578
|
"""One message through the adapter, answering interrupts until the run ends.
|
|
2570
2579
|
|
|
2571
|
-
Returns ``(had_error, produced, saw_interrupt)``.
|
|
2580
|
+
Returns ``(had_error, produced, saw_interrupt, tool_results)``.
|
|
2581
|
+
|
|
2582
|
+
``produced`` is whether the turn showed the user anything. In a terminal that is
|
|
2583
|
+
any text, reasoning, tool frame or interrupt (gh #120). In quiet mode only reply
|
|
2584
|
+
text reaches stdout, so only reply text counts: tool frames, reasoning and the
|
|
2585
|
+
approval banner are all dropped there, and counting them let a turn print nothing
|
|
2586
|
+
and exit 0 (gh #204).
|
|
2587
|
+
|
|
2588
|
+
``tool_results`` is the text of the tool results the turn ended on: those that
|
|
2589
|
+
arrived after the last reply text and the last tool call. A `return_direct` tool
|
|
2590
|
+
leaves its answer there, with no assistant text after it.
|
|
2572
2591
|
"""
|
|
2573
2592
|
from langstage_cli.agui_stream import agui_stream_updates
|
|
2574
2593
|
|
|
@@ -2576,8 +2595,9 @@ async def _stream_turn(
|
|
|
2576
2595
|
print_chunk._streaming_reasoning = False
|
|
2577
2596
|
_md_stream.reset()
|
|
2578
2597
|
had_error = False
|
|
2579
|
-
produced = False #
|
|
2598
|
+
produced = False # see the docstring: what counts depends on quiet mode
|
|
2580
2599
|
saw_interrupt = False
|
|
2600
|
+
tool_results: List[str] = [] # results since the last reply text / tool call
|
|
2581
2601
|
resume = None # first pass sends the message; later passes carry the decision
|
|
2582
2602
|
|
|
2583
2603
|
while True:
|
|
@@ -2600,10 +2620,17 @@ async def _stream_turn(
|
|
|
2600
2620
|
print_chunk(chunk, verbose=verbose)
|
|
2601
2621
|
if chunk.get("status") == "error":
|
|
2602
2622
|
had_error = True
|
|
2603
|
-
if
|
|
2623
|
+
if _QUIET:
|
|
2624
|
+
if chunk.get("chunk"):
|
|
2625
|
+
produced = True
|
|
2626
|
+
elif chunk.get("status") == "interrupt" or any(
|
|
2604
2627
|
chunk.get(k) for k in _CONTENT_KEYS
|
|
2605
2628
|
):
|
|
2606
2629
|
produced = True
|
|
2630
|
+
if chunk.get("chunk") or chunk.get("tool_calls"):
|
|
2631
|
+
tool_results = [] # the agent went on: those weren't the answer
|
|
2632
|
+
elif "tool_result" in chunk:
|
|
2633
|
+
tool_results.append(_tool_result_text(chunk.get("tool_result")))
|
|
2607
2634
|
if chunk.get("status") == "interrupt":
|
|
2608
2635
|
has_interrupt = saw_interrupt = True
|
|
2609
2636
|
interrupt_data = chunk.get("interrupt", {})
|
|
@@ -2656,13 +2683,72 @@ async def _stream_turn(
|
|
|
2656
2683
|
else:
|
|
2657
2684
|
break
|
|
2658
2685
|
|
|
2659
|
-
return had_error, produced, saw_interrupt
|
|
2686
|
+
return had_error, produced, saw_interrupt, tool_results
|
|
2660
2687
|
|
|
2661
2688
|
|
|
2662
|
-
# Chunk keys that carry turn output a user sees (text, reasoning, tool
|
|
2689
|
+
# Chunk keys that carry turn output a user sees in a terminal (text, reasoning, tool
|
|
2690
|
+
# frames). Quiet mode prints only the text, so there only "chunk" counts (gh #204).
|
|
2663
2691
|
_CONTENT_KEYS = ("chunk", "reasoning", "tool_calls", "tool_result", "extraction")
|
|
2664
2692
|
|
|
2665
2693
|
|
|
2694
|
+
def _tool_result_text(result: Any) -> str:
|
|
2695
|
+
"""A tool result as the text a script should capture."""
|
|
2696
|
+
if result is None:
|
|
2697
|
+
return ""
|
|
2698
|
+
if isinstance(result, str):
|
|
2699
|
+
return result
|
|
2700
|
+
if isinstance(result, (dict, list)):
|
|
2701
|
+
try:
|
|
2702
|
+
return json.dumps(result, default=str, ensure_ascii=False)
|
|
2703
|
+
except (TypeError, ValueError):
|
|
2704
|
+
return str(result)
|
|
2705
|
+
return str(result)
|
|
2706
|
+
|
|
2707
|
+
|
|
2708
|
+
async def _ends_on_assistant_message(agent: Any, thread_id: str) -> bool:
|
|
2709
|
+
"""Whether the thread's last message is the assistant's (so not a tool result).
|
|
2710
|
+
|
|
2711
|
+
Frames can't tell a `return_direct` turn from one where the model read the tool
|
|
2712
|
+
result and then wrote an empty message: both stream a tool call, its result and
|
|
2713
|
+
nothing else. The saved state can. ``False`` when there is no readable state.
|
|
2714
|
+
"""
|
|
2715
|
+
graph = getattr(agent, "graph", None)
|
|
2716
|
+
if not thread_id or graph is None or not hasattr(graph, "aget_state"):
|
|
2717
|
+
return False
|
|
2718
|
+
try:
|
|
2719
|
+
state = await graph.aget_state({"configurable": {"thread_id": thread_id}})
|
|
2720
|
+
except Exception: # noqa: BLE001 - no checkpointer / no state: go by the frames
|
|
2721
|
+
return False
|
|
2722
|
+
values = getattr(state, "values", None)
|
|
2723
|
+
messages = values.get("messages") if isinstance(values, dict) else None
|
|
2724
|
+
if not isinstance(messages, (list, tuple)) or not messages:
|
|
2725
|
+
return False
|
|
2726
|
+
last = messages[-1]
|
|
2727
|
+
if isinstance(last, dict):
|
|
2728
|
+
kind = last.get("type") or last.get("role")
|
|
2729
|
+
else:
|
|
2730
|
+
kind = getattr(last, "type", None)
|
|
2731
|
+
return kind in ("ai", "assistant", "AIMessageChunk")
|
|
2732
|
+
|
|
2733
|
+
|
|
2734
|
+
async def _print_final_tool_results(agent: Any, thread_id: str, results: List[str]) -> bool:
|
|
2735
|
+
"""Quiet mode: print the tool results a turn ended on as its reply (gh #204).
|
|
2736
|
+
|
|
2737
|
+
A `return_direct` tool's result *is* the agent's answer, and no assistant text
|
|
2738
|
+
follows it. Quiet mode dropped it as tool chatter, so ``answer=$(langstage-cli …)``
|
|
2739
|
+
captured an empty string with exit 0. Each result the turn ended on is printed
|
|
2740
|
+
as-is, one per line when several tools ran in the last step, the way two messages
|
|
2741
|
+
in one turn are separated. Returns ``False``, printing nothing, when there is no
|
|
2742
|
+
result text or the model had the last word and left it empty: that turn produced
|
|
2743
|
+
nothing, and the caller reports it.
|
|
2744
|
+
"""
|
|
2745
|
+
texts = [text for text in results if text]
|
|
2746
|
+
if not texts or await _ends_on_assistant_message(agent, thread_id):
|
|
2747
|
+
return False
|
|
2748
|
+
safe_write("\n".join(texts), flush=True)
|
|
2749
|
+
return True
|
|
2750
|
+
|
|
2751
|
+
|
|
2666
2752
|
async def _render_empty_turn(agent: Any, thread_id: str) -> bool:
|
|
2667
2753
|
"""A turn completed with no message: show the graph's final state, or fail clearly.
|
|
2668
2754
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: langstage-cli
|
|
3
|
-
Version: 0.6.
|
|
3
|
+
Version: 0.6.54
|
|
4
4
|
Summary: The terminal stage for your LangGraph agent — Claude Code-style CLI for any CompiledGraph
|
|
5
5
|
Author-email: Kedar Dabhadkar <kdabhadk@gmail.com>
|
|
6
6
|
License-Expression: MIT
|
|
@@ -309,8 +309,10 @@ langstage-cli --verify -a my_agent.py:graph || { echo "agent broken" >&2; exit 1
|
|
|
309
309
|
A single-shot run (a `MESSAGE` argument or `-f/--file`) prints only the agent's
|
|
310
310
|
reply — no header, spinner, tool chatter, reasoning, timing, or color — as soon as its
|
|
311
311
|
output is **piped** (stdout isn't a TTY). Errors and diagnostics go to stderr, and so does
|
|
312
|
-
anything your agent module `print`s while it is being imported.
|
|
313
|
-
|
|
312
|
+
anything your agent module `print`s while it is being imported. When a turn ends on a
|
|
313
|
+
tool result with no assistant text after it (a `return_direct` tool), that result is the
|
|
314
|
+
reply. The process exits non-zero if the turn failed or left nothing to print, so a run
|
|
315
|
+
is safe to capture:
|
|
314
316
|
|
|
315
317
|
```bash
|
|
316
318
|
answer=$(langstage-cli --demo "say hi") || echo "run failed" >&2
|
|
@@ -48,6 +48,7 @@ tests/test_no_persist_resume.py
|
|
|
48
48
|
tests/test_paused_session_message.py
|
|
49
49
|
tests/test_persist_env.py
|
|
50
50
|
tests/test_quiet_output.py
|
|
51
|
+
tests/test_quiet_tool_result_204.py
|
|
51
52
|
tests/test_reasoning_render.py
|
|
52
53
|
tests/test_rename_shim.py
|
|
53
54
|
tests/test_render_markdown.py
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "langstage-cli"
|
|
7
|
-
version = "0.6.
|
|
7
|
+
version = "0.6.54"
|
|
8
8
|
description = "The terminal stage for your LangGraph agent — Claude Code-style CLI for any CompiledGraph"
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
requires-python = ">=3.11"
|
|
@@ -0,0 +1,344 @@
|
|
|
1
|
+
"""A quiet single-shot run never prints nothing and exits 0 (gh #204).
|
|
2
|
+
|
|
3
|
+
A turn that ends on a tool result, the shape a ``@tool(return_direct=True)`` agent
|
|
4
|
+
produces, has its answer in that result: no assistant text follows it. Quiet mode
|
|
5
|
+
(piped stdout, or ``-q``) dropped tool frames as chatter, while the "did the turn
|
|
6
|
+
produce anything" check counted them, so ``answer=$(langstage-cli ...)`` captured an
|
|
7
|
+
empty string with exit 0 and nothing on stderr.
|
|
8
|
+
|
|
9
|
+
Quiet mode now prints the result(s) the turn ended on as the reply. When stdout would
|
|
10
|
+
still be empty (a tool call with no result, an empty result, a model that read the
|
|
11
|
+
result and wrote nothing, reasoning only) the run reports the empty turn and exits 1.
|
|
12
|
+
A terminal renders exactly as before.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
import asyncio
|
|
16
|
+
import contextlib
|
|
17
|
+
import io
|
|
18
|
+
import re
|
|
19
|
+
import textwrap
|
|
20
|
+
from pathlib import Path
|
|
21
|
+
|
|
22
|
+
import pytest
|
|
23
|
+
from click.testing import CliRunner
|
|
24
|
+
|
|
25
|
+
pytest.importorskip("ag_ui_langgraph")
|
|
26
|
+
|
|
27
|
+
from langstage_cli import agui_stream, cli # noqa: E402
|
|
28
|
+
from langstage_cli.agui_stream import build_session_agent # noqa: E402
|
|
29
|
+
from langstage_cli.cli import main # noqa: E402
|
|
30
|
+
|
|
31
|
+
_HEAD = textwrap.dedent(
|
|
32
|
+
"""
|
|
33
|
+
from langgraph.graph import StateGraph, START, END
|
|
34
|
+
from langgraph.graph.message import MessagesState
|
|
35
|
+
from langchain_core.messages import AIMessage, ToolMessage
|
|
36
|
+
|
|
37
|
+
def _chain(*steps):
|
|
38
|
+
g = StateGraph(MessagesState)
|
|
39
|
+
prev = START
|
|
40
|
+
for i, step in enumerate(steps):
|
|
41
|
+
g.add_node(f"n{i}", step)
|
|
42
|
+
g.add_edge(prev, f"n{i}")
|
|
43
|
+
prev = f"n{i}"
|
|
44
|
+
g.add_edge(prev, END)
|
|
45
|
+
return g.compile()
|
|
46
|
+
|
|
47
|
+
def call(*calls, text=""):
|
|
48
|
+
def step(state):
|
|
49
|
+
return {"messages": [AIMessage(content=text, tool_calls=[
|
|
50
|
+
{"name": name, "args": {"q": "x"}, "id": cid} for name, cid in calls])]}
|
|
51
|
+
return step
|
|
52
|
+
|
|
53
|
+
def result(*results):
|
|
54
|
+
def step(state):
|
|
55
|
+
return {"messages": [ToolMessage(content=text, tool_call_id=cid, name="lookup")
|
|
56
|
+
for text, cid in results]}
|
|
57
|
+
return step
|
|
58
|
+
|
|
59
|
+
def say(text):
|
|
60
|
+
def step(state):
|
|
61
|
+
return {"messages": [AIMessage(content=text)]}
|
|
62
|
+
return step
|
|
63
|
+
"""
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def _agent(body: str) -> str:
|
|
68
|
+
return _HEAD + textwrap.dedent(body)
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
# The `return_direct` shape: a tool call, its result, and the graph ends.
|
|
72
|
+
_RETURN_DIRECT = _agent(
|
|
73
|
+
"""
|
|
74
|
+
graph = _chain(call(("lookup", "c1")), result(("ANSWER for x", "c1")))
|
|
75
|
+
"""
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
# The usual shape: the model reads the result and replies.
|
|
79
|
+
_TOOL_THEN_TEXT = _agent(
|
|
80
|
+
"""
|
|
81
|
+
graph = _chain(call(("lookup", "c1")), result(("RAW TOOL OUTPUT", "c1")),
|
|
82
|
+
say("The answer is 42."))
|
|
83
|
+
"""
|
|
84
|
+
)
|
|
85
|
+
|
|
86
|
+
# A tool call nobody ran: no result, no reply.
|
|
87
|
+
_CALL_ONLY = _agent(
|
|
88
|
+
"""
|
|
89
|
+
graph = _chain(call(("lookup", "c1")))
|
|
90
|
+
"""
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
# Two tools in the last step, both returned directly.
|
|
94
|
+
_TWO_RESULTS = _agent(
|
|
95
|
+
"""
|
|
96
|
+
graph = _chain(call(("lookup", "c1"), ("lookup", "c2")),
|
|
97
|
+
result(("first answer", "c1"), ("second answer", "c2")))
|
|
98
|
+
"""
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
# An earlier tool round the model read, then a tool whose result ends the turn.
|
|
102
|
+
_TWO_ROUNDS = _agent(
|
|
103
|
+
"""
|
|
104
|
+
graph = _chain(call(("search", "c1")), result(("intermediate notes", "c1")),
|
|
105
|
+
call(("lookup", "c2")), result(("FINAL ANSWER", "c2")))
|
|
106
|
+
"""
|
|
107
|
+
)
|
|
108
|
+
|
|
109
|
+
# The model read the result and wrote an empty message: it had the last word.
|
|
110
|
+
_TOOL_THEN_EMPTY = _agent(
|
|
111
|
+
"""
|
|
112
|
+
graph = _chain(call(("lookup", "c1")), result(("RAW TOOL OUTPUT", "c1")), say(""))
|
|
113
|
+
"""
|
|
114
|
+
)
|
|
115
|
+
|
|
116
|
+
# A tool that returned nothing.
|
|
117
|
+
_EMPTY_RESULT = _agent(
|
|
118
|
+
"""
|
|
119
|
+
graph = _chain(call(("lookup", "c1")), result(("", "c1")))
|
|
120
|
+
"""
|
|
121
|
+
)
|
|
122
|
+
|
|
123
|
+
# A preamble before the tool call, then the result ends the turn.
|
|
124
|
+
_PREAMBLE_THEN_RESULT = _agent(
|
|
125
|
+
"""
|
|
126
|
+
graph = _chain(call(("lookup", "c1"), text="Looking that up."),
|
|
127
|
+
result(("ANSWER for x", "c1")))
|
|
128
|
+
"""
|
|
129
|
+
)
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def _ws(tmp_path, monkeypatch, source: str) -> Path:
|
|
133
|
+
ws = tmp_path / "ws"
|
|
134
|
+
ws.mkdir()
|
|
135
|
+
(ws / "agent_204.py").write_text(source, encoding="utf-8")
|
|
136
|
+
monkeypatch.setenv("LANGSTAGE_CONFIG_HOME", str(tmp_path / "empty_home"))
|
|
137
|
+
monkeypatch.chdir(ws)
|
|
138
|
+
return ws
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def _run(tmp_path, monkeypatch, source: str, *args: str, **kwargs):
|
|
142
|
+
_ws(tmp_path, monkeypatch, source)
|
|
143
|
+
return CliRunner().invoke(main, [*args, "-a", "agent_204.py:graph", "find x"], **kwargs)
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
# --- the turn ends on a tool result -------------------------------------------------
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
def test_piped_run_prints_the_final_tool_result(tmp_path, monkeypatch):
|
|
150
|
+
r = _run(tmp_path, monkeypatch, _RETURN_DIRECT)
|
|
151
|
+
assert r.exit_code == 0, r.output
|
|
152
|
+
assert r.stdout == "ANSWER for x\n", repr(r.stdout) # once, and nothing else
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
def test_quiet_flag_prints_the_final_tool_result(tmp_path, monkeypatch):
|
|
156
|
+
r = _run(tmp_path, monkeypatch, _RETURN_DIRECT, "-q")
|
|
157
|
+
assert r.exit_code == 0, r.output
|
|
158
|
+
assert r.stdout == "ANSWER for x\n", repr(r.stdout)
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def test_final_tool_result_without_persistence(tmp_path, monkeypatch):
|
|
162
|
+
r = _run(tmp_path, monkeypatch, _RETURN_DIRECT, "--no-persist")
|
|
163
|
+
assert r.exit_code == 0, r.output
|
|
164
|
+
assert r.stdout == "ANSWER for x\n", repr(r.stdout)
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
def test_piped_stdin_message_prints_the_final_tool_result(tmp_path, monkeypatch):
|
|
168
|
+
_ws(tmp_path, monkeypatch, _RETURN_DIRECT)
|
|
169
|
+
r = CliRunner().invoke(main, ["-a", "agent_204.py:graph"], input="find x\n")
|
|
170
|
+
assert r.exit_code == 0, r.output
|
|
171
|
+
assert r.stdout == "ANSWER for x\n", repr(r.stdout)
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
def test_real_return_direct_agent(tmp_path, monkeypatch):
|
|
175
|
+
"""The issue's own agent: LangChain's ``create_agent`` with a ``return_direct`` tool."""
|
|
176
|
+
pytest.importorskip("langchain.agents")
|
|
177
|
+
source = textwrap.dedent(
|
|
178
|
+
"""
|
|
179
|
+
from langchain_core.tools import tool
|
|
180
|
+
from langchain_core.language_models.fake_chat_models import FakeMessagesListChatModel
|
|
181
|
+
from langchain_core.messages import AIMessage
|
|
182
|
+
from langchain.agents import create_agent
|
|
183
|
+
|
|
184
|
+
@tool(return_direct=True)
|
|
185
|
+
def lookup(q: str) -> str:
|
|
186
|
+
\"\"\"Look up q.\"\"\"
|
|
187
|
+
return f"ANSWER for {q}"
|
|
188
|
+
|
|
189
|
+
class M(FakeMessagesListChatModel):
|
|
190
|
+
def bind_tools(self, tools, **kw):
|
|
191
|
+
return self
|
|
192
|
+
|
|
193
|
+
model = M(responses=[AIMessage(content="", tool_calls=[
|
|
194
|
+
{"name": "lookup", "args": {"q": "x"}, "id": "c1"}])])
|
|
195
|
+
graph = create_agent(model, tools=[lookup])
|
|
196
|
+
"""
|
|
197
|
+
)
|
|
198
|
+
try:
|
|
199
|
+
from langchain.agents import create_agent # noqa: F401
|
|
200
|
+
except ImportError:
|
|
201
|
+
pytest.skip("langchain.agents.create_agent not available")
|
|
202
|
+
r = _run(tmp_path, monkeypatch, source)
|
|
203
|
+
assert r.exit_code == 0, r.output
|
|
204
|
+
assert r.stdout == "ANSWER for x\n", repr(r.stdout)
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
def test_several_final_results_print_one_per_line(tmp_path, monkeypatch):
|
|
208
|
+
r = _run(tmp_path, monkeypatch, _TWO_RESULTS)
|
|
209
|
+
assert r.exit_code == 0, r.output
|
|
210
|
+
assert r.stdout == "first answer\nsecond answer\n", repr(r.stdout)
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
def test_only_the_last_tool_round_is_the_answer(tmp_path, monkeypatch):
|
|
214
|
+
r = _run(tmp_path, monkeypatch, _TWO_ROUNDS)
|
|
215
|
+
assert r.exit_code == 0, r.output
|
|
216
|
+
assert r.stdout == "FINAL ANSWER\n", repr(r.stdout)
|
|
217
|
+
assert "intermediate" not in r.stdout
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
# --- turns that already had a reply are unchanged -----------------------------------
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
def test_text_after_the_tool_result_is_still_the_only_output(tmp_path, monkeypatch):
|
|
224
|
+
r = _run(tmp_path, monkeypatch, _TOOL_THEN_TEXT)
|
|
225
|
+
assert r.exit_code == 0, r.output
|
|
226
|
+
assert r.stdout == "The answer is 42.\n", repr(r.stdout)
|
|
227
|
+
assert "RAW TOOL OUTPUT" not in r.output
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
def test_a_preamble_is_the_reply_and_the_result_stays_tool_chatter(tmp_path, monkeypatch):
|
|
231
|
+
# The turn already printed assistant text, so stdout isn't empty: quiet output is
|
|
232
|
+
# what it has always been for this turn.
|
|
233
|
+
r = _run(tmp_path, monkeypatch, _PREAMBLE_THEN_RESULT)
|
|
234
|
+
assert r.exit_code == 0, r.output
|
|
235
|
+
assert r.stdout == "Looking that up.\n", repr(r.stdout)
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
# --- nothing to print is a failure, never a silent success --------------------------
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
@pytest.mark.parametrize(
|
|
242
|
+
"source",
|
|
243
|
+
[_CALL_ONLY, _TOOL_THEN_EMPTY, _EMPTY_RESULT],
|
|
244
|
+
ids=["tool-call-no-result", "empty-message-after-result", "empty-result"],
|
|
245
|
+
)
|
|
246
|
+
def test_nothing_on_stdout_exits_1_with_a_diagnostic(tmp_path, monkeypatch, source):
|
|
247
|
+
r = _run(tmp_path, monkeypatch, source)
|
|
248
|
+
assert r.exit_code == 1, r.output
|
|
249
|
+
assert r.stdout.strip() == "", repr(r.stdout)
|
|
250
|
+
assert "RAW TOOL OUTPUT" not in r.output # the model's empty last word isn't replaced
|
|
251
|
+
assert "produced no output" in r.stderr, r.stderr
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
def _stream_of(*chunks):
|
|
255
|
+
async def _fake(agent, message, thread_id, resume=None):
|
|
256
|
+
for chunk in chunks:
|
|
257
|
+
yield chunk
|
|
258
|
+
|
|
259
|
+
return _fake
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
def _quiet_turn(monkeypatch, *chunks) -> tuple[bool, str, str]:
|
|
263
|
+
"""One quiet turn over canned frames, with an agent that has no readable state."""
|
|
264
|
+
monkeypatch.setattr(agui_stream, "agui_stream_updates", _stream_of(*chunks))
|
|
265
|
+
monkeypatch.setattr(cli, "_QUIET", True)
|
|
266
|
+
out, err = io.StringIO(), io.StringIO()
|
|
267
|
+
with contextlib.redirect_stdout(out), contextlib.redirect_stderr(err):
|
|
268
|
+
_elapsed, had_error = asyncio.run(cli.run_single_turn_agui(object(), "hi", "t-204", False))
|
|
269
|
+
return had_error, out.getvalue(), err.getvalue()
|
|
270
|
+
|
|
271
|
+
|
|
272
|
+
def test_reasoning_only_turn_is_an_empty_turn_in_quiet_mode(monkeypatch):
|
|
273
|
+
had_error, out, err = _quiet_turn(
|
|
274
|
+
monkeypatch,
|
|
275
|
+
{"status": "streaming", "reasoning": "thinking it over", "node": "model"},
|
|
276
|
+
{"status": "complete"},
|
|
277
|
+
)
|
|
278
|
+
assert had_error and out == "", (had_error, out)
|
|
279
|
+
assert "produced no output" in err, err
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
def test_frames_alone_decide_when_there_is_no_state_to_read(monkeypatch):
|
|
283
|
+
had_error, out, _err = _quiet_turn(
|
|
284
|
+
monkeypatch,
|
|
285
|
+
{"status": "streaming", "tool_calls": [{"name": "lookup", "args": {}, "id": "c1"}]},
|
|
286
|
+
{"status": "streaming", "tool_result": "ANSWER for x", "id": "c1", "name": "lookup"},
|
|
287
|
+
{"status": "complete"},
|
|
288
|
+
)
|
|
289
|
+
assert not had_error and out == "ANSWER for x", (had_error, out)
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
def test_structured_tool_result_prints_as_json(monkeypatch):
|
|
293
|
+
had_error, out, _err = _quiet_turn(
|
|
294
|
+
monkeypatch,
|
|
295
|
+
{"status": "streaming", "tool_calls": [{"name": "lookup", "args": {}, "id": "c1"}]},
|
|
296
|
+
{"status": "streaming", "tool_result": {"city": "Zürich", "temp": 24}, "id": "c1"},
|
|
297
|
+
{"status": "complete"},
|
|
298
|
+
)
|
|
299
|
+
assert not had_error, out
|
|
300
|
+
assert out == '{"city": "Zürich", "temp": 24}', repr(out)
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
def test_an_errored_turn_does_not_print_a_tool_result_as_the_reply(monkeypatch):
|
|
304
|
+
had_error, out, err = _quiet_turn(
|
|
305
|
+
monkeypatch,
|
|
306
|
+
{"status": "streaming", "tool_calls": [{"name": "lookup", "args": {}, "id": "c1"}]},
|
|
307
|
+
{"status": "streaming", "tool_result": "partial", "id": "c1", "name": "lookup"},
|
|
308
|
+
{"status": "error", "error": "boom"},
|
|
309
|
+
)
|
|
310
|
+
assert had_error and out == "", (had_error, out)
|
|
311
|
+
assert "boom" in err, err
|
|
312
|
+
|
|
313
|
+
|
|
314
|
+
# --- a terminal renders exactly as before -------------------------------------------
|
|
315
|
+
|
|
316
|
+
_ANSI = re.compile(r"\x1b\[[0-9;]*m")
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
def _tty_turn(source: str, monkeypatch) -> tuple[bool, str]:
|
|
320
|
+
ns: dict = {}
|
|
321
|
+
exec(source, ns)
|
|
322
|
+
agent = build_session_agent(ns["graph"])
|
|
323
|
+
monkeypatch.setattr(cli, "Spinner", lambda *a, **k: None)
|
|
324
|
+
assert cli._QUIET is False
|
|
325
|
+
buf = io.StringIO()
|
|
326
|
+
with contextlib.redirect_stdout(buf):
|
|
327
|
+
_elapsed, had_error = asyncio.run(
|
|
328
|
+
cli.run_single_turn_agui(agent, "find x", "t-204-tty", interactive=False)
|
|
329
|
+
)
|
|
330
|
+
return had_error, _ANSI.sub("", buf.getvalue())
|
|
331
|
+
|
|
332
|
+
|
|
333
|
+
def test_terminal_shows_the_tool_card_once_and_no_extra_reply(monkeypatch):
|
|
334
|
+
had_error, out = _tty_turn(_RETURN_DIRECT, monkeypatch)
|
|
335
|
+
assert not had_error
|
|
336
|
+
assert "● lookup" in out and "↳ ANSWER for x" in out, out
|
|
337
|
+
assert out.count("ANSWER for x") == 1, out # the card only: no second, bare copy
|
|
338
|
+
|
|
339
|
+
|
|
340
|
+
def test_terminal_tool_call_without_a_result_still_renders_and_succeeds(monkeypatch):
|
|
341
|
+
# Unchanged: in a terminal the tool card is output the user sees.
|
|
342
|
+
had_error, out = _tty_turn(_CALL_ONLY, monkeypatch)
|
|
343
|
+
assert not had_error
|
|
344
|
+
assert "● lookup" in out, out
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|