yt-gpt 1.0.16__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.
Files changed (81) hide show
  1. yt_gpt-1.0.16/LICENSE +21 -0
  2. yt_gpt-1.0.16/PKG-INFO +121 -0
  3. yt_gpt-1.0.16/README.md +73 -0
  4. yt_gpt-1.0.16/pyproject.toml +93 -0
  5. yt_gpt-1.0.16/setup.cfg +4 -0
  6. yt_gpt-1.0.16/src/yt_gpt/__init__.py +5 -0
  7. yt_gpt-1.0.16/src/yt_gpt/__main__.py +4 -0
  8. yt_gpt-1.0.16/src/yt_gpt/auth/__init__.py +5 -0
  9. yt_gpt-1.0.16/src/yt_gpt/auth/base.py +31 -0
  10. yt_gpt-1.0.16/src/yt_gpt/auth/local.py +123 -0
  11. yt_gpt-1.0.16/src/yt_gpt/auth/pyronites.py +85 -0
  12. yt_gpt-1.0.16/src/yt_gpt/auth/session.py +62 -0
  13. yt_gpt-1.0.16/src/yt_gpt/cli.py +228 -0
  14. yt_gpt-1.0.16/src/yt_gpt/config.py +169 -0
  15. yt_gpt-1.0.16/src/yt_gpt/events.py +41 -0
  16. yt_gpt-1.0.16/src/yt_gpt/ingest/__init__.py +0 -0
  17. yt_gpt-1.0.16/src/yt_gpt/ingest/captions.py +189 -0
  18. yt_gpt-1.0.16/src/yt_gpt/ingest/chunk.py +98 -0
  19. yt_gpt-1.0.16/src/yt_gpt/ingest/clean.py +134 -0
  20. yt_gpt-1.0.16/src/yt_gpt/ingest/jobs.py +248 -0
  21. yt_gpt-1.0.16/src/yt_gpt/ingest/metadata.py +171 -0
  22. yt_gpt-1.0.16/src/yt_gpt/ingest/urls.py +113 -0
  23. yt_gpt-1.0.16/src/yt_gpt/ingest/whisper.py +80 -0
  24. yt_gpt-1.0.16/src/yt_gpt/llm/__init__.py +0 -0
  25. yt_gpt-1.0.16/src/yt_gpt/llm/capabilities.py +60 -0
  26. yt_gpt-1.0.16/src/yt_gpt/llm/chat.py +215 -0
  27. yt_gpt-1.0.16/src/yt_gpt/llm/context.py +145 -0
  28. yt_gpt-1.0.16/src/yt_gpt/llm/embeddings.py +58 -0
  29. yt_gpt-1.0.16/src/yt_gpt/llm/prompts.py +122 -0
  30. yt_gpt-1.0.16/src/yt_gpt/llm/provider.py +281 -0
  31. yt_gpt-1.0.16/src/yt_gpt/llm/tools.py +313 -0
  32. yt_gpt-1.0.16/src/yt_gpt/retrieval/__init__.py +0 -0
  33. yt_gpt-1.0.16/src/yt_gpt/retrieval/bm25.py +92 -0
  34. yt_gpt-1.0.16/src/yt_gpt/retrieval/hybrid.py +118 -0
  35. yt_gpt-1.0.16/src/yt_gpt/server/__init__.py +0 -0
  36. yt_gpt-1.0.16/src/yt_gpt/server/app.py +83 -0
  37. yt_gpt-1.0.16/src/yt_gpt/server/deps.py +76 -0
  38. yt_gpt-1.0.16/src/yt_gpt/server/routes/__init__.py +0 -0
  39. yt_gpt-1.0.16/src/yt_gpt/server/routes/auth.py +50 -0
  40. yt_gpt-1.0.16/src/yt_gpt/server/routes/sessions.py +167 -0
  41. yt_gpt-1.0.16/src/yt_gpt/server/routes/settings.py +76 -0
  42. yt_gpt-1.0.16/src/yt_gpt/server/routes/sources.py +103 -0
  43. yt_gpt-1.0.16/src/yt_gpt/server/routes/videos.py +99 -0
  44. yt_gpt-1.0.16/src/yt_gpt/server/routes/workspaces.py +89 -0
  45. yt_gpt-1.0.16/src/yt_gpt/server/sse.py +43 -0
  46. yt_gpt-1.0.16/src/yt_gpt/server/static/assets/index-Bf9RxVAn.js +233 -0
  47. yt_gpt-1.0.16/src/yt_gpt/server/static/assets/index-DuC3Y8BP.css +1 -0
  48. yt_gpt-1.0.16/src/yt_gpt/server/static/favicon.svg +6 -0
  49. yt_gpt-1.0.16/src/yt_gpt/server/static/fonts/dm-mono-400-normal.woff2 +0 -0
  50. yt_gpt-1.0.16/src/yt_gpt/server/static/fonts/dm-mono-500-normal.woff2 +0 -0
  51. yt_gpt-1.0.16/src/yt_gpt/server/static/fonts/supreme-400-normal.woff2 +0 -0
  52. yt_gpt-1.0.16/src/yt_gpt/server/static/fonts/supreme-500-normal.woff2 +0 -0
  53. yt_gpt-1.0.16/src/yt_gpt/server/static/fonts/supreme-700-normal.woff2 +0 -0
  54. yt_gpt-1.0.16/src/yt_gpt/server/static/index.html +67 -0
  55. yt_gpt-1.0.16/src/yt_gpt/server/static/og.png +0 -0
  56. yt_gpt-1.0.16/src/yt_gpt/server/static.py +51 -0
  57. yt_gpt-1.0.16/src/yt_gpt/settings_store.py +188 -0
  58. yt_gpt-1.0.16/src/yt_gpt/sources/__init__.py +0 -0
  59. yt_gpt-1.0.16/src/yt_gpt/util.py +171 -0
  60. yt_gpt-1.0.16/src/yt_gpt/version.py +3 -0
  61. yt_gpt-1.0.16/src/yt_gpt/vision/__init__.py +0 -0
  62. yt_gpt-1.0.16/src/yt_gpt/vision/frames.py +353 -0
  63. yt_gpt-1.0.16/src/yt_gpt/workspace/__init__.py +11 -0
  64. yt_gpt-1.0.16/src/yt_gpt/workspace/export.py +56 -0
  65. yt_gpt-1.0.16/src/yt_gpt/workspace/models.py +85 -0
  66. yt_gpt-1.0.16/src/yt_gpt/workspace/store.py +854 -0
  67. yt_gpt-1.0.16/src/yt_gpt.egg-info/PKG-INFO +121 -0
  68. yt_gpt-1.0.16/src/yt_gpt.egg-info/SOURCES.txt +79 -0
  69. yt_gpt-1.0.16/src/yt_gpt.egg-info/dependency_links.txt +1 -0
  70. yt_gpt-1.0.16/src/yt_gpt.egg-info/entry_points.txt +2 -0
  71. yt_gpt-1.0.16/src/yt_gpt.egg-info/requires.txt +29 -0
  72. yt_gpt-1.0.16/src/yt_gpt.egg-info/top_level.txt +1 -0
  73. yt_gpt-1.0.16/tests/test_api.py +198 -0
  74. yt_gpt-1.0.16/tests/test_bm25.py +72 -0
  75. yt_gpt-1.0.16/tests/test_chat.py +160 -0
  76. yt_gpt-1.0.16/tests/test_chunk.py +58 -0
  77. yt_gpt-1.0.16/tests/test_clean.py +50 -0
  78. yt_gpt-1.0.16/tests/test_provider.py +45 -0
  79. yt_gpt-1.0.16/tests/test_store.py +108 -0
  80. yt_gpt-1.0.16/tests/test_urls.py +36 -0
  81. yt_gpt-1.0.16/tests/test_vision.py +31 -0
yt_gpt-1.0.16/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 yt-gpt contributors
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.
yt_gpt-1.0.16/PKG-INFO ADDED
@@ -0,0 +1,121 @@
1
+ Metadata-Version: 2.4
2
+ Name: yt-gpt
3
+ Version: 1.0.16
4
+ Summary: A local, NotebookLM-style workspace for YouTube: transcripts, retrieval and grounded chat.
5
+ Author: yt-gpt contributors
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://github.com/anomalyco/yt-gpt
8
+ Project-URL: Documentation, https://github.com/anomalyco/yt-gpt#readme
9
+ Project-URL: Issues, https://github.com/anomalyco/yt-gpt/issues
10
+ Keywords: youtube,transcripts,llm,rag,notebooklm
11
+ Classifier: Development Status :: 4 - Beta
12
+ Classifier: Environment :: Web Environment
13
+ Classifier: Intended Audience :: End Users/Desktop
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Programming Language :: Python :: 3.10
16
+ Classifier: Programming Language :: Python :: 3.11
17
+ Classifier: Programming Language :: Python :: 3.12
18
+ Classifier: Topic :: Multimedia :: Video
19
+ Classifier: Topic :: Text Processing :: Indexing
20
+ Requires-Python: >=3.10
21
+ Description-Content-Type: text/markdown
22
+ License-File: LICENSE
23
+ Requires-Dist: fastapi>=0.115
24
+ Requires-Dist: uvicorn[standard]>=0.30
25
+ Requires-Dist: pydantic>=2.7
26
+ Requires-Dist: httpx>=0.27
27
+ Requires-Dist: yt-dlp>=2024.10.7
28
+ Requires-Dist: litellm>=1.50
29
+ Requires-Dist: numpy>=1.26
30
+ Requires-Dist: keyring>=25
31
+ Requires-Dist: argon2-cffi>=23.1
32
+ Requires-Dist: imageio-ffmpeg>=0.5
33
+ Requires-Dist: Pillow>=10.0
34
+ Requires-Dist: imagehash>=4.3
35
+ Requires-Dist: tomli>=2.0; python_version < "3.11"
36
+ Requires-Dist: tomli-w>=1.0
37
+ Provides-Extra: whisper
38
+ Requires-Dist: faster-whisper>=1.0; extra == "whisper"
39
+ Provides-Extra: dev
40
+ Requires-Dist: pytest>=8.0; extra == "dev"
41
+ Requires-Dist: pytest-asyncio>=0.23; extra == "dev"
42
+ Requires-Dist: ruff>=0.6; extra == "dev"
43
+ Requires-Dist: mypy>=1.10; extra == "dev"
44
+ Requires-Dist: build>=1.2; extra == "dev"
45
+ Provides-Extra: all
46
+ Requires-Dist: yt-gpt[whisper]; extra == "all"
47
+ Dynamic: license-file
48
+
49
+ # yt-gpt backend
50
+
51
+ The FastAPI service, ingestion pipeline, retrieval engine and CLI that power the
52
+ `yt-gpt` package. The compiled frontend is bundled under
53
+ `src/yt_gpt/server/static` and served by the same process.
54
+
55
+ ```bash
56
+ pip install -e ".[dev]"
57
+ yt-gpt serve
58
+ ```
59
+
60
+ Open <http://127.0.0.1:8765>.
61
+
62
+ ## Layout
63
+
64
+ ```
65
+ src/yt_gpt/
66
+ ├── cli.py # yt-gpt serve | doctor | embed | export | version
67
+ ├── config.py # ~/.yt-gpt, config.toml, secret key
68
+ ├── server/ # FastAPI app, routes, SSE, static bundle
69
+ ├── auth/ # AuthProvider + local password profiles
70
+ ├── workspace/ # atomic file store, models, transcripts.md export
71
+ ├── ingest/ # URL normalisation, yt-dlp, captions, cleaning, chunking, jobs
72
+ ├── retrieval/ # BM25 index + optional dense vectors + RRF
73
+ ├── llm/ # LiteLLM wrapper, embeddings, prompts, context, chat, tools
74
+ ├── vision/ # lazy scene sampling, contact sheets, exact frames
75
+ └── sources/ # reserved namespace; sources are stored by workspace/store.py
76
+ ```
77
+
78
+ ## Data
79
+
80
+ Everything lives under `~/.yt-gpt` (override with `YT_GPT_HOME`). There is no
81
+ database. `segments.jsonl` and `meta.json` are the source of truth; `clean.txt`,
82
+ `chunks.jsonl`, `vectors.npy` and `transcripts.md` are derived. `yt-gpt doctor
83
+ --rebuild` rebuilds text artifacts; `yt-gpt embed` rebuilds vectors.
84
+
85
+ API keys are stored in the OS keyring. If no keyring backend is available, a
86
+ `0600` file under `~/.yt-gpt/secrets/` is used instead.
87
+
88
+ ## Development
89
+
90
+ ```bash
91
+ pip install -e ".[dev]"
92
+ pytest
93
+ ruff check .
94
+ ruff format --check .
95
+ mypy src
96
+ ```
97
+
98
+ To run against the Vite dev server, build the frontend with
99
+ `VITE_API_MODE=http VITE_API_URL=http://127.0.0.1:8765 npm run build`, or run
100
+ `npm run dev` and keep the API on port 8765 (CORS for `localhost:5173` and
101
+ `127.0.0.1:5173` is enabled).
102
+
103
+ ## Publishing
104
+
105
+ The workflow at `.github/workflows/autopublish.yml` is manually triggered with a
106
+ version string. It builds the frontend with `VITE_API_MODE=http`, bundles the
107
+ dist output, runs lint/type checks and the test suite, updates
108
+ `yt_gpt/version.py`, builds the wheel/sdist and publishes to PyPI. Configure a PyPI Trusted Publisher for this repository and workflow (or
109
+ add a `PYPI_API_TOKEN` repository secret as a fallback).
110
+
111
+ ## Notes
112
+
113
+ - The server binds to `127.0.0.1` by default. Use `--host` only when you know
114
+ what you are doing.
115
+ - Whisper is optional: `pip install "yt-gpt[whisper]"`.
116
+ - Visual analysis is lazy and opt-in. The workspace and session toggles default to
117
+ off, text-only workspaces never download video, and frames require a vision-capable
118
+ chat model.
119
+ - Retrieval is hybrid when an embedding model is configured: vector search is fused
120
+ with BM25 using reciprocal rank fusion. Without vectors it falls back to BM25.
121
+ - License: MIT (see `LICENSE`).
@@ -0,0 +1,73 @@
1
+ # yt-gpt backend
2
+
3
+ The FastAPI service, ingestion pipeline, retrieval engine and CLI that power the
4
+ `yt-gpt` package. The compiled frontend is bundled under
5
+ `src/yt_gpt/server/static` and served by the same process.
6
+
7
+ ```bash
8
+ pip install -e ".[dev]"
9
+ yt-gpt serve
10
+ ```
11
+
12
+ Open <http://127.0.0.1:8765>.
13
+
14
+ ## Layout
15
+
16
+ ```
17
+ src/yt_gpt/
18
+ ├── cli.py # yt-gpt serve | doctor | embed | export | version
19
+ ├── config.py # ~/.yt-gpt, config.toml, secret key
20
+ ├── server/ # FastAPI app, routes, SSE, static bundle
21
+ ├── auth/ # AuthProvider + local password profiles
22
+ ├── workspace/ # atomic file store, models, transcripts.md export
23
+ ├── ingest/ # URL normalisation, yt-dlp, captions, cleaning, chunking, jobs
24
+ ├── retrieval/ # BM25 index + optional dense vectors + RRF
25
+ ├── llm/ # LiteLLM wrapper, embeddings, prompts, context, chat, tools
26
+ ├── vision/ # lazy scene sampling, contact sheets, exact frames
27
+ └── sources/ # reserved namespace; sources are stored by workspace/store.py
28
+ ```
29
+
30
+ ## Data
31
+
32
+ Everything lives under `~/.yt-gpt` (override with `YT_GPT_HOME`). There is no
33
+ database. `segments.jsonl` and `meta.json` are the source of truth; `clean.txt`,
34
+ `chunks.jsonl`, `vectors.npy` and `transcripts.md` are derived. `yt-gpt doctor
35
+ --rebuild` rebuilds text artifacts; `yt-gpt embed` rebuilds vectors.
36
+
37
+ API keys are stored in the OS keyring. If no keyring backend is available, a
38
+ `0600` file under `~/.yt-gpt/secrets/` is used instead.
39
+
40
+ ## Development
41
+
42
+ ```bash
43
+ pip install -e ".[dev]"
44
+ pytest
45
+ ruff check .
46
+ ruff format --check .
47
+ mypy src
48
+ ```
49
+
50
+ To run against the Vite dev server, build the frontend with
51
+ `VITE_API_MODE=http VITE_API_URL=http://127.0.0.1:8765 npm run build`, or run
52
+ `npm run dev` and keep the API on port 8765 (CORS for `localhost:5173` and
53
+ `127.0.0.1:5173` is enabled).
54
+
55
+ ## Publishing
56
+
57
+ The workflow at `.github/workflows/autopublish.yml` is manually triggered with a
58
+ version string. It builds the frontend with `VITE_API_MODE=http`, bundles the
59
+ dist output, runs lint/type checks and the test suite, updates
60
+ `yt_gpt/version.py`, builds the wheel/sdist and publishes to PyPI. Configure a PyPI Trusted Publisher for this repository and workflow (or
61
+ add a `PYPI_API_TOKEN` repository secret as a fallback).
62
+
63
+ ## Notes
64
+
65
+ - The server binds to `127.0.0.1` by default. Use `--host` only when you know
66
+ what you are doing.
67
+ - Whisper is optional: `pip install "yt-gpt[whisper]"`.
68
+ - Visual analysis is lazy and opt-in. The workspace and session toggles default to
69
+ off, text-only workspaces never download video, and frames require a vision-capable
70
+ chat model.
71
+ - Retrieval is hybrid when an embedding model is configured: vector search is fused
72
+ with BM25 using reciprocal rank fusion. Without vectors it falls back to BM25.
73
+ - License: MIT (see `LICENSE`).
@@ -0,0 +1,93 @@
1
+ [build-system]
2
+ requires = ["setuptools>=69", "wheel"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "yt-gpt"
7
+ dynamic = ["version"]
8
+ description = "A local, NotebookLM-style workspace for YouTube: transcripts, retrieval and grounded chat."
9
+ readme = "README.md"
10
+ requires-python = ">=3.10"
11
+ license = "MIT"
12
+ authors = [{ name = "yt-gpt contributors" }]
13
+ keywords = ["youtube", "transcripts", "llm", "rag", "notebooklm"]
14
+ classifiers = [
15
+ "Development Status :: 4 - Beta",
16
+ "Environment :: Web Environment",
17
+ "Intended Audience :: End Users/Desktop",
18
+ "Programming Language :: Python :: 3",
19
+ "Programming Language :: Python :: 3.10",
20
+ "Programming Language :: Python :: 3.11",
21
+ "Programming Language :: Python :: 3.12",
22
+ "Topic :: Multimedia :: Video",
23
+ "Topic :: Text Processing :: Indexing",
24
+ ]
25
+ dependencies = [
26
+ "fastapi>=0.115",
27
+ "uvicorn[standard]>=0.30",
28
+ "pydantic>=2.7",
29
+ "httpx>=0.27",
30
+ "yt-dlp>=2024.10.7",
31
+ "litellm>=1.50",
32
+ "numpy>=1.26",
33
+ "keyring>=25",
34
+ "argon2-cffi>=23.1",
35
+ "imageio-ffmpeg>=0.5",
36
+ "Pillow>=10.0",
37
+ "imagehash>=4.3",
38
+ "tomli>=2.0; python_version < '3.11'",
39
+ "tomli-w>=1.0",
40
+ ]
41
+
42
+ [project.optional-dependencies]
43
+ whisper = ["faster-whisper>=1.0"]
44
+ dev = [
45
+ "pytest>=8.0",
46
+ "pytest-asyncio>=0.23",
47
+ "ruff>=0.6",
48
+ "mypy>=1.10",
49
+ "build>=1.2",
50
+ ]
51
+ all = ["yt-gpt[whisper]"]
52
+
53
+ [project.scripts]
54
+ yt-gpt = "yt_gpt.cli:main"
55
+
56
+ [project.urls]
57
+ Homepage = "https://github.com/anomalyco/yt-gpt"
58
+ Documentation = "https://github.com/anomalyco/yt-gpt#readme"
59
+ Issues = "https://github.com/anomalyco/yt-gpt/issues"
60
+
61
+ [tool.setuptools]
62
+ package-dir = { "" = "src" }
63
+ include-package-data = true
64
+
65
+ [tool.setuptools.packages.find]
66
+ where = ["src"]
67
+
68
+ [tool.setuptools.dynamic]
69
+ version = { attr = "yt_gpt.version.__version__" }
70
+
71
+ [tool.setuptools.package-data]
72
+ yt_gpt = ["server/static/**/*", "server/static/*"]
73
+
74
+ [tool.pytest.ini_options]
75
+ testpaths = ["tests"]
76
+ asyncio_mode = "auto"
77
+ filterwarnings = ["ignore::DeprecationWarning"]
78
+
79
+ [tool.ruff]
80
+ line-length = 110
81
+ target-version = "py310"
82
+ src = ["src", "tests"]
83
+
84
+ [tool.ruff.lint]
85
+ select = ["E", "F", "I", "UP", "B"]
86
+ ignore = ["E501", "B008"]
87
+
88
+ [tool.mypy]
89
+ python_version = "3.10"
90
+ ignore_missing_imports = true
91
+ check_untyped_defs = true
92
+ no_implicit_optional = true
93
+ warn_redundant_casts = true
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,5 @@
1
+ """yt-gpt: a local NotebookLM-style workspace for YouTube."""
2
+
3
+ from .version import __version__
4
+
5
+ __all__ = ["__version__"]
@@ -0,0 +1,4 @@
1
+ from .cli import main
2
+
3
+ if __name__ == "__main__":
4
+ raise SystemExit(main())
@@ -0,0 +1,5 @@
1
+ from .base import AuthError, AuthProvider
2
+ from .local import LocalAuthProvider, public_user
3
+ from .pyronites import PyronitesAuthProvider
4
+
5
+ __all__ = ["AuthError", "AuthProvider", "LocalAuthProvider", "PyronitesAuthProvider", "public_user"]
@@ -0,0 +1,31 @@
1
+ """The authentication seam.
2
+
3
+ The rest of the server only knows this protocol, so swapping the local password
4
+ provider for an account backend does not touch routes or the workspace store.
5
+ """
6
+
7
+ from __future__ import annotations
8
+
9
+ from typing import Any, Protocol, runtime_checkable
10
+
11
+ User = dict[str, Any]
12
+
13
+
14
+ @runtime_checkable
15
+ class AuthProvider(Protocol):
16
+ async def sign_up(self, name: str, email: str, password: str) -> User: ...
17
+
18
+ async def sign_in(self, email: str, password: str) -> User: ...
19
+
20
+ async def sign_out(self, token: str | None = None) -> None: ...
21
+
22
+ async def current_user(self, token: str | None) -> User | None: ...
23
+
24
+
25
+ class AuthError(Exception):
26
+ """A friendly authentication failure."""
27
+
28
+ def __init__(self, message: str, status_code: int = 400) -> None:
29
+ super().__init__(message)
30
+ self.message = message
31
+ self.status_code = status_code
@@ -0,0 +1,123 @@
1
+ """Local password profiles backed by files under ``~/.yt-gpt/users``."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import asyncio
6
+ import re
7
+ import uuid
8
+ from pathlib import Path
9
+
10
+ from argon2 import PasswordHasher
11
+ from argon2.exceptions import VerifyMismatchError
12
+
13
+ from .. import config
14
+ from ..util import atomic_write_text, ensure_private, read_json, write_json
15
+ from .base import AuthError, User
16
+
17
+ EMAIL_RE = re.compile(r"^[^@\s]+@[^@\s]+\.[^@\s]+$")
18
+ MIN_PASSWORD = 6
19
+
20
+
21
+ def public_user(profile: dict) -> User:
22
+ return {
23
+ "id": profile.get("id", ""),
24
+ "email": profile.get("email", ""),
25
+ "name": profile.get("name", ""),
26
+ "createdAt": profile.get("created_at", ""),
27
+ }
28
+
29
+
30
+ class LocalAuthProvider:
31
+ def __init__(self, root: Path | None = None) -> None:
32
+ self.root = root or config.users_root()
33
+ self._lock = asyncio.Lock()
34
+ self._hasher = PasswordHasher()
35
+
36
+ def _profile_path(self, user_id: str) -> Path:
37
+ return self.root / user_id / "profile.json"
38
+
39
+ def _find_by_email(self, email: str) -> dict | None:
40
+ target = email.strip().lower()
41
+ if not self.root.exists():
42
+ return None
43
+ for child in self.root.iterdir():
44
+ if not child.is_dir():
45
+ continue
46
+ profile = read_json(child / "profile.json")
47
+ if isinstance(profile, dict) and str(profile.get("email", "")).lower() == target:
48
+ return profile
49
+ return None
50
+
51
+ async def sign_up(self, name: str, email: str, password: str) -> User:
52
+ clean_name = (name or "").strip()
53
+ clean_email = (email or "").strip().lower()
54
+ if not clean_name:
55
+ raise AuthError("Add a display name.")
56
+ if not EMAIL_RE.match(clean_email):
57
+ raise AuthError("That email does not look right.")
58
+ if len(password or "") < MIN_PASSWORD:
59
+ raise AuthError(f"Use at least {MIN_PASSWORD} characters for the password.")
60
+
61
+ async with self._lock:
62
+ if self._find_by_email(clean_email):
63
+ raise AuthError("A profile already uses that email.", 409)
64
+ user_id = f"u_{uuid.uuid4().hex[:12]}"
65
+ user_dir = self.root / user_id
66
+ user_dir.mkdir(parents=True, exist_ok=True)
67
+ profile = {
68
+ "id": user_id,
69
+ "email": clean_email,
70
+ "name": clean_name,
71
+ "created_at": _now(),
72
+ "password_hash": await asyncio.to_thread(self._hasher.hash, password),
73
+ }
74
+ path = self._profile_path(user_id)
75
+ write_json(path, profile)
76
+ ensure_private(path)
77
+ (user_dir / "workspaces").mkdir(parents=True, exist_ok=True)
78
+ return public_user(profile)
79
+
80
+ async def sign_in(self, email: str, password: str) -> User:
81
+ clean_email = (email or "").strip().lower()
82
+ async with self._lock:
83
+ profile = await asyncio.to_thread(self._find_by_email, clean_email)
84
+ if not profile:
85
+ raise AuthError("Email or password is wrong.", 401)
86
+ password_hash = profile.get("password_hash")
87
+ if not password_hash:
88
+ raise AuthError("This profile has no password.", 401)
89
+ try:
90
+ await asyncio.to_thread(self._hasher.verify, password_hash, password)
91
+ except VerifyMismatchError:
92
+ raise AuthError("Email or password is wrong.", 401) from None
93
+ except Exception:
94
+ raise AuthError("Email or password is wrong.", 401) from None
95
+ return public_user(profile)
96
+
97
+ async def sign_out(self, token: str | None = None) -> None:
98
+ return None
99
+
100
+ async def current_user(self, token: str | None) -> User | None:
101
+ if not token:
102
+ return None
103
+ if not token.startswith("u_") or any(
104
+ ch not in "abcdefghijklmnopqrstuvwxyz0123456789_" for ch in token
105
+ ):
106
+ return None
107
+ profile = await asyncio.to_thread(read_json, self._profile_path(token))
108
+ if not isinstance(profile, dict):
109
+ return None
110
+ return public_user(profile)
111
+
112
+ async def get_profile(self, user_id: str) -> dict | None:
113
+ profile = await asyncio.to_thread(read_json, self._profile_path(user_id))
114
+ return profile if isinstance(profile, dict) else None
115
+
116
+
117
+ def _now() -> str:
118
+ from ..util import iso_now
119
+
120
+ return iso_now()
121
+
122
+
123
+ __all__ = ["LocalAuthProvider", "public_user", "atomic_write_text"]
@@ -0,0 +1,85 @@
1
+ """Optional Pyronites account provider.
2
+
3
+ The provider is intentionally lazy: the package is only needed when a user
4
+ selects ``[auth] provider = "pyronites"`` in ``config.toml``. The local provider
5
+ remains the default so a fresh install still works offline.
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ import os
11
+
12
+ from .base import AuthError, User
13
+
14
+
15
+ class PyronitesAuthProvider:
16
+ def __init__(self, url: str | None = None, api_key: str | None = None) -> None:
17
+ self.url = url or os.environ.get("PYRONITES_URL", "")
18
+ self.api_key = api_key or os.environ.get("PYRONITES_KEY", "")
19
+ self._client = None
20
+
21
+ def _ensure_client(self):
22
+ if self._client is not None:
23
+ return self._client
24
+ if not self.url or not self.api_key:
25
+ raise AuthError(
26
+ "Pyronites auth needs PYRONITES_URL and PYRONITES_KEY (or the same values in config.toml).",
27
+ 500,
28
+ )
29
+ try:
30
+ from pyronites import create_client # type: ignore[import-not-found]
31
+ except ModuleNotFoundError as exc: # pragma: no cover - optional dependency
32
+ raise AuthError(
33
+ "Pyronites auth is selected but the pyronites package is not installed. "
34
+ 'Install it with `pip install "yt-gpt[pyronites]"` or switch to local auth.',
35
+ 500,
36
+ ) from exc
37
+ self._client = create_client(url=self.url, api_key=self.api_key)
38
+ return self._client
39
+
40
+ @staticmethod
41
+ def _to_user(raw) -> User:
42
+ if isinstance(raw, dict):
43
+ data = raw
44
+ else:
45
+ data = {
46
+ "id": getattr(raw, "id", ""),
47
+ "email": getattr(raw, "email", ""),
48
+ "name": getattr(raw, "name", getattr(raw, "email", "")),
49
+ "created_at": getattr(raw, "created_at", ""),
50
+ }
51
+ return {
52
+ "id": str(data.get("id", "")),
53
+ "email": str(data.get("email", "")),
54
+ "name": str(data.get("name", data.get("email", ""))),
55
+ "createdAt": str(data.get("created_at", data.get("createdAt", ""))),
56
+ }
57
+
58
+ async def sign_up(self, name: str, email: str, password: str) -> User:
59
+ client = self._ensure_client()
60
+ try:
61
+ raw = client.auth.sign_up(email=email, password=password, name=name)
62
+ except Exception as exc: # pragma: no cover - remote backend
63
+ raise AuthError(str(exc) or "Pyronites sign-up failed.", 400) from exc
64
+ return self._to_user(raw)
65
+
66
+ async def sign_in(self, email: str, password: str) -> User:
67
+ client = self._ensure_client()
68
+ try:
69
+ raw = client.auth.sign_in(email=email, password=password)
70
+ except Exception as exc: # pragma: no cover - remote backend
71
+ raise AuthError(str(exc) or "Email or password is wrong.", 401) from exc
72
+ return self._to_user(raw)
73
+
74
+ async def sign_out(self, token: str | None = None) -> None:
75
+ return None
76
+
77
+ async def current_user(self, token: str | None) -> User | None:
78
+ if not token:
79
+ return None
80
+ client = self._ensure_client()
81
+ try:
82
+ raw = client.auth.user(token=token)
83
+ except Exception: # pragma: no cover - remote backend
84
+ return None
85
+ return self._to_user(raw) if raw else None
@@ -0,0 +1,62 @@
1
+ """A tiny HMAC-signed cookie session, no middleware needed."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import base64
6
+ import hashlib
7
+ import hmac
8
+ import json
9
+ import time
10
+ from typing import Any
11
+
12
+ from .. import config
13
+
14
+ COOKIE_NAME = "yt_gpt_session"
15
+ MAX_AGE = 60 * 60 * 24 * 30
16
+
17
+
18
+ def _sign(payload: str) -> str:
19
+ key = config.secret_key().encode("utf-8")
20
+ return hmac.new(key, payload.encode("utf-8"), hashlib.sha256).hexdigest()
21
+
22
+
23
+ def encode_session(user_id: str) -> str:
24
+ body = {"uid": user_id, "iat": int(time.time())}
25
+ payload = base64.urlsafe_b64encode(json.dumps(body, separators=(",", ":")).encode("utf-8")).decode(
26
+ "ascii"
27
+ )
28
+ return f"{payload}.{_sign(payload)}"
29
+
30
+
31
+ def decode_session(value: str | None) -> dict[str, Any] | None:
32
+ if not value or "." not in value:
33
+ return None
34
+ payload, signature = value.rsplit(".", 1)
35
+ if not hmac.compare_digest(signature, _sign(payload)):
36
+ return None
37
+ try:
38
+ raw = base64.urlsafe_b64decode(payload.encode("ascii"))
39
+ data = json.loads(raw)
40
+ except (ValueError, TypeError):
41
+ return None
42
+ if not isinstance(data, dict) or not isinstance(data.get("uid"), str):
43
+ return None
44
+ issued = data.get("iat")
45
+ if isinstance(issued, int) and time.time() - issued > MAX_AGE:
46
+ return None
47
+ return data
48
+
49
+
50
+ def set_session_cookie(response: Any, user_id: str) -> None:
51
+ response.set_cookie(
52
+ COOKIE_NAME,
53
+ encode_session(user_id),
54
+ max_age=MAX_AGE,
55
+ httponly=True,
56
+ samesite="strict",
57
+ path="/",
58
+ )
59
+
60
+
61
+ def clear_session_cookie(response: Any) -> None:
62
+ response.delete_cookie(COOKIE_NAME, path="/")