Some checks failed
build-images / web (push) Has been cancelled
vision2026-ci / Python SDK resource tests (push) Has been cancelled
build-images / api (push) Has been cancelled
build-images / worker-gpu (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (comfy-workflow-schema) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (effects-core) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (eventbus-types) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (growth-analytics) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (provider-adapter-kit) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (publishing-connectors) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (render-core) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (shot-template-schema) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (trends-schema) (push) Has been cancelled
vision2026-ci / Desktop engine crates (test + clippy + fmt) (push) Has been cancelled
codeql / Analyze (javascript-typescript) (push) Has been cancelled
codeql / Analyze (python) (push) Has been cancelled
perf / API hot-path micro-smoke (budgeted) (push) Has been cancelled
perf / Desktop engine crates (release build check) (push) Has been cancelled
vision2026-ci / API phase modules (ruff + pytest) (push) Has been cancelled
vision2026-ci / Alembic upgrade + drift check (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (caption-engine) (push) Has been cancelled
Paid, hosted product at menivor.com, now retired as a company project. Fresh squashed history of the cleaned working tree (secret-scanned, no .env, no customer data, no outreach logs). Built on MoneyPrinterTurbo (MIT, harry0703) — see NOTICE.
109 lines
4.1 KiB
Python
109 lines
4.1 KiB
Python
"""Library (assets the agent generated after approval) and folders."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
from sqlalchemy import select
|
|
|
|
from app.domains.adstudio.extract import clean_text
|
|
from app.domains.brand_agent.models import AgentFolder, AgentLibraryItem
|
|
|
|
MAX_FOLDERS = 100
|
|
|
|
|
|
class LibraryError(Exception):
|
|
def __init__(self, message: str, status_code: int = 400) -> None:
|
|
super().__init__(message)
|
|
self.message = message
|
|
self.status_code = status_code
|
|
|
|
|
|
def item_view(row: AgentLibraryItem) -> dict[str, Any]:
|
|
return {
|
|
"id": row.id,
|
|
"space_id": row.space_id,
|
|
"folder_id": row.folder_id,
|
|
"action_id": row.action_id,
|
|
"kind": row.kind,
|
|
"ref": row.ref,
|
|
"url": row.url,
|
|
"title": row.title,
|
|
"body": row.body or "",
|
|
"created_at": row.created_at.isoformat() if row.created_at else None,
|
|
}
|
|
|
|
|
|
def folder_view(row: AgentFolder) -> dict[str, Any]:
|
|
return {"id": row.id, "space_id": row.space_id, "name": row.name,
|
|
"created_at": row.created_at.isoformat() if row.created_at else None}
|
|
|
|
|
|
#: Inline artifact text (a growth task's brief/CSV/bundle) is capped well below the JSON/Text
|
|
#: column limits so one runaway LLM reply can never bloat the table.
|
|
MAX_BODY = 60_000
|
|
|
|
|
|
def add_item(session: Any, *, user_id: str, space_id: str, action_id: str | None, kind: str, ref: str,
|
|
url: str = "", title: str = "", body: str = "") -> dict[str, Any]:
|
|
safe_url = url if str(url or "").startswith(("https://", "http://")) else ""
|
|
row = AgentLibraryItem(user_id=user_id, space_id=space_id, action_id=action_id, kind=kind[:16],
|
|
ref=str(ref or "")[:200], url=safe_url, title=clean_text(title, 300),
|
|
body=str(body or "")[:MAX_BODY])
|
|
session.add(row)
|
|
session.flush()
|
|
return item_view(row)
|
|
|
|
|
|
def list_items(session: Any, user_id: str, space_id: str | None = None, *,
|
|
folder_id: str | None = None) -> list[dict[str, Any]]:
|
|
stmt = select(AgentLibraryItem).where(AgentLibraryItem.user_id == user_id)
|
|
if space_id:
|
|
stmt = stmt.where(AgentLibraryItem.space_id == space_id)
|
|
if folder_id:
|
|
stmt = stmt.where(AgentLibraryItem.folder_id == folder_id)
|
|
rows = session.execute(stmt.order_by(AgentLibraryItem.created_at.desc(), AgentLibraryItem.id)).scalars().all()
|
|
return [item_view(r) for r in rows]
|
|
|
|
|
|
def _folder(session: Any, user_id: str, folder_id: str) -> AgentFolder:
|
|
row = session.get(AgentFolder, folder_id)
|
|
if row is None or row.user_id != user_id:
|
|
raise LibraryError("folder not found", 404)
|
|
return row
|
|
|
|
|
|
def move(session: Any, user_id: str, item_id: str, folder_id: str | None) -> dict[str, Any]:
|
|
row = session.get(AgentLibraryItem, item_id)
|
|
if row is None or row.user_id != user_id:
|
|
raise LibraryError("library item not found", 404)
|
|
if folder_id:
|
|
folder = _folder(session, user_id, folder_id)
|
|
if folder.space_id != row.space_id:
|
|
raise LibraryError("that folder belongs to another brand", 400)
|
|
row.folder_id = folder_id or None
|
|
session.flush()
|
|
return item_view(row)
|
|
|
|
|
|
def create_folder(session: Any, user_id: str, space_id: str, name: str) -> dict[str, Any]:
|
|
title = clean_text(name, 120)
|
|
if not title:
|
|
raise LibraryError("Give the folder a name.", 422)
|
|
if len(list_folders(session, user_id, space_id)) >= MAX_FOLDERS:
|
|
raise LibraryError(f"You can have up to {MAX_FOLDERS} folders.")
|
|
row = AgentFolder(user_id=user_id, space_id=space_id, name=title)
|
|
session.add(row)
|
|
session.flush()
|
|
return folder_view(row)
|
|
|
|
|
|
def list_folders(session: Any, user_id: str, space_id: str | None = None) -> list[dict[str, Any]]:
|
|
stmt = select(AgentFolder).where(AgentFolder.user_id == user_id)
|
|
if space_id:
|
|
stmt = stmt.where(AgentFolder.space_id == space_id)
|
|
return [folder_view(r) for r in session.execute(stmt.order_by(AgentFolder.created_at)).scalars().all()]
|
|
|
|
|
|
__all__ = ["LibraryError", "add_item", "create_folder", "item_view", "list_folders", "list_items", "move"]
|