feat(ai): support stdin for Gemini CLI and verify with integration test
This commit is contained in:
@@ -12,25 +12,36 @@ class GeminiCliAdapter:
|
||||
"""
|
||||
Sends a message to the Gemini CLI and processes the streaming JSON output.
|
||||
"""
|
||||
command = [self.binary_path, 'run', message, '--output-format', 'stream-json']
|
||||
# On Windows, using shell=True allows executing .cmd/.bat files and
|
||||
# handles command strings with arguments more gracefully.
|
||||
# We pass the message via stdin to avoid command-line length limits.
|
||||
command = f'{self.binary_path} run --output-format stream-json'
|
||||
if self.session_id:
|
||||
command.extend(['--resume', self.session_id])
|
||||
command += f' --resume {self.session_id}'
|
||||
|
||||
print(f"[DEBUG] GeminiCliAdapter: Executing command: {command}")
|
||||
accumulated_text = ""
|
||||
|
||||
# Using subprocess.Popen as requested
|
||||
process = subprocess.Popen(
|
||||
command,
|
||||
stdin=subprocess.PIPE,
|
||||
stdout=subprocess.PIPE,
|
||||
text=True
|
||||
stderr=subprocess.PIPE,
|
||||
text=True,
|
||||
shell=True
|
||||
)
|
||||
|
||||
try:
|
||||
# Send message to stdin and close it
|
||||
process.stdin.write(message)
|
||||
process.stdin.close()
|
||||
|
||||
# Read stdout line by line
|
||||
for line in process.stdout:
|
||||
line = line.strip()
|
||||
if not line:
|
||||
continue
|
||||
print(f"[DEBUG] GeminiCliAdapter stdout: {line}")
|
||||
|
||||
try:
|
||||
data = json.loads(line)
|
||||
@@ -55,8 +66,12 @@ class GeminiCliAdapter:
|
||||
continue
|
||||
|
||||
process.wait()
|
||||
if process.returncode != 0:
|
||||
err = process.stderr.read()
|
||||
print(f"[DEBUG] GeminiCliAdapter failed with exit code {process.returncode}. stderr: {err}")
|
||||
except Exception as e:
|
||||
process.kill()
|
||||
print(f"[DEBUG] GeminiCliAdapter exception: {e}")
|
||||
raise e
|
||||
|
||||
return accumulated_text
|
||||
|
||||
Reference in New Issue
Block a user