Skip to content

feat: split AI context into on-demand tools, route AI through AD4M - #196

Open
HexaField wants to merge 6 commits into
devfrom
feat/ai-context-tools
Open

feat: split AI context into on-demand tools, route AI through AD4M#196
HexaField wants to merge 6 commits into
devfrom
feat/ai-context-tools

Conversation

@HexaField

@HexaField HexaField commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

What

Split the monolithic 297K-char AI context into a compact core prompt (~7K tokens) plus 8 on-demand context tools. Route all AI requests through AD4M's /v1/chat/completions endpoint instead of calling Anthropic or Ollama directly from the browser.

Why

The monolithic context consumed ~75K tokens on every request, regardless of task complexity. Direct browser-to-provider dispatch duplicated transport logic and bypassed AD4M's model management. James's PR #996 adds native provider support (Anthropic, Ollama, OpenAI) to AD4M — WE should use that surface instead of maintaining its own provider implementations.

How

Context split — build-time assembly into three artifacts:

assembler.ts
  assembleCoreContext()     → coreContext.ts       ~7K tokens (rules, routing, models, tokens, tool directory)
  assembleContextSections() → contextSections.ts   ~75K tokens across 8 sections
  assembleContextToolDefs() → contextToolDefs.ts   tool definitions for on-demand loading
Section tool Content Size
we_stores_reference Store members + access patterns 92K
we_schema_operators Node types + operator catalogue 49K
we_component_registry Primitives + components + props 34K
we_common_patterns Layout and form recipes 27K
we_design_props Design system property tables 25K
we_plugin_registries Plugin catalogues 16K
we_store_patterns Store creation patterns 13K
we_panels Panel types + configuration 12K

AD4M routing — single transport path:

WE browser  ──POST /v1/chat/completions──►  AD4M executor
(EditorStore)   Bearer token from SessionStore   (resolves provider)
            ◄──SSE stream (OpenAI format)──
interface Ad4mConnection {
  baseUrl: string;  // from sessionStore.serverUrl()
  token: string;    // from sessionStore.token()
}

Removed: sendAnthropicRequest, sendOllamaRequest, parseAnthropicSSE, parseOllamaStream, AiProtocol, ProviderConfig, buildToolsForProvider.

Added: Ad4mConnection, parseOpenAISSE, buildTools (OpenAI format only), toOpenAIMessage (Anthropic content-block → OpenAI translation).

Tool resolution loop — unchanged, runs in EditorStore. Context tools resolve from the local section map with zero latency.

Builds directly on #996.

Tests: 19 pass — 7 parseOpenAISSE, 3 buildTools, 3 formatExternalManifestForPrompt, 3 loadContextSections, 3 live AD4M integration (skip when executor unavailable).

@HexaField
HexaField requested a review from jhweir as a code owner September 11, 2026 05:10
@netlify

netlify Bot commented Sep 11, 2026

Copy link
Copy Markdown

Deploy Preview for coasys-we ready!

Name Link
🔨 Latest commit 0e30b50
🔍 Latest deploy log https://app.netlify.com/projects/coasys-we/deploys/6aa3efc30e5a7200080e24cf
😎 Deploy Preview https://deploy-preview-196--coasys-we.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@HexaField HexaField changed the title feat: split AI context into on-demand tools, add Ollama provider feat: split AI context into on-demand tools, route AI through AD4M Sep 11, 2026
HexaField and others added 6 commits September 11, 2026 22:03
Split the 297K monolithic schema context into a compact core prompt
(~7K tokens) plus 8 on-demand context tools the model calls to load
schema sections. Context tools resolve locally in the browser from a
pre-compiled section map — zero latency, no round-trip.

Build-time changes (ai-context):
- assembleCoreContext(): rules + routing + entities + tokens + directory
- assembleContextSections(): Record<string, string> per tool name
- assembleContextToolDefs(): provider-neutral tool definitions
- generate.ts writes coreContext.ts, contextSections.ts, contextToolDefs.ts
- schemaContext.ts kept unchanged for IDE agents

Runtime changes (app-shell):
- aiInfra.ts: add Ollama ndjson streaming alongside Anthropic SSE
- aiInfra.ts: sendPromptRequest() dispatches by protocol
- aiInfra.ts: buildToolsForProvider() adapts tool defs per wire format
- EditorStore: handle context tools alongside update_schema in the loop
- EditorStore: provider auto-detection (Anthropic key -> anthropic, else ollama)
- templateSurface.ts: classify aiProtocol + providerReady as WIRING

Token cost: simple edits drop from 74K to ~7K cached tokens (−90%).
Complex edits load only the sections they need.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…ons, live Ollama

24 tests covering the AI infrastructure layer:

Deterministic (mocked streams):
- parseOllamaStream: text extraction, tool calls, split chunks,
  malformed JSON recovery, max_tokens mapping (7 tests)
- parseAnthropicSSE: text extraction, tool_use blocks, [DONE]
  sentinel, non-data lines, malformed recovery (5 tests)
- buildToolsForProvider: Anthropic vs Ollama wire format (3 tests)
- formatExternalManifestForPrompt: properties + relations (3 tests)
- loadContextSections: key names, content, coverage (3 tests)

Live integration (localhost:11434, skip when unavailable):
- Text streaming against real Ollama (1 test)
- Context tool triggering — non-deterministic, graceful skip (1 test)
- Full tool loop: prompt → tool call → resolve → response (1 test)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The CI 'generated files are committed' check regenerates context
files and compares. Lint-staged was reformatting the generator
output through prettier, causing a mismatch. Add coreContext.ts,
contextSections.ts, contextToolDefs.ts, and context.json to
.prettierignore alongside the existing schemaContext.ts entry.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Replace direct Anthropic/Ollama dispatch with a single AD4M endpoint.
WE sends all AI requests to the executor's OpenAI-compatible surface;
the executor handles provider resolution (Anthropic, Ollama, OpenAI).

- Remove AiProtocol, ProviderConfig, sendAnthropicRequest,
  sendOllamaRequest, parseAnthropicSSE, parseOllamaStream,
  buildToolsForProvider, toOllamaWireMessage
- Add Ad4mConnection (baseUrl + token from SessionStore)
- Add parseOpenAISSE for OpenAI delta-chunked SSE streaming
- Add buildTools (OpenAI function-calling format only)
- Add toOpenAIMessage to translate Anthropic content blocks
- EditorStore gets connection from useSessionStore instead of
  hardcoded Ollama/Anthropic config
- Tests rewritten: 7 parseOpenAISSE + 3 buildTools + 3 manifest +
  3 context sections + 3 live AD4M integration = 19 tests, all pass

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Dev added options to DisplayField (PR #195). Regenerate the three
context files whose stores reference now includes the field.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@HexaField
HexaField force-pushed the feat/ai-context-tools branch from 49d5705 to 0e30b50 Compare September 11, 2026 12:10
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.

1 participant