feat: Oracle Canvas, Revision History and Canvas Sharing (#33)

Co-authored-by: Sagnik <sagnik7896@gmail.com>
Reviewed-on: #33
This commit was merged in pull request #33.
This commit is contained in:
2026-04-23 01:20:21 +05:30
parent e519339cc9
commit 6cdc366718
58 changed files with 3187 additions and 705 deletions

View File

@@ -70,6 +70,31 @@ def _json_object(value: Any) -> dict[str, Any]:
return {}
def _json_array(value: Any) -> list[Any]:
if isinstance(value, list):
return value
if isinstance(value, str) and value.strip():
try:
parsed = json.loads(value)
if isinstance(parsed, list):
return parsed
except Exception:
logger.warning("canvas_service: failed to parse JSON array field; using empty array")
return []
def _json_safe(value: Any) -> Any:
if isinstance(value, datetime):
return value.isoformat()
if isinstance(value, dict):
return {str(key): _json_safe(val) for key, val in value.items()}
if isinstance(value, list):
return [_json_safe(item) for item in value]
if isinstance(value, tuple):
return [_json_safe(item) for item in value]
return value
def _normalize_component(component: dict[str, Any]) -> dict[str, Any]:
normalized = deepcopy(component)
normalized["componentId"] = _stringify(normalized.get("componentId"))
@@ -224,9 +249,15 @@ class CanvasService:
async def get_first_page_for_owner(self, *, tenant_id: str, owner_id: str) -> dict[str, Any] | None:
_ensure_ready()
if _is_demo():
for page in _DEMO_PAGES.values():
if page["tenantId"] == tenant_id and page["ownerId"] == owner_id:
return {**page, "components": deepcopy(_DEMO_COMPONENTS.get(page["pageId"], []))}
candidates = [
page
for page in _DEMO_PAGES.values()
if page["tenantId"] == tenant_id and page["ownerId"] == owner_id
]
if candidates:
candidates.sort(key=lambda page: page.get("updatedAt", ""), reverse=True)
page = candidates[0]
return {**page, "components": deepcopy(_DEMO_COMPONENTS.get(page["pageId"], []))}
return None
assert asyncpg is not None
@@ -237,7 +268,7 @@ class CanvasService:
SELECT *
FROM oracle_canvas_pages
WHERE tenant_id = $1 AND owner_id = $2
ORDER BY created_at ASC
ORDER BY updated_at DESC, created_at DESC
LIMIT 1
""",
tenant_id,
@@ -310,7 +341,7 @@ class CanvasService:
"actorId": actor_id,
"executionId": execution_id,
"mergeRequestId": merge_request_id,
"componentsSnapshot": json.dumps(components),
"componentsSnapshot": json.dumps(_json_safe(components)),
"idempotencyKey": idempotency_key,
"createdAt": _now(),
}
@@ -346,7 +377,7 @@ class CanvasService:
"actorId": existing["actor_id"],
"executionId": _stringify(existing["execution_id"]) if existing["execution_id"] else None,
"mergeRequestId": _stringify(existing["merge_request_id"]) if existing["merge_request_id"] else None,
"componentsSnapshot": json.dumps(existing["components_snapshot"]),
"componentsSnapshot": json.dumps(_json_safe(existing["components_snapshot"])),
"idempotencyKey": existing["idempotency_key"],
"createdAt": existing["created_at"].isoformat(),
}
@@ -385,7 +416,7 @@ class CanvasService:
actor_id,
execution_id or "",
merge_request_id or "",
json.dumps(normalized_components),
json.dumps(_json_safe(normalized_components)),
idempotency_key,
)
@@ -411,7 +442,7 @@ class CanvasService:
"actorId": revision["actor_id"],
"executionId": _stringify(revision["execution_id"]) if revision["execution_id"] else None,
"mergeRequestId": _stringify(revision["merge_request_id"]) if revision["merge_request_id"] else None,
"componentsSnapshot": json.dumps(revision["components_snapshot"]),
"componentsSnapshot": json.dumps(_json_safe(revision["components_snapshot"])),
"idempotencyKey": revision["idempotency_key"],
"createdAt": revision["created_at"].isoformat(),
}
@@ -462,13 +493,14 @@ class CanvasService:
)
if not revision:
raise ValueError(f"Revision {target_revision} not found for page {page_id}")
snapshot = _json_array(revision["components_snapshot"])
return await self.commit_revision(
page_id=page_id,
tenant_id=tenant_id,
actor_id=actor_id,
commit_kind="rollback",
commit_summary=f"Rollback to revision {target_revision}",
components=list(revision["components_snapshot"]),
components=snapshot,
idempotency_key=idempotency_key,
)
finally:
@@ -604,15 +636,15 @@ class CanvasService:
component.get("description"),
int(component.get("version", 1)),
component.get("lifecycleState", "active"),
json.dumps(component.get("dataSourceDescriptor", {})),
json.dumps(component.get("visualizationParameters", {})),
json.dumps(component.get("dataBindings", {})),
json.dumps(component.get("provenance", {})),
json.dumps(component.get("renderingHints", {})),
json.dumps(component.get("layout", {})),
json.dumps(component.get("accessControls", {})),
json.dumps(component.get("styleSignature", {})),
json.dumps(component.get("validationState", {})),
json.dumps(_json_safe(component.get("dataSourceDescriptor", {}))),
json.dumps(_json_safe(component.get("visualizationParameters", {}))),
json.dumps(_json_safe(component.get("dataBindings", {}))),
json.dumps(_json_safe(component.get("provenance", {}))),
json.dumps(_json_safe(component.get("renderingHints", {}))),
json.dumps(_json_safe(component.get("layout", {}))),
json.dumps(_json_safe(component.get("accessControls", {}))),
json.dumps(_json_safe(component.get("styleSignature", {}))),
json.dumps(_json_safe(component.get("validationState", {}))),
list(component.get("auditLog", [])),
)