Download api/tool_groups/files.py from fsanyoto/loopable: direct link, hf CLI and curl.
- Browser
- Download file 9.93 kB
-
https://huggingface.co/spaces/fsanyoto/loopable/resolve/main/api/tool_groups/files.py
- Command line
-
hf download hf://spaces/fsanyoto/loopable/api/tool_groups/files.py
-
curl -L -o files.py https://huggingface.co/spaces/fsanyoto/loopable/resolve/main/api/tool_groups/files.py
9.93 kB
| """List current builds through their native reader doors.""" | |
| from dataclasses import replace | |
| from urllib.parse import urlencode | |
| from fastapi import HTTPException | |
| from core import external_tools, grid_events, table_store | |
| from deps import Session, err | |
| import intelligence_store as brain | |
| import wiki_log | |
| KINDS = tuple(sorted(wiki_log.KINDS)) | |
| MISSING_DESCRIPTION = "Description missing." | |
| def _permitted(session, module): | |
| try: | |
| session.require(module) | |
| return True | |
| except HTTPException as exc: | |
| if exc.status_code != 403: | |
| raise | |
| return False | |
| def _views(ctx): | |
| from routes_external import _assert_database_scope | |
| from routes_shares import _field_storage_keys | |
| for database in ctx.list_databases()["databases"]: | |
| if not database.get("permitted"): | |
| continue | |
| key = database["database"] | |
| _assert_database_scope(ctx.session, key) | |
| tops = table_store.make(_field_storage_keys(key)[0], st=ctx.session.runtime) | |
| event = grid_events.EventCtx( | |
| uname=ctx.session.uname, allowed_pids=frozenset(), fields=[], | |
| admin=ctx.session.admin, st=ctx.session.runtime, table=tops, | |
| scope_key={"customer_data": "customer", "product_data": "product"}.get(key, key)) | |
| workspace = grid_events.table_workspace(event, allowed_pids=frozenset(), consume_corrections=False) | |
| for row in (workspace.get("views") or {}).values(): | |
| yield "view", row["id"], key, row | |
| def _workbooks(ctx): | |
| from routes_external import _assert_workbook_scope | |
| from routes_sheet import list_sheets | |
| if _permitted(ctx.session, "sheets"): | |
| for row in list_sheets(session=ctx.session)["workbooks"]: | |
| try: | |
| _assert_workbook_scope(ctx.session, row["id"]) | |
| except HTTPException as exc: | |
| if exc.status_code not in (403, 404): | |
| raise | |
| continue | |
| yield "workbook", row["id"], "", row | |
| def _apps(ctx): | |
| import application_registry as applications | |
| from .apps import _read, list_apps | |
| for row in list_apps(ctx, {})["applications"]: | |
| try: | |
| _read(ctx, row["id"]) | |
| except (applications.ApplicationError, PermissionError): | |
| continue | |
| except HTTPException as exc: | |
| if exc.status_code not in (403, 404): | |
| raise | |
| continue | |
| yield "app", row["id"], "", row | |
| def _automations(ctx): | |
| import automation_engine | |
| from .automations import list_automations | |
| if _permitted(ctx.session, "automation"): | |
| allowed = list_automations(ctx, {}) | |
| definitions = automation_engine.all_definitions(ctx.session.runtime) if allowed else {} | |
| for row in allowed: | |
| yield "automation", row["id"], "", definitions.get(row["id"], row) | |
| def _schedules(ctx): | |
| from .schedules import list_schedules | |
| for row in list_schedules(ctx, {}): | |
| yield "schedule", row["id"], "", row | |
| def _brain(ctx): | |
| if not _permitted(ctx.session, "intelligence"): | |
| return | |
| offset = 0 | |
| while True: | |
| page = brain.list_files(ctx.session, offset=offset, limit=100) | |
| for row in page["files"]: | |
| path = row["path"] | |
| if wiki_log.reserved(path) or wiki_log.purpose_file(path): | |
| continue | |
| kind = "skill" if row["section"] in {"playbooks", "agent-prompts"} else "file" | |
| yield kind, path, "", row | |
| offset += len(page["files"]) | |
| if offset >= page["total"]: | |
| return | |
| if not page["files"]: | |
| raise err(503, "tool_unavailable", "Brain files could not be listed. Try again.") | |
| def _updated(row): | |
| for key in ("updated", "updatedAt", "updated_at"): | |
| if row.get(key): | |
| return row[key] | |
| return None | |
| def _resolve(session, target, documents): | |
| if target["kind"] != "view" or _permitted(session, "intelligence"): | |
| return wiki_log.resolve_object(session, target, documents) | |
| # Wiki resolution requires Brain access. View inventory uses the same native source and share checks. | |
| from routes_shares import _field_storage_keys | |
| tops = table_store.make(_field_storage_keys(target["database"])[0], st=session.runtime) | |
| found = tops.find_view(target["id"], prefer_owner=session.uname) | |
| if not found: | |
| return None | |
| owner, view = found[:2] | |
| if owner != session.uname and not grid_events._shared_view_role( | |
| tops, target["id"], owner, session.uname, st=session.runtime): | |
| return None | |
| return {**target, "title": view.get("name") or target["id"], "owner": owner, | |
| "href": "#/data?" + urlencode({"database": target["database"], "view": target["id"]})} | |
| def list_files(ctx, args): | |
| """List all readable builds in kind and title order, with filters and stable cursor pages.""" | |
| from routes_external import ( | |
| _PreviewRuntime, _decode_query_cursor, _encode_query_cursor, _json_digest, | |
| _tool_context, require_external_analyst, | |
| ) | |
| if not isinstance(ctx, external_tools.ToolContext) or not isinstance(ctx.session, Session): | |
| raise err(403, "tool_context_required", "Use an authenticated AI connection.") | |
| require_external_analyst(ctx.session) | |
| if ctx.session.runtime.key != ctx.session.tenant: | |
| raise err(403, "tool_context_required", "Use this workspace's authenticated AI connection.") | |
| external_tools.validate_arguments(args, GROUP["actions"]["list"]["schema"]) | |
| # Native readers may migrate old workspaces. Keep those writes in a private snapshot. | |
| class InventoryRuntime(_PreviewRuntime): | |
| def _read_strict(self, name): | |
| return self.get(name) | |
| session = replace(ctx.session, runtime=InventoryRuntime(ctx.session.runtime)) | |
| ctx = _tool_context(session) | |
| if not session.runtime.available(): | |
| raise err(503, "store_unavailable", "The workspace store is unavailable. Try again.") | |
| documents = (brain._files(session) | |
| if _permitted(session, "intelligence") else {}) | |
| wanted = args.get("kind") | |
| listers = {"view": _views, "workbook": _workbooks, "app": _apps, | |
| "automation": _automations, "schedule": _schedules, "file": _brain, "skill": _brain} | |
| selected = list(dict.fromkeys(listers[kind] for kind in KINDS if wanted in (None, kind))) | |
| rows = {} | |
| for lister in selected: | |
| for kind, identity, database, native in lister(ctx): | |
| if wanted is not None and wanted != kind: | |
| continue | |
| target = {"kind": kind, "id": identity, "database": database} | |
| obj = _resolve(session, target, documents) | |
| if obj is None: | |
| continue | |
| metadata = (wiki_log.build_metadata(session, obj, documents) if documents else {}) | |
| about = metadata.get("about") if metadata.get("buildKind") == kind else None | |
| tags = sorted(about["tags"]) if about else [] | |
| if args.get("tag") is not None and args["tag"] not in tags: | |
| continue | |
| rows[(kind, identity, database)] = { | |
| **target, "title": obj["title"], "owner": obj["owner"], "href": obj["href"], | |
| "description": about["description"] if about else MISSING_DESCRIPTION, | |
| "descriptionMissing": about is None, "tags": tags, "updated": _updated(native), | |
| } | |
| ordered = sorted(rows.values(), key=lambda row: ( | |
| row["kind"], row["title"].casefold(), row["title"], row["id"], row["database"])) | |
| limit = args.get("limit", 100) | |
| query = _json_digest({"tool": "files.list", "kind": wanted, "tag": args.get("tag"), "limit": limit}) | |
| snapshot = _json_digest(ordered) | |
| offset = (_decode_query_cursor(session, args["cursor"], query, snapshot) | |
| if args.get("cursor") is not None else 0) | |
| if offset > len(ordered): | |
| raise err(409, "query_cursor_stale", "The visible files changed. Start again without a cursor.") | |
| page = ordered[offset:offset + limit] | |
| end = offset + len(page) | |
| cursor = _encode_query_cursor(session, query, snapshot, end) if end < len(ordered) else None | |
| return {"files": page, "total": len(ordered), "page": { | |
| "limit": limit, "offset": offset, "returned": len(page), | |
| "has_more": cursor is not None, "next_cursor": cursor, | |
| }} | |
| GROUP = { | |
| "name": "files", "title": "Files", | |
| "guide": "Use files.list to list current readable builds across Views, Sheet workbooks, Apps, " | |
| "Automations, Schedules, Brain files and Skills. Each row has kind, id, database, title, " | |
| "description, tags, owner, updated and href. Titles come from the current object. " | |
| "Builds without readable C5 metadata set descriptionMissing=true and use 'Description missing.' " | |
| "Updated is null when the native record has no update timestamp. " | |
| "Filter by kind or one canonical tag. Rows sort by kind, then title ignoring case, " | |
| "then exact title, id and database. Default limit is 100, maximum 500 per page. " | |
| "Follow page.next_cursor with the same filters and limit until it is null. " | |
| "There is no total inventory cap. A changed result set requires a restart without a cursor. " | |
| "This action reads metadata only and cannot grant access or change builds.", | |
| "actions": {"list": { | |
| "read": True, "destructive": False, "confirm": False, "handler": list_files, | |
| "schema": {"type": "object", "description": list_files.__doc__, "properties": { | |
| "kind": {"type": "string", "enum": list(KINDS)}, | |
| "tag": {"type": "string", "enum": list(external_tools.BUILD_TAGS)}, | |
| "limit": {"type": "integer", "minimum": 1, "maximum": 500}, | |
| "cursor": {"type": "string", "minLength": 1, "maxLength": 768}, | |
| }, "additionalProperties": False}, | |
| }}, | |
| } | |