Initial upload: Local LLM Prompt Manager CLI tool
This commit is contained in:
95
src/commands/import_cmd.py
Normal file
95
src/commands/import_cmd.py
Normal file
@@ -0,0 +1,95 @@
|
|||||||
|
"""Import prompts from various formats."""
|
||||||
|
|
||||||
|
import json
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
import click
|
||||||
|
import yaml
|
||||||
|
|
||||||
|
from ..models import Prompt
|
||||||
|
from ..storage import PromptStorage
|
||||||
|
|
||||||
|
|
||||||
|
@click.command("import")
|
||||||
|
@click.argument("input", type=click.Path(exists=True))
|
||||||
|
@click.option("--format", "-f", default="auto", type=click.Choice(["auto", "yaml", "json"]))
|
||||||
|
@click.option("--force", is_flag=True, help="Overwrite existing prompts")
|
||||||
|
@click.option("--dir", "prompt_dir", default=None, help="Prompt directory")
|
||||||
|
def import_prompts(
|
||||||
|
input: str,
|
||||||
|
format: str,
|
||||||
|
force: bool,
|
||||||
|
prompt_dir: Optional[str]
|
||||||
|
):
|
||||||
|
"""Import prompts from JSON or YAML files."""
|
||||||
|
storage = PromptStorage(prompt_dir)
|
||||||
|
|
||||||
|
with open(input) as f:
|
||||||
|
content = f.read()
|
||||||
|
|
||||||
|
if format == "auto":
|
||||||
|
if input.endswith(".json"):
|
||||||
|
format = "json"
|
||||||
|
else:
|
||||||
|
format = "yaml"
|
||||||
|
|
||||||
|
if format == "yaml":
|
||||||
|
data = yaml.safe_load(content)
|
||||||
|
if isinstance(data, dict):
|
||||||
|
data = [data]
|
||||||
|
else:
|
||||||
|
data = json.loads(content)
|
||||||
|
if isinstance(data, dict):
|
||||||
|
data = [data]
|
||||||
|
|
||||||
|
imported = 0
|
||||||
|
skipped = 0
|
||||||
|
|
||||||
|
for item in data:
|
||||||
|
try:
|
||||||
|
prompt_data = _normalize_prompt_data(item)
|
||||||
|
prompt = Prompt.from_dict(prompt_data)
|
||||||
|
|
||||||
|
if storage.prompt_exists(prompt.name) and not force:
|
||||||
|
skipped += 1
|
||||||
|
continue
|
||||||
|
|
||||||
|
storage.save_prompt(prompt)
|
||||||
|
for tag in prompt.tags:
|
||||||
|
storage.add_tag_to_prompt(prompt.name, tag)
|
||||||
|
imported += 1
|
||||||
|
except Exception as e:
|
||||||
|
click.echo(f"Error importing prompt: {e}", err=True)
|
||||||
|
|
||||||
|
click.echo(f"Imported {imported} prompts, skipped {skipped}")
|
||||||
|
|
||||||
|
|
||||||
|
def _normalize_prompt_data(data: dict) -> dict:
|
||||||
|
"""Normalize prompt data to internal format."""
|
||||||
|
normalized = {
|
||||||
|
"name": data.get("name", "unnamed"),
|
||||||
|
"template": data.get("template") or data.get("prompt") or "",
|
||||||
|
"description": data.get("description", ""),
|
||||||
|
"tags": data.get("tags", []),
|
||||||
|
"variables": [],
|
||||||
|
"provider": data.get("provider", ""),
|
||||||
|
"model": data.get("model", ""),
|
||||||
|
}
|
||||||
|
|
||||||
|
if "variables" in data:
|
||||||
|
for v in data["variables"]:
|
||||||
|
if isinstance(v, dict):
|
||||||
|
normalized["variables"].append(v)
|
||||||
|
elif isinstance(v, str):
|
||||||
|
normalized["variables"].append({"name": v, "required": True})
|
||||||
|
|
||||||
|
params = data.get("parameters", {})
|
||||||
|
if "properties" in params:
|
||||||
|
for name, prop in params["properties"].items():
|
||||||
|
var = {"name": name}
|
||||||
|
if isinstance(prop, dict):
|
||||||
|
var["description"] = prop.get("description", "")
|
||||||
|
var["required"] = name in params.get("required", [])
|
||||||
|
normalized["variables"].append(var)
|
||||||
|
|
||||||
|
return normalized
|
||||||
Reference in New Issue
Block a user