Skip to content

heymegabyte/agent-skills / 06-build-and-slice-loop

build-and-slice-loop

Verified

Use when implementing features or streaming OnePrompt site builds. Complete vertical slices with real content, strict TypeScript and Zod. Parallel section/data work, deterministic assembly, granular patches and immediate valid previews.

FileKindSize
SKILL.mdskill md7.2 KB
admin-dashboard.mdmarkdown5.5 KB
ai-chat-widget.mdmarkdown6.0 KB
blog-and-content-engine.mdmarkdown6.2 KB
build-breaking-rules.mdmarkdown7.4 KB
chat-native-dashboard.mdmarkdown3.6 KB
contact-forms-and-endpoints.mdmarkdown6.7 KB
copilot-and-ai-features.mdmarkdown7.4 KB
custom-error-pages.mdmarkdown4.5 KB
data-tables.mdmarkdown7.7 KB
domain-provisioning.mdmarkdown4.6 KB
easter-eggs.mdmarkdown4.7 KB
empty-states-and-loading.mdmarkdown5.7 KB
file-uploads-and-storage.mdmarkdown6.7 KB
internationalization.mdmarkdown4.3 KB
keyboard-shortcuts-and-command-palette.mdmarkdown6.7 KB
microcopy-library.mdmarkdown8.0 KB
notification-center.mdmarkdown8.4 KB
onboarding-and-first-run.mdmarkdown5.0 KB
pre-digested-builds.mdmarkdown3.6 KB
pwa-kit.mdmarkdown6.8 KB
realtime-and-websockets.mdmarkdown7.2 KB
rich-text-editor.mdmarkdown7.4 KB
site-search.mdmarkdown4.0 KB
stripe-first-donations.mdmarkdown10.7 KB
web-manifest-system.mdmarkdown11.9 KB
webhook-system.mdmarkdown9.5 KB

ai-chat-widget.md

View on GitHub
---
name: "AI Chat Widget"
version: "2.0.0"
updated: "2026-04-23"
description: "MANDATORY on every service/product. Workers AI + Vectorize RAG chat widget (or CF Agents SDK AIChatAgent for stateful sessions). Auto-indexes all pages into vector store at deploy time. Floating chat bubble, dark theme, streaming responses. Free on CF Workers AI free tier."
---

# AI Chat Widget

## Mandatory Inclusion Rule

**Every product/service built by this system MUST include an AI chat widget** trained on that specific service's content. This is not optional.

The chat must:

1. Be trained exclusively on the service's own pages, docs, and content
2. Auto-re-index at every deploy (content changes = chat knowledge updates)
3. Know the service's name, purpose, pricing, and features
4. Refuse to answer about unrelated topics (stay scoped)
5. Suggest contacting support when it can't help
6. Work within the CF Workers AI free tier (10K neurons/day)

## Context Training Per Service

Each AI chat is unique because it indexes ONLY that service's content:

```typescript
// At deploy time, index THIS service's content
async function indexServiceContent(env: Env) {
  const pages = await getAllPages(); // from sitemap or route map
  const serviceContext = {
    name: env.SITE_NAME,
    description: env.SITE_DESCRIPTION,
    pricing: env.PRICING_INFO || 'See pricing page',
    support_email: env.SUPPORT_EMAIL || 'support@' + env.DOMAIN,
  };

  // Index service metadata as first document
  await indexChunk(env, 'service-context', JSON.stringify(serviceContext), {
    type: 'context', priority: 'high'
  });

  // Index all pages
  for (const page of pages) {
    const chunks = splitIntoChunks(page.content, 500);
    for (const [i, chunk] of chunks.entries()) {
      await indexChunk(env, `${page.url}#${i}`, chunk, {
        url: page.url, title: page.title, type: 'content'
      });
    }
  }
}
```

## System Prompt Template (Per Service)

```typescript
const systemPrompt = `You are the AI assistant for ${env.SITE_NAME}.
${env.SITE_DESCRIPTION}

Rules:
- Only answer questions about ${env.SITE_NAME} and its features
- Use the provided context to give accurate answers
- If the context doesn't cover the question, say: "I don't have information about that. Contact us at ${env.SUPPORT_EMAIL} for help."
- Be concise, friendly, and helpful
- Never make up features or pricing that aren't in the context
- Suggest relevant pages from the site when helpful

Context from ${env.SITE_NAME}:
${context}`;
```

## Architecture

```
User types question → Worker AI embeds query → Vectorize finds relevant chunks
→ Worker AI generates answer from chunks → Stream response to user
```

### Vector Index (Built at Deploy Time)

```typescript
// Index all page content into Vectorize
async function indexSiteContent(env: Env) {
  const pages = await getAllPages(); // from sitemap or route map
  for (const page of pages) {
    // Split into ~500 token chunks
    const chunks = splitIntoChunks(page.content, 500);
    for (const [i, chunk] of chunks.entries()) {
      const embedding = await env.AI.run('@cf/baai/bge-base-en-v1.5', { text: [chunk] });
      await env.VECTORIZE.upsert([{
        id: `${page.url}#${i}`,
        values: embedding.data[0],
        metadata: { url: page.url, title: page.title, chunk },
      }]);
    }
  }
}
```

### Chat API Endpoint

```typescript
app.post('/api/chat', async (c) => {
  const { message } = await c.req.json();
  if (!message || message.length > 500) return c.json({ error: 'Invalid' }, 400);

  // Rate limit: 20 messages per IP per hour
  if (!await rateLimit(c, `chat:${c.req.header('cf-connecting-ip')}`, 20, 3600)) {
    return c.json({ error: 'Too many messages. Try again later.' }, 429);
  }

  // Embed the question
  const queryEmbed = await c.env.AI.run('@cf/baai/bge-base-en-v1.5', { text: [message] });

  // Find relevant content
  const matches = await c.env.VECTORIZE.query(queryEmbed.data[0], { topK: 5, returnMetadata: true });
  const context = matches.matches.map(m => m.metadata?.chunk).filter(Boolean).join('\n\n');

  // Generate answer
  const response = await c.env.AI.run('@cf/meta/llama-3.1-8b-instruct', {
    messages: [
      { role: 'system', content: `You are a helpful assistant for ${c.env.SITE_NAME}. Answer based on this context:\n\n${context}\n\nIf the context doesn't cover the question, say so honestly and suggest contacting support.` },
      { role: 'user', content: message },
    ],
    stream: true,
  });

  return new Response(response, { headers: { 'Content-Type': 'text/event-stream' } });
});
```

### Chat Widget UI

```html
<button id="chatToggle" class="chat-toggle" aria-label="Open chat">
  <svg width="24" height="24" viewBox="0 0 24 24" fill="currentColor">
    <path d="M20 2H4c-1.1 0-2 .9-2 2v18l4-4h14c1.1 0 2-.9 2-2V4c0-1.1-.9-2-2-2z"/>
  </svg>
</button>
<div id="chatWidget" class="chat-widget" hidden>
  <div class="chat-header">
    <span>Ask anything</span>
    <button onclick="document.getElementById('chatWidget').hidden=true">&times;</button>
  </div>
  <div id="chatMessages" class="chat-messages"></div>
  <form id="chatForm" class="chat-input">
    <input type="text" id="chatInput" placeholder="Type your question..." maxlength="500" autocomplete="off">
    <button type="submit">Send</button>
  </form>
</div>
```

### Styling

```css
.chat-toggle {
  position: fixed; bottom: 1.5rem; right: 1.5rem; z-index: 1000;
  width: 56px; height: 56px; border-radius: 50%;
  background: linear-gradient(135deg, #00E5FF, #50AAE3);
  color: #060610; border: none; cursor: pointer;
  box-shadow: 0 4px 12px rgba(0, 229, 255, 0.3);
  transition: transform 0.2s, box-shadow 0.2s;
}
.chat-toggle:hover { transform: scale(1.05); box-shadow: 0 6px 20px rgba(0, 229, 255, 0.4); }
.chat-widget {
  position: fixed; bottom: 5rem; right: 1.5rem; z-index: 1001;
  width: 380px; max-height: 500px;
  background: #0f0f1f; border: 1px solid rgba(255,255,255,0.06);
  border-radius: 12px; overflow: hidden;
  display: flex; flex-direction: column;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.5);
}
```

## Cost

Free on CF Workers AI free tier (10K neurons/day).
receiving-code-reviewobra/superpowersUse when receiving code review feedback, before implementing suggestions, especially if feedback seems unclear or technically questionable - requires technical rigor and verification, not performative agreement or blind implementationStars: 322kUpdated Claude CodeCodexVerifiedInstall
requesting-code-reviewobra/superpowersUse when completing tasks, implementing major features, or before merging to verify work meets requirementsStars: 322kUpdated Claude CodeCodexVerifiedInstall
using-git-worktreesobra/superpowersUse when starting feature work that needs isolation from current workspace or before executing implementation plans - ensures an isolated workspace exists via native tools or git worktree fallbackStars: 322kUpdated Claude CodeCodexVerifiedInstall
verification-before-completionobra/superpowersUse when about to claim work is complete, fixed, or passing, before committing or creating PRs - requires running verification commands and confirming output before making any success claims; evidence before assertions alwaysStars: 322kUpdated Claude CodeCodexVerifiedInstall