SHADOW AI

In the previous chapter, we defined the implementation architecture.
Now we start building.
The target for this chapter is not to build the entire ACAI platform at once.
The target is:
EMPTY FOLDER
↓
NEXT.JS APPLICATION
↓
UI
↓
ENVIRONMENT
↓
AI GATEWAY
↓
CHAT API
↓
CHAT UI
↓
RUN
↓
TEST
↓
FIRST WORKING ACAI
Once this foundation works, later chapters can add:
RAG
Agent
Tools
Image Generation
Video Generation
Memory
Storage
Workers
Billing
Monitoring
The development computer should have:
Windows
Node.js
npm
VS Code
Internet connection
Git (recommended)
Verify Node.js:
node -v
Verify npm:
npm -v
If both commands return version numbers, continue.
Choose a workspace location.
Example:
D:\ACAI
Open PowerShell or the VS Code terminal inside that location.
Then create the application.
The important rule is:
DO NOT RUN npm COMMANDS
FROM THE WRONG DIRECTORY.
Before installing packages, verify that the terminal is inside the ACAI project directory.
Create the project using the current Next.js project generator.
Recommended configuration:
TypeScript → Yes
ESLint → Yes
Tailwind CSS → Yes
App Router → Yes
src/ directory → Yes
Use the project generator rather than manually assembling a Next.js project from dozens of packages.
After creation:
ACAI
├── src
├── public
├── package.json
├── tsconfig.json
└── ...
Before changing anything:
npm run dev
The development server should start.
Open the local address shown by the terminal.
Expected:
Browser
↓
ACAI Next.js application
If the default Next.js page appears:
✓ PROJECT WORKS
Stop here only if an error appears.
If you see:
npm ERR!
next is not recognized
package.json not found
module not found
do not start adding more code.
First fix:
Directory
↓
Node.js
↓
npm install
↓
Project
Then run again.
This prevents later problems from becoming much harder to diagnose.
Open the ACAI folder in VS Code.
The basic structure should look similar to:
ACAI
│
├── src
│ └── app
│
├── public
│
├── package.json
├── tsconfig.json
├── next.config.*
└── ...
The exact generated files can differ depending on the Next.js version and selected options.
The application will eventually contain:
src/
├── app/
├── components/
├── lib/
├── services/
├── types/
└── config/
Create these directories gradually.
Do not create hundreds of empty files just to match an architecture diagram.
The first useful pages are:
/
/dashboard
/chat
/documents
/image
/video
/settings
For Chapter 26, only these are essential:
/
/chat
The remaining pages can be added later.
Replace the initial homepage with a simple ACAI landing interface.
The objective is:
ACAI
Your AI Creative Intelligence Platform
and a button:
Open AI Chat
The button should navigate to:
/chat
The first chat page needs only:
Header
Message area
Input
Send button
Loading state
Error state
Conceptually:
┌──────────────────────────────────────────────┐
│ ACAI │
├──────────────────────────────────────────────┤
│ │
│ AI: Hello. How can I help? │
│ │
│ You: Explain artificial intelligence. │
│ │
│ AI: Artificial intelligence is... │
│ │
├──────────────────────────────────────────────┤
│ Ask ACAI... [Send] │
└──────────────────────────────────────────────┘
The chat component needs basic state:
messages
input
loading
error
Conceptually:
messages = []
input = ""
loading = false
error = null
When the user sends a message:
input
↓
messages
↓
API request
↓
assistant response
↓
messages
Create:
src/lib/ai/
The purpose is to prevent AI-provider logic from being scattered across the application.
Instead of:
Chat UI
↓
Provider SDK
use:
Chat UI
↓
API
↓
AI Gateway
↓
Provider
Create a server-side AI gateway module.
Conceptual responsibility:
generate()
stream()
selectModel()
handleErrors()
The first implementation can contain only one provider.
Later:
Provider A
Provider B
Provider C
Local Model
can be connected.
Without a gateway:
CHAT
↓
PROVIDER A
IMAGE
↓
PROVIDER A
AGENT
↓
PROVIDER A
RAG
↓
PROVIDER A
Changing providers becomes difficult.
With a gateway:
CHAT
\
IMAGE \
AGENT → AI GATEWAY → PROVIDER
RAG /
The rest of the application remains more independent from provider-specific code.
Create the local environment file expected by your selected provider configuration.
Typical structure:
AI_API_KEY=your_key_here
The exact variable name should match the implementation.
Important:
.env.local
should not be committed to Git if it contains secrets.
Incorrect:
React Component
↓
API SECRET
Correct:
React Component
↓
Your API
↓
Server
↓
AI Provider
The browser only knows your application endpoint.
The basic server flow:
POST /api/chat
│
▼
Validate request
│
▼
Get message
│
▼
AI Gateway
│
▼
Provider
│
▼
Return response
Create the API route under:
src/app/api/chat/
The endpoint should accept something conceptually similar to:
{
"message": "Hello ACAI"
}
The server should validate that:
message exists
message is a string
message is not empty
message is within the permitted size
A successful response can have a normalized structure:
{
"message": "Hello! How can I help you?"
}
A production implementation can later include:
requestId
model
usage
metadata
Do not expose provider internals.
Instead of returning:
internal SDK stack trace
return something like:
{
"error": "Unable to generate a response."
}
and log the detailed error on the server.
When the user presses Send:
CHAT INPUT
↓
fetch("/api/chat")
↓
POST
↓
JSON
↓
SERVER
The server returns:
JSON
↓
CHAT UI
↓
ASSISTANT MESSAGE
During generation:
[Send]
changes to:
[Generating...]
or:
AI is thinking...
The user should not be able to accidentally send the same request repeatedly while the request is still processing.
If:
input = ""
then:
SEND
↓
DO NOTHING
The frontend should prevent unnecessary API calls.
The server should also validate the input.
Never depend only on frontend validation.
If the AI request fails:
AI REQUEST
↓
ERROR
↓
SHOW USER-FRIENDLY MESSAGE
Example:
Unable to generate a response right now.
Please try again.
The first test should be extremely simple:
User:
Hello
Expected:
ACAI:
Hello! How can I help you?
If this works:
✓ Frontend
✓ API
✓ AI Gateway
✓ Provider
are connected.
During development, log only useful diagnostic information.
For example:
request received
AI request started
AI request completed
Avoid logging:
API keys
Passwords
Private tokens
Sensitive user data
Generate an identifier for important requests.
Conceptually:
REQUEST
↓
requestId
↓
API
↓
AI Gateway
↓
LOG
If something fails, the request ID helps connect the frontend error with server logs.
An AI provider may become slow or unavailable.
Therefore:
REQUEST
↓
TIME LIMIT
↓
SUCCESS
or:
REQUEST
↓
TIME LIMIT EXCEEDED
↓
CONTROLLED ERROR
Never allow requests to hang indefinitely.
Temporary failures may sometimes be retried.
REQUEST
↓
TEMPORARY FAILURE?
├── NO → RETURN ERROR
└── YES
↓
RETRY
Do not blindly retry every error.
For example:
invalid API key
does not become fixed by repeated retries.
The application should have one central place for the selected model.
Conceptually:
MODEL = "selected-model"
Do not hard-code the model name across ten different files.
Later, the gateway can become:
AI Gateway
│
├── Provider A
├── Provider B
├── Provider C
└── Local Provider
The application calls:
generate()
rather than knowing the internal provider implementation.
If a local model server such as Ollama is later used:
ACAI
↓
AI GATEWAY
↓
LOCAL MODEL SERVER
↓
MODEL
This can be useful for development or privacy-sensitive workloads, but it requires appropriate local hardware and model availability.
Cloud architecture:
ACAI
↓
AI GATEWAY
↓
CLOUD PROVIDER
↓
MODEL
The provider's API key stays on the server.
Eventually ACAI can support:
AI GATEWAY
│
┌──────────┼──────────┐
▼ ▼ ▼
LOCAL CLOUD A CLOUD B
MODEL
Routing can depend on:
Task
Cost
Latency
Privacy
Availability
Model capability
After basic chat works, add persistence.
The database needs at least:
User
Conversation
Message
For the very first prototype, authentication can be temporarily omitted.
But production chat must associate data with an authenticated user.
The new flow becomes:
USER
↓
CHAT UI
↓
API
↓
SAVE USER MESSAGE
↓
AI GATEWAY
↓
MODEL
↓
SAVE AI MESSAGE
↓
RETURN
When the user returns:
LOGIN
↓
OPEN CHAT
↓
LOAD CONVERSATION
↓
DISPLAY HISTORY
The conversation is no longer lost when the browser refreshes.
Every private conversation should have an owner.
Conceptually:
conversation.userId
Then:
REQUEST
↓
AUTHENTICATED USER
↓
CONVERSATION USER ID
↓
MATCH?
├── YES → ALLOW
└── NO → DENY
The development sequence can be:
Prototype
↓
Basic chat
↓
Database
↓
Authentication
↓
Authorization
But before public production:
Authentication
+
Authorization
must be fully implemented.
At the end of this stage, the application should achieve:
[✓] Next.js runs
[✓] ACAI homepage
[✓] Chat page
[✓] Chat input
[✓] API endpoint
[✓] AI gateway
[✓] Provider connection
[✓] AI response
[✓] Loading state
[✓] Error handling
This is the first real milestone.
If the chat does not work, test in this exact order:
1. Is Next.js running?
↓
2. Does /chat open?
↓
3. Does Send trigger the API?
↓
4. Does /api/chat receive the request?
↓
5. Is the environment variable loaded?
↓
6. Can the server reach the provider?
↓
7. Does the provider return a response?
↓
8. Does the API return JSON?
↓
9. Does the UI display it?
Do not randomly change five files.
If PowerShell reports that scripts cannot run, the problem may be the PowerShell execution policy rather than npm itself.
The correct approach is to understand which shell is being used and adjust the development environment appropriately rather than repeatedly reinstalling Node.js.
The important distinction is:
Node.js installed
≠
PowerShell configured correctly
If npm reports:
ENOENT
package.json not found
check:
pwd
or the current directory shown by your terminal.
You must be inside:
ACAI
where:
package.json
exists.
If a package cannot be found:
No matching version found
do not blindly copy an old version number from another tutorial.
First determine:
Current package version
Current framework version
Compatible dependency version
Then install the compatible package.
If port 3000 is already occupied:
Port 3000
↓
already in use
you can either:
stop the existing process
or run the development server on another available port.
The important point is that the application itself is not necessarily broken.
Once the basic application works:
git init
Then create the first commit after ensuring secrets are excluded.
Typical structure:
CODE
↓
GIT
↓
COMMIT
Do not commit:
.env.local
API keys
private credentials
Create a checkpoint:
ACAI CHECKPOINT 01
Meaning:
Project runs
Homepage works
Chat page works
AI request works
If a later change breaks something, you know the last known working state.
The architecture has now moved from:
IDEA
to:
RUNNING NEXT.JS APP
and then:
RUNNING AI CHAT
The first real data flow is:
USER
↓
ACAI CHAT UI
↓
/api/chat
↓
AI GATEWAY
↓
MODEL PROVIDER
↓
RESPONSE
↓
ACAI CHAT UI
That is the foundation for everything else.
The next implementation stage should add persistent infrastructure:
DATABASE
+
AUTHENTICATION
+
CONVERSATION HISTORY
Then:
USER
↓
LOGIN
↓
DASHBOARD
↓
CHAT
↓
DATABASE
↓
AI
After that:
DOCUMENT UPLOAD
↓
STORAGE
↓
PROCESSING
↓
EMBEDDINGS
↓
VECTOR DATABASE
↓
RAG
Then:
AGENT
↓
TOOLS
↓
MEMORY
Finally:
IMAGE
VIDEO
WORKERS
QUEUE
BILLING
MONITORING
DEPLOYMENT
CHAPTER 26
First Working Chat
↓
CHAPTER 27
Database + Authentication
↓
CHAPTER 28
Conversation History + User Dashboard
↓
CHAPTER 29
File Upload + Storage
↓
CHAPTER 30
Document Processing
↓
CHAPTER 31
Embeddings + Vector Database
↓
CHAPTER 32
Complete RAG
↓
CHAPTER 33
Agent Architecture
↓
CHAPTER 34
Tool System
↓
CHAPTER 35
Memory
↓
CHAPTER 36
Image AI
↓
CHAPTER 37
Video AI
↓
CHAPTER 38
Background Jobs + Workers
↓
CHAPTER 39
Usage + Billing
↓
CHAPTER 40
Security Hardening
↓
CHAPTER 41
Testing
↓
CHAPTER 42
Production Deployment
↓
CHAPTER 43
Monitoring + Scaling
The key achievement of Chapter 26 is:
EMPTY FOLDER
↓
NEXT.JS
↓
ACAI UI
↓
CHAT PAGE
↓
API
↓
AI GATEWAY
↓
MODEL
↓
RESPONSE
This is the first real working vertical slice of ACAI.
Do not move to advanced agent, RAG, media generation, or large-scale infrastructure until this basic path is stable.
[✓] Project created
[✓] Next.js configured
[✓] TypeScript enabled
[✓] Tailwind enabled
[✓] Homepage created
[✓] Chat page created
[✓] API route planned
[✓] AI gateway created conceptually
[✓] Server-side secret handling
[✓] Input validation
[✓] Error handling
[✓] Loading state
[✓] Request logging
[✓] Request ID concept
[✓] Timeout concept
[✓] Provider abstraction
[✓] First AI response
[✓] Development checkpoint
[✓] Debugging workflow
The next chapter is:
The implementation will connect:
USER
↓
SIGN UP / LOGIN
↓
SESSION
↓
DASHBOARD
↓
CHAT
↓
DATABASE
↓
CONVERSATION
↓
MESSAGE HISTORY
After Chapter 27, ACAI will have a proper user/account foundation instead of being only a single-user prototype.
END OF CHAPTER 26
Comments
Post a Comment