|
4 | 4 | from aicodebot.output import OurMarkdown, get_console |
5 | 5 | from aicodebot.prompts import get_prompt |
6 | 6 | from pathlib import Path |
7 | | -from pydantic import BaseModel, Field, constr |
| 7 | +from pydantic import BaseModel, Field |
8 | 8 | from rich.panel import Panel |
| 9 | +from typing import Optional |
9 | 10 | import click, os, shutil, subprocess, sys, tempfile |
10 | 11 |
|
11 | 12 |
|
12 | 13 | class CommitMessage(BaseModel): |
13 | | - git_message_summary: constr(max_length=72) = Field( |
14 | | - description="A brief summary of the commit message (max 72 characters)" |
15 | | - ) |
16 | | - git_message_detail: str | None = Field( |
17 | | - default=None, description="An optional detailed explanation of the changes made in this commit" |
| 14 | + # Use Optional[str] instead of str | None for Python 3.9 compatibility |
| 15 | + git_message_detail: Optional[str] = Field( # noqa: UP007 |
| 16 | + default="", |
| 17 | + description="An optional detailed explanation of the changes made in this commit," |
| 18 | + " if the summary doesn't provide enough context", |
18 | 19 | ) |
19 | 20 |
|
| 21 | + git_message_summary: str = Field(description="A brief summary of the commit message (max 72 characters)") |
| 22 | + |
20 | 23 |
|
21 | 24 | @click.command() |
22 | 25 | @click.option("-t", "--response-token-size", type=int, default=250) |
@@ -100,15 +103,18 @@ def commit(response_token_size, yes, skip_pre_commit, files): # noqa: PLR0915 |
100 | 103 | chain = prompt | structured_llm |
101 | 104 | response = chain.invoke({"diff_context": diff_context, "languages": languages}) |
102 | 105 |
|
103 | | - console.print(Panel(OurMarkdown(f"{response.git_message_summary}\n\n{response.git_message_detail}"))) |
| 106 | + commit_message = response["git_message_summary"] |
| 107 | + if response.get("git_message_detail"): |
| 108 | + commit_message += f"\n\n{response['git_message_detail']}" |
| 109 | + |
| 110 | + console.print(Panel(OurMarkdown(commit_message))) |
104 | 111 |
|
105 | 112 | commit_message_approved = not console.is_terminal or click.confirm( |
106 | 113 | "Would you like to use this generated commit message? Type 'n' to edit it.", default=True |
107 | 114 | ) |
108 | 115 |
|
109 | 116 | # Write the commit message to a temporary file |
110 | 117 | with tempfile.NamedTemporaryFile(mode="w", delete=False) as temp: |
111 | | - commit_message = f"{response.git_message_summary}\n\n{response.git_message_detail}" |
112 | 118 | temp.write(commit_message) |
113 | 119 | temp_file_name = temp.name |
114 | 120 |
|
|
0 commit comments