@nelsonlongxiang/dsh-prompt-templates 0.2.10 → 0.4.4
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.
- package/README.md +8 -11
- package/README_zh.md +8 -11
- package/cordis.patch.yml +4 -17
- package/lib/client/Panel.js +154 -23
- package/lib/client/api.js +19 -0
- package/lib/client/index.js +11 -1
- package/lib/client/locales.js +12 -0
- package/lib/client/service.js +52 -3
- package/lib/client.js +423 -67
- package/lib/client.js.map +1 -1
- package/lib/index.js +57 -47
- package/lib/store.js +275 -0
- package/lib/types/client/api.d.ts +14 -1
- package/lib/types/client/locales.d.ts +12 -0
- package/lib/types/client/service.d.ts +19 -3
- package/lib/types/client/slots.d.ts +19 -4
- package/lib/types/index.d.ts +13 -19
- package/lib/types/store.d.ts +57 -0
- package/lib/types/types.d.ts +20 -3
- package/lib/types.js +2 -2
- package/package.json +7 -15
- package/python/README.md +0 -26
- package/python/pyproject.toml +0 -33
- package/python/src/dsh_prompt_templates/__init__.py +0 -22
- package/python/src/dsh_prompt_templates/cli.py +0 -216
- package/python/src/dsh_prompt_templates/db.py +0 -125
- package/python/src/dsh_prompt_templates/models.py +0 -59
- package/python/src/dsh_prompt_templates/server.py +0 -170
- package/python/src/dsh_prompt_templates/store.py +0 -156
- package/python/uv.lock +0 -520
- package/scripts/bootstrap.mjs +0 -38
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nelsonlongxiang/dsh-prompt-templates",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Quick prompt templates for DeepSeek Harness: global and per-session templates, a browser panel, and a
|
|
3
|
+
"version": "0.4.4",
|
|
4
|
+
"description": "Quick prompt templates for DeepSeek Harness: global and per-session templates, a browser panel, and a pure-TS node:sqlite persistence backend",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "lib/index.js",
|
|
7
7
|
"types": "lib/types/index.d.ts",
|
|
@@ -19,14 +19,9 @@
|
|
|
19
19
|
},
|
|
20
20
|
"files": [
|
|
21
21
|
"lib",
|
|
22
|
-
"python/src",
|
|
23
|
-
"python/pyproject.toml",
|
|
24
|
-
"python/uv.lock",
|
|
25
|
-
"python/README.md",
|
|
26
|
-
"scripts",
|
|
27
22
|
"cordis.patch.yml",
|
|
28
|
-
"README.md",
|
|
29
|
-
"README_zh.md",
|
|
23
|
+
"README.md",
|
|
24
|
+
"README_zh.md",
|
|
30
25
|
"images"
|
|
31
26
|
],
|
|
32
27
|
"dsh": {
|
|
@@ -45,10 +40,8 @@
|
|
|
45
40
|
"scripts": {
|
|
46
41
|
"build": "tsc -p tsconfig.json && tsc -p tsconfig.client.json && tsdown",
|
|
47
42
|
"typecheck": "tsc -p tsconfig.json --noEmit && tsc -p tsconfig.client.json --noEmit",
|
|
48
|
-
"
|
|
49
|
-
"
|
|
50
|
-
"test:python": "uv run --project python pytest",
|
|
51
|
-
"verify": "pnpm typecheck && pnpm test:python"
|
|
43
|
+
"prepare": "pnpm build",
|
|
44
|
+
"verify": "pnpm typecheck"
|
|
52
45
|
},
|
|
53
46
|
"peerDependencies": {
|
|
54
47
|
"@deepseek-ai/cordis": ">=4.0.1-rc.1",
|
|
@@ -106,8 +99,7 @@
|
|
|
106
99
|
"react": "^18.2.0",
|
|
107
100
|
"react-dom": "^18.2.0",
|
|
108
101
|
"tsdown": "0.22.2",
|
|
109
|
-
"typescript": "^5.9.3"
|
|
110
|
-
"@nelsonlongxiang/dsh-python-bridge": "file:../dsh-python-bridge"
|
|
102
|
+
"typescript": "^5.9.3"
|
|
111
103
|
},
|
|
112
104
|
"license": "MIT"
|
|
113
105
|
}
|
package/python/README.md
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
# dsh-prompt-templates Python backend
|
|
2
|
-
|
|
3
|
-
Quick prompt-template persistence: SQLAlchemy ORM over one SQLite database
|
|
4
|
-
under `$DSH_HOME/ext/prompt-templates/db.sqlite3`, Pydantic validation, a
|
|
5
|
-
Typer CLI, and a newline-delimited JSON-RPC 2.0 stdio server the plugin's
|
|
6
|
-
Host bridge spawns.
|
|
7
|
-
|
|
8
|
-
## Development
|
|
9
|
-
|
|
10
|
-
```sh
|
|
11
|
-
uv sync --group test
|
|
12
|
-
uv run pytest
|
|
13
|
-
```
|
|
14
|
-
|
|
15
|
-
## CLI
|
|
16
|
-
|
|
17
|
-
```sh
|
|
18
|
-
dsh-prompt-templates init
|
|
19
|
-
dsh-prompt-templates templates list [--scope global|session] [--session-id <id>] [--json]
|
|
20
|
-
dsh-prompt-templates templates add <name> <content> [--scope session] [--session-id <id>]
|
|
21
|
-
dsh-prompt-templates templates get <id> [--json]
|
|
22
|
-
dsh-prompt-templates templates update <id> [--name] [--content] [--description] [--position]
|
|
23
|
-
dsh-prompt-templates templates make-global <id>
|
|
24
|
-
dsh-prompt-templates templates remove <id>
|
|
25
|
-
dsh-prompt-templates serve [--db <path>]
|
|
26
|
-
```
|
package/python/pyproject.toml
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
[build-system]
|
|
2
|
-
requires = ["hatchling==1.30.1"]
|
|
3
|
-
build-backend = "hatchling.build"
|
|
4
|
-
|
|
5
|
-
[project]
|
|
6
|
-
name = "dsh-prompt-templates-backend"
|
|
7
|
-
version = "0.1.0"
|
|
8
|
-
description = "Quick prompt-template backend for the dsh-prompt-templates plugin (CLI + SQLite + stdio JSON-RPC)"
|
|
9
|
-
readme = "README.md"
|
|
10
|
-
requires-python = ">=3.10"
|
|
11
|
-
license = "MIT"
|
|
12
|
-
dependencies = [
|
|
13
|
-
"pydantic>=2.12,<3",
|
|
14
|
-
"sqlalchemy>=2.0",
|
|
15
|
-
"typer>=0.12",
|
|
16
|
-
]
|
|
17
|
-
|
|
18
|
-
[dependency-groups]
|
|
19
|
-
test = ["pytest>=8"]
|
|
20
|
-
|
|
21
|
-
[project.urls]
|
|
22
|
-
Homepage = "https://github.com/NelsonLongXiang/dsh-prompt-templates"
|
|
23
|
-
Source = "https://github.com/NelsonLongXiang/dsh-prompt-templates"
|
|
24
|
-
|
|
25
|
-
[project.scripts]
|
|
26
|
-
dsh-prompt-templates = "dsh_prompt_templates.cli:main"
|
|
27
|
-
|
|
28
|
-
[tool.pytest.ini_options]
|
|
29
|
-
addopts = "-q"
|
|
30
|
-
testpaths = ["tests"]
|
|
31
|
-
|
|
32
|
-
[tool.hatch.build.targets.wheel]
|
|
33
|
-
packages = ["src/dsh_prompt_templates"]
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
"""dsh-prompt-templates: quick prompt-template backend for DeepSeek Harness.
|
|
2
|
-
|
|
3
|
-
One SQLite database owned by Python code (SQLAlchemy ORM + Pydantic
|
|
4
|
-
validation), exposed through a CLI and a newline-delimited JSON-RPC 2.0
|
|
5
|
-
server over stdio for Host-side bridge integration.
|
|
6
|
-
"""
|
|
7
|
-
|
|
8
|
-
from __future__ import annotations
|
|
9
|
-
|
|
10
|
-
from .db import default_db_path
|
|
11
|
-
from .models import PromptTemplate, PromptTemplateCreate, PromptTemplateUpdate
|
|
12
|
-
from .store import PromptTemplateStore
|
|
13
|
-
|
|
14
|
-
__all__ = [
|
|
15
|
-
"PromptTemplate",
|
|
16
|
-
"PromptTemplateCreate",
|
|
17
|
-
"PromptTemplateStore",
|
|
18
|
-
"PromptTemplateUpdate",
|
|
19
|
-
"default_db_path",
|
|
20
|
-
]
|
|
21
|
-
|
|
22
|
-
__version__ = "0.0.0.dev0"
|
|
@@ -1,216 +0,0 @@
|
|
|
1
|
-
"""dsh-prompt-templates command-line interface."""
|
|
2
|
-
|
|
3
|
-
from __future__ import annotations
|
|
4
|
-
|
|
5
|
-
import json
|
|
6
|
-
from pathlib import Path
|
|
7
|
-
from typing import Optional
|
|
8
|
-
|
|
9
|
-
import typer
|
|
10
|
-
|
|
11
|
-
from .db import default_db_path
|
|
12
|
-
from .models import PromptTemplateCreate, PromptTemplateUpdate
|
|
13
|
-
from .server import JsonRpcServer, default_domains
|
|
14
|
-
from .store import PromptTemplateStore
|
|
15
|
-
|
|
16
|
-
app = typer.Typer(help="Quick prompt-templates backend for DeepSeek Harness.")
|
|
17
|
-
templates = typer.Typer(help="Manage prompt templates.")
|
|
18
|
-
app.add_typer(templates, name="templates")
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
def _store(db: Optional[Path]) -> PromptTemplateStore:
|
|
22
|
-
return PromptTemplateStore(db if db is not None else default_db_path())
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
def _fail(exc: Exception) -> None:
|
|
26
|
-
typer.secho(f"error: {exc}", fg=typer.colors.RED, err=True)
|
|
27
|
-
raise typer.Exit(code=1)
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
@app.command()
|
|
31
|
-
def init(
|
|
32
|
-
db: Optional[Path] = typer.Option(
|
|
33
|
-
None,
|
|
34
|
-
"--db",
|
|
35
|
-
help="Database path (default: $DSH_HOME/ext/prompt-templates/db.sqlite3).",
|
|
36
|
-
),
|
|
37
|
-
) -> None:
|
|
38
|
-
"""Create the database schema if absent."""
|
|
39
|
-
store = _store(db)
|
|
40
|
-
typer.echo(f"database ready at {store.path}")
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
@app.command()
|
|
44
|
-
def serve(
|
|
45
|
-
templates_db: Optional[Path] = typer.Option(
|
|
46
|
-
None,
|
|
47
|
-
"--db",
|
|
48
|
-
"--templates-db",
|
|
49
|
-
help="Prompt-template database path (default: $DSH_HOME/ext/prompt-templates/db.sqlite3).",
|
|
50
|
-
),
|
|
51
|
-
) -> None:
|
|
52
|
-
"""Run the stdio JSON-RPC server for Host bridge integration."""
|
|
53
|
-
JsonRpcServer(
|
|
54
|
-
default_domains(str(templates_db) if templates_db is not None else None)
|
|
55
|
-
).serve()
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
@templates.command("list")
|
|
59
|
-
def list_templates(
|
|
60
|
-
scope: Optional[str] = typer.Option(
|
|
61
|
-
None, "--scope", help="Filter by scope: global|session."
|
|
62
|
-
),
|
|
63
|
-
session_id: Optional[str] = typer.Option(
|
|
64
|
-
None, "--session-id", help="Filter by owning session id."
|
|
65
|
-
),
|
|
66
|
-
json_output: bool = typer.Option(False, "--json", help="Emit a JSON array."),
|
|
67
|
-
db: Optional[Path] = typer.Option(
|
|
68
|
-
None,
|
|
69
|
-
"--db",
|
|
70
|
-
help="Database path (default: $DSH_HOME/ext/prompt-templates/db.sqlite3).",
|
|
71
|
-
),
|
|
72
|
-
) -> None:
|
|
73
|
-
"""List prompt templates."""
|
|
74
|
-
items = _store(db).list(scope=scope, session_id=session_id)
|
|
75
|
-
if json_output:
|
|
76
|
-
typer.echo(
|
|
77
|
-
json.dumps(
|
|
78
|
-
[item.model_dump(mode="json") for item in items],
|
|
79
|
-
ensure_ascii=False,
|
|
80
|
-
indent=2,
|
|
81
|
-
)
|
|
82
|
-
)
|
|
83
|
-
else:
|
|
84
|
-
for item in items:
|
|
85
|
-
typer.echo(f"{item.id}\t{item.scope}\t{item.name}\t{item.position}")
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
@templates.command("add")
|
|
89
|
-
def add_template(
|
|
90
|
-
name: str = typer.Argument(..., help="Template name."),
|
|
91
|
-
content: str = typer.Argument(..., help="Template body."),
|
|
92
|
-
scope: str = typer.Option("global", "--scope", help="global|session."),
|
|
93
|
-
session_id: Optional[str] = typer.Option(
|
|
94
|
-
None, "--session-id", help="Owning session id; required when scope=session."
|
|
95
|
-
),
|
|
96
|
-
description: Optional[str] = typer.Option(None, "--description"),
|
|
97
|
-
position: int = typer.Option(0, "--position"),
|
|
98
|
-
db: Optional[Path] = typer.Option(
|
|
99
|
-
None,
|
|
100
|
-
"--db",
|
|
101
|
-
help="Database path (default: $DSH_HOME/ext/prompt-templates/db.sqlite3).",
|
|
102
|
-
),
|
|
103
|
-
) -> None:
|
|
104
|
-
"""Add a prompt template and print its id."""
|
|
105
|
-
try:
|
|
106
|
-
data = PromptTemplateCreate(
|
|
107
|
-
name=name,
|
|
108
|
-
content=content,
|
|
109
|
-
scope=scope,
|
|
110
|
-
session_id=session_id,
|
|
111
|
-
description=description,
|
|
112
|
-
position=position,
|
|
113
|
-
)
|
|
114
|
-
item = _store(db).create(data)
|
|
115
|
-
except ValueError as exc:
|
|
116
|
-
_fail(exc)
|
|
117
|
-
typer.echo(item.id)
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
@templates.command("get")
|
|
121
|
-
def get_template(
|
|
122
|
-
template_id: str = typer.Argument(..., help="Template id."),
|
|
123
|
-
json_output: bool = typer.Option(False, "--json", help="Emit the record as JSON."),
|
|
124
|
-
db: Optional[Path] = typer.Option(
|
|
125
|
-
None,
|
|
126
|
-
"--db",
|
|
127
|
-
help="Database path (default: $DSH_HOME/ext/prompt-templates/db.sqlite3).",
|
|
128
|
-
),
|
|
129
|
-
) -> None:
|
|
130
|
-
"""Show one prompt template."""
|
|
131
|
-
item = _store(db).get(template_id)
|
|
132
|
-
if item is None:
|
|
133
|
-
_fail(ValueError(f"template {template_id!r} not found"))
|
|
134
|
-
if json_output:
|
|
135
|
-
typer.echo(json.dumps(item.model_dump(mode="json"), ensure_ascii=False, indent=2))
|
|
136
|
-
else:
|
|
137
|
-
typer.echo(f"{item.id}\t{item.scope}\t{item.name}")
|
|
138
|
-
typer.echo(item.content)
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
@templates.command("update")
|
|
142
|
-
def update_template(
|
|
143
|
-
template_id: str = typer.Argument(..., help="Template id."),
|
|
144
|
-
name: Optional[str] = typer.Option(None, "--name"),
|
|
145
|
-
content: Optional[str] = typer.Option(None, "--content"),
|
|
146
|
-
description: Optional[str] = typer.Option(None, "--description"),
|
|
147
|
-
position: Optional[int] = typer.Option(None, "--position"),
|
|
148
|
-
db: Optional[Path] = typer.Option(
|
|
149
|
-
None,
|
|
150
|
-
"--db",
|
|
151
|
-
help="Database path (default: $DSH_HOME/ext/prompt-templates/db.sqlite3).",
|
|
152
|
-
),
|
|
153
|
-
) -> None:
|
|
154
|
-
"""Update one prompt template."""
|
|
155
|
-
try:
|
|
156
|
-
fields = {
|
|
157
|
-
"name": name,
|
|
158
|
-
"content": content,
|
|
159
|
-
"description": description,
|
|
160
|
-
"position": position,
|
|
161
|
-
}
|
|
162
|
-
# Only fields the caller actually passed become part of the patch;
|
|
163
|
-
# an untouched typer Option defaults to None and must stay absent so
|
|
164
|
-
# `exclude_unset` keeps the stored column intact.
|
|
165
|
-
patch = PromptTemplateUpdate(
|
|
166
|
-
**{key: value for key, value in fields.items() if value is not None}
|
|
167
|
-
)
|
|
168
|
-
item = _store(db).update(template_id, patch)
|
|
169
|
-
except ValueError as exc:
|
|
170
|
-
_fail(exc)
|
|
171
|
-
if item is None:
|
|
172
|
-
_fail(ValueError(f"template {template_id!r} not found"))
|
|
173
|
-
typer.echo(item.id)
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
@templates.command("make-global")
|
|
177
|
-
def make_global_template(
|
|
178
|
-
template_id: str = typer.Argument(..., help="Template id."),
|
|
179
|
-
db: Optional[Path] = typer.Option(
|
|
180
|
-
None,
|
|
181
|
-
"--db",
|
|
182
|
-
help="Database path (default: $DSH_HOME/ext/prompt-templates/db.sqlite3).",
|
|
183
|
-
),
|
|
184
|
-
) -> None:
|
|
185
|
-
"""Promote one session template to the global scope."""
|
|
186
|
-
try:
|
|
187
|
-
item = _store(db).make_global(template_id)
|
|
188
|
-
except ValueError as exc:
|
|
189
|
-
_fail(exc)
|
|
190
|
-
if item is None:
|
|
191
|
-
_fail(ValueError(f"template {template_id!r} not found"))
|
|
192
|
-
typer.echo(item.id)
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
@templates.command("remove")
|
|
196
|
-
def remove_template(
|
|
197
|
-
template_id: str = typer.Argument(..., help="Template id."),
|
|
198
|
-
db: Optional[Path] = typer.Option(
|
|
199
|
-
None,
|
|
200
|
-
"--db",
|
|
201
|
-
help="Database path (default: $DSH_HOME/ext/prompt-templates/db.sqlite3).",
|
|
202
|
-
),
|
|
203
|
-
) -> None:
|
|
204
|
-
"""Remove one prompt template."""
|
|
205
|
-
removed = _store(db).delete(template_id)
|
|
206
|
-
if not removed:
|
|
207
|
-
_fail(ValueError(f"template {template_id!r} not found"))
|
|
208
|
-
typer.echo("removed")
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
def main() -> None:
|
|
212
|
-
app()
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
if __name__ == "__main__":
|
|
216
|
-
main()
|
|
@@ -1,125 +0,0 @@
|
|
|
1
|
-
"""Shared SQLite plumbing for extension domains plus the prompt-template schema.
|
|
2
|
-
|
|
3
|
-
Each extension domain owns its own database file under `$DSH_HOME/ext/<domain>/`
|
|
4
|
-
and its own `DeclarativeBase`; this module owns the cross-domain pieces: the
|
|
5
|
-
home-path rule, private-file hardening, WAL, and the monotonic `user_version`
|
|
6
|
-
gate (an unversioned empty database initializes at the domain's schema version;
|
|
7
|
-
any other on-disk version rejects instead of migrating).
|
|
8
|
-
"""
|
|
9
|
-
|
|
10
|
-
from __future__ import annotations
|
|
11
|
-
|
|
12
|
-
import os
|
|
13
|
-
from datetime import datetime
|
|
14
|
-
from pathlib import Path
|
|
15
|
-
from typing import Optional
|
|
16
|
-
|
|
17
|
-
from sqlalchemy import DateTime, Integer, String, Text, create_engine, event, func
|
|
18
|
-
from sqlalchemy.engine import Engine
|
|
19
|
-
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
|
|
20
|
-
|
|
21
|
-
TEMPLATES_SCHEMA_VERSION = 1
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
class Base(DeclarativeBase):
|
|
25
|
-
pass
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
class TemplateRow(Base):
|
|
29
|
-
"""One prompt template row. `scope` discriminates global vs session; a
|
|
30
|
-
session-scoped row carries the owning session id in `session_id`."""
|
|
31
|
-
|
|
32
|
-
__tablename__ = "templates"
|
|
33
|
-
|
|
34
|
-
id: Mapped[str] = mapped_column(String(32), primary_key=True)
|
|
35
|
-
name: Mapped[str] = mapped_column(String(128))
|
|
36
|
-
content: Mapped[str] = mapped_column(Text)
|
|
37
|
-
scope: Mapped[str] = mapped_column(String(16))
|
|
38
|
-
session_id: Mapped[Optional[str]] = mapped_column(String(128), nullable=True)
|
|
39
|
-
description: Mapped[Optional[str]] = mapped_column(String(512), nullable=True)
|
|
40
|
-
position: Mapped[int] = mapped_column(Integer, nullable=False, server_default="0")
|
|
41
|
-
created_at: Mapped[datetime] = mapped_column(
|
|
42
|
-
DateTime, nullable=False, server_default=func.now()
|
|
43
|
-
)
|
|
44
|
-
updated_at: Mapped[datetime] = mapped_column(
|
|
45
|
-
DateTime, nullable=False, server_default=func.now(), onupdate=func.now()
|
|
46
|
-
)
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
def ext_home() -> Path:
|
|
50
|
-
"""Resolve `$DSH_HOME` (default `~/.dsh`), matching the repository
|
|
51
|
-
home-paths convention."""
|
|
52
|
-
home = os.environ.get("DSH_HOME")
|
|
53
|
-
return Path(home).expanduser() if home and home.strip() else Path.home() / ".dsh"
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
def default_domain_db_path(domain: str) -> Path:
|
|
57
|
-
"""Resolve `<ext_home>/ext/<domain>/db.sqlite3` for one extension domain.
|
|
58
|
-
|
|
59
|
-
The `ext/` subdirectory keeps extension data out of the root (never flat
|
|
60
|
-
files beside `settings.yaml`)."""
|
|
61
|
-
return ext_home() / "ext" / domain / "db.sqlite3"
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
def default_db_path() -> Path:
|
|
65
|
-
"""The prompt-template domain's default database path."""
|
|
66
|
-
return default_domain_db_path("prompt-templates")
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
def _ensure_private_dir(path: Path) -> None:
|
|
70
|
-
path.mkdir(parents=True, exist_ok=True)
|
|
71
|
-
try:
|
|
72
|
-
os.chmod(path, 0o700)
|
|
73
|
-
except OSError:
|
|
74
|
-
# Best-effort on platforms without POSIX modes.
|
|
75
|
-
pass
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
def _ensure_private_file(path: Path) -> None:
|
|
79
|
-
try:
|
|
80
|
-
os.chmod(path, 0o600)
|
|
81
|
-
except OSError:
|
|
82
|
-
pass
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
def _enable_wal(dbapi_connection, _record) -> None:
|
|
86
|
-
cursor = dbapi_connection.cursor()
|
|
87
|
-
cursor.execute("PRAGMA journal_mode=WAL")
|
|
88
|
-
cursor.execute("PRAGMA busy_timeout=5000")
|
|
89
|
-
cursor.close()
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
def open_domain_engine(db_path: str | Path, base: type[DeclarativeBase], schema_version: int) -> Engine:
|
|
93
|
-
"""Create the SQLite engine for one domain database, creating the schema
|
|
94
|
-
when absent and gating on the monotonic `user_version`.
|
|
95
|
-
|
|
96
|
-
The parent directory is created owner-only and the database file is
|
|
97
|
-
chmodded to owner-only, matching the TS persistence backends. The
|
|
98
|
-
in-memory database skips all filesystem handling. An empty database with
|
|
99
|
-
`user_version` 0 initializes at `schema_version`; any other non-current
|
|
100
|
-
version rejects rather than migrating."""
|
|
101
|
-
if str(db_path) == ":memory:":
|
|
102
|
-
engine = create_engine("sqlite:///:memory:", future=True)
|
|
103
|
-
else:
|
|
104
|
-
path = Path(db_path)
|
|
105
|
-
_ensure_private_dir(path.parent)
|
|
106
|
-
engine = create_engine(f"sqlite:///{path}", future=True)
|
|
107
|
-
event.listen(engine, "connect", _enable_wal)
|
|
108
|
-
with engine.begin() as connection:
|
|
109
|
-
on_disk = connection.exec_driver_sql("PRAGMA user_version").scalar_one()
|
|
110
|
-
if on_disk == 0:
|
|
111
|
-
base.metadata.create_all(connection)
|
|
112
|
-
connection.exec_driver_sql(f"PRAGMA user_version={schema_version}")
|
|
113
|
-
elif on_disk != schema_version:
|
|
114
|
-
raise RuntimeError(
|
|
115
|
-
f"{db_path} has schema version {on_disk}, "
|
|
116
|
-
f"incompatible with this build (expected {schema_version})"
|
|
117
|
-
)
|
|
118
|
-
if str(db_path) != ":memory:":
|
|
119
|
-
_ensure_private_file(Path(db_path))
|
|
120
|
-
return engine
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
def create_engine_for(db_path: str | Path) -> Engine:
|
|
124
|
-
"""Create the prompt-template domain engine (see {@link open_domain_engine})."""
|
|
125
|
-
return open_domain_engine(db_path, Base, TEMPLATES_SCHEMA_VERSION)
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
"""Pydantic models for the dsh-ext domain."""
|
|
2
|
-
|
|
3
|
-
from __future__ import annotations
|
|
4
|
-
|
|
5
|
-
from datetime import datetime
|
|
6
|
-
from typing import Literal, Optional
|
|
7
|
-
|
|
8
|
-
from pydantic import BaseModel, ConfigDict, Field, model_validator
|
|
9
|
-
|
|
10
|
-
PromptScope = Literal["global", "session"]
|
|
11
|
-
|
|
12
|
-
_NAME_MAX = 128
|
|
13
|
-
_CONTENT_MIN = 1
|
|
14
|
-
_DESCRIPTION_MAX = 512
|
|
15
|
-
_SESSION_ID_MAX = 128
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
class PromptTemplateCreate(BaseModel):
|
|
19
|
-
"""Payload for creating one prompt template."""
|
|
20
|
-
|
|
21
|
-
name: str = Field(min_length=1, max_length=_NAME_MAX)
|
|
22
|
-
content: str = Field(min_length=_CONTENT_MIN)
|
|
23
|
-
scope: PromptScope = "global"
|
|
24
|
-
session_id: Optional[str] = Field(default=None, max_length=_SESSION_ID_MAX)
|
|
25
|
-
description: Optional[str] = Field(default=None, max_length=_DESCRIPTION_MAX)
|
|
26
|
-
position: int = Field(default=0, ge=0)
|
|
27
|
-
|
|
28
|
-
@model_validator(mode="after")
|
|
29
|
-
def _scope_session_consistency(self) -> PromptTemplateCreate:
|
|
30
|
-
if self.scope == "session" and not self.session_id:
|
|
31
|
-
raise ValueError("scope='session' requires session_id")
|
|
32
|
-
if self.scope == "global" and self.session_id is not None:
|
|
33
|
-
raise ValueError("scope='global' must not carry session_id")
|
|
34
|
-
return self
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
class PromptTemplateUpdate(BaseModel):
|
|
38
|
-
"""Patch payload; every field is optional."""
|
|
39
|
-
|
|
40
|
-
name: Optional[str] = Field(default=None, min_length=1, max_length=_NAME_MAX)
|
|
41
|
-
content: Optional[str] = Field(default=None, min_length=_CONTENT_MIN)
|
|
42
|
-
description: Optional[str] = Field(default=None, max_length=_DESCRIPTION_MAX)
|
|
43
|
-
position: Optional[int] = Field(default=None, ge=0)
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
class PromptTemplate(BaseModel):
|
|
47
|
-
"""Canonical template record."""
|
|
48
|
-
|
|
49
|
-
model_config = ConfigDict(from_attributes=True)
|
|
50
|
-
|
|
51
|
-
id: str
|
|
52
|
-
name: str
|
|
53
|
-
content: str
|
|
54
|
-
scope: PromptScope
|
|
55
|
-
session_id: Optional[str] = None
|
|
56
|
-
description: Optional[str] = None
|
|
57
|
-
position: int
|
|
58
|
-
created_at: datetime
|
|
59
|
-
updated_at: datetime
|