Spaces:
Sleeping
Sleeping
File size: 15,450 Bytes
06671aa | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 | from __future__ import annotations
import json
import os
import sqlite3
import uuid
from datetime import datetime, timezone
from pathlib import Path
from typing import Any, Optional
from fastapi import HTTPException
from models import (
ApplyResult,
ChatConversation,
ChatConversationDetail,
ChatConversationSummary,
ChatMessage,
ReviewResponse,
RunDetail,
RunSummary,
)
PUBLIC_AGENT_NAME = "TabularAgent"
ROOT_DIR = Path(__file__).resolve().parent
DATA_DIR = Path(os.environ.get("MIKE_PLAYBOOK_DATA_DIR", ROOT_DIR / "data"))
DB_PATH = DATA_DIR / "tabular_review.sqlite3"
def utc_now() -> str:
return datetime.now(timezone.utc).isoformat()
def new_id(prefix: str) -> str:
return f"{prefix}_{datetime.now(timezone.utc).strftime('%Y%m%d_%H%M%S')}_{uuid.uuid4().hex[:10]}"
def reviewed_docx_name(document_name: str) -> str:
stem = Path(document_name or "document.docx").stem or "document"
safe_stem = "".join(
ch if ch.isalnum() or ch in "._- " else "_" for ch in stem
).strip("._ ")
safe_stem = safe_stem.replace(" ", "_")
while "__" in safe_stem:
safe_stem = safe_stem.replace("__", "_")
return f"{safe_stem or 'document'}_tabular_reviewed.docx"
def short_timestamp(value: str) -> str:
try:
dt = datetime.fromisoformat(value)
except ValueError:
return value[:16]
return dt.astimezone(timezone.utc).strftime("%Y-%m-%d %H:%M")
def connection() -> sqlite3.Connection:
DATA_DIR.mkdir(parents=True, exist_ok=True)
conn = sqlite3.connect(DB_PATH)
conn.row_factory = sqlite3.Row
return conn
def init_db() -> None:
with connection() as conn:
conn.executescript(
"""
create table if not exists workflow_runs (
run_id text primary key,
owner_username text not null default 'admin',
created_at text not null,
document_name text not null,
original_docx_path text not null,
commented_docx_path text,
document_text text not null,
playbook_id text not null,
playbook_name text not null,
playbook_text text not null,
max_findings integer not null,
status text not null,
result_json text not null,
apply_report_json text not null,
model text,
usage_json text
);
create table if not exists chat_conversations (
conversation_id text primary key,
owner_username text not null default 'admin',
title text not null,
created_at text not null,
updated_at text not null
);
create table if not exists chat_messages (
message_id text primary key,
conversation_id text not null,
owner_username text not null default 'admin',
created_at text not null,
role text not null,
content text not null,
document_name text,
foreign key(conversation_id) references chat_conversations(conversation_id)
);
"""
)
_ensure_column(
conn, "workflow_runs", "owner_username", "text not null default 'admin'"
)
_ensure_column(
conn,
"chat_conversations",
"owner_username",
"text not null default 'admin'",
)
_ensure_column(
conn, "chat_messages", "owner_username", "text not null default 'admin'"
)
def _ensure_column(
conn: sqlite3.Connection, table: str, column: str, definition: str
) -> None:
existing = {
row["name"] for row in conn.execute(f"pragma table_info({table})").fetchall()
}
if column not in existing:
conn.execute(f"alter table {table} add column {column} {definition}")
def save_workflow_run(
*,
owner_username: str,
run_id: str,
document_name: str,
original_docx_path: Path,
commented_docx_path: Optional[Path],
document_text: str,
playbook_id: str,
playbook_name: str,
playbook_text: str,
max_findings: int,
status: str,
result: ReviewResponse,
apply_report: list[ApplyResult],
model: Optional[str],
usage: Optional[dict[str, Any]],
) -> None:
with connection() as conn:
conn.execute(
"""
insert or replace into workflow_runs (
run_id, owner_username, created_at, document_name, original_docx_path, commented_docx_path,
document_text, playbook_id, playbook_name, playbook_text, max_findings,
status, result_json, apply_report_json, model, usage_json
)
values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""",
(
run_id,
owner_username,
utc_now(),
document_name,
str(original_docx_path),
str(commented_docx_path) if commented_docx_path else None,
document_text,
playbook_id,
playbook_name,
playbook_text,
max_findings,
status,
result.model_dump_json(),
json.dumps(
[item.model_dump() for item in apply_report], ensure_ascii=False
),
model,
json.dumps(usage, ensure_ascii=False) if usage else None,
),
)
def _summary_from_row(row: sqlite3.Row) -> RunSummary:
result = json.loads(row["result_json"])
apply_report = json.loads(row["apply_report_json"])
findings = result.get("findings") if isinstance(result, dict) else []
applied_count = sum(1 for item in apply_report if item.get("status") == "applied")
return RunSummary(
run_id=row["run_id"],
created_at=row["created_at"],
created_short=short_timestamp(row["created_at"]),
document_name=row["document_name"],
commented_docx_name=reviewed_docx_name(row["document_name"]),
playbook_id=row["playbook_id"],
playbook_name=row["playbook_name"],
status=row["status"],
finding_count=len(findings) if isinstance(findings, list) else 0,
applied_count=applied_count,
model=PUBLIC_AGENT_NAME,
)
def list_runs(
owner_username: str, limit: int = 50, playbook_id: str | None = None
) -> list[RunSummary]:
with connection() as conn:
if playbook_id:
rows = conn.execute(
"select * from workflow_runs where owner_username = ? and playbook_id = ? order by created_at desc limit ?",
(owner_username, playbook_id, limit),
).fetchall()
else:
rows = conn.execute(
"select * from workflow_runs where owner_username = ? order by created_at desc limit ?",
(owner_username, limit),
).fetchall()
return [_summary_from_row(row) for row in rows]
def get_run(run_id: str, owner_username: str) -> RunDetail:
with connection() as conn:
row = conn.execute(
"select * from workflow_runs where run_id = ? and owner_username = ?",
(run_id, owner_username),
).fetchone()
if not row:
raise HTTPException(status_code=404, detail=f"Workflow run not found: {run_id}")
summary = _summary_from_row(row)
usage = json.loads(row["usage_json"]) if row["usage_json"] else None
commented_path = (
Path(row["commented_docx_path"]) if row["commented_docx_path"] else None
)
return RunDetail(
**summary.model_dump(),
document_text=row["document_text"],
playbook_text=row["playbook_text"],
result=ReviewResponse.model_validate_json(row["result_json"]),
apply_report=[
ApplyResult.model_validate(item)
for item in json.loads(row["apply_report_json"])
],
usage=usage,
commented_docx_available=bool(commented_path and commented_path.exists()),
)
def get_commented_docx_path(run_id: str, owner_username: str) -> Path:
with connection() as conn:
row = conn.execute(
"select commented_docx_path from workflow_runs where run_id = ? and owner_username = ?",
(run_id, owner_username),
).fetchone()
if not row or not row["commented_docx_path"]:
raise HTTPException(
status_code=404, detail="Commented DOCX is not available for this run."
)
path = Path(row["commented_docx_path"])
if not path.exists():
raise HTTPException(status_code=404, detail="Commented DOCX file is missing.")
return path
def save_chat_message(
*,
owner_username: str,
conversation_id: Optional[str],
role: str,
content: str,
document_name: Optional[str] = None,
) -> str:
now = utc_now()
conv_id = conversation_id or new_id("conv")
title = content.strip().splitlines()[0][:80] or "New conversation"
message_id = new_id("msg")
with connection() as conn:
existing = conn.execute(
"select conversation_id from chat_conversations where conversation_id = ? and owner_username = ?",
(conv_id, owner_username),
).fetchone()
if not existing:
conn.execute(
"insert into chat_conversations (conversation_id, owner_username, title, created_at, updated_at) values (?, ?, ?, ?, ?)",
(conv_id, owner_username, title, now, now),
)
else:
conn.execute(
"update chat_conversations set updated_at = ? where conversation_id = ? and owner_username = ?",
(now, conv_id, owner_username),
)
conn.execute(
"""
insert into chat_messages (
message_id, conversation_id, owner_username, created_at, role, content, document_name
)
values (?, ?, ?, ?, ?, ?, ?)
""",
(
message_id,
conv_id,
owner_username,
now,
role,
content,
document_name,
),
)
return conv_id
def list_conversations(
owner_username: str, limit: int = 20
) -> list[ChatConversationSummary]:
with connection() as conn:
rows = conn.execute(
"""
select conversation_id, title, created_at, updated_at
from chat_conversations
where owner_username = ?
order by updated_at desc
limit ?
""",
(owner_username, limit),
).fetchall()
return [
ChatConversationSummary(
conversation_id=row["conversation_id"],
title=row["title"],
created_at=row["created_at"],
updated_at=row["updated_at"],
)
for row in rows
]
def get_conversation(
conversation_id: str, owner_username: str
) -> ChatConversationDetail:
with connection() as conn:
row = conn.execute(
"""
select conversation_id, owner_username, title, created_at, updated_at
from chat_conversations
where conversation_id = ? and owner_username = ?
""",
(conversation_id, owner_username),
).fetchone()
if not row:
raise HTTPException(
status_code=404, detail=f"Conversation not found: {conversation_id}"
)
messages = conn.execute(
"""
select message_id, created_at, role, content, document_name
from chat_messages
where conversation_id = ? and owner_username = ?
order by created_at asc
""",
(conversation_id, owner_username),
).fetchall()
return ChatConversationDetail(
conversation_id=row["conversation_id"],
owner_username=row["owner_username"],
title=row["title"],
created_at=row["created_at"],
updated_at=row["updated_at"],
messages=[
ChatMessage(
id=message["message_id"],
role=message["role"],
content=message["content"],
created_at=message["created_at"],
document_name=message["document_name"],
)
for message in messages
],
)
def run_to_markdown(run_id: str, owner_username: str) -> str:
run = get_run(run_id, owner_username)
lines = [
f"# Review Run: {run.document_name}",
"",
"## Metadata",
"",
f"- Run ID: `{run.run_id}`",
f"- Created: `{run.created_short}`",
f"- Playbook: `{run.playbook_name}` (`{run.playbook_id}`)",
f"- Status: `{run.status}`",
f"- Agent: `{PUBLIC_AGENT_NAME}`",
f"- Findings: `{run.finding_count}`",
f"- Comments applied: `{run.applied_count}`",
"",
"## Findings",
"",
]
for index, finding in enumerate(run.result.findings, start=1):
apply_result = next(
(item for item in run.apply_report if item.id == finding.id), None
)
lines.extend(
[
f"### {index}. {finding.clause_type} - {finding.risk_level}",
"",
f"**Issue:** {finding.issue}",
"",
f"**Comment inserted:** {finding.comment}",
"",
f"**Internal reasoning:**\n- **Clause/Section:** {finding.review_reasoning.get('clause_section', 'N/A') if isinstance(finding.review_reasoning, dict) else finding.review_reasoning}\n- **Violated Standard:** {finding.review_reasoning.get('violated_standard', 'N/A') if isinstance(finding.review_reasoning, dict) else 'N/A'}\n- **Zamil Exposure:** {finding.review_reasoning.get('zamil_exposure', 'N/A') if isinstance(finding.review_reasoning, dict) else 'N/A'}",
"",
f"**Confidence:** {finding.confidence}",
"",
f"**Apply status:** {apply_result.status if apply_result else 'not_applied'}",
"",
"**Target text:**",
"",
"```text",
finding.target_text,
"```",
"",
]
)
if not run.result.findings:
lines.append("No findings were returned.")
lines.append("")
lines.extend(
[
"## Apply Report",
"",
"```json",
json.dumps(
[item.model_dump() for item in run.apply_report],
indent=2,
ensure_ascii=False,
),
"```",
"",
"## Document Text",
"",
"```text",
run.document_text,
"```",
"",
"## Playbook Text",
"",
"```yaml",
run.playbook_text,
"```",
"",
"## Raw Result JSON",
"",
"```json",
run.result.model_dump_json(indent=2),
"```",
"",
]
)
return "\n".join(lines)
|