ai-mini-box-core 5.0.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.
- ai_mini_box_core-5.0.0/.gitignore +15 -0
- ai_mini_box_core-5.0.0/LICENSE +21 -0
- ai_mini_box_core-5.0.0/PKG-INFO +149 -0
- ai_mini_box_core-5.0.0/README.md +100 -0
- ai_mini_box_core-5.0.0/ai_mini_box/__init__.py +0 -0
- ai_mini_box_core-5.0.0/ai_mini_box/__main__.py +3 -0
- ai_mini_box_core-5.0.0/ai_mini_box/cli.py +178 -0
- ai_mini_box_core-5.0.0/ai_mini_box/core/__init__.py +0 -0
- ai_mini_box_core-5.0.0/ai_mini_box/core/container.py +40 -0
- ai_mini_box_core-5.0.0/ai_mini_box/core/exceptions.py +15 -0
- ai_mini_box_core-5.0.0/ai_mini_box/core/models.py +78 -0
- ai_mini_box_core-5.0.0/ai_mini_box/core/repositories.py +160 -0
- ai_mini_box_core-5.0.0/ai_mini_box/infrastructure/__init__.py +0 -0
- ai_mini_box_core-5.0.0/ai_mini_box/infrastructure/config.py +182 -0
- ai_mini_box_core-5.0.0/ai_mini_box/infrastructure/database.py +67 -0
- ai_mini_box_core-5.0.0/ai_mini_box/infrastructure/logger.py +10 -0
- ai_mini_box_core-5.0.0/ai_mini_box/infrastructure/mapping.py +39 -0
- ai_mini_box_core-5.0.0/ai_mini_box/infrastructure/orm_models.py +85 -0
- ai_mini_box_core-5.0.0/ai_mini_box/infrastructure/repositories/__init__.py +11 -0
- ai_mini_box_core-5.0.0/ai_mini_box/infrastructure/repositories/contact_repo.py +74 -0
- ai_mini_box_core-5.0.0/ai_mini_box/infrastructure/repositories/message_repo.py +55 -0
- ai_mini_box_core-5.0.0/ai_mini_box/infrastructure/repositories/order_repo.py +53 -0
- ai_mini_box_core-5.0.0/ai_mini_box/infrastructure/repositories/product_repo.py +73 -0
- ai_mini_box_core-5.0.0/ai_mini_box/testing.py +141 -0
- ai_mini_box_core-5.0.0/ai_mini_box/tools/__init__.py +0 -0
- ai_mini_box_core-5.0.0/pyproject.toml +42 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Kibertum
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: ai-mini-box-core
|
|
3
|
+
Version: 5.0.0
|
|
4
|
+
Summary: AI mini box core — system core for small business automation
|
|
5
|
+
Project-URL: Homepage, https://github.com/Kibertum/ai-mini-box
|
|
6
|
+
Project-URL: Repository, https://github.com/Kibertum/ai-mini-box
|
|
7
|
+
Project-URL: Documentation, https://github.com/Kibertum/ai-mini-box
|
|
8
|
+
License: MIT License
|
|
9
|
+
|
|
10
|
+
Copyright (c) 2026 Kibertum
|
|
11
|
+
|
|
12
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
13
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
14
|
+
in the Software without restriction, including without limitation the rights
|
|
15
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
16
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
17
|
+
furnished to do so, subject to the following conditions:
|
|
18
|
+
|
|
19
|
+
The above copyright notice and this permission notice shall be included in all
|
|
20
|
+
copies or substantial portions of the Software.
|
|
21
|
+
|
|
22
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
23
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
24
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
25
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
26
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
27
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
28
|
+
SOFTWARE.
|
|
29
|
+
License-File: LICENSE
|
|
30
|
+
Classifier: Development Status :: 4 - Beta
|
|
31
|
+
Classifier: Intended Audience :: Developers
|
|
32
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
33
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
34
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
35
|
+
Classifier: Topic :: Office/Business
|
|
36
|
+
Requires-Python: >=3.12
|
|
37
|
+
Requires-Dist: alembic>=1.13
|
|
38
|
+
Requires-Dist: cryptography>=42
|
|
39
|
+
Requires-Dist: loguru>=0.7
|
|
40
|
+
Requires-Dist: pydantic>=2.0
|
|
41
|
+
Requires-Dist: python-dotenv>=1.0
|
|
42
|
+
Requires-Dist: sqlalchemy>=2.0
|
|
43
|
+
Requires-Dist: typer>=0.12
|
|
44
|
+
Provides-Extra: dev
|
|
45
|
+
Requires-Dist: pytest-cov>=5; extra == 'dev'
|
|
46
|
+
Requires-Dist: pytest-xdist>=3; extra == 'dev'
|
|
47
|
+
Requires-Dist: pytest>=8; extra == 'dev'
|
|
48
|
+
Description-Content-Type: text/markdown
|
|
49
|
+
|
|
50
|
+
# AI mini box
|
|
51
|
+
|
|
52
|
+
[](https://github.com/Kibertum/ai-mini-box/actions/workflows/tests.yml)
|
|
53
|
+
[](https://pypi.org/project/ai-mini-box-core/)
|
|
54
|
+
[](https://pypi.org/project/ai-mini-box-core/)
|
|
55
|
+
[](LICENSE)
|
|
56
|
+
|
|
57
|
+
Системное ядро для автоматизации малого бизнеса. Управление контактами, продуктами, заказами, сообщениями через единый CLI и плагинную систему сервисов.
|
|
58
|
+
|
|
59
|
+
## Установка
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
pip install ai-mini-box-core
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Для разработки — с тестовыми зависимостями:
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
git clone https://github.com/Kibertum/ai-mini-box
|
|
69
|
+
cd ai-mini-box
|
|
70
|
+
pip install -e packages/core[dev]
|
|
71
|
+
pip install -e packages/demo[dev] # пример сервиса
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## Быстрый старт
|
|
75
|
+
|
|
76
|
+
```bash
|
|
77
|
+
# Инициализация проекта
|
|
78
|
+
ai-mini-box init
|
|
79
|
+
|
|
80
|
+
# Проверка БД
|
|
81
|
+
ai-mini-box check-db
|
|
82
|
+
|
|
83
|
+
# Настройка
|
|
84
|
+
ai-mini-box config show
|
|
85
|
+
ai-mini-box config set telegram_bot_token "your_token"
|
|
86
|
+
ai-mini-box config set poll_interval 15
|
|
87
|
+
ai-mini-box config unset poll_interval
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## Команды CLI
|
|
91
|
+
|
|
92
|
+
| Команда | Описание |
|
|
93
|
+
|---------|----------|
|
|
94
|
+
| `init` | Создать config.json, БД, директории data/ |
|
|
95
|
+
| `check-db` | Проверить подключение к БД и схему |
|
|
96
|
+
| `config show` | Показать конфигурацию (с группировкой по секциям) |
|
|
97
|
+
| `config set <key> <value>` | Установить значение (чувствительные поля шифруются) |
|
|
98
|
+
| `config unset <key>` | Сбросить на значение по умолчанию |
|
|
99
|
+
|
|
100
|
+
Плагины (сервисы) регистрируют свои команды через entry points:
|
|
101
|
+
|
|
102
|
+
| Команда (из demo) | Описание |
|
|
103
|
+
|---|---|
|
|
104
|
+
| `demo-list` | Список контактов |
|
|
105
|
+
| `demo-get <id>` | Контакт по ID |
|
|
106
|
+
| `demo-add <name> <phone>` | Добавить контакт |
|
|
107
|
+
|
|
108
|
+
Опции: `--verbose`, `--help` доступны для всех команд.
|
|
109
|
+
|
|
110
|
+
## Архитектура
|
|
111
|
+
|
|
112
|
+
```
|
|
113
|
+
ai_mini_box/
|
|
114
|
+
├── core/ # Слой домена
|
|
115
|
+
│ ├── models.py # Pydantic-модели (Contact, Product, Message, Order)
|
|
116
|
+
│ ├── repositories.py # ABC репозиториев + QueryBuilder
|
|
117
|
+
│ ├── container.py # RepoContainer + AppContext (DI)
|
|
118
|
+
│ └── exceptions.py # Кастомные исключения
|
|
119
|
+
├── infrastructure/ # Слой инфраструктуры
|
|
120
|
+
│ ├── database.py # SQLAlchemy engine, session, get_db()
|
|
121
|
+
│ ├── config.py # JsonConfigManager с шифрованием
|
|
122
|
+
│ ├── logger.py # Loguru-логгер
|
|
123
|
+
│ ├── orm_models.py # SQLAlchemy ORM-модели
|
|
124
|
+
│ ├── mapping.py # Мапперы Pydantic ↔ ORM
|
|
125
|
+
│ └── repositories/ # SQLAlchemy-реализации репозиториев
|
|
126
|
+
└── cli.py # Typer CLI + плагинная загрузка
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
**Слои:** `core/` (Pydantic + ABC) → `infrastructure/` (SQLAlchemy + файлы) → `tools/` (CLI-сервисы через entry points).
|
|
130
|
+
|
|
131
|
+
## Разработка сервиса
|
|
132
|
+
|
|
133
|
+
См. [docs/developer-guide.md](docs/developer-guide.md).
|
|
134
|
+
|
|
135
|
+
## Сервисы (инструменты)
|
|
136
|
+
|
|
137
|
+
Проект включает 30 спецификаций сервисов в `tool-*.md` — от телеграм-бота и email до CRM-синхронизации и юристов. Каждый сервис — отдельный Python-пакет, подключаемый через entry point `ai_mini_box.tools`.
|
|
138
|
+
|
|
139
|
+
## Тестирование
|
|
140
|
+
|
|
141
|
+
```bash
|
|
142
|
+
pytest packages/core/tests/ packages/demo/tests/ -v
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
Текущий статус: **72 теста, все зелёные** (63 core + 9 demo).
|
|
146
|
+
|
|
147
|
+
## Лицензия
|
|
148
|
+
|
|
149
|
+
MIT
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
# AI mini box
|
|
2
|
+
|
|
3
|
+
[](https://github.com/Kibertum/ai-mini-box/actions/workflows/tests.yml)
|
|
4
|
+
[](https://pypi.org/project/ai-mini-box-core/)
|
|
5
|
+
[](https://pypi.org/project/ai-mini-box-core/)
|
|
6
|
+
[](LICENSE)
|
|
7
|
+
|
|
8
|
+
Системное ядро для автоматизации малого бизнеса. Управление контактами, продуктами, заказами, сообщениями через единый CLI и плагинную систему сервисов.
|
|
9
|
+
|
|
10
|
+
## Установка
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
pip install ai-mini-box-core
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
Для разработки — с тестовыми зависимостями:
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
git clone https://github.com/Kibertum/ai-mini-box
|
|
20
|
+
cd ai-mini-box
|
|
21
|
+
pip install -e packages/core[dev]
|
|
22
|
+
pip install -e packages/demo[dev] # пример сервиса
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Быстрый старт
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
# Инициализация проекта
|
|
29
|
+
ai-mini-box init
|
|
30
|
+
|
|
31
|
+
# Проверка БД
|
|
32
|
+
ai-mini-box check-db
|
|
33
|
+
|
|
34
|
+
# Настройка
|
|
35
|
+
ai-mini-box config show
|
|
36
|
+
ai-mini-box config set telegram_bot_token "your_token"
|
|
37
|
+
ai-mini-box config set poll_interval 15
|
|
38
|
+
ai-mini-box config unset poll_interval
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Команды CLI
|
|
42
|
+
|
|
43
|
+
| Команда | Описание |
|
|
44
|
+
|---------|----------|
|
|
45
|
+
| `init` | Создать config.json, БД, директории data/ |
|
|
46
|
+
| `check-db` | Проверить подключение к БД и схему |
|
|
47
|
+
| `config show` | Показать конфигурацию (с группировкой по секциям) |
|
|
48
|
+
| `config set <key> <value>` | Установить значение (чувствительные поля шифруются) |
|
|
49
|
+
| `config unset <key>` | Сбросить на значение по умолчанию |
|
|
50
|
+
|
|
51
|
+
Плагины (сервисы) регистрируют свои команды через entry points:
|
|
52
|
+
|
|
53
|
+
| Команда (из demo) | Описание |
|
|
54
|
+
|---|---|
|
|
55
|
+
| `demo-list` | Список контактов |
|
|
56
|
+
| `demo-get <id>` | Контакт по ID |
|
|
57
|
+
| `demo-add <name> <phone>` | Добавить контакт |
|
|
58
|
+
|
|
59
|
+
Опции: `--verbose`, `--help` доступны для всех команд.
|
|
60
|
+
|
|
61
|
+
## Архитектура
|
|
62
|
+
|
|
63
|
+
```
|
|
64
|
+
ai_mini_box/
|
|
65
|
+
├── core/ # Слой домена
|
|
66
|
+
│ ├── models.py # Pydantic-модели (Contact, Product, Message, Order)
|
|
67
|
+
│ ├── repositories.py # ABC репозиториев + QueryBuilder
|
|
68
|
+
│ ├── container.py # RepoContainer + AppContext (DI)
|
|
69
|
+
│ └── exceptions.py # Кастомные исключения
|
|
70
|
+
├── infrastructure/ # Слой инфраструктуры
|
|
71
|
+
│ ├── database.py # SQLAlchemy engine, session, get_db()
|
|
72
|
+
│ ├── config.py # JsonConfigManager с шифрованием
|
|
73
|
+
│ ├── logger.py # Loguru-логгер
|
|
74
|
+
│ ├── orm_models.py # SQLAlchemy ORM-модели
|
|
75
|
+
│ ├── mapping.py # Мапперы Pydantic ↔ ORM
|
|
76
|
+
│ └── repositories/ # SQLAlchemy-реализации репозиториев
|
|
77
|
+
└── cli.py # Typer CLI + плагинная загрузка
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
**Слои:** `core/` (Pydantic + ABC) → `infrastructure/` (SQLAlchemy + файлы) → `tools/` (CLI-сервисы через entry points).
|
|
81
|
+
|
|
82
|
+
## Разработка сервиса
|
|
83
|
+
|
|
84
|
+
См. [docs/developer-guide.md](docs/developer-guide.md).
|
|
85
|
+
|
|
86
|
+
## Сервисы (инструменты)
|
|
87
|
+
|
|
88
|
+
Проект включает 30 спецификаций сервисов в `tool-*.md` — от телеграм-бота и email до CRM-синхронизации и юристов. Каждый сервис — отдельный Python-пакет, подключаемый через entry point `ai_mini_box.tools`.
|
|
89
|
+
|
|
90
|
+
## Тестирование
|
|
91
|
+
|
|
92
|
+
```bash
|
|
93
|
+
pytest packages/core/tests/ packages/demo/tests/ -v
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
Текущий статус: **72 теста, все зелёные** (63 core + 9 demo).
|
|
97
|
+
|
|
98
|
+
## Лицензия
|
|
99
|
+
|
|
100
|
+
MIT
|
|
File without changes
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import importlib.metadata
|
|
2
|
+
import json
|
|
3
|
+
import os
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
|
|
6
|
+
import typer
|
|
7
|
+
|
|
8
|
+
from ai_mini_box.infrastructure.database import get_db, init_db
|
|
9
|
+
|
|
10
|
+
app = typer.Typer(
|
|
11
|
+
name="ai-mini-box",
|
|
12
|
+
help="AI mini box — automation of small business",
|
|
13
|
+
no_args_is_help=True,
|
|
14
|
+
pretty_exceptions_short=os.environ.get("AI_BOX_VERBOSE") != "1",
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
@app.callback()
|
|
19
|
+
def main_callback(
|
|
20
|
+
verbose: bool = typer.Option(False, "--verbose", "-v", help="Enable verbose output"),
|
|
21
|
+
):
|
|
22
|
+
if verbose:
|
|
23
|
+
os.environ["AI_BOX_VERBOSE"] = "1"
|
|
24
|
+
config_app = typer.Typer(help="Manage configuration")
|
|
25
|
+
app.add_typer(config_app, name="config")
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
@app.command()
|
|
29
|
+
def init(
|
|
30
|
+
force: bool = typer.Option(False, "--force", help="Overwrite existing config"),
|
|
31
|
+
config_path: str = typer.Option("data/config.json", "--config", help="Config path"),
|
|
32
|
+
db_path: str = typer.Option("data/app.db", "--db", help="Database path"),
|
|
33
|
+
):
|
|
34
|
+
"""Initialize project: create config, database, directories."""
|
|
35
|
+
from ai_mini_box.infrastructure.config import JsonConfigManager
|
|
36
|
+
|
|
37
|
+
dirs = [Path("data"), Path("data/backup"), Path("data/models"), Path("data/training")]
|
|
38
|
+
for d in dirs:
|
|
39
|
+
d.mkdir(parents=True, exist_ok=True)
|
|
40
|
+
typer.echo(f" Created: {d}")
|
|
41
|
+
|
|
42
|
+
cfg_path = Path(config_path)
|
|
43
|
+
if not cfg_path.exists() or force:
|
|
44
|
+
manager = JsonConfigManager(cfg_path)
|
|
45
|
+
manager.save(manager.load())
|
|
46
|
+
typer.echo(f" Created: {cfg_path}" + (" (overwritten)" if force else ""))
|
|
47
|
+
|
|
48
|
+
init_db(db_path)
|
|
49
|
+
typer.echo(f" Created: {db_path}")
|
|
50
|
+
typer.echo("Done.")
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
@app.command()
|
|
54
|
+
def check_db():
|
|
55
|
+
"""Check database connection and schema."""
|
|
56
|
+
try:
|
|
57
|
+
from sqlalchemy import text
|
|
58
|
+
|
|
59
|
+
with get_db() as session:
|
|
60
|
+
result = session.execute(text("SELECT 1"))
|
|
61
|
+
val = result.scalar()
|
|
62
|
+
if val == 1:
|
|
63
|
+
typer.echo(" Database: connected")
|
|
64
|
+
from ai_mini_box.infrastructure.database import Base
|
|
65
|
+
|
|
66
|
+
tables = list(Base.metadata.tables.keys())
|
|
67
|
+
typer.echo(f" Tables: {', '.join(tables) if tables else 'none'}")
|
|
68
|
+
except Exception as e:
|
|
69
|
+
typer.echo(f" Database: ERROR - {e}")
|
|
70
|
+
raise typer.Exit(code=1)
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
@config_app.command(name="show")
|
|
74
|
+
def config_show(
|
|
75
|
+
config_path: str = typer.Option("data/config.json", "--config", help="Config path"),
|
|
76
|
+
):
|
|
77
|
+
"""Show current configuration grouped by sections."""
|
|
78
|
+
from ai_mini_box.infrastructure.config import AppConfig, JsonConfigManager, SENSITIVE_FIELDS
|
|
79
|
+
|
|
80
|
+
manager = JsonConfigManager(config_path)
|
|
81
|
+
config = manager.load()
|
|
82
|
+
|
|
83
|
+
env_overrides = _detect_env_overrides(config)
|
|
84
|
+
|
|
85
|
+
groups: dict[str, list[tuple[str, Any, bool]]] = {}
|
|
86
|
+
for key in AppConfig.model_fields:
|
|
87
|
+
section = AppConfig.guess_section(key)
|
|
88
|
+
value = getattr(config, key)
|
|
89
|
+
is_env = key in env_overrides
|
|
90
|
+
masked = _mask_value(key, str(value)) if key in SENSITIVE_FIELDS and value else str(value)
|
|
91
|
+
groups.setdefault(section, []).append((key, masked, is_env))
|
|
92
|
+
|
|
93
|
+
for section in sorted(groups, key=_section_order):
|
|
94
|
+
typer.echo(typer.style(f"[{section}]", bold=True, fg=typer.colors.CYAN))
|
|
95
|
+
for key, value, is_env in groups[section]:
|
|
96
|
+
suffix = typer.style(" (env)", dim=True, fg=typer.colors.YELLOW) if is_env else ""
|
|
97
|
+
typer.echo(f" {key} = {value}{suffix}")
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
@config_app.command(name="set")
|
|
101
|
+
def config_set(
|
|
102
|
+
key: str = typer.Argument(..., help="Config key"),
|
|
103
|
+
value: str = typer.Argument(..., help="Config value"),
|
|
104
|
+
config_path: str = typer.Option("data/config.json", "--config", help="Config path"),
|
|
105
|
+
):
|
|
106
|
+
"""Set a config value."""
|
|
107
|
+
from ai_mini_box.infrastructure.config import JsonConfigManager, SENSITIVE_FIELDS
|
|
108
|
+
|
|
109
|
+
manager = JsonConfigManager(config_path)
|
|
110
|
+
try:
|
|
111
|
+
manager.set(key, value)
|
|
112
|
+
except ValueError as e:
|
|
113
|
+
typer.echo(typer.style(f"Error: {e}", fg=typer.colors.RED))
|
|
114
|
+
raise typer.Exit(code=1)
|
|
115
|
+
|
|
116
|
+
label = f"{key} = {value}"
|
|
117
|
+
if key in SENSITIVE_FIELDS:
|
|
118
|
+
label = f"{key} = ***"
|
|
119
|
+
typer.echo(typer.style(f" Updated: {label}", fg=typer.colors.GREEN))
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
@config_app.command(name="unset")
|
|
123
|
+
def config_unset(
|
|
124
|
+
key: str = typer.Argument(..., help="Config key to reset to default"),
|
|
125
|
+
config_path: str = typer.Option("data/config.json", "--config", help="Config path"),
|
|
126
|
+
):
|
|
127
|
+
"""Reset a config key to its default value."""
|
|
128
|
+
from ai_mini_box.infrastructure.config import JsonConfigManager
|
|
129
|
+
|
|
130
|
+
manager = JsonConfigManager(config_path)
|
|
131
|
+
try:
|
|
132
|
+
changed = manager.unset(key)
|
|
133
|
+
except ValueError as e:
|
|
134
|
+
typer.echo(typer.style(f"Error: {e}", fg=typer.colors.RED))
|
|
135
|
+
raise typer.Exit(code=1)
|
|
136
|
+
|
|
137
|
+
if changed:
|
|
138
|
+
typer.echo(typer.style(f" Reset: {key} to default", fg=typer.colors.GREEN))
|
|
139
|
+
else:
|
|
140
|
+
typer.echo(f" {key} already at default")
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
def _mask_value(key: str, value: str) -> str:
|
|
144
|
+
if len(value) <= 4:
|
|
145
|
+
return "****"
|
|
146
|
+
return value[:2] + "*" * (len(value) - 5) + value[-3:]
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
def _detect_env_overrides(config) -> set[str]:
|
|
150
|
+
from ai_mini_box.infrastructure.config import AppConfig
|
|
151
|
+
|
|
152
|
+
overrides = set()
|
|
153
|
+
prefix = "AI_BOX_"
|
|
154
|
+
for field_name in AppConfig.model_fields:
|
|
155
|
+
env_key = f"{prefix}{field_name.upper()}"
|
|
156
|
+
if os.environ.get(env_key) is not None:
|
|
157
|
+
overrides.add(field_name)
|
|
158
|
+
return overrides
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
_SECTION_ORDER = {
|
|
162
|
+
"Telegram": 1, "Email": 2, "LLM": 3, "Schedule": 4,
|
|
163
|
+
"WhatsApp": 5, "Notifications": 6, "SMS": 7,
|
|
164
|
+
"YooKassa": 8, "Tinkoff": 9, "Sber": 10, "General": 11,
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
def _section_order(s: str) -> int:
|
|
169
|
+
return _SECTION_ORDER.get(s, 99)
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
for ep in importlib.metadata.entry_points(group="ai_mini_box.tools"):
|
|
173
|
+
try:
|
|
174
|
+
register_func = ep.load()
|
|
175
|
+
if callable(register_func):
|
|
176
|
+
register_func(app)
|
|
177
|
+
except Exception as e:
|
|
178
|
+
typer.echo(f"Warning: failed to load tool {ep.name}: {e}", err=True)
|
|
File without changes
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
from dataclasses import dataclass, field
|
|
2
|
+
from typing import Optional
|
|
3
|
+
|
|
4
|
+
from sqlalchemy.orm import Session
|
|
5
|
+
|
|
6
|
+
from ai_mini_box.infrastructure.repositories import (
|
|
7
|
+
SqliteContactRepo,
|
|
8
|
+
SqliteMessageRepo,
|
|
9
|
+
SqliteOrderRepo,
|
|
10
|
+
SqliteProductRepo,
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class RepoContainer:
|
|
15
|
+
def __init__(self, session: Session):
|
|
16
|
+
self._session = session
|
|
17
|
+
self.contacts = SqliteContactRepo(session)
|
|
18
|
+
self.products = SqliteProductRepo(session)
|
|
19
|
+
self.messages = SqliteMessageRepo(session)
|
|
20
|
+
self.orders = SqliteOrderRepo(session)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
@dataclass
|
|
24
|
+
class AppContext:
|
|
25
|
+
repos: RepoContainer
|
|
26
|
+
config_path: str = "data/config.json"
|
|
27
|
+
verbose: bool = False
|
|
28
|
+
|
|
29
|
+
_instance: Optional["AppContext"] = field(default=None, repr=False)
|
|
30
|
+
|
|
31
|
+
@classmethod
|
|
32
|
+
def init(cls, repos: RepoContainer, **kwargs) -> "AppContext":
|
|
33
|
+
cls._instance = cls(repos=repos, **kwargs)
|
|
34
|
+
return cls._instance
|
|
35
|
+
|
|
36
|
+
@classmethod
|
|
37
|
+
def get(cls) -> "AppContext":
|
|
38
|
+
if cls._instance is None:
|
|
39
|
+
raise RuntimeError("AppContext not initialized. Call AppContext.init() first.")
|
|
40
|
+
return cls._instance
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
class AppError(Exception):
|
|
2
|
+
def __init__(self, message: str, exit_code: int = 1):
|
|
3
|
+
self.message = message
|
|
4
|
+
self.exit_code = exit_code
|
|
5
|
+
super().__init__(message)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class NotFoundError(AppError):
|
|
9
|
+
def __init__(self, entity: str, id: int):
|
|
10
|
+
super().__init__(f"{entity} with id {id} not found", exit_code=2)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class ConfigError(AppError):
|
|
14
|
+
def __init__(self, message: str):
|
|
15
|
+
super().__init__(message, exit_code=3)
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
from datetime import datetime
|
|
2
|
+
from enum import Enum
|
|
3
|
+
from typing import Optional
|
|
4
|
+
|
|
5
|
+
from pydantic import BaseModel, Field
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class Topic(str, Enum):
|
|
9
|
+
PRICES = "Цены"
|
|
10
|
+
ORDER = "Заказ"
|
|
11
|
+
COMPLAINT = "Жалоба"
|
|
12
|
+
SCHEDULE = "График"
|
|
13
|
+
OTHER = "Другое"
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class MessageSource(str, Enum):
|
|
17
|
+
TELEGRAM = "telegram"
|
|
18
|
+
EMAIL = "email"
|
|
19
|
+
WHATSAPP = "whatsapp"
|
|
20
|
+
SMS = "sms"
|
|
21
|
+
MANUAL = "manual"
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class OrderStatus(str, Enum):
|
|
25
|
+
NEW = "new"
|
|
26
|
+
PROCESSING = "processing"
|
|
27
|
+
COMPLETED = "completed"
|
|
28
|
+
CANCELLED = "cancelled"
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class Contact(BaseModel):
|
|
32
|
+
id: Optional[int] = None
|
|
33
|
+
name: str = ""
|
|
34
|
+
phone: Optional[str] = None
|
|
35
|
+
email: Optional[str] = None
|
|
36
|
+
telegram: Optional[str] = None
|
|
37
|
+
whatsapp: Optional[str] = None
|
|
38
|
+
source: MessageSource = MessageSource.MANUAL
|
|
39
|
+
notes: Optional[str] = None
|
|
40
|
+
total_spent: int = 0
|
|
41
|
+
created_at: datetime = Field(default_factory=datetime.now)
|
|
42
|
+
updated_at: datetime = Field(default_factory=datetime.now)
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
class Product(BaseModel):
|
|
46
|
+
id: Optional[int] = None
|
|
47
|
+
name: str = ""
|
|
48
|
+
description: Optional[str] = None
|
|
49
|
+
price_kopecks: int = 0
|
|
50
|
+
stock: int = 0
|
|
51
|
+
unit: str = "шт"
|
|
52
|
+
category: Optional[str] = None
|
|
53
|
+
created_at: datetime = Field(default_factory=datetime.now)
|
|
54
|
+
updated_at: datetime = Field(default_factory=datetime.now)
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
class Message(BaseModel):
|
|
58
|
+
id: Optional[int] = None
|
|
59
|
+
source: MessageSource = MessageSource.MANUAL
|
|
60
|
+
external_id: Optional[str] = None
|
|
61
|
+
chat_id: Optional[str] = None
|
|
62
|
+
contact_id: Optional[int] = None
|
|
63
|
+
text: str = ""
|
|
64
|
+
topic: Optional[Topic] = None
|
|
65
|
+
draft_response: Optional[str] = None
|
|
66
|
+
sent_response: bool = False
|
|
67
|
+
received_at: datetime = Field(default_factory=datetime.now)
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
class Order(BaseModel):
|
|
71
|
+
id: Optional[int] = None
|
|
72
|
+
contact_id: Optional[int] = None
|
|
73
|
+
status: OrderStatus = OrderStatus.NEW
|
|
74
|
+
total_kopecks: int = 0
|
|
75
|
+
notes: Optional[str] = None
|
|
76
|
+
source_message_id: Optional[int] = None
|
|
77
|
+
created_at: datetime = Field(default_factory=datetime.now)
|
|
78
|
+
updated_at: datetime = Field(default_factory=datetime.now)
|