Skip to content

Commit 7f8ede1

Browse files
committed
Add streaming response support
1 parent 8d3aa25 commit 7f8ede1

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[project]
22
name = "autocmd-cli"
3-
version = "0.1.3"
4-
description = "Natural language to shell command translator using Claude"
3+
version = "0.1.4"
4+
description = "Natural language to shell command."
55
readme = "README.md"
66
requires-python = ">=3.8"
77
license = {text = "MIT"}

src/autocmd_cli/__init__.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -120,16 +120,30 @@ def main() -> None:
120120

121121
try:
122122
client = Anthropic(api_key=api_key)
123-
response = client.messages.create(
123+
with client.messages.stream(
124124
model=os.environ.get("AUTOCMD_MODEL", DEFAULT_MODEL),
125125
max_tokens=200,
126126
messages=[{"role": "user", "content": f"Convert to {os.environ.get('SHELL', 'bash')} command (no markdown): {' '.join(sys.argv[1:])}"}]
127-
)
128-
cmd = re.sub(r'^```\w*\n?|```$', '', response.content[0].text).strip()
129-
if not cmd:
130-
print("No command generated", file=sys.stderr)
131-
sys.exit(1)
132-
print(cmd)
127+
) as stream:
128+
full_response = ""
129+
for text in stream.text_stream:
130+
print(text, end="", flush=True, file=sys.stderr)
131+
full_response += text
132+
133+
# Clear the stderr line so the final command is clean on stdout
134+
# \r goes to start of line, \033[K clears line
135+
if sys.stderr.isatty():
136+
print("\r\033[K", end="", file=sys.stderr, flush=True)
137+
else:
138+
# If not a TTY, just print a newline
139+
print("", file=sys.stderr)
140+
141+
cmd = re.sub(r'^```\w*\n?|```$', '', full_response).strip()
142+
if not cmd:
143+
print("No command generated", file=sys.stderr)
144+
sys.exit(1)
145+
print(cmd)
146+
133147
except KeyboardInterrupt:
134148
print("\nCancelled", file=sys.stderr)
135149
sys.exit(130)

0 commit comments

Comments
 (0)