arcade-mcp/toolkits/confluence/arcade_confluence/tools/page.py
Sam Partee b6b4cd0a4c
🏗️ Restructure: Multi-Package Architecture + uv Migration (#412)
### Overview
Major restructuring from monolithic `arcade-ai` package to modular
library architecture with standardized uv-based dependency management.

![arcade-ai Monorepo
(2)](https://github.com/user-attachments/assets/25f102b0-bb87-4a04-9701-d227d05664b1)

### New Package Structure
- **`arcade-tdk`** - Lightweight toolkit development kit (core
decorators, auth)
- **`arcade-core`** - Core execution engine and catalog functionality  
- **`arcade-serve`** - FastAPI/MCP server components
- **`arcade-ai`** - Meta package that includes CLI functionality.
Optionally include evals via the `evals` extra. Optionally include all
packages via the `all` extra.

### Key Benefits
- **Lighter Dependencies**: Toolkits now depend only on `arcade-tdk` (~2
deps) vs full `arcade-ai` (~30+ deps)
- **Faster Builds**: uv provides 10-100x faster dependency resolution
and installation
- **Better Modularity**: Clear separation of concerns, consumers import
only what they need
- **Standard Tooling**: Eliminates custom poetry scripts, uses standard
Python packaging

### Migration Impact
- All 20 toolkits converted from poetry → uv with `arcade-tdk`
dependencies plus `arcade-ai[evals]` and `arcade-serve` dev
dependencies. When developing locally, devs should install toolkits via
`make install-local`.
- Modern Python 3.10+ type hints throughout
- Standardized build system with hatchling backend
- Enhanced Makefile with robust toolkit management commands
- Removed `arcade dev` CLI command
- Reduce the number of files created by `arcade new` and add an option
to not generate a tests and evals folder.

This foundation enables faster development cycles and cleaner dependency
chains for the growing toolkit ecosystem.

### Todo After this PR is merged
- [ ] Post-merge workflow(s) (release & publish containers, etc)
- [ ] Release order plan. @EricGustin suggests releasing in the
following order:
    1. `arcade-core` version 0.1.0
    2. `arcade-serve` version 0.1.0 and `arcade-tdk` version 0.1.0
    3. `arcade-ai` version 2.0.0
4. Patch release for all toolkits (all changes in toolkits are internal
refactors)
- [ ] [Update docs](https://github.com/ArcadeAI/docs/pull/318)

---------

Co-authored-by: Eric Gustin <eric@arcade.dev>
Co-authored-by: Eric Gustin <34000337+EricGustin@users.noreply.github.com>
2025-06-11 16:48:17 -07:00

236 lines
8.4 KiB
Python

from typing import Annotated
from arcade_tdk import ToolContext, tool
from arcade_tdk.auth import Atlassian
from arcade_tdk.errors import ToolExecutionError
from arcade_confluence.client import ConfluenceClientV2
from arcade_confluence.enums import BodyFormat, PageSortOrder, PageUpdateMode
from arcade_confluence.utils import remove_none_values, validate_ids
@tool(
requires_auth=Atlassian(
scopes=["read:page:confluence", "write:page:confluence"],
)
)
async def create_page(
context: ToolContext,
space_identifier: Annotated[str, "The ID or title of the space to create the page in"],
title: Annotated[str, "The title of the page"],
content: Annotated[str, "The content of the page. Only plain text is supported"],
parent_id: Annotated[
str | None,
"The ID of the parent. If not provided, the page will be created at the root of the space.",
] = None,
is_private: Annotated[
bool,
"If true, then only the user who creates this page will be able to see it. "
"Defaults to False",
] = False,
is_draft: Annotated[
bool,
"If true, then the page will be created as a draft. Defaults to False",
] = False,
) -> Annotated[dict, "The page"]:
"""Create a new page at the root of the given space."""
client = ConfluenceClientV2(context.get_auth_token_or_empty())
space_id = await client.get_space_id(space_identifier)
parent_id = parent_id or (await client.get_space_homepage(space_id)).get("id")
params = remove_none_values({
"root-level": False,
"private": is_private,
})
body = remove_none_values({
"spaceId": space_id,
"status": "draft" if is_draft else None,
"parentId": parent_id,
"title": title,
"body": {
"storage": {
"value": content,
"representation": BodyFormat.STORAGE.to_api_value(),
}
},
})
page = await client.post("pages", params=params, json=body)
return client.transform_page_response(page)
@tool(
requires_auth=Atlassian(
scopes=["read:page:confluence", "write:page:confluence"],
)
)
async def update_page_content(
context: ToolContext,
page_identifier: Annotated[
str, "The ID or title of the page to update. Numerical titles are NOT supported."
],
content: Annotated[str, "The content of the page. Only plain text is supported"],
update_mode: Annotated[
PageUpdateMode,
"The mode of update. Defaults to 'append'.",
] = PageUpdateMode.APPEND,
) -> Annotated[dict, "The page"]:
"""Update a page's content."""
# Get the page to update
client = ConfluenceClientV2(context.get_auth_token_or_empty())
page_id = await client.get_page_id(page_identifier)
page = await get_page(context, page_id)
if not page.get("page"):
raise ToolExecutionError(message=f"No page found with identifier: '{page_identifier}'")
status = page.get("page", {}).get("status", "current")
title = page.get("page", {}).get("title", "Untitled page")
body = page.get("page", {}).get("body", {})
old_content = body.get(BodyFormat.STORAGE, {}).get("value", "")
old_version_number = page.get("page", {}).get("version", {}).get("number", 0)
# Update the page content
payload = client.prepare_update_page_content_payload(
content=content,
update_mode=update_mode,
old_content=old_content,
page_id=page_id,
status=status,
title=title,
body_representation=BodyFormat.STORAGE,
old_version_number=old_version_number,
)
updated_page = await client.put(f"pages/{page_id}", json=payload)
return client.transform_page_response(updated_page)
@tool(
requires_auth=Atlassian(
scopes=["read:page:confluence", "write:page:confluence"],
)
)
async def rename_page(
context: ToolContext,
page_identifier: Annotated[
str, "The ID or title of the page to rename. Numerical titles are NOT supported."
],
title: Annotated[str, "The title of the page"],
) -> Annotated[dict, "The page"]:
"""Rename a page by changing its title."""
# Get the page to rename
client = ConfluenceClientV2(context.get_auth_token_or_empty())
page_id = await client.get_page_id(page_identifier)
page = await get_page(context, page_id)
if not page.get("page"):
raise ToolExecutionError(message=f"No page found with identifier: '{page_identifier}'")
status = page.get("page", {}).get("status", "current")
content = page.get("page", {}).get("body", {}).get(BodyFormat.STORAGE, {}).get("value", "")
old_version_number = page.get("page", {}).get("version", {}).get("number", 0)
# Rename the page
payload = client.prepare_update_page_payload(
page_id=page_id,
status=status,
title=title,
body_representation=BodyFormat.STORAGE,
body_value=content,
version_number=old_version_number + 1,
version_message="Rename the page",
)
updated_page = await client.put(f"pages/{page_id}", json=payload)
return client.transform_page_response(updated_page)
@tool(
requires_auth=Atlassian(
scopes=["read:page:confluence"],
)
)
async def get_page(
context: ToolContext,
page_identifier: Annotated[
str, "Can be a page's ID or title. Numerical titles are NOT supported."
],
) -> Annotated[dict, "The page"]:
"""Retrieve a SINGLE page's content by its ID or title.
If a title is provided, then the first page with an exact matching title will be returned.
IMPORTANT: For retrieving MULTIPLE pages, use `get_pages_by_id` instead
for a massive performance and efficiency boost. If you call this function multiple times
instead of using `get_pages_by_id`, then the universe will explode.
"""
client = ConfluenceClientV2(context.get_auth_token_or_empty())
if page_identifier.isdigit():
return await client.get_page_by_id(page_identifier, BodyFormat.STORAGE)
else:
return await client.get_page_by_title(page_identifier, BodyFormat.STORAGE)
@tool(
requires_auth=Atlassian(
scopes=["read:page:confluence"],
)
)
async def get_pages_by_id(
context: ToolContext,
page_ids: Annotated[
list[str],
"The IDs of the pages to get. IDs are numeric. Titles of pages are NOT supported. "
"Maximum of 250 page ids supported.",
],
) -> Annotated[dict, "The pages"]:
"""Get the content of MULTIPLE pages by their ID in a single efficient request.
IMPORTANT: Always use this function when you need to retrieve content from more than one page,
rather than making multiple separate calls to get_page, because this function is significantly
more efficient than calling get_page multiple times.
"""
validate_ids(page_ids, max_length=250)
client = ConfluenceClientV2(context.get_auth_token_or_empty())
pages = await client.get(
"pages", params={"id": page_ids, "body-format": BodyFormat.STORAGE.to_api_value()}
)
return client.transform_get_multiple_pages_response(pages)
@tool(
requires_auth=Atlassian(
scopes=["read:page:confluence"],
)
)
async def list_pages(
context: ToolContext,
space_ids: Annotated[
list[str] | None,
"Restrict the response to only include pages in these spaces. "
"Only space IDs are supported. Titles of spaces are NOT supported. "
"If not provided, then no restriction is applied. "
"Maximum of 100 space ids supported.",
] = None,
sort_by: Annotated[
PageSortOrder,
"The order of the pages to sort by. Defaults to created-date-newest-to-oldest",
] = PageSortOrder.CREATED_DATE_DESCENDING,
limit: Annotated[int, "The maximum number of pages to return. Defaults to 25. Max is 250"] = 25,
pagination_token: Annotated[
str | None,
"The pagination token to use for the next page of results",
] = None,
) -> Annotated[dict, "The pages"]:
"""Get the content of multiple pages by their ID"""
validate_ids(space_ids, max_length=100)
limit = max(1, min(limit, 250))
client = ConfluenceClientV2(context.get_auth_token_or_empty())
params = remove_none_values({
"space-id": space_ids,
"sort": sort_by.to_api_value(),
"body-format": BodyFormat.STORAGE.to_api_value(),
"limit": limit,
"cursor": pagination_token,
})
pages = await client.get("pages", params=params)
return client.transform_list_pages_response(pages)