|
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
5 | | -from typing import Optional |
| 5 | +from typing import Callable, Optional |
6 | 6 | from typing_extensions import override |
7 | 7 |
|
8 | 8 | from .._client import Runloop |
| 9 | +from .._streaming import Stream |
| 10 | +from ..types.devboxes.execution_update_chunk import ExecutionUpdateChunk |
9 | 11 | from ..types.devbox_async_execution_detail_view import DevboxAsyncExecutionDetailView |
10 | 12 |
|
11 | 13 |
|
@@ -56,35 +58,79 @@ def failed(self) -> bool: |
56 | 58 | exit_code = self.exit_code |
57 | 59 | return exit_code is not None and exit_code != 0 |
58 | 60 |
|
59 | | - # TODO: add pagination support once we have it in the API |
| 61 | + def _count_non_empty_lines(self, text: str) -> int: |
| 62 | + """Count non-empty lines in text, excluding trailing empty strings.""" |
| 63 | + if not text: |
| 64 | + return 0 |
| 65 | + # Remove trailing newlines, split, and count non-empty lines |
| 66 | + return sum(1 for line in text.rstrip("\n").split("\n") if line) |
| 67 | + |
| 68 | + def _get_last_n_lines(self, text: str, n: int) -> str: |
| 69 | + """Extract the last N lines from text.""" |
| 70 | + if n <= 0 or not text: |
| 71 | + return "" |
| 72 | + # Remove trailing newlines before splitting and slicing |
| 73 | + return "\n".join(text.rstrip("\n").split("\n")[-n:]) |
| 74 | + |
| 75 | + def _get_output( |
| 76 | + self, |
| 77 | + current_output: str, |
| 78 | + is_truncated: bool, |
| 79 | + num_lines: Optional[int], |
| 80 | + stream_fn: Callable[[], Stream[ExecutionUpdateChunk]], |
| 81 | + ) -> str: |
| 82 | + """Common logic for getting output with optional line limiting and streaming.""" |
| 83 | + # Check if we have enough lines already |
| 84 | + if num_lines is not None and (not is_truncated or self._count_non_empty_lines(current_output) >= num_lines): |
| 85 | + return self._get_last_n_lines(current_output, num_lines) |
| 86 | + |
| 87 | + # Stream full output if truncated |
| 88 | + if is_truncated: |
| 89 | + output = "".join(chunk.output for chunk in stream_fn()) |
| 90 | + return self._get_last_n_lines(output, num_lines) if num_lines is not None else output |
| 91 | + |
| 92 | + # Return current output, optionally limited to last N lines |
| 93 | + return self._get_last_n_lines(current_output, num_lines) if num_lines is not None else current_output |
| 94 | + |
60 | 95 | def stdout(self, num_lines: Optional[int] = None) -> str: |
61 | | - """Return captured standard output.""" |
62 | | - text = self._result.stdout or "" |
63 | | - return _tail_lines(text, num_lines) |
| 96 | + """ |
| 97 | + Return captured standard output, streaming full output if truncated. |
| 98 | +
|
| 99 | + Args: |
| 100 | + num_lines: Optional number of lines to return from the end (most recent) |
| 101 | +
|
| 102 | + Returns: |
| 103 | + stdout content, optionally limited to last N lines |
| 104 | + """ |
| 105 | + return self._get_output( |
| 106 | + self._result.stdout or "", |
| 107 | + self._result.stdout_truncated is True, |
| 108 | + num_lines, |
| 109 | + lambda: self._client.devboxes.executions.stream_stdout_updates( |
| 110 | + self.execution_id, devbox_id=self._devbox_id |
| 111 | + ), |
| 112 | + ) |
64 | 113 |
|
65 | | - # TODO: add pagination support once we have it in the API |
66 | 114 | def stderr(self, num_lines: Optional[int] = None) -> str: |
67 | | - """Return captured standard error.""" |
68 | | - text = self._result.stderr or "" |
69 | | - return _tail_lines(text, num_lines) |
| 115 | + """ |
| 116 | + Return captured standard error, streaming full output if truncated. |
| 117 | +
|
| 118 | + Args: |
| 119 | + num_lines: Optional number of lines to return from the end (most recent) |
| 120 | +
|
| 121 | + Returns: |
| 122 | + stderr content, optionally limited to last N lines |
| 123 | + """ |
| 124 | + return self._get_output( |
| 125 | + self._result.stderr or "", |
| 126 | + self._result.stderr_truncated is True, |
| 127 | + num_lines, |
| 128 | + lambda: self._client.devboxes.executions.stream_stderr_updates( |
| 129 | + self.execution_id, devbox_id=self._devbox_id |
| 130 | + ), |
| 131 | + ) |
70 | 132 |
|
71 | 133 | @property |
72 | 134 | def raw(self) -> DevboxAsyncExecutionDetailView: |
73 | 135 | """Access the underlying API response.""" |
74 | 136 | return self._result |
75 | | - |
76 | | - |
77 | | -def _tail_lines(text: str, num_lines: Optional[int]) -> str: |
78 | | - if not text: |
79 | | - return "" |
80 | | - if num_lines is None or num_lines <= 0: |
81 | | - return text |
82 | | - |
83 | | - lines = text.splitlines() |
84 | | - if not lines: |
85 | | - return text |
86 | | - |
87 | | - clipped = "\n".join(lines[-num_lines:]) |
88 | | - if text.endswith("\n"): |
89 | | - clipped += "\n" |
90 | | - return clipped |
|
0 commit comments