Skip to content

Skills for Claude Code CLI such as full stack dev Cloudflare, React, Tailwind v4, and AI integrations.

feat(better-chatbot): Enhance skill v2.0.0 with architecture deep-dive

Jez84d6e5a22ec2
diff --git a/CHANGELOG.md b/CHANGELOG.md
index c855f6a0..51381c6d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -9,6 +9,44 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
+### Enhanced - better-chatbot Skill v2.0.0 ✅
+
+**Date**: 2025-11-04
+
+**Major Enhancement**: Added comprehensive architecture deep-dive and practical templates for building features that align with the original developer's vision.
+
+#### better-chatbot v2.0.0 (Skill #52 - Enhanced)
+- **New Content**: Architecture deep-dive, design philosophy, practical feature templates
+- **Token Savings**: ~60% (maintained)
+- **Errors Prevented**: 8 (maintained)
+- **Lines Added**: ~600 lines of architectural guidance
+
+**What's New**:
+- ✅ **API Architecture & Design Patterns**: Route handler templates, shared business logic pattern, defensive programming with `safe()`, streaming-first architecture
+- ✅ **Tool System Deep Dive**: Three-tier architecture (MCP/Workflow/Default), tool lifecycle, convention-based extension, why global MCP singleton
+- ✅ **Component & Design Philosophy**: Organization by feature, compound component pattern, client wrapper pattern, tool result rendering separation
+- ✅ **Database & Repository Patterns**: Interface-first design, query optimization strategies, schema evolution workflow
+- ✅ **Architectural Principles**: Progressive enhancement, convention over configuration, defensive programming, streaming-first, type-driven development
+- ✅ **Practical Templates**: Adding new tools, API routes, repositories (complete copy-paste examples)
+- ✅ **References Directory**: Added upstream AGENTS.md and CONTRIBUTING.md from better-chatbot repo
+
+**New Keywords Added** (19):
+- repository pattern, three-tier tool system, progressive enhancement architecture
+- streaming-first design, compound component pattern, defensive programming
+- safe() wrapper, shared business logic, tool lifecycle, API route patterns
+- How-to keywords: "how to add new tool", "how to add API route", "repository implementation", "database query optimization", "component composition pattern", "tool rendering separation"
+
+**Enhancement Goal**: Help contributors build features that fit the existing architectural vision by understanding the "why" behind design decisions, not just the "what" of conventions.
+
+**Use Cases Enabled**:
+- Understanding why tools are separated into three tiers
+- Building new tools/routes/repositories that match existing patterns
+- Making architectural decisions aligned with the project philosophy
+- Contributing features without breaking established patterns
+- Optimizing database queries using proven strategies
+
+---
+
### Added - hugo Skill v1.0.0 ✅
**Date**: 2025-11-04
diff --git a/skills/better-chatbot/README.md b/skills/better-chatbot/README.md
index 0d3ec127..444f5696 100644
--- a/skills/better-chatbot/README.md
+++ b/skills/better-chatbot/README.md
@@ -1,7 +1,7 @@
# better-chatbot
**Status**: Production Ready ✅
-**Last Updated**: 2025-10-29
+**Last Updated**: 2025-11-04 (v2.0.0 - Enhanced with architecture deep-dive)
**Production Tested**: https://betterchatbot.vercel.app
---
@@ -28,6 +28,16 @@ Claude Code automatically discovers this skill when you mention:
- Vercel AI SDK chatbot
- Next.js chatbot
- multi-AI provider
+- repository pattern
+- three-tier tool system
+- progressive enhancement architecture
+- streaming-first design
+- compound component pattern
+- defensive programming
+- safe() wrapper
+- shared business logic
+- tool lifecycle
+- API route patterns
### Error-Based Keywords
- "unauthorized users accessing actions"
@@ -38,23 +48,31 @@ Claude Code automatically discovers this skill when you mention:
- "E2E tests failing on clean database"
- "missing environment variables"
- "conventional commit format"
+- "how to add new tool"
+- "how to add API route"
+- "repository implementation"
+- "database query optimization"
+- "component composition pattern"
+- "tool rendering separation"
---
## What This Skill Does
-This skill provides project-specific coding conventions, repository structure standards, testing patterns, and contribution guidelines for the better-chatbot project. It ensures contributions follow established patterns for server actions, tool abstractions, workflow systems, and E2E testing orchestration.
+This skill provides project-specific coding conventions, repository structure standards, testing patterns, architectural principles, and contribution guidelines for the better-chatbot project. It ensures contributions follow established patterns and understand the "why" behind design decisions.
### Core Capabilities
+✅ API architecture & design patterns (route handlers, shared logic, streaming)
+✅ Three-tier tool system (MCP, Workflow, Default)
+✅ Component & design philosophy (compound patterns, separation of concerns)
+✅ Database & repository patterns (interface-first, query optimization)
+✅ Architectural principles (progressive enhancement, defensive programming)
+✅ Practical templates (adding tools, routes, repositories)
✅ Server action validators (auth, validation, FormData)
-✅ Tool abstraction system (MCP/Workflow/Default)
✅ Workflow execution patterns (DAG streaming)
✅ Playwright E2E test orchestration
✅ Conventional Commit enforcement
-✅ Zod validation patterns
-✅ Zustand state management conventions
-✅ Multi-AI provider integration standards
---
@@ -174,9 +192,9 @@ git push origin feat/my-feature
```
better-chatbot/
-├── SKILL.md # Complete documentation
-├── README.md # This file
-└── references/ # Upstream docs (loaded on demand)
+├── SKILL.md # Complete documentation with architecture deep-dive
+├── README.md # This file (quick reference)
+└── references/ # Upstream docs from better-chatbot repo
├── AGENTS.md # Full repository guidelines
└── CONTRIBUTING.md # Complete contribution process
```
diff --git a/skills/better-chatbot/SKILL.md b/skills/better-chatbot/SKILL.md
index 76909590..a5a32649 100644
--- a/skills/better-chatbot/SKILL.md
+++ b/skills/better-chatbot/SKILL.md
@@ -1,26 +1,29 @@
---
name: better-chatbot
description: |
- This skill provides project-specific coding conventions, repository structure standards, testing patterns, and contribution guidelines for the better-chatbot project (https://github.com/cgoinglove/better-chatbot). Use this skill when contributing to or working with better-chatbot to ensure code follows established patterns for server actions, tool abstractions, workflow systems, and E2E testing orchestration.
+ This skill provides project-specific coding conventions, architectural principles, repository structure standards, testing patterns, and contribution guidelines for the better-chatbot project (https://github.com/cgoinglove/better-chatbot). Use this skill when contributing to or working with better-chatbot to understand the design philosophy and ensure code follows established patterns.
- Use when: working in better-chatbot repository, contributing features/fixes, following server action validators, setting up Playwright tests, understanding tool abstraction system (MCP/Workflow/Default), implementing workflows, handling multi-AI provider integration
+ Includes: API architecture deep-dive, three-tier tool system (MCP/Workflow/Default), component design patterns, database repository patterns, architectural principles (progressive enhancement, defensive programming, streaming-first), practical templates for adding features (tools, routes, repositories).
- Keywords: better-chatbot, chatbot contribution, better-chatbot standards, chatbot development, AI chatbot patterns, Next.js chatbot, Vercel AI SDK chatbot, MCP tools, workflow builder, server action validators, better-chatbot testing, validatedActionWithUser, tool abstraction, DAG workflows
+ Use when: working in better-chatbot repository, contributing features/fixes, understanding architectural decisions, following server action validators, implementing tools/workflows, setting up Playwright tests, adding API routes, designing database queries, building UI components, handling multi-AI provider integration
+
+ Keywords: better-chatbot, chatbot contribution, better-chatbot standards, chatbot development, AI chatbot patterns, API architecture, three-tier tool system, repository pattern, progressive enhancement, defensive programming, streaming-first, compound component pattern, Next.js chatbot, Vercel AI SDK chatbot, MCP tools, workflow builder, server action validators, tool abstraction, DAG workflows, shared business logic, safe() wrapper, tool lifecycle
license: MIT
metadata:
- version: 1.0.0
+ version: 2.0.0
author: Jeremy Dawes (Jez) | Jezweb
upstream: https://github.com/cgoinglove/better-chatbot
- last_verified: 2025-10-29
+ last_verified: 2025-11-04
tech_stack: Next.js 15, Vercel AI SDK 5, Better Auth, Drizzle ORM, PostgreSQL, Playwright
token_savings: ~60%
errors_prevented: 8
+ enhancement_date: 2025-11-04
---
# better-chatbot Contribution & Standards Skill
**Status**: Production Ready
-**Last Updated**: 2025-10-29
+**Last Updated**: 2025-11-04 (v2.0.0 - Enhanced with architecture deep-dive)
**Dependencies**: None (references better-chatbot project)
**Latest Versions**: Next.js 15.3.2, Vercel AI SDK 5.0.82, Better Auth 1.3.34, Drizzle ORM 0.41.0
@@ -67,9 +70,784 @@ better-chatbot/
└── drizzle/ # Database migrations
```
-### Key Concepts
+---
+
+## API Architecture & Design Patterns
+
+### Route Structure Philosophy
+
+**Convention**: RESTful resources with Next.js App Router conventions
+
+```
+/api/[resource]/route.ts → GET/POST collection endpoints
+/api/[resource]/[id]/route.ts → GET/PUT/DELETE item endpoints
+/api/[resource]/actions.ts → Server actions (mutations)
+```
+
+### Standard Route Handler Pattern
+
+**Location**: `src/app/api/`
+
+**Template structure**:
+```typescript
+export async function POST(request: Request) {
+ try {
+ // 1. Parse and validate request body with Zod
+ const json = await request.json();
+ const parsed = zodSchema.parse(json);
+
+ // 2. Check authentication
+ const session = await getSession();
+ if (!session?.user.id) return new Response("Unauthorized", { status: 401 });
+
+ // 3. Check authorization (ownership/permissions)
+ if (resource.userId !== session.user.id) return new Response("Forbidden", { status: 403 });
+
+ // 4. Load/compose dependencies (tools, context, etc.)
+ const tools = await loadMcpTools({ mentions, allowedMcpServers });
+
+ // 5. Execute with streaming if applicable
+ const stream = createUIMessageStream({ execute: async ({ writer }) => { ... } });
+
+ // 6. Return response
+ return createUIMessageStreamResponse({ stream });
+ } catch (error) {
+ logger.error(error);
+ return Response.json({ message: error.message }, { status: 500 });
+ }
+}
+```
+
+### Shared Business Logic Pattern
+
+**Key Insight**: Extract complex orchestration logic into shared utilities
+
+**Example**: `src/app/api/chat/shared.chat.ts`
+
+This file demonstrates how to handle:
+- Tool loading (`loadMcpTools`, `loadWorkFlowTools`, `loadAppDefaultTools`)
+- Filtering and composition (`filterMCPToolsByMentions`, `excludeToolExecution`)
+- System prompt building (`mergeSystemPrompt`)
+- Manual tool execution handling
+
+**Pattern**:
+```typescript
+// Shared utility function
+export const loadMcpTools = (opt?) =>
+ safe(() => mcpClientsManager.tools())
+ .map((tools) => {
+ if (opt?.mentions?.length) {
+ return filterMCPToolsByMentions(tools, opt.mentions);
+ }
+ return filterMCPToolsByAllowedMCPServers(tools, opt?.allowedMcpServers);
+ })
+ .orElse({} as Record<string, VercelAIMcpTool>);
+
+// Used in multiple routes
+// - /api/chat/route.ts
+// - /api/chat/temporary/route.ts
+// - /api/workflow/[id]/execute/route.ts
+```
+
+**Why**: DRY principle, single source of truth, consistent behavior
+
+### Defensive Programming with safe()
+
+**Library**: `ts-safe` for functional error handling
+
+**Philosophy**: Never crash the chat - degrade features gracefully
+
+```typescript
+// Returns empty object on failure, chat continues
+const MCP_TOOLS = await safe()
+ .map(errorIf(() => !isToolCallAllowed && "Not allowed"))
+ .map(() => loadMcpTools({ mentions, allowedMcpServers }))
+ .orElse({}); // Graceful fallback
+```
+
+### Streaming-First Architecture
+
+**Pattern**: Use Vercel AI SDK streaming utilities
+
+```typescript
+// In route handler
+const stream = createUIMessageStream({
+ execute: async ({ writer }) => {
+ // Stream intermediate results
+ writer.write({ type: "text", content: "Processing..." });
+
+ // Execute with streaming
+ const result = await streamText({
+ model,
+ messages,
+ tools,
+ onChunk: (chunk) => writer.write({ type: "text-delta", delta: chunk })
+ });
+
+ return { output: result };
+ }
+});
+
+return createUIMessageStreamResponse({ stream });
+```
+
+**Why**: Live feedback, better UX, handles long-running operations
+
+---
+
+## Tool System Deep Dive
+
+### Three-Tier Tool Architecture
+
+**Design Goal**: Balance extensibility (MCP), composability (workflows), and batteries-included (default tools)
+
+```
+Tier 1: MCP Tools (External)
+ ↓ Can be used in
+Tier 2: Workflow Tools (User-Created)
+ ↓ Can be used in
+Tier 3: Default Tools (Built-In)
+```
+
+### Tier 1: MCP Tools (External Integrations)
+
+**Location**: `src/lib/ai/mcp/`
+
+**Philosophy**: Model Context Protocol servers become first-class tools
+
+**Manager Pattern**:
+```typescript
+// mcp-manager.ts - Singleton for all MCP clients
+export const mcpClientsManager = globalThis.__mcpClientsManager__;
+
+// API:
+mcpClientsManager.init() // Initialize configured servers
+mcpClientsManager.getClients() // Get connected clients
+mcpClientsManager.tools() // Get all tools as Vercel AI SDK tools
+mcpClientsManager.toolCall(serverId, toolName, args) // Execute tool
+```
+
+**Why Global Singleton?**
+- Next.js dev hot-reloading → reconnecting MCP servers on every change is expensive
+- Persists across HMR updates
+- Production: only one instance needed
+
+**Tool Wrapping**:
+```typescript
+// MCP tools are tagged with metadata for filtering
+type VercelAIMcpTool = Tool & {
+ _mcpServerId: string;
+ _originToolName: string;
+ _toolName: string; // Transformed for AI SDK
+};
+
+// Branded type for runtime checking
+VercelAIMcpToolTag.create(tool)
+```
+
+### Tier 2: Workflow Tools (Visual Composition)
+
+**Location**: `src/lib/ai/workflow/`
+
+**Philosophy**: Visual workflows become callable tools via `@workflow_name`
+
+**Node Types**:
+```typescript
+enum NodeKind {
+ Input = "input", // Entry point
+ LLM = "llm", // AI reasoning
+ Tool = "tool", // Call MCP/default tools
+ Http = "http", // HTTP requests
+ Template = "template",// Text processing
+ Condition = "condition", // Branching logic
+ Output = "output", // Exit point
+}
+```
+
+**Execution with Streaming**:
+```typescript
+// Workflows stream intermediate results
+executor.subscribe((e) => {
+ if (e.eventType == "NODE_START") {
+ dataStream.write({
+ type: "tool-output-available",
+ toolCallId,
+ output: { status: "running", node: e.nodeId }
+ });
+ }
+ if (e.eventType == "NODE_END") {
+ dataStream.write({
+ type: "tool-output-available",
+ toolCallId,
+ output: { status: "complete", result: e.result }
+ });
+ }
+});
+```
+
+**Key Feature**: Live progress updates in chat UI
+
+### Tier 3: Default Tools (Built-In Capabilities)
+
+**Location**: `src/lib/ai/tools/`
+
+**Categories**:
+```typescript
+export const APP_DEFAULT_TOOL_KIT = {
+ [AppDefaultToolkit.Visualization]: {
+ CreatePieChart, CreateBarChart, CreateLineChart,
+ CreateTable, CreateTimeline
+ },
+ [AppDefaultToolkit.WebSearch]: {
+ WebSearch, WebContent
+ },
+ [AppDefaultToolkit.Http]: {
+ Http
+ },
+ [AppDefaultToolkit.Code]: {
+ JavascriptExecution, PythonExecution
+ },
+};
+```
+
+**Tool Implementation Pattern**:
+```typescript
+// Execution returns "Success", rendering happens client-side
+export const createTableTool = createTool({
+ description: "Create an interactive table...",
+ inputSchema: z.object({
+ title: z.string(),
+ columns: z.array(...),
+ data: z.array(...)
+ }),
+ execute: async () => "Success"
+});
+
+// Client-side rendering in components/tool-invocation/
+export function InteractiveTable({ part }) {
+ const args = part.input;
+ return <DataTable columns={args.columns} data={args.data} />;
+}
+```
+
+**Why Separation?**
+- Server: Pure data/business logic
+- Client: Rich visualization/interaction
+- Easier testing, better performance
+
+### Tool Lifecycle
+
+```
+1. Request → /api/chat/route.ts
+2. Parse mentions (@tool, @workflow, @agent)
+3. Load tools based on mentions/permissions:
+ - loadMcpTools() → filters by mentions or allowedMcpServers
+ - loadWorkFlowTools() → converts workflows to tools
+ - loadAppDefaultTools() → filters default toolkits
+4. Merge all tools into single Record<string, Tool>
+5. Handle toolChoice mode:
+ - "manual" → LLM proposes, user confirms
+ - "auto" → full execution
+ - "none" → no tools loaded
+6. Pass tools to streamText()
+7. Stream results back
+```
+
+### Convention-Based Extension
+
+**Adding a new tool type is simple**:
+1. Add enum to `AppDefaultToolkit`
+2. Implement tool with `createTool()`
+3. Add to `APP_DEFAULT_TOOL_KIT`
+4. Tool automatically available via `@toolname`
+
+---
+
+## Component & Design Philosophy
+
+### Organization by Feature
+
+**Location**: `src/components/`
+
+```
+components/
+├── ui/ → shadcn/ui primitives
+├── layouts/ → App structure
+├── agent/ → Agent-specific
+├── workflow/ → Workflow editor
+├── tool-invocation/ → Tool result rendering
+└── *.tsx → Shared components
+```
+
+**Principle**: Group by feature, not by type
+
+### Compound Component Pattern
+
+**Example**: `message.tsx` + `message-parts.tsx`
+
+**Philosophy**: Break complex components into composable parts
+
+```typescript
+// message.tsx exports multiple related components
+export function PreviewMessage({ message }) { ... }
+export function ErrorMessage({ error }) { ... }
+
+// message-parts.tsx handles polymorphic content
+export function MessageParts({ parts }) {
+ return parts.map(part => {
+ if (isToolUIPart(part)) return <ToolInvocation part={part} />;
+ if (part.type === 'text') return <Markdown text={part.text} />;
+ // ... other types
+ });
+}
+```
+
+### Client Component Wrapper Pattern
+
+**Example**: `chat-bot.tsx`
+
+**Structure**:
+```typescript
+export default function ChatBot({ threadId, initialMessages }) {
+ // 1. State management (Zustand)
+ const [model, toolChoice] = appStore(useShallow(state => [...]));
+
+ // 2. Vercel AI SDK hook
+ const { messages, append, status } = useChat({
+ id: threadId,
+ initialMessages,
+ body: { chatModel: model, toolChoice },
+ });
+
+ // 3. Render orchestration
+ return (
+ <>
+ <ChatGreeting />
+ <MessageList messages={messages} />
+ <PromptInput onSubmit={append} />
+ </>
+ );
+}
+```
+
+**Why**: Top-level orchestrates, delegates rendering to specialized components
+
+### Tool Result Rendering Separation
+
+**Key Architecture Decision**:
+- Tool **execution** lives in `lib/ai/tools/`
+- Tool **rendering** lives in `components/tool-invocation/`
+
+**Example**:
+```typescript
+// Server-side (lib/ai/tools/create-table.ts)
+execute: async (params) => "Success"
+
+// Client-side (components/tool-invocation/interactive-table.tsx)
+export function InteractiveTable({ part }) {
+ const { columns, data } = part.input;
+ return <DataTable columns={columns} data={data} />;
+}
+```
+
+**Benefits**:
+- Clear separation of concerns
+- Easier testing
+- Client can be rich/interactive without server complexity
+
+---
+
+## Database & Repository Patterns
+
+### Repository Pattern Architecture
+
+**Location**: `src/lib/db/`
+
+**Structure**:
+```
+db/
+├── repository.ts → Single import point
+├── pg/
+│ ├── db.pg.ts → Drizzle connection
+│ ├── schema.pg.ts → Table definitions
+│ └── repositories/ → Feature queries
+└── migrations/ → Drizzle migrations
+```
+
+**Philosophy**: Abstract DB behind repository interfaces
+
+### Interface-First Design
+
+**Pattern**:
+```typescript
+// 1. Define interface in src/types/[domain].ts
+export type ChatRepository = {
+ insertThread(thread: Omit<ChatThread, "createdAt">): Promise<ChatThread>;
+ selectThread(id: string): Promise<ChatThread | null>;
+ selectThreadDetails(id: string): Promise<ThreadDetails | null>;
+};
+
+// 2. Implement in src/lib/db/pg/repositories/[domain]-repository.pg.ts
+export const pgChatRepository: ChatRepository = {
+ selectThreadDetails: async (id: string) => {
+ const [thread] = await db
+ .select()
+ .from(ChatThreadTable)
+ .leftJoin(UserTable, eq(ChatThreadTable.userId, UserTable.id))
+ .where(eq(ChatThreadTable.id, id));
+
+ if (!thread) return null;
+
+ const messages = await pgChatRepository.selectMessagesByThreadId(id);
+
+ return {
+ id: thread.chat_thread.id,
+ title: thread.chat_thread.title,
+ userId: thread.chat_thread.userId,
+ createdAt: thread.chat_thread.createdAt,
+ userPreferences: thread.user?.preferences,
+ messages,
+ };
+ },
+};
+
+// 3. Export from src/lib/db/repository.ts
+export const chatRepository = pgChatRepository;
+```
+
+**Why**:
+- Easy to swap implementations (pg → sqlite)
+- Testable without database
+- Consistent API across codebase
+
+### Query Optimization Strategies
+
+**1. Indexes on Foreign Keys**:
+```typescript
+export const ChatThreadTable = pgTable("chat_thread", {
+ id: uuid("id").primaryKey(),
+ userId: uuid("user_id").references(() => UserTable.id),
+}, (table) => ({
+ userIdIdx: index("chat_thread_user_id_idx").on(table.userId),
+}));
+```
+
+**2. Selective Loading**:
+```typescript
+// Load minimal data
+selectThread(id) → { id, title, userId, createdAt }
+
+// Load full data when needed
+selectThreadDetails(id) → { ...thread, messages, userPreferences }
+```
+
+**3. SQL Aggregation**:
+```typescript
+// Get threads with last message timestamp
+const threadsWithActivity = await db
+ .select({
+ threadId: ChatThreadTable.id,
+ lastMessageAt: sql<string>`MAX(${ChatMessageTable.createdAt})`,
+ })
+ .from(ChatThreadTable)
+ .leftJoin(ChatMessageTable, eq(ChatThreadTable.id, ChatMessageTable.threadId))
+ .groupBy(ChatThreadTable.id)
+ .orderBy(desc(sql`last_message_at`));
+```
+
+### Schema Evolution Workflow
+
+```bash
+# 1. Modify schema in src/lib/db/pg/schema.pg.ts
+export const NewTable = pgTable("new_table", { ... });
+
+# 2. Generate migration
+pnpm db:generate
+
+# 3. Review generated SQL in drizzle/migrations/
+# 4. Apply migration
+pnpm db:migrate
+
+# 5. Optional: Visual DB exploration
+pnpm db:studio
+```
+
+---
+
+## Architectural Principles
+
+### 1. Progressive Enhancement
+
+Features build in layers:
+
+```
+Base Layer: Chat + LLM
+ ↓
+Tool Layer: Default + MCP
+ ↓
+Composition Layer: Workflows (tools as nodes)
+ ↓
+Personalization Layer: Agents (workflows + prompts)
+```
+
+**Evidence**:
+- Agents can have `instructions.mentions` (inject tools/workflows)
+- Workflows can call MCP + default tools
+- Chat API composes all three tiers
+
+**User Journey**:
+1. Start with default tools (no setup)
+2. Add MCP servers for specialized needs
+3. Combine into workflows for automation
+4. Package into agents for personas
+
+### 2. Convention Over Configuration
+
+**New Tool?**
+- Add to `AppDefaultToolkit` enum → auto-available
+
+**New Workflow Node?**
+- Add to `NodeKind` enum → executor handles it
+
+**New MCP Server?**
+- Just configure via UI → manager handles lifecycle
+
+### 3. Defensive Programming
+
+**Use `safe()` everywhere**:
+```typescript
+const tools = await safe(() => loadMcpTools())
+ .orElse({}); // Returns default on failure
+```
+
+**Philosophy**: Never crash the chat - degrade gracefully
+
+### 4. Streaming-First
+
+**Evidence**:
+- Chat API uses `createUIMessageStream()`
+- Workflow execution streams intermediate steps
+- Tool calls stream progress updates
+
+**Why**: Live feedback, better UX, handles long operations
+
+### 5. Type-Driven Development
+
+**Pattern**:
+```typescript
+// Zod defines runtime validation AND TypeScript types
+const schema = z.object({ name: z.string() });
+type SchemaType = z.infer<typeof schema>;
+
+// Discriminated unions for polymorphic data
+type WorkflowNodeData =
+ | { kind: "input"; ... }
+ | { kind: "llm"; ... }
+ | { kind: "tool"; ... };
+
+// Brand types for runtime checking
+VercelAIMcpToolTag.isMaybe(tool)
+```
+
+---
+
+## Practical Templates
+
+### Template: Adding a New Default Tool
+
+```typescript
+// 1. Define in lib/ai/tools/[category]/[tool-name].ts
+import { tool as createTool } from "ai";
+import { z } from "zod";
+
+export const myNewTool = createTool({
+ description: "Clear description for LLM to understand when to use this",
+ inputSchema: z.object({
+ param: z.string().describe("What this parameter does"),
+ }),
+ execute: async (params) => {
+ // For visualization tools: return "Success"
+ // For data tools: return actual data
+ return "Success";
+ },
+});
+
+// 2. Add to lib/ai/tools/tool-kit.ts
+import { DefaultToolName } from "./index";
+import { myNewTool } from "./[category]/[tool-name]";
+
+export enum DefaultToolName {
+ // ... existing
+ MyNewTool = "my_new_tool",
+}
+
+export const APP_DEFAULT_TOOL_KIT = {
+ [AppDefaultToolkit.MyCategory]: {
+ [DefaultToolName.MyNewTool]: myNewTool,
+ },
+};
+
+// 3. Create rendering in components/tool-invocation/my-tool-invocation.tsx
+export function MyToolInvocation({ part }: { part: ToolUIPart }) {
+ const args = part.input as z.infer<typeof myNewTool.inputSchema>;
+ return <div>{/* Render based on args */}</div>;
+}
+
+// 4. Add to components/tool-invocation/index.tsx switch
+if (toolName === DefaultToolName.MyTool) {
+ return <MyToolInvocation part={part} />;
+}
+```
+
+### Template: Adding a New API Route
+
+```typescript
+// src/app/api/[resource]/route.ts
+import { getSession } from "auth/server";
+import { [resource]Repository } from "lib/db/repository";
+import { z } from "zod";
+
+const querySchema = z.object({
+ limit: z.coerce.number().default(10),
+});
+
+export async function GET(request: Request) {
+ // 1. Auth check
+ const session = await getSession();
+ if (!session?.user.id) {
+ return new Response("Unauthorized", { status: 401 });
+ }
+
+ // 2. Parse & validate
+ try {
+ const url = new URL(request.url);
+ const params = querySchema.parse(Object.fromEntries(url.searchParams));
+
+ // 3. Use repository
+ const data = await [resource]Repository.selectByUserId(
+ session.user.id,
+ params.limit
+ );
+
+ return Response.json(data);
+ } catch (error) {
+ if (error instanceof z.ZodError) {
+ return Response.json(
+ { error: "Invalid params", details: error.message },
+ { status: 400 }
+ );
+ }
+ console.error("Failed:", error);
+ return new Response("Internal Server Error", { status: 500 });
+ }
+}
+
+export async function POST(request: Request) {
+ const session = await getSession();
+ if (!session?.user.id) {
+ return new Response("Unauthorized", { status: 401 });
+ }
+
+ try {
+ const body = await request.json();
+ const data = createSchema.parse(body);
+
+ const item = await [resource]Repository.insert({
+ ...data,
+ userId: session.user.id,
+ });
+
+ return Response.json(item);
+ } catch (error) {
+ if (error instanceof z.ZodError) {
+ return Response.json({ error: "Invalid input" }, { status: 400 });
+ }
+ return Response.json({ error: "Internal error" }, { status: 500 });
+ }
+}
+```
+
+### Template: Adding a New Repository
+
+```typescript
+// 1. Define interface in src/types/[domain].ts
+export type MyRepository = {
+ selectById(id: string): Promise<MyType | null>;
+ selectByUserId(userId: string, limit?: number): Promise<MyType[]>;
+ insert(data: InsertType): Promise<MyType>;
+ update(id: string, data: Partial<InsertType>): Promise<MyType>;
+ delete(id: string): Promise<void>;
+};
+
+// 2. Add table to src/lib/db/pg/schema.pg.ts
+export const MyTable = pgTable("my_table", {
+ id: uuid("id").primaryKey().defaultRandom(),
+ userId: uuid("user_id").references(() => UserTable.id).notNull(),
+ name: text("name").notNull(),
+ createdAt: timestamp("created_at").defaultNow().notNull(),
+}, (table) => ({
+ userIdIdx: index("my_table_user_id_idx").on(table.userId),
+}));
+
+// 3. Implement in src/lib/db/pg/repositories/my-repository.pg.ts
+import { pgDb as db } from "../db.pg";
+import { MyTable } from "../schema.pg";
+import { eq, desc } from "drizzle-orm";
+
+export const pgMyRepository: MyRepository = {
+ selectById: async (id) => {
+ const [result] = await db
+ .select()
+ .from(MyTable)
+ .where(eq(MyTable.id, id));
+ return result ?? null;
+ },
+
+ selectByUserId: async (userId, limit = 10) => {
+ return await db
+ .select()
+ .from(MyTable)
+ .where(eq(MyTable.userId, userId))
+ .orderBy(desc(MyTable.createdAt))
+ .limit(limit);
+ },
+
+ insert: async (data) => {
+ const [result] = await db
+ .insert(MyTable)
+ .values(data)
+ .returning();
+ return result;
+ },
+
+ update: async (id, data) => {
+ const [result] = await db
+ .update(MyTable)
+ .set(data)
+ .where(eq(MyTable.id, id))
+ .returning();
+ return result;
+ },
+
+ delete: async (id) => {
+ await db.delete(MyTable).where(eq(MyTable.id, id));
+ },
+};
+
+// 4. Export from src/lib/db/repository.ts
+export { pgMyRepository as myRepository } from "./pg/repositories/my-repository.pg";
+
+// 5. Generate and run migration
+// pnpm db:generate
+// pnpm db:migrate
+```
+
+---
+
+## Server Action Validators & Coding Standards
+
+### Server Action Validators (`lib/action-utils.ts`)
-**1. Server Action Validators** (`lib/action-utils.ts`)
Centralized pattern for validated, permission-gated server actions:
```typescript
diff --git a/skills/better-chatbot/references/AGENTS.md b/skills/better-chatbot/references/AGENTS.md
new file mode 100644
index 00000000..c40891b7
--- /dev/null
+++ b/skills/better-chatbot/references/AGENTS.md
@@ -0,0 +1,42 @@
+# Repository Guidelines
+
+## Project Structure & Module Organization
+- App code lives in `src`.
+ - `src/app` (Next.js routes, API, middleware)
+ - `src/components` (UI; reusable components in PascalCase)
+ - `src/lib` (helpers: auth, db, ai, validations, etc.)
+ - `src/hooks` (React hooks: `useX`)
+- Assets in `public/`. End‑to‑end tests in `tests/`. Scripts in `scripts/`. Docker files in `docker/`.
+
+## Build, Test, and Development Commands
+- `pnpm dev` — Run the app locally (Next.js dev server).
+- `pnpm build` / `pnpm start` — Production build and run.
+- `pnpm lint` / `pnpm lint:fix` — ESLint + Biome checks and autofix.
+- `pnpm format` — Format with Biome.
+- `pnpm test` / `pnpm test:watch` — Unit tests (Vitest).
+- `pnpm test:e2e` — Playwright tests; uses `playwright.config.ts` webServer.
+- DB: `pnpm db:push`, `pnpm db:studio`, `pnpm db:migrate` (Drizzle Kit).
+- Docker: `pnpm docker-compose:up` / `:down` to run local stack.
+
+## Coding Style & Naming Conventions
+- TypeScript everywhere. Prefer `zod` for validation.
+- Formatting via Biome: 2 spaces, LF, width 80, double quotes.
+- Components: `PascalCase.tsx`; hooks/utilities: `camelCase.ts`.
+- Co-locate small module tests next to code; larger suites under `tests/`.
+- Keep modules focused; avoid circular deps; use `src/lib` for shared logic.
+
+## Testing Guidelines
+- Unit tests: Vitest, filename `*.test.ts(x)`.
+- E2E: Playwright under `tests/`, filename `*.spec.ts`.
+- Run locally: `pnpm test` and `pnpm test:e2e` (ensure app is running or let Playwright start via config).
+- Add tests for new features and bug fixes; cover happy path + one failure mode.
+
+## Commit & Pull Request Guidelines
+- Conventional Commits: `feat:`, `fix:`, `chore:`, `docs:`, etc. Example: `feat: add image generation tool`.
+- Branch names: `feat/…`, `fix/…`, `chore/…`.
+- PRs: clear description, linked issues, screenshots or terminal output when UI/CLI changes; list test coverage and manual steps.
+- Before opening PR: `pnpm check` (lint+types+tests) should pass.
+
+## Security & Configuration Tips
+- Copy `.env.example` to `.env`; never commit secrets. For local HTTP use `NO_HTTPS=1` or `pnpm build:local`.
+- If using DB/Redis locally, start services via Docker scripts or your own stack.
diff --git a/skills/better-chatbot/references/CONTRIBUTING.md b/skills/better-chatbot/references/CONTRIBUTING.md
new file mode 100644
index 00000000..1cd5c5b9
--- /dev/null
+++ b/skills/better-chatbot/references/CONTRIBUTING.md
@@ -0,0 +1,197 @@
+# Contributing to better-chatbot
+
+Thank you for your interest in contributing to better-chatbot! We welcome contributions from the community and truly appreciate your effort to improve the project.
+
+---
+
+## Before You Start
+
+### 🚨 Feature Requests & Major Changes
+
+**For new features or significant changes, please create an issue first to discuss your idea before submitting a PR.**
+
+This helps us:
+
+- Align on the feature direction and design
+- Avoid duplicate work
+- Ensure the feature fits with the project roadmap
+- Save your valuable time on implementation
+
+**What requires discussion:**
+
+- New UI components or major UI changes
+- New API endpoints or data models
+- Integration with external services
+- Performance optimizations that change behavior
+- Breaking changes
+
+**What doesn't require discussion:**
+
+- Bug fixes
+- Documentation improvements
+- Minor UI tweaks
+- Code refactoring (without behavior changes)
+
+---
+
+## Getting Started
+
+1. **Fork this repository** on GitHub.
+
+2. **Clone your fork** locally:
+
+ ```bash
+ git clone https://github.com/YOUR_USERNAME/better-chatbot.git
+ cd better-chatbot
+ ```
+
+3. **Create a new branch** for your changes:
+
+ ```bash
+ git checkout -b feature/your-feature-name
+ # or
+ git checkout -b fix/your-bug-fix
+ ```
+
+4. **Implement your changes**, following the existing code style and structure.
+
+For any new logic, please add comprehensive unit tests. For any UI changes, please add or modify e2e tests.
+If you are fixing a bug, please add tests to prevent the same bug from happening again.
+
+5. **Test your changes thoroughly**:
+
+ ```bash
+ pnpm dev
+ pnpm test
+ ```
+
+6. **Run e2e tests**:
+
+ ```bash
+ pnpm playwright:install # install playwright browsers
+ pnpm test:e2e # run all e2e tests (48 tests covering core functionality)
+
+ # Optional: run specific test suites
+ pnpm test:e2e -- tests/agents/
+ pnpm test:e2e -- tests/models/
+
+ # Debug specific test
+ pnpm test:e2e -- tests/agents/agent-visibility.spec.ts --headed
+ ```
+
+ **E2E Test Requirements:**
+
+ - PostgreSQL database (use `pnpm docker:pg` for quick setup)
+ - At least one LLM provider API key (OpenAI, Anthropic, or Google)
+ - `BETTER_AUTH_SECRET` environment variable set
+
+---
+
+## Releasing and PR Title Rules
+
+We use [Release Please](https://github.com/googleapis/release-please) to automate GitHub releases.
+**Only the Pull Request title** needs to follow the [Conventional Commits](https://www.conventionalcommits.org/) format. Commit messages can be written freely.
+
+### ✅ PR Title Examples
+
+- `fix: voice chat audio not initializing`
+- `feat: support multi-language UI toggle`
+- `chore: update dependencies`
+
+### ⚠️ Important Notes
+
+- PR **titles must start** with one of the following prefixes:
+
+ ```
+ feat: ...
+ fix: ...
+ chore: ...
+ docs: ...
+ style: ...
+ refactor: ...
+ test: ...
+ perf: ...
+ build: ...
+ ```
+
+- Only the PR title is used for changelog and versioning
+
+- We use **squash merge** to keep the history clean
+
+- Changelog entries and GitHub Releases are **automatically generated** after merging
+
+---
+
+## Submitting a Pull Request
+
+1. **Format, check code quality and run tests**:
+
+ ```bash
+ pnpm check # lint, type check, and run unit tests
+ pnpm test:e2e # run comprehensive e2e test suite (recommended)
+ ```
+
+2. **Commit and push**:
+
+ ```bash
+ git add .
+ git commit -m "your internal message"
+ git push origin your-branch-name
+ ```
+
+3. **Open a Pull Request**:
+
+ - **Title**: Must follow the Conventional Commit format
+ - **Description**: Explain what you changed, why you made the change, and how the changes were verified and tested.
+ - Link to related issues, if any
+ - **Include screenshots or demos** for any UI changes:
+ - **Before/After images** are highly recommended
+ - **Screen recordings** for interactive features
+ - **Mobile/Desktop views** if responsive changes are made
+
+### 📸 Visual Documentation Guidelines
+
+When submitting **Issues** or **Pull Requests**:
+
+**For UI changes:**
+
+- **Always include before/after screenshots** when possible
+- Use **clear, high-quality images** that show the changes
+- **Highlight the changed areas** with arrows or borders if needed
+- For **responsive changes**, include both desktop and mobile views
+- For **interactive features**, consider adding a short screen recording
+
+**For feature requests:**
+
+- Include **reference images** or **mockups** to illustrate your idea
+- Add **screenshots from similar apps** if applicable
+- Use **diagrams** to explain complex workflows or integrations
+
+**For bug reports:**
+
+- Include **screenshots** showing the issue
+- Add **console errors** or **network logs** if relevant
+- Show **expected vs actual behavior** with images when possible
+
+**Example:**
+
+```markdown
+## Before
+
+![before](./before-image.png)
+
+## After
+
+![after](./after-image.png)
+
+## Reference
+
+![reference](./reference-design.png)
+```
+
+---
+
+## Thank You
+
+We sincerely appreciate your contribution to better-chatbot.
+Let’s build a powerful, well tested and lightweight AI experience together! 🚀