llm-ui-context-provider
v0.1.0
Published
Bridge React UI state and visible DOM data to LLMs via a lightweight context provider
Maintainers
Readme
llm-ui-context-provider
Lightweight React library that captures your application's UI state and visible DOM content into a structured snapshot — ready to inject into any LLM system prompt.
Install
npm install llm-ui-context-providerQuick Start
1. Wrap your app
import { AIContextProvider } from "llm-ui-context-provider";
function App() {
return (
<AIContextProvider
maskRules={[{ preset: "email" }, { preset: "creditCard" }]}
>
<YourApp />
</AIContextProvider>
);
}2. Track React state
import { useState } from "react";
import { useTrackAIState } from "llm-ui-context-provider";
function CartPage() {
const [items, setItems] = useState([]);
useTrackAIState("cart.items", items);
return <div>...</div>;
}3. Tag DOM elements
<div data-ai-context="cart-summary">3 items — $42.00</div>4. Get the prompt
import { usePromptContext } from "llm-ui-context-provider";
function ChatBot() {
const getPromptContext = usePromptContext();
const ask = async () => {
const context = getPromptContext();
// Send `context` as part of your LLM system prompt
};
}Privacy Masking
Built-in presets: email, phone, creditCard, ssn, number.
<AIContextProvider
maskRules={[
{ preset: "email" },
{ preset: "creditCard" },
{ pattern: /secret-\w+/g, replacement: "[SECRET]" },
]}
>API
| Export | Type | Description |
|---|---|---|
| AIContextProvider | Component | Wrap your app to enable context capture |
| useTrackAIState(key, value) | Hook | Register local state into the snapshot |
| usePromptContext() | Hook | Returns getPromptContext() function |
| useAIContext() | Hook | Full access to the context value |
| scrapeDOM() | Utility | Manual DOM scraping |
| applyMaskRules() | Utility | Apply masking to a string |
Provider Props
| Prop | Default | Description |
|---|---|---|
| maskRules | [] | Array of masking rules |
| dataAttribute | "data-ai-context" | DOM attribute to scan |
| scrapeIntervalMs | 1000 | Auto-scrape interval (0 to disable) |
| debug | false | Log snapshots to console |
License
MIT
