Skip to content

Commit 0816c93

Browse files
committed
fix(agent): add missing newlines and improve formatting
1 parent c02e38a commit 0816c93

File tree

233 files changed

+6324
-3664
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

233 files changed

+6324
-3664
lines changed

scripts/agent/add_prompt.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,4 @@ def save_clipboard_prompt() -> None:
3838

3939

4040
if __name__ == "__main__":
41-
save_clipboard_prompt()
41+
save_clipboard_prompt()

scripts/agent/agent.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,41 @@
11
import pyperclip
22
import os
33

4+
45
def save_to_file(content, filename="answer.md"):
56
with open(filename, "a") as f:
67
f.write(content + "\n\n")
78

9+
810
def main():
911
print("Hello! I'm GitHub Copilot. What can I help you with?")
10-
12+
1113
while True:
1214
user_input = input("You: ")
1315
if user_input.lower() in ["exit", "quit"]:
1416
print("Goodbye!")
1517
break
16-
18+
1719
# Copy user input to clipboard
1820
pyperclip.copy(user_input)
19-
print("I've copied your input to the clipboard. Please ask the chatbot and copy their answer. When ready, just press Enter.")
20-
21+
print(
22+
"I've copied your input to the clipboard. Please ask the chatbot and copy their answer. When ready, just press Enter."
23+
)
24+
2125
# Wait for user to press Enter after copying the chatbot's answer
2226
input("Press Enter when you have the answer copied...")
23-
27+
2428
# Get the answer from clipboard
2529
answer = pyperclip.paste()
2630
print("Answer received. Saving to answer.md...")
27-
31+
2832
# Save the interaction to file
29-
interaction = f"**User Input:**\n{user_input}\n\n**Chatbot Answer:**\n{answer}\n{'-'*50}"
33+
interaction = (
34+
f"**User Input:**\n{user_input}\n\n**Chatbot Answer:**\n{answer}\n{'-'*50}"
35+
)
3036
save_to_file(interaction)
3137
print("Saved to answer.md. Anything else I can help with?")
3238

39+
3340
if __name__ == "__main__":
34-
main()
41+
main()

scripts/agent/fix_agent.py

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import os
55
import subprocess
66

7-
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..')))
7+
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..")))
88

99
from scripts.translation.openrouter_client import call_openrouter_api
1010
from scripts.translation.openrouter_client import MODEL_MAPPING
@@ -13,63 +13,84 @@
1313
def run_script(script_path):
1414
"""Run a Python or Rust script and capture its output."""
1515
script_path = Path(script_path)
16-
if script_path.suffix == '.py':
16+
if script_path.suffix == ".py":
1717
try:
18-
result = subprocess.run(['python3', str(script_path)], capture_output=True, text=True)
18+
result = subprocess.run(
19+
["python3", str(script_path)], capture_output=True, text=True
20+
)
1921
return result.stdout + result.stderr
2022
except Exception as e:
2123
return str(e)
22-
elif script_path.suffix == '.rs':
24+
elif script_path.suffix == ".rs":
2325
# For Rust, we need to run cargo build in the directory containing the Rust project
2426
project_dir = script_path.parent
2527
try:
26-
result = subprocess.run(['cargo', 'build'], cwd=project_dir, capture_output=True, text=True)
28+
result = subprocess.run(
29+
["cargo", "build"], cwd=project_dir, capture_output=True, text=True
30+
)
2731
return result.stdout + result.stderr
2832
except Exception as e:
2933
return str(e)
3034
else:
3135
return f"Unsupported file extension for {script_path}"
3236

37+
3338
def fix_script(script_path, error_message, model):
3439
"""Use OpenRouter API to suggest fixes for the script errors and apply them."""
3540
script_path = Path(script_path)
36-
lang = 'Python' if script_path.suffix == '.py' else 'Rust' if script_path.suffix == '.rs' else 'Unknown'
41+
lang = (
42+
"Python"
43+
if script_path.suffix == ".py"
44+
else "Rust" if script_path.suffix == ".rs" else "Unknown"
45+
)
3746
prompt = f"Error in {lang} script {script_path}:\n{error_message}\nPlease provide only the corrected code for the entire script without any explanations, markdown syntax, or additional text."
3847
response = call_openrouter_api(prompt, model=model)
3948
# Remove any markdown code block syntax from the response
40-
response = response.replace("```python", "").replace("```rust", "").replace("```", "")
49+
response = (
50+
response.replace("```python", "").replace("```rust", "").replace("```", "")
51+
)
4152
# Write the fixed code back to the original script file
4253
try:
43-
with open(script_path, 'w') as f:
54+
with open(script_path, "w") as f:
4455
f.write(response.strip())
4556
return f"Applied fix to {script_path}"
4657
except Exception as e:
4758
return f"Failed to apply fix: {str(e)}"
4859

60+
4961
# Model mapping for user-friendly names
5062

63+
5164
def main():
5265
parser = argparse.ArgumentParser(description="Run and fix Python or Rust scripts.")
53-
parser.add_argument('script', type=str, help="Path to the script to run and fix (Python .py or Rust .rs).")
54-
parser.add_argument('--model', type=str, default='deepseek-v3',
55-
choices=list(MODEL_MAPPING.keys()),
56-
help="Model to use for OpenRouter API (e.g., claude-opus, gemini-pro).")
66+
parser.add_argument(
67+
"script",
68+
type=str,
69+
help="Path to the script to run and fix (Python .py or Rust .rs).",
70+
)
71+
parser.add_argument(
72+
"--model",
73+
type=str,
74+
default="deepseek-v3",
75+
choices=list(MODEL_MAPPING.keys()),
76+
help="Model to use for OpenRouter API (e.g., claude-opus, gemini-pro).",
77+
)
5778
args = parser.parse_args()
58-
79+
5980
script_path = Path(args.script)
6081
if not script_path.exists():
6182
print(f"Script {script_path} does not exist.")
6283
sys.exit(1)
63-
84+
6485
# Get the actual model name from the mapping
6586
selected_model = MODEL_MAPPING[args.model]
66-
87+
6788
# Run the script initially to capture output
6889
print("Running script for the first time...")
6990
output = run_script(script_path)
7091
print("Initial Script Output:")
7192
print(output)
72-
93+
7394
if "error" in output.lower() or "traceback" in output.lower():
7495
print("Error detected, attempting to fix...")
7596
fix_result = fix_script(script_path, output, selected_model)
@@ -80,11 +101,12 @@ def main():
80101
output = run_script(script_path)
81102
print("Updated Script Output:")
82103
print(output)
83-
104+
84105
if "error" in output.lower() or "traceback" in output.lower():
85106
print("Error still present after fix attempt.")
86107
print("Final Script Output:")
87108
print(output)
88109

110+
89111
if __name__ == "__main__":
90112
main()

scripts/agent/format_agent.py

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import os
55
import subprocess
66

7-
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..')))
7+
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..")))
88

99
from scripts.translation.openrouter_client import call_openrouter_api
1010
from scripts.translation.openrouter_client import MODEL_MAPPING
@@ -13,15 +13,21 @@
1313
def format_script(script_path):
1414
"""Format a Python or Rust script using standard formatting tools."""
1515
script_path = Path(script_path)
16-
if script_path.suffix == '.py':
16+
if script_path.suffix == ".py":
1717
try:
18-
result = subprocess.run(['black', str(script_path)], capture_output=True, text=True)
18+
result = subprocess.run(
19+
["black", str(script_path)], capture_output=True, text=True
20+
)
1921
return result.stdout + result.stderr
2022
except Exception as e:
2123
return str(e)
22-
elif script_path.suffix == '.rs':
24+
elif script_path.suffix == ".rs":
2325
try:
24-
result = subprocess.run(['rustfmt', str(script_path), '--edition', '2021'], capture_output=True, text=True)
26+
result = subprocess.run(
27+
["rustfmt", str(script_path), "--edition", "2021"],
28+
capture_output=True,
29+
text=True,
30+
)
2531
return result.stdout + result.stderr
2632
except Exception as e:
2733
return str(e)
@@ -32,40 +38,59 @@ def format_script(script_path):
3238
def ai_format_script(script_path, model):
3339
"""Use OpenRouter API to suggest formatting improvements for the script."""
3440
script_path = Path(script_path)
35-
lang = 'Python' if script_path.suffix == '.py' else 'Rust' if script_path.suffix == '.rs' else 'Unknown'
36-
with open(script_path, 'r') as f:
41+
lang = (
42+
"Python"
43+
if script_path.suffix == ".py"
44+
else "Rust" if script_path.suffix == ".rs" else "Unknown"
45+
)
46+
with open(script_path, "r") as f:
3747
code_content = f.read()
3848
prompt = f"Please format the following {lang} code for better readability and consistency. Provide only the formatted code without explanations or markdown syntax:\n\n{code_content}"
3949
response = call_openrouter_api(prompt, model=model)
4050
# Remove any markdown code block syntax from the response
41-
response = response.replace("```python", "").replace("```rust", "").replace("```", "")
51+
response = (
52+
response.replace("```python", "").replace("```rust", "").replace("```", "")
53+
)
4254
# Write the AI-formatted code back to the original script file
4355
try:
44-
with open(script_path, 'w') as f:
56+
with open(script_path, "w") as f:
4557
f.write(response.strip())
4658
return f"Applied AI formatting to {script_path}"
4759
except Exception as e:
4860
return f"Failed to apply AI formatting: {str(e)}"
4961

5062

5163
def main():
52-
parser = argparse.ArgumentParser(description="Format Python or Rust scripts using standard tools and AI.")
53-
parser.add_argument('script', type=str, help="Path to the script to format (Python .py or Rust .rs).")
54-
parser.add_argument('--model', type=str, default='deepseek-v3',
55-
choices=list(MODEL_MAPPING.keys()),
56-
help="Model to use for OpenRouter API (e.g., claude-opus, gemini-pro).")
57-
parser.add_argument('--ai-only', action='store_true',
58-
help="Use only AI formatting instead of standard tools like black or rustfmt.")
64+
parser = argparse.ArgumentParser(
65+
description="Format Python or Rust scripts using standard tools and AI."
66+
)
67+
parser.add_argument(
68+
"script",
69+
type=str,
70+
help="Path to the script to format (Python .py or Rust .rs).",
71+
)
72+
parser.add_argument(
73+
"--model",
74+
type=str,
75+
default="deepseek-v3",
76+
choices=list(MODEL_MAPPING.keys()),
77+
help="Model to use for OpenRouter API (e.g., claude-opus, gemini-pro).",
78+
)
79+
parser.add_argument(
80+
"--ai-only",
81+
action="store_true",
82+
help="Use only AI formatting instead of standard tools like black or rustfmt.",
83+
)
5984
args = parser.parse_args()
60-
85+
6186
script_path = Path(args.script)
6287
if not script_path.exists():
6388
print(f"Script {script_path} does not exist.")
6489
sys.exit(1)
65-
90+
6691
# Get the actual model name from the mapping
6792
selected_model = MODEL_MAPPING[args.model]
68-
93+
6994
if args.ai_only:
7095
print("Applying AI-based formatting...")
7196
ai_result = ai_format_script(script_path, selected_model)
@@ -76,7 +101,7 @@ def main():
76101
format_result = format_script(script_path)
77102
print("Standard Formatting Output:")
78103
print(format_result)
79-
104+
80105
if "error" in format_result.lower():
81106
print("Standard formatting failed, falling back to AI formatting...")
82107
ai_result = ai_format_script(script_path, selected_model)

scripts/agent/grammar_agent.py

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from pathlib import Path
44
import os
55

6-
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..')))
6+
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..")))
77

88
from scripts.translation.openrouter_client import call_openrouter_api
99

@@ -36,48 +36,54 @@ def fix_grammar_with_ai(content):
3636
print(f"Error calling AI API: {e}", file=sys.stderr)
3737
return None
3838

39+
3940
def process_file(file_path, output_only=False):
4041
"""Process a single Jekyll post file."""
4142
try:
42-
with open(file_path, 'r', encoding='utf-8') as f:
43+
with open(file_path, "r", encoding="utf-8") as f:
4344
content = f.read()
44-
45+
4546
fixed_content = fix_grammar_with_ai(content)
46-
47+
4748
if fixed_content is None:
4849
return None
49-
50+
5051
if output_only:
5152
print(fixed_content)
5253
else:
5354
print(f"Grammar-fixed content for {file_path}:")
5455
print(fixed_content)
5556
print()
56-
57+
5758
return fixed_content
58-
59+
5960
except Exception as e:
6061
print(f"Error processing {file_path}: {e}", file=sys.stderr)
6162
return None
6263

64+
6365
def main():
64-
parser = argparse.ArgumentParser(description='Fix grammar in Jekyll posts using AI')
65-
parser.add_argument('files', nargs='*', help='Markdown files to process')
66-
parser.add_argument('--output-only', action='store_true',
67-
help='Output only fixed content without file info')
68-
66+
parser = argparse.ArgumentParser(description="Fix grammar in Jekyll posts using AI")
67+
parser.add_argument("files", nargs="*", help="Markdown files to process")
68+
parser.add_argument(
69+
"--output-only",
70+
action="store_true",
71+
help="Output only fixed content without file info",
72+
)
73+
6974
args = parser.parse_args()
70-
75+
7176
if not args.files:
7277
# If no files specified, look for markdown files in current directory
73-
md_files = list(Path('.').glob('*.md'))
78+
md_files = list(Path(".").glob("*.md"))
7479
if not md_files:
7580
print("No markdown files found. Please specify files to process.")
7681
sys.exit(1)
7782
args.files = md_files
78-
83+
7984
for file_path in args.files:
8085
process_file(file_path, args.output_only)
8186

87+
8288
if __name__ == "__main__":
8389
main()

0 commit comments

Comments
 (0)