AI Context Documentation
This section provides context for AI coding agents working on the TendSocial codebase.
Purpose
AI Context documentation is designed for:
- AI Coding Agents (Cursor, Copilot, Claude, etc.)
- Quick Reference for key patterns and constraints
- Agent Rules that must be followed
Key Resources
Agent Rules
The primary rules file is located at:
.agent/agent-rules.mdThis file contains critical constraints:
- ESM module requirements (
.jsextensions) - Prisma v7 patterns (no
$use, use$extends) - Multi-tenant data isolation (
getTenantPrisma) - Locked dependency versions
- Strict Typing (No
any, explicit schemas)
Common Patterns
Database Access
typescript
// Always get tenant-scoped Prisma client
import { getTenantPrisma } from '../infra/prisma.js';
async function handler(request) {
const { companyId } = request.user;
const tenantPrisma = getTenantPrisma(companyId);
const posts = await tenantPrisma.post.findMany();Route Pattern
typescript
import type { FastifyInstance } from 'fastify';
import { z } from 'zod';
import type { ZodTypeProvider } from 'fastify-type-provider-zod';
export default async function routes(fastify: FastifyInstance) {
const app = fastify.withTypeProvider<ZodTypeProvider>();
// routes...
}ESM Imports
typescript
// ✅ CORRECT - Always include .js extension
// WRONG - Global Client (Data Leak Risk)
import prisma from '../infra/prisma.js'; // Don't use default export for queries
// ❌ WRONG - Missing extension
// WRONG - Global Client (Data Leak Risk)
import prisma from '../infra/prisma.js'; // Don't use default export for queriesShared Types System
typescript
// Use @tendsocial/shared-types for major entities
import { BrandProfile, AppSettingsSchema } from '@tendsocial/shared-types';
// Use directly for Zod validation/transformation
const validated = BrandProfileSchema.parse(rawDataFromDb);Page Rendering and State Contract
All frontend page screens must render their root JSX wrapped in the <Page> component imported from @/components/shared/Page to ensure layout consistency across all routing contexts:
- Title and Description Props: Page title and description must be passed as
titleanddescriptionprops, not as manual h1/h2 tags or inline descriptions inside the children. - Desktop Header Hiding: The
<Page>component automatically hides titles/descriptions on desktop using responsive tailwind classes to prevent duplication with the layout'sTopbar. Always keep this default behavior unless explicitly overridden. - Unified Loading and Error States: Wrap loading states and error states in
<Page>to maintain consistent layout structure. For errors, use the<ErrorState>component from@/components/states.
Project Structure
apps/
├── backend/ # Fastify API server
├── frontend/ # React app (app.tendsocial.com)
├── docs/ # VitePress documentation
└── marketing/ # Marketing websiteEpic 3 Hook Patterns (Added in Stories 3.1–3.5)
useBrandProfile / useUpdateBrandProfile
- Location:
apps/frontend/src/features/brand-identity/hooks/useBrandProfile.ts - GET
/api/brand-profile— fetches singleton brand profile for tenant - PUT
/api/brand-profile— updates brand profile withBrandProfileUpdate(all fields optional) - queryKey:
queryKeys.brandProfile.detail(companyId)/queryKeys.brandProfile.all - Gate:
enabled: !!companyId— only fires when user is authenticated
useBlogPosts / useCreateBlogPost / useUpdateBlogPost
- Location:
apps/frontend/src/features/blog/hooks/useBlogPosts.ts - GET
/api/blog/posts— list with optionalstatus,limit,offsetparams - POST
/api/blog/posts— create with{ title, slug, markdownBody, status? } - PUT
/api/blog/posts/:id— update with{ title?, slug?, markdownBody?, status? }(noidin body) - queryKey:
queryKeys.blog.*— seelib/queryKeys.tsfor full shape - Note: Backend returns
DRAFT/PUBLISHEDetc.; hook normalizes to lowercasedraft/published
useSocialPosts / useCreateSocialPost
- Location:
apps/frontend/src/features/posts/hooks/useSocialPosts.ts - GET
/api/social-posts— list; pass{ campaignId }filter to add?campaignId=query param - POST
/api/social-posts— create with{ content: string, platforms: string[] } - queryKey:
queryKeys.posts.* - Note: Response is raw array; hook normalizes via
normalizePost
useGenerateCampaignContent
- Location:
apps/frontend/src/features/campaigns/hooks/useCampaigns.ts - POST
/api/campaigns/:id/generate-content— triggers AI content generation for campaign - Input:
GenerateContentInput(empty{}is valid — uses campaign context from backend) - Output:
GenerateContentResultwithcountsandideas.content.{ socialPosts, blogIdeas, videoIdeas } - Used by:
CampaignGeneratePanelcomponent
IdentityContextPanel
- Location:
apps/frontend/src/features/brand-identity/components/ - Displays brand identity completeness summary with setup guidance
- Reads from
useBrandProfile; showsIncompleteContextStatewhen identity fields are missing
CampaignGeneratePanel
- Location:
apps/frontend/src/features/campaigns/components/CampaignGeneratePanel.tsx - Gate: checks
isIdentityComplete(profile)before allowing generation - Uses
useGenerateCampaignContent(campaignId)mutation - Shows
PermissionRestrictedStatefor non-admin/owner roles
queryKeys.ts additions in Epic 3
blogsection added in Story 3.2:all,lists(companyId),listFiltered(companyId, filters),detail(id, companyId)brandProfileandpostssections were pre-existing
See Also
Critical References (Read First)
- Zero Tolerance Rules - Absolute requirements (no
any, noas unknown as, only Pino, only neverthrow) - Logging Guide - Comprehensive Pino structured logging patterns
- Error Handling Guide - neverthrow Result types and AppError hierarchy
Quick References (for AI Context)
- Feature Reference - All features with routes, pages, models
- API Reference - All API endpoints quick reference
- Models Reference - Prisma models and relationships
- AI Tasks Reference - AI task IDs and their features
- Campaign System - Campaign feature context for AI agents
- Manual Posting Flow - Guide for Copy & Post workflow
- Social Posts V2 Layout - Architecture & Layout patterns
- Team Integrations - Slack/Discord/Teams integration
Standards
- Agent Context Standards - Detailed agent context format
- Coding Standards - Code style guidelines
- Testing Conventions - Test locations, naming, and patterns
- Architecture Overview - System design