Skip to main content
The ConsoleApiClient is the primary TypeScript interface used by the Palyra Web Console to communicate with the palyrad daemon’s Admin and Console HTTP APIs. It encapsulates session management, security protocols (CSRF and Bound-Principals), and specialized data streaming for real-time chat and logs.

ConsoleApiClient Implementation

The ConsoleApiClient class is defined in apps/web/src/consoleApi.ts. It handles the lifecycle of HTTP requests, including the injection of security tokens and the parsing of NDJSON (Newline Delimited JSON) streams.

Key Functions and Methods

  • getSession(): Fetches the current session state from /console/v1/auth/session apps/web/src/consoleApi.test.ts#37-41.
  • login(payload): Performs a login operation and initializes the internal CSRF token state apps/web/src/consoleApi.test.ts#67-72.
  • fetch(path, init): A private wrapper around the global fetch API that automatically attaches the x-palyra-csrf-token header for mutating requests (POST, PUT, DELETE) apps/web/src/consoleApi.test.ts#82-90.
  • Streaming: Supports NDJSON streaming for chat runs and log tailing, allowing the UI to react to partial updates as they arrive from the daemon.

Data Flow: Request Lifecycle

The following diagram illustrates how a request from the React UI flows through the ConsoleApiClient to the palyrad daemon. Title: Web Console Request Pipeline Sources: apps/web/src/consoleApi.ts#1-100, apps/web/src/consoleApi.test.ts#44-90

Authentication and Session Bootstrap

Palyra uses a multi-stage bootstrap process to establish a secure session between the browser and the daemon.

Session Bootstrap Logic

The application attempts to recover a session in the following order:
  1. Browser Handoff: If a desktop_handoff_token is present in the URL (typically passed from the Desktop Companion), the client calls /console/v1/auth/browser-handoff/session apps/web/src/App.test.tsx#69-83.
  2. Existing Session: If no handoff token exists, it attempts to refresh the current session via /console/v1/auth/session apps/web/src/App.test.tsx#20-23.
  3. Auth Screen: If both fail, the user is redirected to the ConsoleAuthScreen apps/web/src/App.tsx#20-30.

CSRF Token Lifecycle

CSRF protection is enforced for all state-changing operations. Sources: apps/web/src/App.tsx#11-33, apps/web/src/App.test.tsx#38-67, apps/web/src/consoleApi.test.ts#44-64

Security Constraints

Bound-Principal Security Lock

The daemon can be configured with a “Bound Principal” lock (via PALYRA_ADMIN_BOUND_PRINCIPAL). This ensures that the Admin/Console API only accepts requests from a specific identity, such as admin:web-console crates/palyra-daemon/tests/admin_surface.rs#22-23.

Security Headers

The HTTP surface enforces strict security headers to prevent cross-site attacks: Sources: crates/palyra-daemon/tests/admin_surface.rs#30-71, crates/palyra-daemon/tests/openai_auth_surface.rs#35-38

Model Provider OAuth and API Key Flows

The Web Console facilitates the connection of Model Providers (OpenAI, Anthropic) through specialized flows that integrate with the palyra-vault.

API Key Connection

Users can provide API keys directly through the AuthSection.
  1. The client calls connectOpenAiApiKey or connectAnthropicApiKey apps/web/src/console/hooks/useAuthDomain.ts#130-166.
  2. The daemon validates the key against the provider’s /v1/models endpoint crates/palyra-daemon/tests/openai_auth_surface.rs#138-143.
  3. If valid, the key is stored in the Vault, and a vault_ref is returned to be stored in the configuration crates/palyra-daemon/tests/openai_auth_surface.rs#92-104.

OpenAI OAuth Flow

For OAuth-based authentication:
  1. Bootstrap: Client calls startOpenAiProviderBootstrap to get an authorization URL apps/web/src/console/hooks/useAuthDomain.ts#188-196.
  2. Popup: A browser popup handles the external provider interaction apps/web/src/console/hooks/useAuthDomain.ts#71.
  3. Callback: The daemon receives the callback at /console/v1/auth/providers/openai/callback crates/palyra-daemon/src/openai_surface.rs#13.
  4. Polling: The Web Console polls checkOpenAiCallbackState until the exchange is complete apps/web/src/console/hooks/useAuthDomain.ts#86.
Title: Model Provider Auth Integration Sources: apps/web/src/console/sections/AuthSection.tsx#47-52, apps/web/src/console/hooks/useAuthDomain.ts#147-160, crates/palyra-daemon/src/openai_surface.rs#16-76, crates/palyra-daemon/tests/openai_auth_surface.rs#29-81

Error Handling

The ConsoleApiClient uses a structured error class, ControlPlaneApiError, to categorize failures: Sources: apps/web/src/consoleApi.test.ts#5, apps/web/src/App.test.tsx#20-23, apps/web/src/App.test.tsx#38-50