Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:AMAP ML SkillClaw SkillClawAPIServer handle request

From Leeroopedia
Knowledge Sources
Domains Proxy_Server, Skill_Injection, Session_Recording
Last Updated 2026-10-22 00:00 GMT

Overview

Concrete tool for intercepting LLM API requests, injecting skill catalogs, forwarding upstream, and recording conversation sessions, provided by the SkillClaw client package.

Description

The SkillClawAPIServer class is the core FastAPI proxy server. Its `_handle_request` method is the central request processing pipeline: it normalizes protocol-specific bodies, compresses system prompts (OpenClaw only), injects an OpenClaw-compatible skill catalog via `_inject_skills`, truncates to fit context limits, forwards the modified request to the upstream LLM, normalizes the response, records per-turn session data via `_buffer_record`, and triggers PRM scoring. The `_inject_skills` method delegates to SkillManager to build the catalog and appends it to the system message.

Session recording is handled internally by `_buffer_record` and `_flush_pending_record`, which accumulate per-turn data (prompt text, response text, tool calls, injected skills) and write to `conversations.jsonl` and `prm_scores.jsonl` files for later evolution.

Usage

Import and use when you need to:

  • Understand how the SkillClaw proxy intercepts and processes LLM requests
  • Build a custom request handler that wraps the proxy pipeline
  • Trace session recording and PRM scoring logic
  • Understand skill injection into system messages

Code Reference

Source Location

  • Repository: SkillClaw
  • File: `skillclaw/api_server.py:L1434-1514` (constructor), `L2323-2555` (_handle_request), `L3182-3225` (_inject_skills)

Signature

class SkillClawAPIServer:
    """FastAPI proxy server for LLM request interception and skill injection."""

    def __init__(
        self,
        config: SkillClawConfig,
        sampling_client=None,
        skill_manager: Optional[SkillManager] = None,
        prm_scorer: Optional[PRMScorer] = None,
        last_request_tracker=None,
    ) -> None:

    def start(self) -> None:
        """Start the uvicorn server in a background thread."""

    def stop(self) -> None:
        """Stop the server and clean up."""

    async def _handle_request(
        self,
        body: dict[str, Any],
        session_id: str,
        turn_type: str,
        session_done: bool,
        forwarder=None,
    ) -> dict[str, Any]:
        """Core request pipeline: inject skills, forward, normalize, record."""

    def _inject_skills(
        self, messages: list[dict]
    ) -> tuple[list[dict], list[str]]:
        """Inject OpenClaw-compatible skill catalog into the system message."""

    def _buffer_record(
        self,
        session_id: str,
        turn_num: int,
        messages: list,
        prompt_text: str,
        response_text: str,
        tool_calls: list,
    ) -> None:
        """Buffer per-turn session data for recording."""

    def _flush_pending_record(
        self, session_id: str, last_msg: dict
    ) -> None:
        """Flush pending record to conversations.jsonl."""

Import

from skillclaw.api_server import SkillClawAPIServer

I/O Contract

Inputs

Name Type Required Description
config SkillClawConfig Yes Full runtime config (proxy, LLM, skills, PRM settings)
skill_manager None No Skill injection manager (None = no injection)
prm_scorer None No PRM quality scorer (None = no scoring)
body dict Yes (_handle_request) Full request body with messages, tools, model
session_id str Yes Session identifier from X-Session-Id header
turn_type str Yes "main" or "side" channel
session_done bool Yes Whether this is the last turn in the session

Outputs

Name Type Description
_handle_request returns dict[str, Any] {"response": <upstream LLM response>}
_inject_skills returns tuple[list[dict], list[str]] (modified messages, injected skill names)
Side effects Files conversations.jsonl, prm_scores.jsonl in record directory
Side effects In-memory Session turn list for upload to evolve server

Usage Examples

Server Creation and Start

from skillclaw.api_server import SkillClawAPIServer
from skillclaw.skill_manager import SkillManager
from skillclaw.config_store import ConfigStore

# Load config and skill manager
cs = ConfigStore()
cfg = cs.to_skillclaw_config()
skill_manager = SkillManager(skills_dir=cfg.skills_dir, retrieval_mode="template")

# Create and start the proxy server
server = SkillClawAPIServer(
    config=cfg,
    skill_manager=skill_manager,
    prm_scorer=None,  # No PRM scoring
)
server.start()
# Proxy is running on cfg.proxy_host:cfg.proxy_port

Understanding the Request Pipeline

# The _handle_request pipeline (simplified):
# 1. Normalize protocol (Anthropic/Responses -> OpenAI chat format)
# 2. Compress system prompt (OpenClaw only)
# 3. Inject skills into system message (_inject_skills)
# 4. Truncate to fit max_context_tokens
# 5. Forward to upstream LLM (_forward_to_llm)
# 6. Normalize response (extract content, tool calls)
# 7. Record session turn (_buffer_record)
# 8. Trigger PRM scoring (if configured)
# 9. Return {"response": <LLM response>}

Related Pages

Implements Principle

Requires Environment

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment