Skip to content

Openclaw plugin native#36

Open
dalongbao wants to merge 9 commits intomainfrom
openclaw-plugin-native
Open

Openclaw plugin native#36
dalongbao wants to merge 9 commits intomainfrom
openclaw-plugin-native

Conversation

@dalongbao
Copy link
Copy Markdown
Collaborator

addresses #35

SecretSettler and others added 8 commits March 25, 2026 01:19
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)
@SecretSettler SecretSettler self-requested a review April 5, 2026 17:29
Copy link
Copy Markdown
Member

@SecretSettler SecretSettler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix the bugs and pass the tests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants