Revise plain estimate answers from inline price zones
This commit is contained in:
@@ -639,6 +639,38 @@ def get_linked_estimate(username: str, tab_id: str, estimate_id: int) -> dict |
|
||||
return data if answer else None
|
||||
|
||||
|
||||
def get_scoped_assistant(username: str, tab_id: str, message_id: int) -> dict | None:
|
||||
"""Plain answer and the question it answered, scoped to one user's dialog."""
|
||||
with _conn() as conn:
|
||||
answer = conn.execute(
|
||||
"SELECT id, content, timestamp FROM messages "
|
||||
"WHERE id=? AND username=? AND tab_id=? AND role='assistant'",
|
||||
(message_id, username, tab_id),
|
||||
).fetchone()
|
||||
if not answer:
|
||||
return None
|
||||
question = conn.execute(
|
||||
"SELECT content FROM messages WHERE username=? AND tab_id=? AND role='user' "
|
||||
"AND (timestamp < ? OR (timestamp = ? AND id < ?)) "
|
||||
"ORDER BY timestamp DESC, id DESC LIMIT 1",
|
||||
(username, tab_id, answer["timestamp"], answer["timestamp"], message_id),
|
||||
).fetchone()
|
||||
return {"content": answer["content"], "question": question["content"] if question else ""}
|
||||
|
||||
|
||||
def replace_scoped_assistant(username: str, tab_id: str, message_id: int,
|
||||
old_content: str, new_content: str) -> bool:
|
||||
"""Atomically reject another tab's edit instead of overwriting it."""
|
||||
with _conn() as conn:
|
||||
conn.execute("BEGIN IMMEDIATE")
|
||||
cursor = conn.execute(
|
||||
"UPDATE messages SET content=? WHERE id=? AND username=? AND tab_id=? "
|
||||
"AND role='assistant' AND content=?",
|
||||
(new_content, message_id, username, tab_id, old_content),
|
||||
)
|
||||
return cursor.rowcount == 1
|
||||
|
||||
|
||||
def replace_linked_estimate(username: str, tab_id: str, estimate_id: int,
|
||||
expected_revision: int, data: dict, answer: str) -> str:
|
||||
"""Compare-and-swap answer and estimate in one SQLite transaction."""
|
||||
|
||||
Reference in New Issue
Block a user