arena/094f314c-aram
add-index-html-10623938925177053894
arena/094f314c-aram
chatgpt-phase1b2-autopilot
codex/adr-future-connectors-pi-v1
codex/aram-account-note-source-trace-20261031
codex/aram-actions-node24-maintenance-20261031
codex/aram-analyst-review-workbook-20261031
codex/aram-bs-candidate-statement-20261031
codex/aram-cash-flow-direct-candidate-20261031
codex/aram-clean-reference-year-integrity-20261030
codex/aram-cross-statement-controls-20261031
codex/aram-equity-changes-candidate-20261031
codex/aram-five-statement-cli-20261031
codex/aram-five-statement-integration-20261031
codex/aram-five-statement-orchestrator-20261031
codex/aram-gate3-coverage-target-guard-20261030
codex/aram-gate3-hidden-xlsx-guard-20261030
codex/aram-gate3-integration-validation-20261030
codex/aram-gate3-mapping-change-preview-20261030
codex/aram-gate3-stale-note-guard-20261030
codex/aram-income-statement-candidate-20261031
codex/aram-manifest-referential-integrity-20261031
codex/aram-mapping-source-parser-20261101
codex/aram-multidimensional-mapping-20261101
codex/aram-multistatement-mapping-20261031
codex/aram-pipeline-manifest-schema-20261031
codex/aram-readonly-mapping-coverage-20261031
codex/aram-reconciliation-review-gate-20261031
codex/aram-same-flush-fiscal-lock-20261030
codex/aram-synthetic-windows-demo-20261031
codex/aram-trial-balance-diagnostic-20261031
codex/aram-windows-background-detail-search-20261031
codex/aram-windows-detail-review-20261031
codex/aram-windows-five-statement-pilot-20261031
codex/aram-windows-statement-figures-20261031
codex/gate-2a-pilot-workbook
codex/gate-2a-pilot-workbook-6864754244772568979
codex/gate3-authenticated-principal-boundary-20261030
codex/gate3-authored-draft-staging-20261030
codex/gate3-carry-forward-preview-20261023
codex/gate3-draft-staging-audit-20261030
codex/gate3-mapping-batch-freshness-20261030
codex/gate3-mapping-batch-review-20261030
codex/gate3-mapping-core
codex/gate3-mapping-coverage-20261023
codex/gate3-mapping-excel-20261023
codex/gate3-note-mapping-review-20261023
codex/gate3-signed-year-scope-20261030
codex/gate3-unapproved-proposal-storage-20261030
codex/payvast-mapping-discovery-20261023
codex/phase-2a-backend-foundation
codex/phase-2b-post-merge-record
codex/phase-2b-source-registration
codex/phase-2b-source-registration-12955190277775369594
codex/post-gate2a-control-reconciliation
codex/post-pr18-acceptance-record
codex/project-control-foundation
codex/t1-repo-hygiene-20261023
codex/t2-ci-hardening-20261023
codex/t3-explicit-migrations-20261023
codex/t6a-readonly-diagnostics-20261023
codex/v1-01-safety-gate-hardening
codex/v1-year-isolation-followup-20261023
codex/windows-native-pilot-shell-20261030
docs/aram-master-control-plan-20261111
docs/aram-roadmap-dashboard-20261030
docs/financial-statement-reconciliation-1402-1404-20261110
feat/gate3-governed-mapping-20261101
feat/gate3-package9-1-security-20261103
feat/gate3-package9-isolated-20261103
fix/password-unicode-validation-20261111
governance/aram-5-1-3-controls-20261030
governance/role-based-operating-model
jules-1168663913887856558-73b8a885
jules-gate-2b-mapping-workflow-foundation-4137724387867065330
phase-1b1-audit-report-7365389436990903239
phase-2a-backend-foundation-10237597358138521261
phase1b2-postmerge-metadata-fix
refactor/drg-parent-year-id-15088443704684163304
source-data-cleanup
| 1 | """JSON-in / JSON-out bridge that runs the exact Aram engine in the browser. |
| 2 | |
| 3 | This mirrors backend/src/aram_backend/pilot_api.py but without FastAPI, so the |
| 4 | identical, tested engine/store/editing code runs client-side under Pyodide. |
| 5 | Everything stays in memory; the durable artifacts are the exported workbooks. |
| 6 | """ |
| 7 | |
| 8 | from __future__ import annotations |
| 9 | |
| 10 | import base64 |
| 11 | import json |
| 12 | import re |
| 13 | |
| 14 | from aram_backend.pilot_demo import build_demo_workbook |
| 15 | from aram_backend.pilot_editing import ( |
| 16 | DuplicateAccountError, |
| 17 | DuplicateNoteError, |
| 18 | EditingError, |
| 19 | NoteInUseError, |
| 20 | UnknownNoteError, |
| 21 | make_company, |
| 22 | make_note, |
| 23 | ) |
| 24 | from aram_backend.pilot_engine import ( |
| 25 | DEFERRED_STATEMENTS, |
| 26 | RENDERED_STATEMENTS, |
| 27 | SECTION_LABELS_FA, |
| 28 | SECTION_POLICY, |
| 29 | STATEMENT_LABELS_FA, |
| 30 | PilotModelError, |
| 31 | accounts_table, |
| 32 | build_report, |
| 33 | export_report_workbook, |
| 34 | export_working_workbook, |
| 35 | notes_table, |
| 36 | parse_pilot_model, |
| 37 | ) |
| 38 | from aram_backend.pilot_store import ( |
| 39 | InvalidApprovalError, |
| 40 | PilotSessionStore, |
| 41 | UnknownAccountError, |
| 42 | UnknownSessionError, |
| 43 | UnknownTargetError, |
| 44 | ) |
| 45 | |
| 46 | store = PilotSessionStore() |
| 47 | |
| 48 | |
| 49 | def _payload(session_id: str) -> dict: |
| 50 | session, working, trail = store.snapshot(session_id) |
| 51 | report = build_report(working, working.mappings) |
| 52 | return { |
| 53 | "session": { |
| 54 | "id": session.id, "created_at": session.created_at, |
| 55 | "source": session.source, "filename": session.filename, |
| 56 | }, |
| 57 | "report": report, |
| 58 | "accounts": accounts_table(working, working.mappings), |
| 59 | "notes": notes_table(working), |
| 60 | "audit_trail": trail, |
| 61 | } |
| 62 | |
| 63 | |
| 64 | def _meta() -> dict: |
| 65 | return { |
| 66 | "rendered_statements": list(RENDERED_STATEMENTS), |
| 67 | "deferred_statements": list(DEFERRED_STATEMENTS), |
| 68 | "statement_labels_fa": STATEMENT_LABELS_FA, |
| 69 | "section_policy": SECTION_POLICY, |
| 70 | "section_labels_fa": SECTION_LABELS_FA, |
| 71 | "contract": "Pilot workbook v0.1", |
| 72 | "disclaimer_fa": "خروجی این ابزار پیشنویس کاری برای بررسی است، نه صورت مالی رسمی.", |
| 73 | } |
| 74 | |
| 75 | |
| 76 | def _create(data: bytes, source: str, filename: str) -> dict: |
| 77 | model = parse_pilot_model(data) |
| 78 | session = store.create(model, source=source, filename=filename) |
| 79 | return _payload(session.id) |
| 80 | |
| 81 | |
| 82 | def _export(session_id: str, kind: str) -> dict: |
| 83 | session = store.get(session_id) |
| 84 | _, working, trail = store.snapshot(session_id) |
| 85 | safe = re.sub(r"[^A-Za-z0-9_-]", "", working.company.code) or "session" |
| 86 | if kind == "workspace": |
| 87 | data = export_working_workbook(working) |
| 88 | name = f"aram-workspace-{safe}-{working.company.fiscal_year}.xlsx" |
| 89 | else: |
| 90 | report = build_report(working, working.mappings) |
| 91 | data = export_report_workbook(working, working.mappings, report, trail, session.source) |
| 92 | name = f"aram-pilot-{safe}-{working.company.fiscal_year}.xlsx" |
| 93 | return {"filename": name, "bytes_b64": base64.b64encode(data).decode("ascii")} |
| 94 | |
| 95 | |
| 96 | def _dispatch(action: str, d: dict): |
| 97 | if action == "meta": |
| 98 | return _meta() |
| 99 | if action == "demo": |
| 100 | return _create(build_demo_workbook(), "demo", "aram-synthetic-demo.xlsx") |
| 101 | if action == "upload": |
| 102 | raw = base64.b64decode(d["bytes_b64"]) |
| 103 | if not raw: |
| 104 | raise EditingError("فایل خالی است.") |
| 105 | return _create(raw, "upload", d.get("filename", "upload.xlsx")) |
| 106 | if action == "get": |
| 107 | return _payload(d["sid"]) |
| 108 | if action == "delete_session": |
| 109 | store.delete(d["sid"]) |
| 110 | return {"deleted": d["sid"]} |
| 111 | if action == "set_mapping": |
| 112 | store.set_mapping( |
| 113 | d["sid"], d["code"], statement=d["statement"].strip(), section=d["section"].strip(), |
| 114 | note_number=d["note_number"].strip(), subnote_code=d.get("subnote_code", "").strip(), |
| 115 | approval_status=d.get("approval_status", "Draft"), reviewer=d.get("reviewer", ""), |
| 116 | actor=d.get("actor", ""), |
| 117 | ) |
| 118 | return _payload(d["sid"]) |
| 119 | if action == "clear_mapping": |
| 120 | store.clear_mapping(d["sid"], d["code"], actor=d.get("actor", "")) |
| 121 | return _payload(d["sid"]) |
| 122 | if action == "add_account": |
| 123 | store.add_account( |
| 124 | d["sid"], full_code=d["full_code"], name=d["name"], general_code=d["general_code"], |
| 125 | general_name=d["general_name"], opening=d.get("opening", "0"), debit=d.get("debit", "0"), |
| 126 | credit=d.get("credit", "0"), source_row=d.get("source_row"), row_status=d.get("row_status", "Draft"), |
| 127 | subsidiary_code=d.get("subsidiary_code", ""), subsidiary_name=d.get("subsidiary_name", ""), |
| 128 | actor=d.get("actor", ""), |
| 129 | ) |
| 130 | return _payload(d["sid"]) |
| 131 | if action == "update_account": |
| 132 | store.update_account( |
| 133 | d["sid"], d["code"], name=d["name"], general_code=d["general_code"], |
| 134 | general_name=d["general_name"], opening=d.get("opening", "0"), debit=d.get("debit", "0"), |
| 135 | credit=d.get("credit", "0"), row_status=d.get("row_status", "Draft"), |
| 136 | subsidiary_code=d.get("subsidiary_code", ""), subsidiary_name=d.get("subsidiary_name", ""), |
| 137 | actor=d.get("actor", ""), |
| 138 | ) |
| 139 | return _payload(d["sid"]) |
| 140 | if action == "rename_account": |
| 141 | store.rename_account(d["sid"], d["code"], d["new_code"], actor=d.get("actor", "")) |
| 142 | return _payload(d["sid"]) |
| 143 | if action == "delete_account": |
| 144 | store.delete_account(d["sid"], d["code"], actor=d.get("actor", "")) |
| 145 | return _payload(d["sid"]) |
| 146 | if action == "add_note": |
| 147 | note = make_note( |
| 148 | statement=d["statement"], section=d["section"], note_number=d["note_number"], |
| 149 | note_title=d["note_title"], subnote_code=d.get("subnote_code", ""), |
| 150 | subnote_title=d.get("subnote_title", ""), |
| 151 | ) |
| 152 | store.add_note(d["sid"], note, actor=d.get("actor", "")) |
| 153 | return _payload(d["sid"]) |
| 154 | if action == "update_note": |
| 155 | key = (d["statement"].strip(), d["section"].strip(), d["note_number"].strip(), d.get("subnote_code", "").strip()) |
| 156 | note = make_note( |
| 157 | statement=d["statement"], section=d["section"], note_number=d["note_number"], |
| 158 | note_title=d["note_title"], subnote_code=d.get("subnote_code", ""), |
| 159 | subnote_title=d.get("subnote_title", ""), |
| 160 | ) |
| 161 | store.update_note(d["sid"], key, note, actor=d.get("actor", "")) |
| 162 | return _payload(d["sid"]) |
| 163 | if action == "delete_note": |
| 164 | key = (d["statement"].strip(), d["section"].strip(), d["note_number"].strip(), d.get("subnote_code", "").strip()) |
| 165 | store.delete_note(d["sid"], key, actor=d.get("actor", "")) |
| 166 | return _payload(d["sid"]) |
| 167 | if action == "update_company": |
| 168 | company = make_company( |
| 169 | code=d["code"], name=d["name"], fiscal_year=d["fiscal_year"], |
| 170 | currency=d.get("currency", ""), unit=d.get("unit", ""), |
| 171 | prepared_by=d.get("prepared_by", ""), status=d.get("status", "Draft"), |
| 172 | ) |
| 173 | store.update_company(d["sid"], company, actor=d.get("actor", "")) |
| 174 | return _payload(d["sid"]) |
| 175 | if action == "export_report": |
| 176 | return _export(d["sid"], "report") |
| 177 | if action == "export_workspace": |
| 178 | return _export(d["sid"], "workspace") |
| 179 | raise EditingError(f"اقدام ناشناخته: {action}") |
| 180 | |
| 181 | |
| 182 | def handle(action: str, data_json: str = "") -> str: |
| 183 | """Single entry point called from JavaScript. Always returns a JSON string.""" |
| 184 | try: |
| 185 | data = json.loads(data_json) if data_json else {} |
| 186 | except json.JSONDecodeError: |
| 187 | data = {} |
| 188 | try: |
| 189 | return json.dumps({"ok": True, "data": _dispatch(action, data)}, ensure_ascii=False) |
| 190 | except PilotModelError as exc: |
| 191 | return json.dumps({ |
| 192 | "ok": False, |
| 193 | "error": "فایل با قرارداد پایلوت v0.1 سازگار نیست؛ خطاهای زیر را رفع کنید.", |
| 194 | "issues": [ |
| 195 | {"severity": i.severity, "code": i.code, "sheet": i.sheet, |
| 196 | "row": i.row, "field": i.field, "message": i.message} |
| 197 | for i in exc.issues |
| 198 | ], |
| 199 | }, ensure_ascii=False) |
| 200 | except (UnknownSessionError, UnknownAccountError, UnknownNoteError) as exc: |
| 201 | return json.dumps({"ok": False, "error": str(exc) or "مورد یافت نشد.", "status": 404}, ensure_ascii=False) |
| 202 | except (DuplicateAccountError, DuplicateNoteError, NoteInUseError) as exc: |
| 203 | return json.dumps({"ok": False, "error": str(exc), "status": 409}, ensure_ascii=False) |
| 204 | except (EditingError, UnknownTargetError, InvalidApprovalError) as exc: |
| 205 | return json.dumps({"ok": False, "error": str(exc), "status": 422}, ensure_ascii=False) |
| 206 | except Exception as exc: # noqa: BLE001 - surface a safe message to the UI |
| 207 | return json.dumps({"ok": False, "error": f"خطای غیرمنتظره: {exc}"}, ensure_ascii=False) |