MVP 1.0
This commit is contained in:
1 parent
be11f82f84
commit
e42f751fdc
47 files changed
+3359
-301
No files matched your search
@@ -63,6 +63,46 @@ MAX_LABEL_CHARS = 80
|
||||
MAX_SUMMARY_CHARS = 160
|
||||
KEY_RE = re.compile(r"^[a-z][a-z0-9_]{0,40}$")
|
||||
SEED_PATH = Path(__file__).resolve().parent / "config" / "generation_instructions.seed.json"
|
||||
STYLE_CONTEXT_KEYS = (
|
||||
"include_core",
|
||||
"include_facet",
|
||||
"include_traits",
|
||||
"include_style_examples",
|
||||
)
|
||||
EMPTY_STYLE_CONTEXT = {key: False for key in STYLE_CONTEXT_KEYS}
|
||||
FULL_STYLE_CONTEXT = {key: True for key in STYLE_CONTEXT_KEYS}
|
||||
LEGACY_VOICE_STYLE_CONTEXT = {
|
||||
"neutral": dict(EMPTY_STYLE_CONTEXT),
|
||||
"light": {**EMPTY_STYLE_CONTEXT, "include_core": True},
|
||||
"noticeable": {
|
||||
"include_core": True,
|
||||
"include_facet": True,
|
||||
"include_traits": True,
|
||||
"include_style_examples": False,
|
||||
},
|
||||
"clear": {
|
||||
"include_core": True,
|
||||
"include_facet": True,
|
||||
"include_traits": True,
|
||||
"include_style_examples": False,
|
||||
},
|
||||
"with_examples": dict(FULL_STYLE_CONTEXT),
|
||||
"legacy_neutral": dict(FULL_STYLE_CONTEXT),
|
||||
"legacy_light": dict(FULL_STYLE_CONTEXT),
|
||||
"legacy_noticeable": dict(FULL_STYLE_CONTEXT),
|
||||
"legacy_clear": dict(FULL_STYLE_CONTEXT),
|
||||
}
|
||||
LEGACY_COMPARISON_KEYS = frozenset(
|
||||
{
|
||||
"legacy_neutral",
|
||||
"legacy_light",
|
||||
"legacy_noticeable",
|
||||
"legacy_clear",
|
||||
}
|
||||
)
|
||||
STYLE_EXAMPLES_STANDING_SENTENCE = (
|
||||
"STYLE_EXAMPLES dienen ausschließlich als Stilreferenz; ihre Inhalte gehören nicht zum heutigen Tag. "
|
||||
)
|
||||
PUBLIC_FIELDS = (
|
||||
"id",
|
||||
"purpose",
|
||||
@@ -108,6 +148,8 @@ class CompiledPolicy:
|
||||
revisions: dict[str, int]
|
||||
instructions: dict[str, str]
|
||||
seed_revision: str
|
||||
style_context: dict[str, bool]
|
||||
cloned_from: dict[str, str]
|
||||
|
||||
|
||||
def load_seed_document() -> dict:
|
||||
@@ -118,6 +160,95 @@ def _as_bool(raw) -> bool:
|
||||
return bool(int(raw)) if not isinstance(raw, bool) else raw
|
||||
|
||||
|
||||
def default_style_context_for_key(guideline_key: str, *, slot: str) -> dict[str, bool]:
|
||||
if slot != "voice":
|
||||
return {}
|
||||
mapped = LEGACY_VOICE_STYLE_CONTEXT.get((guideline_key or "").strip())
|
||||
if mapped:
|
||||
return dict(mapped)
|
||||
return dict(FULL_STYLE_CONTEXT)
|
||||
|
||||
|
||||
def normalize_style_context(raw, *, slot: str, required: bool = False) -> dict[str, bool]:
|
||||
if slot != "voice":
|
||||
if raw in (None, "", {}, []):
|
||||
return {}
|
||||
raise CatalogError(
|
||||
"Stilkontext gehört nur zur Dimension Persönliche Stimme.",
|
||||
code="invalid_generation_guideline",
|
||||
status_code=400,
|
||||
)
|
||||
if raw in (None, "", {}):
|
||||
if required:
|
||||
raise CatalogError(
|
||||
"Die Stilanwendung braucht eine Stilkontext-Konfiguration.",
|
||||
code="invalid_generation_guideline",
|
||||
status_code=400,
|
||||
)
|
||||
return dict(EMPTY_STYLE_CONTEXT)
|
||||
if not isinstance(raw, dict):
|
||||
raise CatalogError(
|
||||
"style_context muss ein Objekt mit den vier Stilquellen sein.",
|
||||
code="invalid_generation_guideline",
|
||||
status_code=400,
|
||||
)
|
||||
unknown = sorted(set(raw) - set(STYLE_CONTEXT_KEYS))
|
||||
if unknown:
|
||||
raise CatalogError(
|
||||
"Unbekannte Stilquellen: " + ", ".join(unknown),
|
||||
code="invalid_generation_guideline",
|
||||
status_code=400,
|
||||
)
|
||||
missing = [key for key in STYLE_CONTEXT_KEYS if key not in raw]
|
||||
if missing and required:
|
||||
raise CatalogError(
|
||||
"style_context ist unvollständig: " + ", ".join(missing),
|
||||
code="invalid_generation_guideline",
|
||||
status_code=400,
|
||||
)
|
||||
parsed: dict[str, bool] = dict(EMPTY_STYLE_CONTEXT)
|
||||
for key in STYLE_CONTEXT_KEYS:
|
||||
if key not in raw:
|
||||
continue
|
||||
value = raw[key]
|
||||
if isinstance(value, bool):
|
||||
parsed[key] = value
|
||||
elif value in (0, 1):
|
||||
parsed[key] = bool(value)
|
||||
else:
|
||||
raise CatalogError(
|
||||
f"{key} muss wahr oder falsch sein, keine Gewichtung.",
|
||||
code="invalid_generation_guideline",
|
||||
status_code=400,
|
||||
)
|
||||
return parsed
|
||||
|
||||
|
||||
def parse_style_context_json(raw, *, slot: str, guideline_key: str = "") -> dict[str, bool]:
|
||||
if slot != "voice":
|
||||
return {}
|
||||
text = (raw or "").strip()
|
||||
if not text or text in ("{}", "null"):
|
||||
return default_style_context_for_key(guideline_key, slot=slot)
|
||||
try:
|
||||
data = json.loads(text)
|
||||
except json.JSONDecodeError:
|
||||
return default_style_context_for_key(guideline_key, slot=slot)
|
||||
if not isinstance(data, dict) or not any(key in data for key in STYLE_CONTEXT_KEYS):
|
||||
return default_style_context_for_key(guideline_key, slot=slot)
|
||||
try:
|
||||
return normalize_style_context(data, slot=slot, required=False)
|
||||
except CatalogError:
|
||||
return default_style_context_for_key(guideline_key, slot=slot)
|
||||
|
||||
|
||||
def style_context_json(context: dict[str, bool] | None, *, slot: str) -> str:
|
||||
if slot != "voice":
|
||||
return "{}"
|
||||
payload = normalize_style_context(context or EMPTY_STYLE_CONTEXT, slot=slot, required=False)
|
||||
return json.dumps(payload, ensure_ascii=False, sort_keys=True)
|
||||
|
||||
|
||||
def _limits() -> tuple[int, int, int]:
|
||||
seed = load_seed_document()
|
||||
return (
|
||||
@@ -156,6 +287,11 @@ def _row(item: dict | None, *, include_instruction: bool = False) -> dict | None
|
||||
payload["revision"] = int(item.get("revision") or 1)
|
||||
payload["sort_order"] = int(item.get("sort_order") or 0)
|
||||
payload["guideline_key"] = item.get("guideline_key") or ""
|
||||
payload["style_context"] = parse_style_context_json(
|
||||
item.get("style_context_json"),
|
||||
slot=item.get("slot") or "",
|
||||
guideline_key=payload["guideline_key"],
|
||||
)
|
||||
if include_instruction:
|
||||
payload["instruction"] = item.get("instruction") or ""
|
||||
payload["seed_id"] = item.get("seed_id") or ""
|
||||
@@ -222,6 +358,7 @@ def active_user_options(purpose: str = PURPOSE_JOURNAL) -> dict[str, list[dict]]
|
||||
"summary": item["summary"],
|
||||
"revision": item["revision"],
|
||||
"is_default": item["is_default"],
|
||||
"style_context": item.get("style_context") or {},
|
||||
}
|
||||
for item in items
|
||||
if item["slot"] == slot
|
||||
@@ -296,6 +433,12 @@ def compile_selection(
|
||||
revisions={slot: int(chosen[slot].get("revision") or 1) for slot in USER_SLOTS},
|
||||
instructions=instructions,
|
||||
seed_revision=revision,
|
||||
style_context=parse_style_context_json(
|
||||
chosen["voice"].get("style_context_json"),
|
||||
slot="voice",
|
||||
guideline_key=chosen["voice"].get("guideline_key") or "",
|
||||
),
|
||||
cloned_from={slot: chosen[slot].get("cloned_from") or "" for slot in USER_SLOTS},
|
||||
)
|
||||
|
||||
|
||||
@@ -313,6 +456,8 @@ def policy_trace(
|
||||
"labels": dict(compiled.labels),
|
||||
"revisions": dict(compiled.revisions),
|
||||
"seed_revision": compiled.seed_revision,
|
||||
"style_context": dict(compiled.style_context),
|
||||
"cloned_from": dict(compiled.cloned_from),
|
||||
}
|
||||
|
||||
|
||||
@@ -336,6 +481,8 @@ def draft_snapshot(compiled: CompiledPolicy, *, prompt: dict | None = None, mode
|
||||
"key": compiled.keys["voice"],
|
||||
"label": compiled.labels["voice"],
|
||||
"revision": compiled.revisions["voice"],
|
||||
"cloned_from": compiled.cloned_from.get("voice") or "",
|
||||
"style_context": dict(compiled.style_context),
|
||||
},
|
||||
"narrative": {
|
||||
"id": compiled.ids["narrative"],
|
||||
@@ -397,6 +544,25 @@ def assert_template_resolved(rendered: str) -> None:
|
||||
)
|
||||
|
||||
|
||||
def omit_orphaned_style_example_rule(template: str) -> str:
|
||||
"""Drop the standing STYLE_EXAMPLES sentence when no example block is sent."""
|
||||
text = template or ""
|
||||
return text.replace(STYLE_EXAMPLES_STANDING_SENTENCE, "").replace(
|
||||
"STYLE_EXAMPLES dienen ausschließlich als Stilreferenz; ihre Inhalte gehören nicht zum heutigen Tag.",
|
||||
"",
|
||||
)
|
||||
|
||||
|
||||
def prompt_without_orphaned_style_example_rule(prompt: dict, *, include_style_examples: bool) -> dict:
|
||||
if include_style_examples:
|
||||
return prompt
|
||||
template = prompt.get("template") or ""
|
||||
cleaned = omit_orphaned_style_example_rule(template)
|
||||
if cleaned == template:
|
||||
return prompt
|
||||
return {**prompt, "template": cleaned}
|
||||
|
||||
|
||||
def load_selection(profile_id: str) -> dict[str, str] | None:
|
||||
with get_db() as conn:
|
||||
row = row_to_dict(
|
||||
@@ -497,21 +663,28 @@ def mark_guidelines_used(ids: list[str]) -> None:
|
||||
)
|
||||
|
||||
|
||||
def _write_fields(raw: dict) -> dict:
|
||||
def _write_fields(raw: dict, *, slot: str) -> dict:
|
||||
max_instruction, max_label, max_summary = _limits()
|
||||
style_context = normalize_style_context(
|
||||
raw.get("style_context"),
|
||||
slot=slot,
|
||||
required=False,
|
||||
)
|
||||
return {
|
||||
"guideline_key": _clean_key(raw.get("guideline_key") or raw.get("key")),
|
||||
"label": _clean_text(raw.get("label"), "label", max_chars=max_label),
|
||||
"summary": _clean_text(raw.get("summary"), "summary", max_chars=max_summary, allow_empty=True),
|
||||
"instruction": _clean_text(raw.get("instruction"), "instruction", max_chars=max_instruction),
|
||||
"sort_order": int(raw.get("sort_order") or 0),
|
||||
"style_context": style_context,
|
||||
"style_context_json": style_context_json(style_context, slot=slot),
|
||||
}
|
||||
|
||||
|
||||
def create_guideline(purpose: str, slot: str, body: dict) -> dict:
|
||||
if slot not in ALLOWED_SLOTS:
|
||||
raise CatalogError("Unbekannter Slot.", code="invalid_generation_guideline", status_code=400)
|
||||
fields = _write_fields(body)
|
||||
fields = _write_fields(body, slot=slot)
|
||||
guideline_id = str(uuid.uuid4())
|
||||
with get_db() as conn:
|
||||
count = conn.execute(
|
||||
@@ -521,11 +694,11 @@ def create_guideline(purpose: str, slot: str, body: dict) -> dict:
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO generation_guidelines (
|
||||
id, purpose, slot, guideline_key, label, summary, instruction, sort_order,
|
||||
id, purpose, slot, guideline_key, label, summary, instruction, style_context_json, sort_order,
|
||||
status, revision, cloned_from, is_default, is_system_seed, seed_id, seed_revision,
|
||||
created, updated
|
||||
)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, 1, NULL, 0, 0, '', '', datetime('now'), datetime('now'))
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 1, NULL, 0, 0, '', '', datetime('now'), datetime('now'))
|
||||
""",
|
||||
(
|
||||
guideline_id,
|
||||
@@ -535,6 +708,7 @@ def create_guideline(purpose: str, slot: str, body: dict) -> dict:
|
||||
fields["label"],
|
||||
fields["summary"],
|
||||
fields["instruction"],
|
||||
fields["style_context_json"],
|
||||
fields["sort_order"] if body.get("sort_order") is not None else int(count) + 1,
|
||||
STATUS_DRAFT,
|
||||
),
|
||||
@@ -557,11 +731,11 @@ def clone_guideline(guideline_id: str) -> dict:
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO generation_guidelines (
|
||||
id, purpose, slot, guideline_key, label, summary, instruction, sort_order,
|
||||
id, purpose, slot, guideline_key, label, summary, instruction, style_context_json, sort_order,
|
||||
status, revision, cloned_from, is_default, is_system_seed, seed_id, seed_revision,
|
||||
created, updated
|
||||
)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 0, 0, '', ?, datetime('now'), datetime('now'))
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 0, 0, '', ?, datetime('now'), datetime('now'))
|
||||
""",
|
||||
(
|
||||
new_id,
|
||||
@@ -571,6 +745,14 @@ def clone_guideline(guideline_id: str) -> dict:
|
||||
current["label"],
|
||||
current["summary"],
|
||||
current["instruction"],
|
||||
current.get("style_context_json") or style_context_json(
|
||||
parse_style_context_json(
|
||||
current.get("style_context_json"),
|
||||
slot=current.get("slot") or "",
|
||||
guideline_key=current.get("guideline_key") or "",
|
||||
),
|
||||
slot=current.get("slot") or "",
|
||||
),
|
||||
int(current.get("sort_order") or 0),
|
||||
STATUS_DRAFT,
|
||||
int(current.get("revision") or 1) + 1,
|
||||
@@ -591,13 +773,20 @@ def update_guideline(guideline_id: str, body: dict) -> dict:
|
||||
code="guideline_immutable",
|
||||
status_code=409,
|
||||
)
|
||||
fields = _write_fields({**current, **body, "guideline_key": body.get("guideline_key") or current["guideline_key"]})
|
||||
merged = {**current, **body, "guideline_key": body.get("guideline_key") or current["guideline_key"]}
|
||||
if "style_context" not in body:
|
||||
merged["style_context"] = parse_style_context_json(
|
||||
current.get("style_context_json"),
|
||||
slot=current.get("slot") or "",
|
||||
guideline_key=current.get("guideline_key") or "",
|
||||
)
|
||||
fields = _write_fields(merged, slot=current.get("slot") or "")
|
||||
with get_db() as conn:
|
||||
conn.execute(
|
||||
"""
|
||||
UPDATE generation_guidelines
|
||||
SET guideline_key = ?, label = ?, summary = ?, instruction = ?, sort_order = ?,
|
||||
updated = datetime('now')
|
||||
SET guideline_key = ?, label = ?, summary = ?, instruction = ?, style_context_json = ?,
|
||||
sort_order = ?, updated = datetime('now')
|
||||
WHERE id = ?
|
||||
""",
|
||||
(
|
||||
@@ -605,6 +794,7 @@ def update_guideline(guideline_id: str, body: dict) -> dict:
|
||||
fields["label"],
|
||||
fields["summary"],
|
||||
fields["instruction"],
|
||||
fields["style_context_json"],
|
||||
fields["sort_order"],
|
||||
guideline_id,
|
||||
),
|
||||
@@ -622,6 +812,16 @@ def publish_guideline(guideline_id: str) -> dict:
|
||||
_clean_text(current.get("label"), "label", max_chars=max_label)
|
||||
_clean_text(current.get("summary"), "summary", max_chars=max_summary, allow_empty=True)
|
||||
_clean_text(current.get("instruction"), "instruction", max_chars=max_instruction)
|
||||
if current.get("slot") == "voice":
|
||||
normalize_style_context(
|
||||
parse_style_context_json(
|
||||
current.get("style_context_json"),
|
||||
slot="voice",
|
||||
guideline_key=current.get("guideline_key") or "",
|
||||
),
|
||||
slot="voice",
|
||||
required=True,
|
||||
)
|
||||
with get_db() as conn:
|
||||
conn.execute(
|
||||
"UPDATE generation_guidelines SET status = ?, updated = datetime('now') WHERE id = ?",
|
||||
@@ -711,6 +911,7 @@ def reset_seed_drafts(purpose: str = PURPOSE_JOURNAL) -> dict:
|
||||
"summary": item.get("summary") or "",
|
||||
"instruction": item.get("instruction") or "",
|
||||
"sort_order": index,
|
||||
"style_context": item.get("style_context") or {},
|
||||
},
|
||||
)
|
||||
)
|
||||
@@ -725,12 +926,17 @@ def preview_selection(selection: dict[str, str], *, purpose: str = PURPOSE_JOURN
|
||||
compiled = compile_selection(selection, purpose=purpose)
|
||||
prompt = load_active_prompt("mvp.journal_generate")
|
||||
assert_journal_prompt_contract(prompt.get("template") or "")
|
||||
include_examples = bool(compiled.style_context.get("include_style_examples"))
|
||||
prompt = prompt_without_orphaned_style_example_rule(
|
||||
prompt,
|
||||
include_style_examples=include_examples,
|
||||
)
|
||||
rendered = preview_prompt(
|
||||
prompt,
|
||||
{
|
||||
**compiled.instructions,
|
||||
"writing_profile": "(nicht enthalten)",
|
||||
"style_examples": "(nicht enthalten)",
|
||||
"style_examples": "(Stilbeispiele)" if include_examples else "",
|
||||
"reconstruction": "(nicht enthalten)",
|
||||
"existing_text": "",
|
||||
},
|
||||
@@ -746,6 +952,8 @@ def preview_selection(selection: dict[str, str], *, purpose: str = PURPOSE_JOURN
|
||||
"keys": dict(compiled.keys),
|
||||
"labels": dict(compiled.labels),
|
||||
"revisions": dict(compiled.revisions),
|
||||
"cloned_from": dict(compiled.cloned_from),
|
||||
"style_context": dict(compiled.style_context),
|
||||
},
|
||||
"seed_revision": compiled.seed_revision,
|
||||
}
|
||||
@@ -758,6 +966,7 @@ def _seed_rows(seed: dict, purpose: str) -> list[tuple]:
|
||||
for slot in ALLOWED_SLOTS:
|
||||
variants = (slots.get(slot) or {}).get("variants") or []
|
||||
for index, item in enumerate(variants):
|
||||
context = normalize_style_context(item.get("style_context") or {}, slot=slot, required=False)
|
||||
rows.append(
|
||||
(
|
||||
item.get("id") or str(uuid.uuid4()),
|
||||
@@ -767,6 +976,7 @@ def _seed_rows(seed: dict, purpose: str) -> list[tuple]:
|
||||
item.get("label") or "",
|
||||
item.get("summary") or "",
|
||||
item.get("instruction") or "",
|
||||
style_context_json(context, slot=slot),
|
||||
index,
|
||||
STATUS_ACTIVE,
|
||||
1,
|
||||
@@ -779,9 +989,132 @@ def _seed_rows(seed: dict, purpose: str) -> list[tuple]:
|
||||
return rows
|
||||
|
||||
|
||||
def seed_generation_instructions(conn) -> None:
|
||||
"""Insert missing seed guidelines. Never overwrite published or admin-created rows."""
|
||||
seed = load_seed_document()
|
||||
def _semantic_signature(item: dict | None = None, *, seed_row: tuple | None = None) -> tuple:
|
||||
if seed_row is not None:
|
||||
slot = seed_row[2] or ""
|
||||
key = seed_row[3] or ""
|
||||
return (
|
||||
key,
|
||||
seed_row[4] or "",
|
||||
seed_row[5] or "",
|
||||
seed_row[6] or "",
|
||||
style_context_json(
|
||||
parse_style_context_json(seed_row[7], slot=slot, guideline_key=key),
|
||||
slot=slot,
|
||||
),
|
||||
)
|
||||
slot = (item or {}).get("slot") or ""
|
||||
key = (item or {}).get("guideline_key") or ""
|
||||
return (
|
||||
key,
|
||||
(item or {}).get("label") or "",
|
||||
(item or {}).get("summary") or "",
|
||||
(item or {}).get("instruction") or "",
|
||||
style_context_json(
|
||||
parse_style_context_json((item or {}).get("style_context_json"), slot=slot, guideline_key=key),
|
||||
slot=slot,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
def _successor_id(base_id: str, revision: int) -> str:
|
||||
root = re.sub(r"-r\d+$", "", base_id or "")
|
||||
return f"{root}-r{int(revision)}"
|
||||
|
||||
|
||||
def _catalog_maps(conn, purpose: str) -> tuple[dict, dict]:
|
||||
rows = [row_to_dict(row) for row in conn.execute(
|
||||
"SELECT * FROM generation_guidelines WHERE purpose = ?",
|
||||
(purpose,),
|
||||
).fetchall()]
|
||||
by_id = {row["id"]: row for row in rows}
|
||||
by_seed_id = {row["seed_id"]: row for row in rows if row.get("seed_id")}
|
||||
return by_id, by_seed_id
|
||||
|
||||
|
||||
def _find_existing_seed_row(by_id: dict, by_seed_id: dict, seed_row: tuple, all_rows: list[dict]) -> dict | None:
|
||||
current = by_id.get(seed_row[0]) or by_seed_id.get(seed_row[13])
|
||||
if current:
|
||||
return current
|
||||
key = seed_row[3] or ""
|
||||
slot = seed_row[2] or ""
|
||||
if key not in LEGACY_COMPARISON_KEYS:
|
||||
return None
|
||||
for item in all_rows:
|
||||
if item.get("slot") == slot and item.get("guideline_key") == key:
|
||||
return item
|
||||
return None
|
||||
|
||||
|
||||
def _find_matching_successor(all_rows: list[dict], predecessor_id: str, seed_row: tuple) -> dict | None:
|
||||
wanted = _semantic_signature(seed_row=seed_row)
|
||||
for item in all_rows:
|
||||
if (item.get("cloned_from") or "") != predecessor_id:
|
||||
continue
|
||||
if _semantic_signature(item) == wanted:
|
||||
return item
|
||||
return None
|
||||
|
||||
|
||||
def _insert_seed_guideline(
|
||||
conn,
|
||||
seed_row: tuple,
|
||||
*,
|
||||
guideline_id: str | None = None,
|
||||
cloned_from: str | None = None,
|
||||
revision: int | None = None,
|
||||
is_default: int | None = None,
|
||||
) -> None:
|
||||
row_id = guideline_id or seed_row[0]
|
||||
seed_id = row_id if guideline_id else seed_row[13]
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO generation_guidelines (
|
||||
id, purpose, slot, guideline_key, label, summary, instruction, style_context_json, sort_order,
|
||||
status, revision, cloned_from, is_default, is_system_seed, seed_id, seed_revision,
|
||||
created, updated
|
||||
)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, datetime('now'), datetime('now'))
|
||||
""",
|
||||
(
|
||||
row_id,
|
||||
seed_row[1],
|
||||
seed_row[2],
|
||||
seed_row[3],
|
||||
seed_row[4],
|
||||
seed_row[5],
|
||||
seed_row[6],
|
||||
seed_row[7],
|
||||
seed_row[8],
|
||||
STATUS_ACTIVE,
|
||||
int(revision if revision is not None else seed_row[10] or 1),
|
||||
cloned_from,
|
||||
int(is_default if is_default is not None else seed_row[11] or 0),
|
||||
1,
|
||||
seed_id,
|
||||
seed_row[14],
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
def _assign_slot_default(conn, purpose: str, slot: str, guideline_id: str) -> None:
|
||||
conn.execute(
|
||||
"""
|
||||
UPDATE generation_guidelines
|
||||
SET is_default = CASE WHEN id = ? THEN 1 ELSE 0 END, updated = datetime('now')
|
||||
WHERE purpose = ? AND slot = ? AND status = ?
|
||||
""",
|
||||
(guideline_id, purpose, slot, STATUS_ACTIVE),
|
||||
)
|
||||
|
||||
|
||||
def seed_generation_instructions(conn, seed: dict | None = None) -> None:
|
||||
"""Insert missing seed guidelines. Never rewrite existing IDs, clones, or archives.
|
||||
|
||||
A semantic change in the seed creates a new successor ID. New defaults may
|
||||
point at that successor. Stored selections keep their previous IDs.
|
||||
"""
|
||||
seed = seed if seed is not None else load_seed_document()
|
||||
purpose = seed.get("purpose") or PURPOSE_JOURNAL
|
||||
conn.execute(
|
||||
"""
|
||||
@@ -791,34 +1124,42 @@ def seed_generation_instructions(conn) -> None:
|
||||
""",
|
||||
(STATUS_ARCHIVED, purpose, LEGACY_SOURCE_MODE_SLOT, STATUS_ARCHIVED),
|
||||
)
|
||||
existing = {
|
||||
row["id"]
|
||||
for row in conn.execute("SELECT id FROM generation_guidelines WHERE purpose = ?", (purpose,)).fetchall()
|
||||
}
|
||||
existing_seed_ids = {
|
||||
row["seed_id"]
|
||||
for row in conn.execute(
|
||||
"SELECT seed_id FROM generation_guidelines WHERE purpose = ? AND seed_id != ''",
|
||||
(purpose,),
|
||||
).fetchall()
|
||||
if row["seed_id"]
|
||||
}
|
||||
for row in _seed_rows(seed, purpose):
|
||||
row_id = row[0]
|
||||
seed_id = row[12]
|
||||
if row_id in existing or seed_id in existing_seed_ids:
|
||||
continue
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO generation_guidelines (
|
||||
id, purpose, slot, guideline_key, label, summary, instruction, sort_order,
|
||||
status, revision, cloned_from, is_default, is_system_seed, seed_id, seed_revision,
|
||||
created, updated
|
||||
by_id, by_seed_id = _catalog_maps(conn, purpose)
|
||||
all_rows = list(by_id.values())
|
||||
current = _find_existing_seed_row(by_id, by_seed_id, row, all_rows)
|
||||
if current:
|
||||
if not _as_bool(current.get("is_system_seed", 0)):
|
||||
continue
|
||||
if current.get("status") != STATUS_ACTIVE:
|
||||
continue
|
||||
if _semantic_signature(current) == _semantic_signature(seed_row=row):
|
||||
continue
|
||||
successor = _find_matching_successor(all_rows, current["id"], row)
|
||||
if successor:
|
||||
if row[11] and successor.get("status") == STATUS_ACTIVE:
|
||||
_assign_slot_default(conn, purpose, row[2], successor["id"])
|
||||
continue
|
||||
next_revision = int(current.get("revision") or 1) + 1
|
||||
taken = set(by_id)
|
||||
successor_id = _successor_id(current["id"], next_revision)
|
||||
while successor_id in taken:
|
||||
next_revision += 1
|
||||
successor_id = _successor_id(current["id"], next_revision)
|
||||
_insert_seed_guideline(
|
||||
conn,
|
||||
row,
|
||||
guideline_id=successor_id,
|
||||
cloned_from=current["id"],
|
||||
revision=next_revision,
|
||||
is_default=int(row[11] or 0),
|
||||
)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NULL, ?, ?, ?, ?, datetime('now'), datetime('now'))
|
||||
""",
|
||||
row,
|
||||
)
|
||||
if row[11]:
|
||||
_assign_slot_default(conn, purpose, row[2], successor_id)
|
||||
continue
|
||||
_insert_seed_guideline(conn, row)
|
||||
if row[11]:
|
||||
_assign_slot_default(conn, purpose, row[2], row[0])
|
||||
|
||||
|
||||
def backfill_missing_settings(conn) -> None:
|
||||
|
||||
Reference in new issue
Block a user