Skip to content

Commit 2c04c67

Browse files
d99krismpcabete
andauthored
add support for custom auto-compose timeout (#430)
* add support for custom auto-compose timeout --------- Co-authored-by: Mateus Cabete <mateus.cabete@gmail.com>
1 parent 7a8dde3 commit 2c04c67

4 files changed

Lines changed: 12 additions & 7 deletions

File tree

doc/AUTOCOMPOSE.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ Example usage with OpenAI:
2828

2929
./src/compose -s openai -c doc/example-history.txt
3030

31-
Example usage with OpenAI and custom model:
31+
Example usage with OpenAI and custom model and longer timeout of 60 secs:
3232

33-
./src/compose -s openai -m gpt-5-nano -c doc/example-history.txt
33+
./src/compose -s openai -m gpt-5-nano -T 60 -c doc/example-history.txt
3434

3535
Example usage with Google Gemini:
3636

lib/common/src/version.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77

88
#pragma once
99

10-
#define NCHAT_VERSION "5.12.3"
10+
#define NCHAT_VERSION "5.12.4"

src/compose

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def build_payload(model: str, your_name: str, chat_messages: list[dict], tempera
8383
payload["temperature"] = temperature
8484
return payload
8585

86-
def send_request(payload: dict, api_url: str, api_key: str, verbose: bool=False) -> str:
86+
def send_request(payload: dict, api_url: str, api_key: str, verbose: bool, timeout: int) -> str:
8787
headers = {
8888
"Content-Type": "application/json",
8989
"Authorization": f"Bearer {api_key}",
@@ -99,7 +99,7 @@ def send_request(payload: dict, api_url: str, api_key: str, verbose: bool=False)
9999
)
100100

101101
try:
102-
with urllib.request.urlopen(req, timeout=10) as resp:
102+
with urllib.request.urlopen(req, timeout=timeout) as resp:
103103
body = resp.read().decode("utf-8")
104104
result = json.loads(body)
105105
except urllib.error.HTTPError as e:
@@ -140,8 +140,13 @@ def main():
140140
help="Sampling temperature (e.g., 0.2).")
141141
parser.add_argument("-v", "--verbose", action="store_true",
142142
help="Print request payload to stderr.")
143+
parser.add_argument("-T", "--timeout", type=int, default=10,
144+
help="Network timeout in seconds (default: 10).")
143145
args = parser.parse_args()
144146

147+
if args.timeout <= 0:
148+
print("Error: -T / --timeout must be > 0 seconds.", file=sys.stderr)
149+
sys.exit(1)
145150

146151
# Resolve service -> api_url, api_key_env, and default model
147152
service = args.service.strip()
@@ -165,7 +170,7 @@ def main():
165170
lines = read_chat_file(args.chat_completion)
166171
your_name, chat_messages = parse_chat(lines)
167172
payload = build_payload(model, your_name, chat_messages, args.temperature)
168-
reply = send_request(payload, api_url, api_key, verbose=args.verbose)
173+
reply = send_request(payload, api_url, api_key, verbose=args.verbose, timeout=args.timeout)
169174
reply_without_name = reply.split(":", 1)[1].strip() if ":" in reply else reply
170175
print(reply_without_name)
171176

src/nchat.1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
.\" DO NOT MODIFY THIS FILE! It was generated by help2man.
2-
.TH NCHAT "1" "November 2025" "nchat 5.12.3" "User Commands"
2+
.TH NCHAT "1" "November 2025" "nchat 5.12.4" "User Commands"
33
.SH NAME
44
nchat \- ncurses chat
55
.SH SYNOPSIS

0 commit comments

Comments
 (0)