MVP 0.92
This commit is contained in:
1 parent
60f0d2830d
commit
53b1eff833
62 files changed
+6578
-355
No files matched your search
+82
-5
@@ -5,6 +5,12 @@ from typing import Any
|
||||
|
||||
from db import get_db, row_to_dict
|
||||
from journal_body import plain_text
|
||||
from prompt_budget import (
|
||||
ERROR_PROMPT_BUDGET_EXCEEDED,
|
||||
JournalBudgetError,
|
||||
day_message_safety_cap,
|
||||
estimate_tokens,
|
||||
)
|
||||
|
||||
# Implementation caps. Not part of the selection-spec contract.
|
||||
_DAY_MESSAGE_CAP = 80
|
||||
@@ -28,11 +34,58 @@ def retrieve(profile_id: str, spec: dict[str, Any]) -> list[dict]:
|
||||
return []
|
||||
|
||||
|
||||
def format_day_messages(messages: list[dict], *, with_source_ids: bool = False) -> str:
|
||||
parts = []
|
||||
for message in messages:
|
||||
role = message.get("role") or "user"
|
||||
body = message.get("body") or ""
|
||||
source_id = message.get("source_id")
|
||||
if with_source_ids and source_id:
|
||||
parts.append(f"[{source_id}] {role}: {body}")
|
||||
else:
|
||||
parts.append(f"{role}: {body}")
|
||||
return "\n".join(parts).strip()
|
||||
|
||||
|
||||
def pair_user_priority_messages(messages: list[dict]) -> list[dict]:
|
||||
"""Keep every user message. Assistant context stays inside the same conversation."""
|
||||
selected: list[dict] = []
|
||||
pending_assistant = None
|
||||
pending_conversation = None
|
||||
for message in messages:
|
||||
conversation_id = message.get("conversation_id")
|
||||
role = message.get("role") or "user"
|
||||
if pending_assistant is not None and conversation_id != pending_conversation:
|
||||
pending_assistant = None
|
||||
pending_conversation = None
|
||||
if role == "assistant":
|
||||
pending_assistant = message
|
||||
pending_conversation = conversation_id
|
||||
continue
|
||||
if role == "user":
|
||||
if pending_assistant is not None and pending_conversation == conversation_id:
|
||||
selected.append(pending_assistant)
|
||||
selected.append(message)
|
||||
pending_assistant = None
|
||||
pending_conversation = None
|
||||
continue
|
||||
pending_assistant = None
|
||||
pending_conversation = None
|
||||
return selected
|
||||
|
||||
|
||||
def _day_messages(profile_id: str, spec: dict[str, Any]) -> list[dict]:
|
||||
journal_day_id = spec.get("journal_day_id")
|
||||
conversation_ids = spec.get("conversation_ids") or []
|
||||
if not journal_day_id:
|
||||
return []
|
||||
overflow = spec.get("overflow") or "limit"
|
||||
if overflow == "abort":
|
||||
cap = int(spec.get("message_cap") or day_message_safety_cap())
|
||||
fetch_limit = cap + 1
|
||||
else:
|
||||
cap = int(spec.get("message_cap") or _DAY_MESSAGE_CAP)
|
||||
fetch_limit = cap
|
||||
with get_db() as conn:
|
||||
if conversation_ids:
|
||||
placeholders = ",".join("?" * len(conversation_ids))
|
||||
@@ -42,10 +95,10 @@ def _day_messages(profile_id: str, spec: dict[str, Any]) -> list[dict]:
|
||||
FROM messages m
|
||||
JOIN conversations c ON c.id = m.conversation_id
|
||||
WHERE m.profile_id = ? AND c.journal_day_id = ? AND m.conversation_id IN ({placeholders})
|
||||
ORDER BY c.created, m.seq
|
||||
ORDER BY c.created, c.rowid, m.seq, m.id
|
||||
LIMIT ?
|
||||
""",
|
||||
(profile_id, journal_day_id, *conversation_ids, _DAY_MESSAGE_CAP),
|
||||
(profile_id, journal_day_id, *conversation_ids, fetch_limit),
|
||||
).fetchall()
|
||||
else:
|
||||
rows = conn.execute(
|
||||
@@ -54,12 +107,36 @@ def _day_messages(profile_id: str, spec: dict[str, Any]) -> list[dict]:
|
||||
FROM messages m
|
||||
JOIN conversations c ON c.id = m.conversation_id
|
||||
WHERE m.profile_id = ? AND c.journal_day_id = ?
|
||||
ORDER BY c.created, m.seq
|
||||
ORDER BY c.created, c.rowid, m.seq, m.id
|
||||
LIMIT ?
|
||||
""",
|
||||
(profile_id, journal_day_id, _DAY_MESSAGE_CAP),
|
||||
(profile_id, journal_day_id, fetch_limit),
|
||||
).fetchall()
|
||||
return [row_to_dict(row) for row in rows]
|
||||
messages = [row_to_dict(row) for row in rows]
|
||||
if overflow == "abort":
|
||||
if len(messages) > cap:
|
||||
raise JournalBudgetError(
|
||||
ERROR_PROMPT_BUDGET_EXCEEDED,
|
||||
diagnostics={"reason": "day_message_cap", "message_cap": cap, "fetched": len(messages)},
|
||||
)
|
||||
from journal_reconstruct import assign_source_ids
|
||||
|
||||
paired = assign_source_ids(pair_user_priority_messages(messages))
|
||||
max_tokens = spec.get("max_estimated_tokens")
|
||||
if max_tokens is not None:
|
||||
used = estimate_tokens(format_day_messages(paired, with_source_ids=True))
|
||||
if used > int(max_tokens):
|
||||
raise JournalBudgetError(
|
||||
ERROR_PROMPT_BUDGET_EXCEEDED,
|
||||
diagnostics={
|
||||
"reason": "day_dialogue_tokens",
|
||||
"estimated_tokens": used,
|
||||
"available_tokens": int(max_tokens),
|
||||
"message_count": len(paired),
|
||||
},
|
||||
)
|
||||
return paired
|
||||
return messages
|
||||
|
||||
|
||||
def _space_entries(profile_id: str, spec: dict[str, Any]) -> list[dict]:
|
||||
|
||||
Reference in new issue
Block a user