AI Search + AI Gateway + Workers AI Integration
Date: 2026-06-09 14:20 UTC
Status: ✅ INTEGRATED INTO INTELLIGENCE WORKER
Architecture: v3.6 Consolidated (no new workers)
Architecture Decision
REJECTED: Standalone integratewise-ai-orchestrator worker
ADOPTED: Integrate AI Search RAG into existing intelligence worker
Rationale:
- v3.6 architecture already consolidates AI functionality into
intelligence - Existing AI router (Workers AI + OpenRouter + AI Gateway) is working
- AI Search namespace already created:
integratewise-intelligence - Adding RAG is a feature enhancement, not a new service
What's Wired (2026-06-09)
✅ Intelligence Worker Bindings
File: services/intelligence/wrangler.toml
# AI Search for RAG
[[ai_search_namespaces]]
binding = "AI_SEARCH"
namespace = "integratewise-intelligence"
# Workers AI for fast inference
[ai]
binding = "AI"
# OpenRouter API key (free tier)
[[secrets_store_secrets]]
binding = "OPENROUTER_API_KEY"
store_id = "1fd83fc5881e4cd296ae3c2fbe80693c"
secret_name = "OPENROUTER_API_KEY"
# Env vars
[vars]
AI_GATEWAY_ID = "integratewise-ai-dev"
✅ AI Router with RAG Support
File: services/intelligence/src/lib/ai-router.ts
Features:
- 3-tier fallback: Workers AI → OpenRouter free → OpenRouter fusion
- Optional RAG context from AI Search
- All requests through AI Gateway (caching + logging)
- Source attribution for RAG responses
New Interface:
export interface AIRequest {
prompt: string;
systemPrompt?: string;
maxTokens?: number;
temperature?: number;
tier?: "fast" | "balanced" | "critical";
useRAG?: boolean; // NEW: Enable AI Search RAG
}
export interface AIResponse {
content: string;
model: string;
cached: boolean;
provider: "workers-ai" | "openrouter";
cost: number;
sources?: Array<{ title: string; url?: string; score?: number }>; // NEW: RAG sources
}
RAG Flow:
- If
useRAG: true, search AI Search namespace with user query - Retrieve top 5 results
- Augment prompt with retrieved context
- Route through AI tier (fast/balanced/critical)
- Return answer + source attribution
✅ Test Endpoint
File: services/intelligence/src/index.ts
New Route: POST /v1/test/rag
Request:
{
"question": "What integrations does integratewise.ai support?",
"tier": "fast"
}
Response:
{
"success": true,
"data": {
"content": "IntegrateWise supports...",
"model": "@cf/meta/llama-3.1-8b-instruct",
"cached": false,
"provider": "workers-ai",
"cost": 0,
"sources": [
{
"title": "Supported Integrations",
"url": "https://integratewise.ai/integrations",
"score": 0.92
}
]
}
}
Deployment
Step 1: Deploy Intelligence Worker
cd /Users/nirmal/Github/integratewise-live
unset CLOUDFLARE_API_TOKEN
wrangler deploy --config services/intelligence/wrangler.toml --env=""
Expected Output:
- ✅ Worker deployed
- ✅ AI_SEARCH binding verified
- ✅ AI binding verified
- ✅ OPENROUTER_API_KEY binding verified
Step 2: Test RAG Search
# Test RAG-enabled query (fast tier)
curl -X POST https://intelligence.dev.integratewise.ai/v1/test/rag \
-H "Content-Type: application/json" \
-d '{
"question": "What is IntegrateWise and what does it do?",
"tier": "fast"
}'
# Test with balanced tier (OpenRouter free)
curl -X POST https://intelligence.dev.integratewise.ai/v1/test/rag \
-H "Content-Type: application/json" \
-d '{
"question": "How does IntegrateWise handle entity resolution?",
"tier": "balanced"
}'
# Test with critical tier (OpenRouter fusion)
curl -X POST https://intelligence.dev.integratewise.ai/v1/test/rag \
-H "Content-Type: application/json" \
-d '{
"question": "Compare IntegrateWise to other integration platforms",
"tier": "critical"
}'
Expected Behavior:
- Query hits AI Search namespace
- Retrieves relevant docs from integratewise.ai
- Augments prompt with context
- Returns answer + sources
Step 3: Verify AI Gateway Logs
- Open Cloudflare Dashboard
- Navigate to AI Gateway →
integratewise-ai-dev - Check logs for:
- Cache hit rate
- Request counts by provider (workers-ai, openrouter)
- Token usage
- Error rates
AI Search Content Status
Current Status
Namespace: integratewise-intelligence
Status: ✅ Created
Content: ⚠️ EMPTY (needs bulk upload)
Next: Bulk Upload Content
Option A: Crawl Website (Recommended)
# Enable Cloudflare AI Search crawler
# 1. Go to Cloudflare Dashboard → AI → AI Crawl Control
# 2. Allow "Cloudflare-AI-Search" bot for integratewise.ai domain
# 3. Trigger initial crawl:
curl -X POST \
"https://api.cloudflare.com/client/v4/accounts/a1bbbb12a32cdbb68dd170b09fe8b5f3/ai-search/rags/integratewise-intelligence/refresh" \
-H "Authorization: Bearer ${CLOUDFLARE_API_TOKEN}" \
-H "Content-Type: application/json"
Option B: Bulk Upload from org_memory (Manual)
# Create script to upload org_memory content
# File: services/intelligence/scripts/upload-to-ai-search.ts
const documents = await fetchOrgMemoryDocs(); // From Supabase
for (const doc of documents) {
await env.AI_SEARCH.upsert({
id: doc.id,
text: doc.content_md,
metadata: {
title: doc.title,
url: doc.url,
tags: doc.tags,
topics: doc.topics,
governance_state: doc.governance_state,
},
});
}
Option C: Sync from R2 Bucket
# If org_memory content is already in R2, sync to AI Search
# (Requires AI Search API integration)
What's Missing (Optional)
Hugging Face API Key
Status: Not configured
Priority: 🟢 LOW (optional, OpenRouter covers this)
To Add:
wrangler secrets-store secret create 1fd83fc5881e4cd296ae3c2fbe80693c \
--name=HUGGINGFACE_API_KEY \
--scopes=workers
# Add binding to intelligence/wrangler.toml
[[secrets_store_secrets]]
binding = "HF_API_KEY"
store_id = "1fd83fc5881e4cd296ae3c2fbe80693c"
secret_name = "HUGGINGFACE_API_KEY"
Grok API Key
Status: Not configured
Priority: 🟢 LOW (optional, covered by OpenRouter + Workers AI)
To Add:
wrangler secrets-store secret create 1fd83fc5881e4cd296ae3c2fbe80693c \
--name=GROK_API_KEY \
--scopes=workers
# Add binding to intelligence/wrangler.toml
[[secrets_store_secrets]]
binding = "GROK_API_KEY"
store_id = "1fd83fc5881e4cd296ae3c2fbe80693c"
secret_name = "GROK_API_KEY"
Note: These are optional fallback providers. Current setup (Workers AI + OpenRouter free) is sufficient for Phase 2.
Usage in Production
Morning Brief with RAG
async function generateMorningBrief(env: Env) {
const response = await routeAI(
{
prompt: "Summarize overnight system changes and pending actions",
systemPrompt: "You are a system monitor. Use knowledge base context.",
useRAG: true,
tier: "fast",
maxTokens: 500,
},
env
);
return {
content: response.content,
sources: response.sources, // Attribution
provider: response.provider,
};
}
Signal Analysis with RAG
async function analyzeSignal(env: Env, signalData: unknown) {
const response = await routeAI(
{
prompt: `Analyze this signal: ${JSON.stringify(signalData)}`,
systemPrompt: "You are a signal analyzer. Reference knowledge base for context.",
useRAG: true,
tier: "balanced",
maxTokens: 500,
},
env
);
return {
analysis: response.content,
sources: response.sources,
};
}
Critical Decision with Fusion
async function criticalDecision(env: Env, context: unknown) {
const response = await routeAI(
{
prompt: `Critical decision required: ${JSON.stringify(context)}`,
systemPrompt: "Multi-model deliberation. Use knowledge base + reasoning.",
useRAG: true,
tier: "critical", // OpenRouter fusion
maxTokens: 1000,
},
env
);
return {
recommendation: response.content,
sources: response.sources,
model: response.model, // Will show "openrouter/fusion"
};
}
Cost Analysis
| Tier | Provider | Model | Cost | Use Case |
|---|---|---|---|---|
| Fast | Workers AI | llama-3.1-8b | FREE | Default, morning briefs, quick queries |
| Balanced | Workers AI | moonshotai/kimi-k3 | Paid (per-token) | Signal analysis, medium reasoning, 1M context |
| Critical | Workers AI | moonshotai/kimi-k3 (reasoning_effort: 'max') | Paid (per-token) | Critical decisions, deep deliberation |
| Fallback | OpenRouter | openrouter/auto | FREE (tier) | Fallback when Workers AI unavailable |
Kimi K3 (
moonshotai/kimi-k3): 2.8T parameter frontier model on Workers AI. 1M token context window, always-on reasoning withreasoning_effortparameter. Edge-native — no external API call needed. See Cloudflare docs.
AI Gateway Benefits:
- Request caching (reduces API calls)
- Rate limiting (prevents abuse)
- Unified logs (all providers in one place)
- Cost tracking
Success Criteria
Phase 2 Complete When:
- ✅ Intelligence worker deployed with AI Search binding
- ✅ AI router supports RAG (
useRAG: true) - ✅ Test endpoint
/v1/test/ragworking - ✅ AI Gateway logs showing traffic
- ⏳ AI Search namespace populated with content (bulk upload)
- ⏳ Production code uses RAG for morning briefs + signal analysis
Next Steps
Immediate (After Deployment)
-
Deploy intelligence worker:
wrangler deploy --config services/intelligence/wrangler.toml --env="" -
Test RAG endpoint:
curl -X POST https://intelligence.dev.integratewise.ai/v1/test/rag \ -H "Content-Type: application/json" \ -d '{"question":"What is IntegrateWise?","tier":"fast"}' -
Verify AI Gateway logs:
- Check cache hit rate
- Verify requests routing correctly
Phase 3: Content Upload
-
Option A: Website crawl (simplest)
- Enable AI Search crawler for integratewise.ai
- Trigger initial crawl
- Wait ~1 hour for indexing
-
Option B: Bulk upload org_memory (more control)
- Write script to fetch org_memory (governance_state=approved)
- Upload to AI Search via API
- Map metadata (title, url, tags, topics)
Phase 4: Production Integration
- Update morning brief handler to use RAG
- Update signal analysis to use RAG
- Add RAG to MCP tools (knowledge search)
- Monitor usage + costs
Files Modified
-
services/intelligence/src/lib/ai-router.ts- Added
useRAGparameter to AIRequest - Added
sourcesfield to AIResponse - Implemented AI Search integration in routeAI()
- RAG context augmentation before AI call
- Added
-
services/intelligence/src/index.ts- Added
POST /v1/test/ragendpoint - Integrated routeAI with RAG support
- Added
-
services/intelligence/wrangler.toml- Already has AI_SEARCH binding ✅
- Already has AI binding ✅
- Already has OPENROUTER_API_KEY binding ✅
Status: READY FOR DEPLOYMENT
Breaking Changes: None
Rollback Available: Yes
Last Updated: 2026-06-09T14:20:00Z