Platform Workspace Context

Provide workspace-level context and agent configuration from the platform database

Workspace context lets platform-hosted agents load settings and instructions scoped to a workspace.

python
from praisonaiagents import Agent
from praisonaiagents.auth import WorkspaceContextProtocol

class PlatformWorkspaceContext:
    async def get_workspace_context(self, workspace_id: str) -> str | None:
        # Load workspace instructions from your database
        return "You support the Acme Corp workspace."

    async def get_agent_config(self, workspace_id: str, agent_id: str) -> dict | None:
        return {
            "system_prompt": "Be concise and professional.",
            "model": "gpt-4o-mini",
            "tools": ["web_search"],
            "max_concurrent_tasks": 3,
        }

agent = Agent(
    name="workspace-assistant",
    instructions="Help users in their workspace.",
)

The user opens a workspace thread; the agent loads scoped instructions from platform context.

Diagram

🤖 Agent

📋 WorkspaceContext

🏢 Workspace Settings

⚙️ Agent Config

📝 System Prompt

Quick Start

  1. Simple Usage

    Implement WorkspaceContextProtocol with workspace-level instructions:

    python
    from praisonaiagents.auth import WorkspaceContextProtocol
    
    class PlatformWorkspaceContext:
        def __init__(self, db):
            self.db = db
    
        async def get_workspace_context(self, workspace_id: str) -> str | None:
            workspace = await self.db.get_workspace(workspace_id)
            if not workspace:
                return None
            return workspace.instructions
    
        async def get_agent_config(self, workspace_id: str, agent_id: str) -> dict | None:
            agent = await self.db.get_agent(workspace_id, agent_id)
            if not agent:
                return None
            return {
                "system_prompt": agent.system_prompt,
                "model": agent.model,
                "tools": agent.tools,
                "max_concurrent_tasks": agent.max_concurrent_tasks,
            }
  2. With Configuration

    Merge workspace and agent settings before starting the agent:

    python
    from praisonaiagents import Agent
    
    async def start_platform_agent(ctx: PlatformWorkspaceContext, workspace_id: str, agent_id: str):
        workspace_text = await ctx.get_workspace_context(workspace_id)
        config = await ctx.get_agent_config(workspace_id, agent_id) or {}
    
        instructions = config.get("system_prompt", "You are a helpful assistant.")
        if workspace_text:
            instructions = f"{workspace_text}\n\n{instructions}"
    
        agent = Agent(
            name=config.get("name", "assistant"),
            instructions=instructions,
            llm=config.get("model"),
        )
        return agent

How It Works

Diagram
AgentDBWorkspaceContextPlatformAgentDBWorkspaceContextPlatformget_workspace_context(ws_id)fetch workspaceinstructionsget_agent_config(ws_id, agent_id)fetch agent (scoped)config dictmerged prompt + settings
MethodReturns
get_workspace_context(workspace_id)Workspace instructions string, or None
get_agent_config(workspace_id, agent_id)Config dict with system_prompt, model, tools, max_concurrent_tasks

Configuration Options

Protocol fieldTypeDescription
get_workspace_context`async (workspace_id) → str \None`Workspace-level instructions
get_agent_config`async (workspace_id, agent_id) → dict \None`Per-agent runtime settings

Expected keys in the agent config dict: system_prompt, model, tools, max_concurrent_tasks.


Best Practices

Always scope agent queries

Filter by both agent_id and workspace_id — never fetch agents globally.

Return None for missing resources

Missing workspace or agent records should return None, not raise.

Inject database sessions

Pass AsyncSession via constructor — do not create sessions inside the context provider.

Keep return values JSON-serialisable

Convert UUIDs and datetimes to strings before returning config dicts.


Related