librecoding 0.3.0__tar.gz → 0.4.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: librecoding
3
- Version: 0.3.0
3
+ Version: 0.4.0
4
4
  Summary: Free and open-source coding library
5
5
  Author: ar
6
6
  License-Expression: MIT
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "librecoding"
7
- version = "0.3.0"
7
+ version = "0.4.0"
8
8
  description = "Free and open-source coding library"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.10"
@@ -0,0 +1,177 @@
1
+ """librecoding - вызываемый модуль: спрашивает промпт, шлёт в OpenRouter, пишет .txt."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import json
6
+ import os
7
+ import sys
8
+ import urllib.error
9
+ import urllib.request
10
+ from datetime import datetime
11
+ from pathlib import Path
12
+ from types import ModuleType
13
+
14
+ from .core import greet
15
+
16
+ __version__ = "0.4.0"
17
+
18
+ API_URL = "https://openrouter.ai/api/v1/chat/completions"
19
+ DEFAULT_MODEL = "deepseek/deepseek-v4.1-flash"
20
+ KEY_FILE = Path.home() / ".config" / "librecoding" / "api_key"
21
+ PROMPT = "Введи промпт: "
22
+ PAUSE_TEXT = "Нажми Enter, чтобы закрыть..."
23
+
24
+ _state: dict = {"key": None, "model": DEFAULT_MODEL}
25
+
26
+
27
+ def set_api_key(key: str) -> None:
28
+ """Задать ключ OpenRouter на время работы процесса."""
29
+ _state["key"] = key.strip()
30
+
31
+
32
+ def set_model(model: str) -> None:
33
+ """Сменить модель, например 'deepseek/deepseek-chat'."""
34
+ _state["model"] = model
35
+
36
+
37
+ def get_model() -> str:
38
+ return _state["model"]
39
+
40
+
41
+ def _api_key() -> str:
42
+ """Ключ: set_api_key() -> OPENROUTER_API_KEY -> ~/.config/librecoding/api_key."""
43
+ if _state["key"]:
44
+ return _state["key"]
45
+
46
+ env = os.environ.get("OPENROUTER_API_KEY", "").strip()
47
+ if env:
48
+ return env
49
+
50
+ if KEY_FILE.is_file():
51
+ for line in KEY_FILE.read_text(encoding="utf-8").splitlines():
52
+ line = line.strip()
53
+ if line and not line.startswith("#"):
54
+ return line
55
+
56
+ raise RuntimeError(
57
+ "API-ключ OpenRouter не найден. Задай его так:\n"
58
+ f" echo 'sk-or-v1-...' > {KEY_FILE}\n"
59
+ f" chmod 600 {KEY_FILE}\n"
60
+ " либо: export OPENROUTER_API_KEY=sk-or-v1-...\n"
61
+ " либо: librecoding.set_api_key('sk-or-v1-...')"
62
+ )
63
+
64
+
65
+ def ask(prompt, model=None, temperature=0.7, timeout=180) -> str:
66
+ """Отправить промпт в OpenRouter, вернуть текст ответа."""
67
+ payload = {
68
+ "model": model or _state["model"],
69
+ "messages": [{"role": "user", "content": prompt}],
70
+ "temperature": temperature,
71
+ }
72
+ req = urllib.request.Request(
73
+ API_URL,
74
+ data=json.dumps(payload).encode("utf-8"),
75
+ headers={
76
+ "Authorization": f"Bearer {_api_key()}",
77
+ "Content-Type": "application/json",
78
+ "HTTP-Referer": "https://pypi.org/project/librecoding/",
79
+ "X-Title": "librecoding",
80
+ },
81
+ method="POST",
82
+ )
83
+
84
+ try:
85
+ with urllib.request.urlopen(req, timeout=timeout) as resp:
86
+ data = json.loads(resp.read().decode("utf-8"))
87
+ except urllib.error.HTTPError as exc:
88
+ body = exc.read().decode("utf-8", "replace")[:400]
89
+ raise RuntimeError(f"OpenRouter HTTP {exc.code}: {body}") from None
90
+ except urllib.error.URLError as exc:
91
+ raise RuntimeError(f"нет сети / таймаут: {exc.reason}") from None
92
+
93
+ try:
94
+ return data["choices"][0]["message"]["content"] or ""
95
+ except (KeyError, IndexError, TypeError):
96
+ raise RuntimeError(f"неожиданный ответ: {str(data)[:400]}") from None
97
+
98
+
99
+ def pause(prompt: str = PAUSE_TEXT) -> None:
100
+ """Ждёт Enter, чтобы терминал не закрылся. Молчит без tty."""
101
+ if not sys.stdin.isatty():
102
+ return
103
+ try:
104
+ input(prompt)
105
+ except (EOFError, KeyboardInterrupt):
106
+ print()
107
+
108
+
109
+ def _caller_dir() -> Path:
110
+ """Папка запущенного скрипта (test.py), а не библиотеки."""
111
+ path = getattr(sys.modules.get("__main__"), "__file__", None)
112
+ if path:
113
+ return Path(path).resolve().parent
114
+ if sys.argv and sys.argv[0]:
115
+ return Path(sys.argv[0]).resolve().parent
116
+ return Path.cwd()
117
+
118
+
119
+ def interactive(prompt=None, filename=None, model=None):
120
+ """Спросить промпт -> запросить OpenRouter -> сохранить .txt рядом со скриптом."""
121
+ if not sys.stdin.isatty():
122
+ raise RuntimeError("нужен интерактивный терминал: запусти файл в консоли")
123
+
124
+ if prompt is None:
125
+ try:
126
+ prompt = input(PROMPT).strip()
127
+ except (EOFError, KeyboardInterrupt):
128
+ print()
129
+ return None
130
+
131
+ if not prompt:
132
+ print("Пустой промпт — выход.")
133
+ return None
134
+
135
+ mdl = model or _state["model"]
136
+ print(f"[{mdl}] думаю...")
137
+ answer = ask(prompt, model=mdl)
138
+
139
+ print("\n--- ответ ---")
140
+ print(answer.strip())
141
+ print("--- конец ---")
142
+
143
+ name = filename or f"response_{datetime.now():%Y-%m-%d_%H-%M-%S}.txt"
144
+ path = _caller_dir() / name
145
+ path.write_text(
146
+ f"Модель: {mdl}\n"
147
+ f"Дата: {datetime.now():%Y-%m-%d %H:%M:%S}\n\n"
148
+ f"Промпт:\n{prompt}\n\n"
149
+ f"Ответ:\n{answer.strip()}\n",
150
+ encoding="utf-8",
151
+ )
152
+ print(f"\nСохранено: {path}")
153
+ return path
154
+
155
+
156
+ class _LibreCodingModule(ModuleType):
157
+ def __call__(self, obj=None, *args, **kwargs):
158
+ if callable(obj) and not args and not kwargs:
159
+ return obj
160
+ try:
161
+ if isinstance(obj, str):
162
+ kwargs["prompt"] = obj
163
+ interactive(*args, **kwargs)
164
+ except Exception as exc:
165
+ print(f"Ошибка: {exc}", file=sys.stderr)
166
+ if os.environ.get("LIBRECODING_DEBUG"):
167
+ raise
168
+ pause()
169
+ return None
170
+
171
+
172
+ sys.modules[__name__].__class__ = _LibreCodingModule
173
+
174
+ __all__ = [
175
+ "greet", "ask", "interactive", "pause",
176
+ "set_api_key", "set_model", "get_model",
177
+ ]
@@ -1,43 +0,0 @@
1
- """librecoding - модуль, который можно вызвать как функцию."""
2
-
3
- import sys
4
- from types import ModuleType
5
-
6
- from .core import greet
7
-
8
- __version__ = "0.3.0"
9
-
10
- BANNER = "Привет, мир! (из librecoding)"
11
- PROMPT = "Нажми Enter, чтобы закрыть..."
12
-
13
-
14
- def hello() -> str:
15
- """Явный способ попросить приветствие."""
16
- print(BANNER)
17
- return BANNER
18
-
19
-
20
- def pause(prompt: str = PROMPT) -> None:
21
- """Ждёт Enter, чтобы терминал не закрылся сразу."""
22
- if not sys.stdin.isatty():
23
- return
24
- try:
25
- input(prompt)
26
- except (EOFError, KeyboardInterrupt):
27
- print()
28
-
29
-
30
- class _LibreCodingModule(ModuleType):
31
- """Модуль, который ведёт себя как функция: librecoding()."""
32
-
33
- def __call__(self, obj=None, *args, **kwargs):
34
- print(BANNER)
35
- if callable(obj) and not args and not kwargs:
36
- return obj
37
- pause()
38
- return BANNER
39
-
40
-
41
- sys.modules[__name__].__class__ = _LibreCodingModule
42
-
43
- __all__ = ["greet", "hello", "pause", "BANNER"]
File without changes