Open
Conversation
Complete rewrite of the OpenClaw plugin to run entirely in-process without requiring the Python proxy server. Users just install and go: openclaw plugins install @contextpilot/openclaw-plugin Engine modules ported from Python to TypeScript: - engine/extract.ts: Document extraction from system/tool_results (XML tags, numbered lists, JSON results, markdown headers, separators) for both OpenAI Chat and Anthropic Messages API formats (1042→969 lines) - engine/dedup.ts: Cross-turn block-level deduplication using content-defined chunking + SHA-256 hashing (250→355 lines) - engine/cache-control.ts: Anthropic cache_control injection for system messages and tool_result content blocks (144 lines) - engine/reorder.ts: Simplified LCP-based document reordering that maximizes prefix cache sharing across turns — no numpy/scipy needed Plugin integration: - Uses OpenClaw's wrapStreamFn to intercept requests before they reach the LLM backend, apply all optimizations, then forward - Registers contextpilot provider with dynamic model resolution - contextpilot_status tool reports engine state and savings
Tests cover all four engine modules: - extract (18 tests): XML/numbered/JSON extraction, OpenAI/Anthropic format handlers, system+tool_result extraction, reconstruction - dedup (7 tests): content-defined chunking, block hashing, cross-message deduplication for chat+responses API formats - cache-control (6 tests): Anthropic cache_control injection for system/tool_results, immutability, OpenAI no-op, dispatcher - reorder (7 tests): deterministic hash sort, cross-turn prefix stability, reset behavior, index mapping correctness All 38 tests pass in 18ms.
…sts) Tests simulate the exact wrapStreamFn pipeline end-to-end: - Anthropic: system XML extraction+reorder+cache_control, tool_result cache injection, scope=system/tool_results filtering - OpenAI: system reorder, duplicate tool result dedup with reference hints, responses API dedup, passthrough for plain messages - Multi-turn: ReorderState preserves doc order across turns, reset clears history - Edge cases: empty body, no messages, single doc (no reorder), short content (no dedup), null messages, system as block array Total test suite: 54 tests (38 unit + 16 integration), all pass in 165ms.
Full port of all Python ContextPilot modules to zero-dependency TypeScript: Core index (replaces numpy+scipy): - tree-nodes.ts: ClusterNode + NodeManager (334 lines) - compute-distance.ts: O(n) merge-based distance computation (224 lines) - index-construction.ts: hierarchical clustering with pure-TS linkage() (348 lines) - intra-ordering.ts: within-context reordering via tree prefix (349 lines) - inter-scheduler.ts: cross-context scheduling via path grouping (116 lines) Live engine: - live-index.ts: ContextPilot class — search/insert/evict/reorder (1232 lines) - metadata.ts: NodeMetadata for per-node runtime tracking (82 lines) - eviction-heap.ts: LRU min-heap for SGLang cache eviction sync (317 lines) - conversation-tracker.ts: multi-turn document deduplication (241 lines) - http-client.ts: native fetch() client for index server comms (267 lines) Previously ported: - extract.ts: document extraction from system/tool_results (969 lines) - dedup.ts: cross-turn block-level content dedup (355 lines) - cache-control.ts: Anthropic cache_control injection (144 lines) - reorder.ts: simplified LCP reorder fallback (109 lines)
index.ts now supports three backend modes: - anthropic: in-process ContextPilot engine (clustering + reorder + dedup + cache_control) - openai: in-process engine (same pipeline, OpenAI cache is automatic) - sglang: remote ContextPilotIndexClient → index server for cache-aware reorder, in-process dedup, no cache_control injection (RadixAttention handles caching) Config additions: - backendProvider now accepts 'sglang' - indexServerUrl: URL for ContextPilot index server (default: http://localhost:8765) contextpilot_status tool shows engine stats (cloud) or server health (sglang)
…ticAI/ContextPilot into openclaw-plugin-native
SecretSettler
requested changes
Apr 5, 2026
Member
SecretSettler
left a comment
There was a problem hiding this comment.
Please fix the bugs and pass the tests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
addresses #35