ai-chat-vault 0.2.1__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_chat_vault-0.2.1/LICENSE +21 -0
- ai_chat_vault-0.2.1/PKG-INFO +109 -0
- ai_chat_vault-0.2.1/README.md +84 -0
- ai_chat_vault-0.2.1/pyproject.toml +42 -0
- ai_chat_vault-0.2.1/setup.cfg +4 -0
- ai_chat_vault-0.2.1/src/ai_chat_vault/__init__.py +7 -0
- ai_chat_vault-0.2.1/src/ai_chat_vault/__main__.py +6 -0
- ai_chat_vault-0.2.1/src/ai_chat_vault/cli.py +88 -0
- ai_chat_vault-0.2.1/src/ai_chat_vault/exporter.py +70 -0
- ai_chat_vault-0.2.1/src/ai_chat_vault/importers.py +287 -0
- ai_chat_vault-0.2.1/src/ai_chat_vault/models.py +27 -0
- ai_chat_vault-0.2.1/src/ai_chat_vault/server.py +661 -0
- ai_chat_vault-0.2.1/src/ai_chat_vault/vault.py +231 -0
- ai_chat_vault-0.2.1/src/ai_chat_vault.egg-info/PKG-INFO +109 -0
- ai_chat_vault-0.2.1/src/ai_chat_vault.egg-info/SOURCES.txt +18 -0
- ai_chat_vault-0.2.1/src/ai_chat_vault.egg-info/dependency_links.txt +1 -0
- ai_chat_vault-0.2.1/src/ai_chat_vault.egg-info/entry_points.txt +2 -0
- ai_chat_vault-0.2.1/src/ai_chat_vault.egg-info/requires.txt +3 -0
- ai_chat_vault-0.2.1/src/ai_chat_vault.egg-info/top_level.txt +1 -0
- ai_chat_vault-0.2.1/tests/test_vault.py +82 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 HbuAsherLucas
|
|
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,109 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: ai-chat-vault
|
|
3
|
+
Version: 0.2.1
|
|
4
|
+
Summary: Local-first archive, clean Markdown/Obsidian exporter, and instant full-text search (CLI + Web UI) for ChatGPT, Claude, and Gemini conversations.
|
|
5
|
+
Author: HbuAsherLucas
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/HbuAsherLucas/ai-chat-vault
|
|
8
|
+
Project-URL: Repository, https://github.com/HbuAsherLucas/ai-chat-vault
|
|
9
|
+
Project-URL: Issues, https://github.com/HbuAsherLucas/ai-chat-vault/issues
|
|
10
|
+
Keywords: chatgpt,claude,gemini,obsidian,markdown,sqlite,fts5,local-first,cli
|
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Intended Audience :: End Users/Desktop
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Topic :: Utilities
|
|
18
|
+
Classifier: Topic :: Text Processing :: Indexing
|
|
19
|
+
Requires-Python: >=3.11
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
License-File: LICENSE
|
|
22
|
+
Provides-Extra: dev
|
|
23
|
+
Requires-Dist: pytest>=8.0; extra == "dev"
|
|
24
|
+
Dynamic: license-file
|
|
25
|
+
|
|
26
|
+
# ai-chat-vault
|
|
27
|
+
|
|
28
|
+
[](https://pypi.org/project/ai-chat-vault/)
|
|
29
|
+
[](https://github.com/HbuAsherLucas/ai-chat-vault/actions/workflows/ci.yml)
|
|
30
|
+
[](LICENSE)
|
|
31
|
+
[](pyproject.toml)
|
|
32
|
+
[-brightgreen.svg)](pyproject.toml)
|
|
33
|
+
|
|
34
|
+
**Own your AI conversations.** `ai-chat-vault` is a **zero-dependency, local-first** CLI and modern Web UI that archives exports from **ChatGPT**, **Claude**, and **Gemini** into a single **SQLite + FTS5** vault, lets you full-text search across every chat in milliseconds, and exports clean **Obsidian-ready Markdown** files.
|
|
35
|
+
|
|
36
|
+

|
|
37
|
+
|
|
38
|
+
## Why `ai-chat-vault`?
|
|
39
|
+
|
|
40
|
+
If you use ChatGPT, Claude, and Gemini side by side:
|
|
41
|
+
- Your best prompts, debugging sessions, and ideas are scattered across three web apps.
|
|
42
|
+
- Native web search is slow or missing across accounts/workspaces.
|
|
43
|
+
- Switching accounts, losing access, or canceling a plan puts your history at risk.
|
|
44
|
+
- Exporting clean Markdown with YAML frontmatter for Obsidian / Notion / Git takes manual work.
|
|
45
|
+
|
|
46
|
+
`ai-chat-vault` runs **100% offline** with **zero external Python dependencies** (standard library only) and binds its local Web UI strictly to `127.0.0.1`.
|
|
47
|
+
|
|
48
|
+
## 30-Second Quickstart
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
pip install ai-chat-vault
|
|
52
|
+
|
|
53
|
+
# 1. Import your ChatGPT, Claude, or Gemini export (.zip or .json):
|
|
54
|
+
ai-chat-vault import conversations.zip
|
|
55
|
+
|
|
56
|
+
# 2. See stats across all providers:
|
|
57
|
+
ai-chat-vault stats
|
|
58
|
+
|
|
59
|
+
# 3. Instant full-text search (SQLite FTS5 + BM25):
|
|
60
|
+
ai-chat-vault search "Obsidian"
|
|
61
|
+
ai-chat-vault search "bm25" --provider chatgpt
|
|
62
|
+
|
|
63
|
+
# 4. Export clean Obsidian-ready Markdown + _INDEX.md:
|
|
64
|
+
ai-chat-vault export --out ./my-ai-notes
|
|
65
|
+
|
|
66
|
+
# 5. Launch the local dark/light Web UI with drag-and-drop import (127.0.0.1:8765):
|
|
67
|
+
ai-chat-vault serve
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## Supported Export Formats
|
|
71
|
+
|
|
72
|
+
| Provider | How to export | What to pass to `import` |
|
|
73
|
+
|---|---|---|
|
|
74
|
+
| **ChatGPT** | Settings -> Data Controls -> Export Data | The `.zip` archive or `conversations.json` |
|
|
75
|
+
| **Claude** | Settings -> Privacy -> Export Data | The `.zip` archive or `conversations.json` |
|
|
76
|
+
| **Gemini** | Google Takeout -> Gemini Apps (`MyActivity.json`) or structured JSON | The `.zip` archive or `.json` file |
|
|
77
|
+
|
|
78
|
+
Re-importing the same export is **idempotent** — conversations are upserted by provider + ID so you never get duplicates.
|
|
79
|
+
|
|
80
|
+
## CLI Reference
|
|
81
|
+
|
|
82
|
+
```bash
|
|
83
|
+
ai-chat-vault [--db ai-chat-vault.db] import <file.zip | file.json | dir/>
|
|
84
|
+
ai-chat-vault [--db ai-chat-vault.db] search "<query>" [--provider all|chatgpt|claude|gemini] [--limit 15] [--json]
|
|
85
|
+
ai-chat-vault [--db ai-chat-vault.db] export [--out exported] [--provider all|chatgpt|claude|gemini]
|
|
86
|
+
ai-chat-vault [--db ai-chat-vault.db] stats [--json]
|
|
87
|
+
ai-chat-vault [--db ai-chat-vault.db] serve [--host 127.0.0.1] [--port 8765]
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## Obsidian Markdown Output
|
|
91
|
+
|
|
92
|
+
Every exported file includes clean YAML frontmatter compatible with Obsidian Properties & Dataview:
|
|
93
|
+
|
|
94
|
+
```markdown
|
|
95
|
+
---
|
|
96
|
+
id: "claude:conv-claude-001"
|
|
97
|
+
title: "Designing an Obsidian Markdown frontmatter schema"
|
|
98
|
+
provider: claude
|
|
99
|
+
created_at: 2026-10-15T08:30:00Z
|
|
100
|
+
updated_at: 2026-10-15T08:35:00Z
|
|
101
|
+
tags: [ai-chat, claude]
|
|
102
|
+
---
|
|
103
|
+
|
|
104
|
+
# Designing an Obsidian Markdown frontmatter schema
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
## License
|
|
108
|
+
|
|
109
|
+
MIT — see [LICENSE](LICENSE).
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
# ai-chat-vault
|
|
2
|
+
|
|
3
|
+
[](https://pypi.org/project/ai-chat-vault/)
|
|
4
|
+
[](https://github.com/HbuAsherLucas/ai-chat-vault/actions/workflows/ci.yml)
|
|
5
|
+
[](LICENSE)
|
|
6
|
+
[](pyproject.toml)
|
|
7
|
+
[-brightgreen.svg)](pyproject.toml)
|
|
8
|
+
|
|
9
|
+
**Own your AI conversations.** `ai-chat-vault` is a **zero-dependency, local-first** CLI and modern Web UI that archives exports from **ChatGPT**, **Claude**, and **Gemini** into a single **SQLite + FTS5** vault, lets you full-text search across every chat in milliseconds, and exports clean **Obsidian-ready Markdown** files.
|
|
10
|
+
|
|
11
|
+

|
|
12
|
+
|
|
13
|
+
## Why `ai-chat-vault`?
|
|
14
|
+
|
|
15
|
+
If you use ChatGPT, Claude, and Gemini side by side:
|
|
16
|
+
- Your best prompts, debugging sessions, and ideas are scattered across three web apps.
|
|
17
|
+
- Native web search is slow or missing across accounts/workspaces.
|
|
18
|
+
- Switching accounts, losing access, or canceling a plan puts your history at risk.
|
|
19
|
+
- Exporting clean Markdown with YAML frontmatter for Obsidian / Notion / Git takes manual work.
|
|
20
|
+
|
|
21
|
+
`ai-chat-vault` runs **100% offline** with **zero external Python dependencies** (standard library only) and binds its local Web UI strictly to `127.0.0.1`.
|
|
22
|
+
|
|
23
|
+
## 30-Second Quickstart
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
pip install ai-chat-vault
|
|
27
|
+
|
|
28
|
+
# 1. Import your ChatGPT, Claude, or Gemini export (.zip or .json):
|
|
29
|
+
ai-chat-vault import conversations.zip
|
|
30
|
+
|
|
31
|
+
# 2. See stats across all providers:
|
|
32
|
+
ai-chat-vault stats
|
|
33
|
+
|
|
34
|
+
# 3. Instant full-text search (SQLite FTS5 + BM25):
|
|
35
|
+
ai-chat-vault search "Obsidian"
|
|
36
|
+
ai-chat-vault search "bm25" --provider chatgpt
|
|
37
|
+
|
|
38
|
+
# 4. Export clean Obsidian-ready Markdown + _INDEX.md:
|
|
39
|
+
ai-chat-vault export --out ./my-ai-notes
|
|
40
|
+
|
|
41
|
+
# 5. Launch the local dark/light Web UI with drag-and-drop import (127.0.0.1:8765):
|
|
42
|
+
ai-chat-vault serve
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Supported Export Formats
|
|
46
|
+
|
|
47
|
+
| Provider | How to export | What to pass to `import` |
|
|
48
|
+
|---|---|---|
|
|
49
|
+
| **ChatGPT** | Settings -> Data Controls -> Export Data | The `.zip` archive or `conversations.json` |
|
|
50
|
+
| **Claude** | Settings -> Privacy -> Export Data | The `.zip` archive or `conversations.json` |
|
|
51
|
+
| **Gemini** | Google Takeout -> Gemini Apps (`MyActivity.json`) or structured JSON | The `.zip` archive or `.json` file |
|
|
52
|
+
|
|
53
|
+
Re-importing the same export is **idempotent** — conversations are upserted by provider + ID so you never get duplicates.
|
|
54
|
+
|
|
55
|
+
## CLI Reference
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
ai-chat-vault [--db ai-chat-vault.db] import <file.zip | file.json | dir/>
|
|
59
|
+
ai-chat-vault [--db ai-chat-vault.db] search "<query>" [--provider all|chatgpt|claude|gemini] [--limit 15] [--json]
|
|
60
|
+
ai-chat-vault [--db ai-chat-vault.db] export [--out exported] [--provider all|chatgpt|claude|gemini]
|
|
61
|
+
ai-chat-vault [--db ai-chat-vault.db] stats [--json]
|
|
62
|
+
ai-chat-vault [--db ai-chat-vault.db] serve [--host 127.0.0.1] [--port 8765]
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Obsidian Markdown Output
|
|
66
|
+
|
|
67
|
+
Every exported file includes clean YAML frontmatter compatible with Obsidian Properties & Dataview:
|
|
68
|
+
|
|
69
|
+
```markdown
|
|
70
|
+
---
|
|
71
|
+
id: "claude:conv-claude-001"
|
|
72
|
+
title: "Designing an Obsidian Markdown frontmatter schema"
|
|
73
|
+
provider: claude
|
|
74
|
+
created_at: 2026-10-15T08:30:00Z
|
|
75
|
+
updated_at: 2026-10-15T08:35:00Z
|
|
76
|
+
tags: [ai-chat, claude]
|
|
77
|
+
---
|
|
78
|
+
|
|
79
|
+
# Designing an Obsidian Markdown frontmatter schema
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## License
|
|
83
|
+
|
|
84
|
+
MIT — see [LICENSE](LICENSE).
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=77"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "ai-chat-vault"
|
|
7
|
+
version = "0.2.1"
|
|
8
|
+
description = "Local-first archive, clean Markdown/Obsidian exporter, and instant full-text search (CLI + Web UI) for ChatGPT, Claude, and Gemini conversations."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
license-files = ["LICENSE"]
|
|
12
|
+
requires-python = ">=3.11"
|
|
13
|
+
authors = [{ name = "HbuAsherLucas" }]
|
|
14
|
+
keywords = ["chatgpt", "claude", "gemini", "obsidian", "markdown", "sqlite", "fts5", "local-first", "cli"]
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Development Status :: 4 - Beta",
|
|
17
|
+
"Intended Audience :: Developers",
|
|
18
|
+
"Intended Audience :: End Users/Desktop",
|
|
19
|
+
"Programming Language :: Python :: 3",
|
|
20
|
+
"Programming Language :: Python :: 3.11",
|
|
21
|
+
"Programming Language :: Python :: 3.12",
|
|
22
|
+
"Topic :: Utilities",
|
|
23
|
+
"Topic :: Text Processing :: Indexing",
|
|
24
|
+
]
|
|
25
|
+
dependencies = []
|
|
26
|
+
|
|
27
|
+
[project.urls]
|
|
28
|
+
Homepage = "https://github.com/HbuAsherLucas/ai-chat-vault"
|
|
29
|
+
Repository = "https://github.com/HbuAsherLucas/ai-chat-vault"
|
|
30
|
+
Issues = "https://github.com/HbuAsherLucas/ai-chat-vault/issues"
|
|
31
|
+
|
|
32
|
+
[project.optional-dependencies]
|
|
33
|
+
dev = ["pytest>=8.0"]
|
|
34
|
+
|
|
35
|
+
[project.scripts]
|
|
36
|
+
ai-chat-vault = "ai_chat_vault.cli:main"
|
|
37
|
+
|
|
38
|
+
[tool.setuptools.packages.find]
|
|
39
|
+
where = ["src"]
|
|
40
|
+
|
|
41
|
+
[tool.pytest.ini_options]
|
|
42
|
+
testpaths = ["tests"]
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"""CLI for ai-chat-vault: import | search | export | stats | serve."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
import argparse
|
|
5
|
+
import json
|
|
6
|
+
import sys
|
|
7
|
+
|
|
8
|
+
from .exporter import export_vault_markdown
|
|
9
|
+
from .importers import load_export_path
|
|
10
|
+
from .server import serve_vault
|
|
11
|
+
from .vault import ChatVault
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def main(argv: list[str] | None = None) -> int:
|
|
15
|
+
parser = argparse.ArgumentParser(
|
|
16
|
+
prog="ai-chat-vault",
|
|
17
|
+
description="Local-first archive, Markdown/Obsidian exporter, and instant full-text search for ChatGPT, Claude, and Gemini.",
|
|
18
|
+
)
|
|
19
|
+
parser.add_argument("--db", default="ai-chat-vault.db", help="Path to local SQLite vault database.")
|
|
20
|
+
sub = parser.add_subparsers(dest="command", required=True)
|
|
21
|
+
|
|
22
|
+
p_imp = sub.add_parser("import", help="Import ChatGPT, Claude, or Gemini export (.json, .zip, or directory).")
|
|
23
|
+
p_imp.add_argument("path", help="Export file, ZIP archive, or directory.")
|
|
24
|
+
|
|
25
|
+
p_search = sub.add_parser("search", help="Full-text search archived conversations.")
|
|
26
|
+
p_search.add_argument("query", help="Search keywords.")
|
|
27
|
+
p_search.add_argument("--provider", choices=["all", "chatgpt", "claude", "gemini"], default="all")
|
|
28
|
+
p_search.add_argument("--limit", type=int, default=15)
|
|
29
|
+
p_search.add_argument("--json", action="store_true", dest="as_json")
|
|
30
|
+
|
|
31
|
+
p_exp = sub.add_parser("export", help="Export vault conversations to Obsidian-friendly Markdown files.")
|
|
32
|
+
p_exp.add_argument("--out", default="exported", help="Output directory.")
|
|
33
|
+
p_exp.add_argument("--provider", choices=["all", "chatgpt", "claude", "gemini"], default="all")
|
|
34
|
+
|
|
35
|
+
p_stats = sub.add_parser("stats", help="Show archive statistics.")
|
|
36
|
+
p_stats.add_argument("--json", action="store_true", dest="as_json")
|
|
37
|
+
|
|
38
|
+
p_srv = sub.add_parser("serve", help="Start local Web UI for browsing and instant search.")
|
|
39
|
+
p_srv.add_argument("--host", default="127.0.0.1")
|
|
40
|
+
p_srv.add_argument("--port", type=int, default=8765)
|
|
41
|
+
|
|
42
|
+
args = parser.parse_args(argv)
|
|
43
|
+
|
|
44
|
+
with ChatVault(args.db) as vault:
|
|
45
|
+
if args.command == "import":
|
|
46
|
+
convs = load_export_path(args.path)
|
|
47
|
+
c_n, m_n = vault.upsert_conversations(convs)
|
|
48
|
+
print(f"Imported {c_n} conversation(s) ({m_n} message(s)) into {args.db}")
|
|
49
|
+
return 0
|
|
50
|
+
|
|
51
|
+
if args.command == "search":
|
|
52
|
+
rows = vault.search(args.query, provider=args.provider, limit=args.limit)
|
|
53
|
+
if args.as_json:
|
|
54
|
+
print(json.dumps(rows, indent=2, ensure_ascii=False))
|
|
55
|
+
else:
|
|
56
|
+
if not rows:
|
|
57
|
+
print("No matching conversations found.")
|
|
58
|
+
for r in rows:
|
|
59
|
+
snip = (r.get("snippet") or "").replace("<mark>", "[").replace("</mark>", "]")
|
|
60
|
+
print(f"[{r['provider']}] {r['title']} ({(r.get('created_at') or '')[:10]})")
|
|
61
|
+
if snip:
|
|
62
|
+
print(f" {snip}")
|
|
63
|
+
return 0
|
|
64
|
+
|
|
65
|
+
if args.command == "export":
|
|
66
|
+
n = export_vault_markdown(vault, args.out, provider=args.provider)
|
|
67
|
+
print(f"Exported {n} conversation(s) to {args.out}/")
|
|
68
|
+
return 0
|
|
69
|
+
|
|
70
|
+
if args.command == "stats":
|
|
71
|
+
st = vault.stats()
|
|
72
|
+
if args.as_json:
|
|
73
|
+
print(json.dumps(st, indent=2))
|
|
74
|
+
else:
|
|
75
|
+
print(f"Total: {st['total_conversations']} conversations, {st['total_messages']} messages, {st['total_words']} words")
|
|
76
|
+
for prov, v in st["by_provider"].items():
|
|
77
|
+
print(f" - {prov}: {v['conversations']} conversations, {v['messages']} messages, {v['words']} words")
|
|
78
|
+
return 0
|
|
79
|
+
|
|
80
|
+
if args.command == "serve":
|
|
81
|
+
serve_vault(vault, host=args.host, port=args.port)
|
|
82
|
+
return 0
|
|
83
|
+
|
|
84
|
+
return 2
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
if __name__ == "__main__":
|
|
88
|
+
sys.exit(main())
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"""Clean Markdown / Obsidian exporter for archived AI conversations."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
import re
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
|
|
7
|
+
from .models import Conversation
|
|
8
|
+
from .vault import ChatVault
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def _slugify(text: str, max_len: int = 60) -> str:
|
|
12
|
+
slug = re.sub(r"[^\w\s-]", "", text, flags=re.U).strip().lower()
|
|
13
|
+
slug = re.sub(r"[-\s]+", "-", slug)
|
|
14
|
+
return (slug[:max_len].strip("-") or "untitled")
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def render_markdown(conv: Conversation) -> str:
|
|
18
|
+
date_part = conv.created_at[:10] if len(conv.created_at) >= 10 else ""
|
|
19
|
+
safe_title = conv.title.replace('"', '\\"')
|
|
20
|
+
lines = [
|
|
21
|
+
"---",
|
|
22
|
+
f'id: "{conv.id}"',
|
|
23
|
+
f'title: "{safe_title}"',
|
|
24
|
+
f"provider: {conv.provider}",
|
|
25
|
+
]
|
|
26
|
+
if conv.model:
|
|
27
|
+
lines.append(f"model: {conv.model}")
|
|
28
|
+
if conv.created_at:
|
|
29
|
+
lines.append(f"created_at: {conv.created_at}")
|
|
30
|
+
if conv.updated_at:
|
|
31
|
+
lines.append(f"updated_at: {conv.updated_at}")
|
|
32
|
+
lines.append(f"tags: [ai-chat, {conv.provider}]")
|
|
33
|
+
lines.append("---")
|
|
34
|
+
lines.append("")
|
|
35
|
+
lines.append(f"# {conv.title}")
|
|
36
|
+
if date_part:
|
|
37
|
+
lines.append("")
|
|
38
|
+
lines.append(f"> Source: **{conv.provider}** | Date: {date_part} | Messages: {len(conv.messages)}")
|
|
39
|
+
lines.append("")
|
|
40
|
+
|
|
41
|
+
for msg in conv.messages:
|
|
42
|
+
heading = "## You" if msg.role == "user" else f"## Assistant ({conv.provider})"
|
|
43
|
+
lines.append(heading)
|
|
44
|
+
lines.append("")
|
|
45
|
+
lines.append(msg.content.rstrip())
|
|
46
|
+
lines.append("")
|
|
47
|
+
return "\n".join(lines).rstrip() + "\n"
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def export_vault_markdown(vault: ChatVault, out_dir: str | Path, provider: str | None = None) -> int:
|
|
51
|
+
out = Path(out_dir)
|
|
52
|
+
out.mkdir(parents=True, exist_ok=True)
|
|
53
|
+
items = vault.list_conversations(provider=provider, limit=100_000)
|
|
54
|
+
index_lines = ["# AI Chat Vault Index", ""]
|
|
55
|
+
count = 0
|
|
56
|
+
for meta in items:
|
|
57
|
+
conv = vault.get_conversation(meta["id"])
|
|
58
|
+
if not conv:
|
|
59
|
+
continue
|
|
60
|
+
pdir = out / conv.provider
|
|
61
|
+
pdir.mkdir(parents=True, exist_ok=True)
|
|
62
|
+
date_prefix = conv.created_at[:10] if len(conv.created_at) >= 10 else "undated"
|
|
63
|
+
fname = f"{date_prefix}_{_slugify(conv.title)}_{conv.id.split(':')[-1][:6]}.md"
|
|
64
|
+
target = pdir / fname
|
|
65
|
+
target.write_text(render_markdown(conv), encoding="utf-8")
|
|
66
|
+
rel = f"{conv.provider}/{fname}"
|
|
67
|
+
index_lines.append(f"- [{conv.title}]({rel}) — `{conv.provider}` ({date_prefix}, {len(conv.messages)} msgs)")
|
|
68
|
+
count += 1
|
|
69
|
+
(out / "_INDEX.md").write_text("\n".join(index_lines) + "\n", encoding="utf-8")
|
|
70
|
+
return count
|