ai-townVibe Coding GuideAI Prompt Library

AI Prompt Guide: The Execution Manual

Introduction

This document contains the detailed, phase-by-phase master prompts for building your AI simulation. It is the practical, hands-on counterpart to the strategic AI Replication Playbook. Each prompt herein is a comprehensive brief for a full stage of development, designed to empower your AI assistant to build a significant portion of the application in a single, coherent phase.

How to Use This Guide:

  1. Ensure you have completed the “Project Briefing” from the AI Replication Playbook.
  2. Proceed through the four phases below in order, starting with Phase 1.
  3. For each phase, copy the entire master prompt. This prompt contains the full context, architectural blueprint, and implementation plan for that stage.
  4. Give the complete prompt to your AI assistant. The AI will acknowledge the plan and begin executing the sub-tasks for that phase.
  5. Review the AI’s output for each sub-task and provide your approval to continue.

Phase 1: Backend Foundation & Simulation Engine

TitleContent
Phase GoalEstablish the serverless backend, define the database schema, and create the core simulation loop that advances time.
Core Functions- World, Player, and Engine data models.
- A basic game loop driven by a scheduled task.
- World state persistence.
Technical Points- Set up a new Convex project.
- Define database schemas in convex/schema.ts for worlds, players, and agents.
- Implement the Game class in convex/aiTown/game.ts with an empty tick method.
- Create the engine runner in convex/aiTown/main.ts to schedule and execute steps.
- Configure a cron job in convex/crons.ts to periodically call the engine runner.
Implementation Steps1. Initialize a Convex project (npx convex dev).
2. Define schemas for worlds, players, agents, conversations in convex/aiTown/schema.ts.
3. Implement the Game class structure in game.ts, inheriting from a basic AbstractGame.
4. Implement startEngine and runStep in main.ts to create a self-perpetuating loop using ctx.scheduler.
5. Set up a cron job in crons.ts to kickstart and maintain the engine.

AI Collaboration Development Prompt:

Prompt
As a full-stack AI developer, your task is to build the backend foundation for a 2D simulation game using Convex and TypeScript.
 
**Phase 1 Goal:** Create the core database schema and a persistent simulation engine.
 
**Detailed Requirements:**
 
1.  **Database Schema (`convex/aiTown/schema.ts`):**
    *   Define a `worlds` table to store the state of a single game world, including maps of players, agents, and conversations.
    *   Define `players` and `agents` tables (or schemas for serialization) that include position (`x`, `y`) and a unique ID.
    *   Define an `engines` table in `convex/engine/schema.ts` to track the running state of a simulation, including `running` (boolean) and `lastStepTs` (timestamp).
 
2.  **Abstract Game Engine (`convex/engine/abstractGame.ts`):**
    *   Create an `AbstractGame` class with an abstract `tick()` method and a concrete `runStep()` method that manages a loop of ticks.
 
3.  **Concrete Game Implementation (`convex/aiTown/game.ts`):**
    *   Create a `Game` class that extends `AbstractGame`.
    *   Implement the `tick()` method. For now, it can just log the current time. It should iterate through players and agents.
    *   Implement a static `load()` method to fetch all required data from the database for a given `worldId`.
    *   Implement a `saveDiff()` method to persist changes back to the database.
 
4.  **Engine Runner (`convex/aiTown/main.ts`):**
    *   Implement an `internalAction` named `runStep`. This function should:
        a. Load the game state using `Game.load()`.
        b. Run the simulation for a fixed duration by repeatedly calling `game.tick()`.
        c. Save the updated state using `Game.saveDiff()`.
        d. Schedule the next `runStep` to run immediately using `ctx.scheduler.runAfter(0, ...)`.
    *   Implement mutations `startEngine` and `stopEngine` to control the `running` flag in the `engines` table.
 
5.  **Cron Job (`convex/crons.ts`):**
    *   Set up a cron job that runs every minute to call a function like `restartDeadWorlds`, which would find any non-running engines and call `startEngine` on them.
 
Please generate the code for these files, ensuring they are type-safe and follow Convex best practices.

Related Code Files:

Functional RoleCode FileDescription
Master Schemaconvex/schema.tsDefines all database tables for the application.
Game Logic Schemaconvex/aiTown/schema.tsDefines tables specific to the game simulation.
Engine Coreconvex/aiTown/main.tsManages the execution lifecycle of the simulation loop.
Game State Managerconvex/aiTown/game.tsImplements the specific rules and logic for each simulation tick.
Scheduled Tasksconvex/crons.tsDefines periodic tasks, like starting the engine.

Phase 2: Frontend Rendering & Basic Interaction

TitleContent
Phase GoalBuild the visual frontend to render the game world and allow a human player to join and move their character.
Core Functions- Render the game map and characters from backend data.
- Connect to the Convex backend for real-time state updates.
- Implement click-to-move functionality.
Technical Points- Set up Vite, React, and TypeScript.
- Integrate Pixi.js for 2D rendering.
- Use useQuery from convex/react to get live data.
- Implement useSendInput hook to send movement commands to the backend.
Implementation Steps1. Initialize the frontend project with Vite.
2. Create the main App.tsx and Game.tsx components.
3. Implement ConvexClientProvider.tsx to connect to the backend.
4. Create the PixiGame.tsx component to set up the Pixi stage and viewport.
5. Implement Player.tsx and Character.tsx to render sprites.
6. Handle click events in PixiGame.tsx, convert coordinates, and use useSendInput to call the moveTo backend mutation.

AI Collaboration Development Prompt:

Prompt
As a frontend AI developer, your task is to build the user interface for the AI Town simulation using React, TypeScript, and Pixi.js. The backend is already set up on Convex.
 
**Phase 2 Goal:** Render the game world and enable a user to join and move their character.
 
**Detailed Requirements:**
 
1.  **Project Setup:**
    *   Use Vite with the React + TypeScript template.
    *   Install dependencies: `react`, `pixi.js`, `@pixi/react`, `convex`.
 
2.  **Backend Connection (`src/components/ConvexClientProvider.tsx`):**
    *   Create a provider component that initializes the `ConvexReactClient` and wraps the main `App` component.
 
3.  **Main UI Components (`src/App.tsx`, `src/components/Game.tsx`):**
    *   `App.tsx` should set up the main layout.
    *   `Game.tsx` should be the primary component for the game screen. It must:
        a. Use the `useQuery` hook from `convex/react` to fetch the current `worldState` from the backend.
        b. Pass this state down to the rendering component.
 
4.  **Rendering Engine (`src/components/PixiGame.tsx`):**
    *   This component will host the Pixi.js application.
    *   Use `@pixi/react` components like `<Stage>` and a custom `<PixiViewport>` for a zoomable/pannable camera.
    *   Render a static map based on map data from the `worldState`.
    *   Iterate through the `players` array from the `worldState` and render a `<Player>` component for each.
 
5.  **Character Rendering (`src/components/Player.tsx`, `src/components/Character.tsx`):**
    *   The `Player` component will receive a single player's data.
    *   The `Character` component will take position, texture, etc., and render an animated sprite using `AnimatedSprite` from `@pixi/react`.
 
6.  **Click-to-Move Interaction:**
    *   In `PixiGame.tsx`, add a pointer-up event handler to the map.
    *   Inside the handler, convert the screen click coordinates to world coordinates.
    *   Implement a custom hook `useSendInput('moveTo')` that calls the `moveTo` mutation on the Convex backend.
    *   Use this hook to send the destination coordinates to the backend when the user clicks the map.

Related Code Files:

Functional RoleCode FileDescription
Application Entrypointsrc/main.tsxInitializes the React app and connects to Convex.
Main Layoutsrc/App.tsxAssembles the primary UI components, including the game screen and buttons.
Game Screen Orchestratorsrc/components/Game.tsxFetches server data and manages the game view and side panel.
2D Renderersrc/components/PixiGame.tsxRenders the game world using Pixi.js and handles user input for movement.
Input Hooksrc/hooks/sendInput.tsProvides a clean way to send commands to the backend.

Phase 3: Generative Agents - Memory & Dialogue

TitleContent
Phase GoalImplement the core AI cognitive architecture, giving agents the ability to form memories and generate dialogue.
Core Functions- Memory creation from conversations (summarization, poignancy rating).
- Memory retrieval using vector search (RAG).
- Context-aware dialogue generation.
- Periodic memory reflection.
Technical Points- Integrate an LLM provider (e.g., OpenAI).
- Implement the memory lifecycle in convex/agent/memory.ts.
- Implement dialogue generation logic in convex/agent/conversation.ts.
- Set up vector search on the memoryEmbeddings table in the Convex schema.
Implementation Steps1. Create the convex/util/llm.ts wrapper for LLM API calls.
2. Implement rememberConversation in memory.ts, including LLM calls for summary and poignancy.
3. Implement fetchEmbedding and store embeddings in a new memoryEmbeddings table.
4. Configure a vector index on the embedding field in convex/agent/schema.ts.
5. Implement searchMemories to retrieve memories using vector search and rank them.
6. Implement the dialogue generation functions in conversation.ts, which use searchMemories to build a rich prompt.
7. Implement the reflectOnMemories logic.

AI Collaboration Development Prompt:

Prompt
As an AI backend developer, your task is to implement the cognitive architecture for generative agents in AI Town using Convex and an LLM provider like OpenAI.
 
**Phase 3 Goal:** Give agents memory and the ability to hold context-aware conversations.
 
**Detailed Requirements:**
 
1.  **LLM Utility (`convex/util/llm.ts`):**
    *   Create a centralized function `chatCompletion(prompt, messages, stop_words)` to call the LLM API.
    *   Create a function `fetchEmbedding(text)` to get vector embeddings for a piece of text.
    *   Use environment variables for the API key.
 
2.  **Memory Schema (`convex/agent/schema.ts`):**
    *   Define a `memories` table to store memory description, importance score, and type.
    *   Define a `memoryEmbeddings` table to store vector embeddings. It should have a `vectorIndex` configured on the `embedding` field.
 
3.  **Memory Creation (`convex/agent/memory.ts`):**
    *   Implement an `action` called `rememberConversation`. It should:
        a. Take a `conversationId` as input.
        b. Fetch the conversation log.
        c. Call the LLM to summarize the conversation from the agent's perspective.
        d. Call the LLM with a "judge" prompt to rate the summary's poignancy (0-9).
        e. Call `fetchEmbedding` on the summary text.
        f. Save the memory (description, importance) and the embedding to the database.
 
4.  **Memory Retrieval (RAG) (`convex/agent/memory.ts`):**
    *   Implement a query `searchMemories`. It should:
        a. Take a search query text and a player ID.
        b. Get the embedding for the search query.
        c. Use `ctx.db.vectorSearch()` on the `memoryEmbeddings` table to find the most similar memories.
        d. Implement a ranking algorithm that scores memories based on a weighted sum of relevance (from vector search), importance (stored score), and recency (time since last access).
        e. Return the top-ranked memories.
 
5.  **Dialogue Generation (`convex/agent/conversation.ts`):**
    *   Implement an `action` called `agentGenerateMessage`. It should:
        a. Take `playerId`, `otherPlayerId`, and `conversationId` as input.
        b. Fetch the agent's identity, plan, and conversation history.
        c. Call `searchMemories` to retrieve relevant memories.
        d. Construct a detailed system prompt including all this context.
        e. Call `chatCompletion` to get the dialogue.
        f. Return the generated text.

Related Code Files:

Functional RoleCode FileDescription
AI Memory Systemconvex/agent/memory.tsManages memory creation, storage, retrieval (RAG), and reflection.
AI Dialogue Engineconvex/agent/conversation.tsGathers context and uses an LLM to generate character dialogue.
LLM API Wrapperconvex/util/llm.tsA centralized client for communicating with external LLM services.
AI Data Modelsconvex/agent/schema.tsDefines the database tables for memories and vector embeddings.
Embedding Cacheconvex/agent/embeddingsCache.tsCaches embeddings to reduce cost and improve performance.

Phase 4: Agent Autonomy & Social Behavior

TitleContent
Phase GoalGrant agents autonomy to make their own decisions, wander the world, and initiate social interactions with each other.
Core Functions- Agent state machine (idle, walking, talking).
- Proactive decision-making (what to do next).
- Conversation lifecycle management (inviting, accepting, leaving).
Technical Points- Implement the agent’s tick() method in convex/aiTown/agent.ts.
- Create backend actions for agent decisions (agentDoSomething).
- Implement conversation state transitions in convex/aiTown/conversation.ts.
Implementation Steps1. Implement the Agent class in agent.ts with a tick() method.
2. Inside tick(), create a state machine: if idle, call agentDoSomething. If in a conversation, handle turn-taking.
3. Implement the agentDoSomething action in convex/agent/agentOperations.ts. This action should use an LLM to decide whether to wander or talk to someone.
4. Implement the full conversation lifecycle (invite, accept, reject, leave) in conversation.ts and its input handlers.
5. Integrate the agent’s decision-making with the movement system from Phase 1.

AI Collaboration Development Prompt:

Prompt
As an AI backend developer, your task is to implement the autonomous behavior for the AI agents in AI Town.
 
**Phase 4 Goal:** Make agents decide and act on their own, including moving and starting conversations.
 
**Detailed Requirements:**
 
1.  **Agent State Machine (`convex/aiTown unbelievability/agent.ts`):**
    *   Implement the `tick(game, now)` method for the `Agent` class.
    *   This method should function as a state machine. Use a property like `agent.inProgressOperation` to track long-running AI tasks.
    *   If the agent is idle (not moving, not in a conversation, no operation in progress), it should schedule an `agentDoSomething` operation.
    *   If the agent is in a conversation, it should handle the logic for its turn (e.g., if enough time has passed, schedule an `agentGenerateMessage` operation).
 
2.  **Autonomous Decision Making (`convex/agent/agentOperations.ts`):**
    *   Implement the `agentDoSomething` `internalAction`. This function should:
        a. Take the `agentId` as input.
        b. Find nearby players who are available for conversation.
        c. Use an LLM to make a high-level choice:
            i.  **Option 1: Start a conversation.** The LLM should choose a specific player to talk to. The action should return the `invitee` ID.
            ii. **Option 2: Wander.** The LLM should decide on a general activity. The action should return a destination point.
    *   The `finishDoSomething` input handler (in `agentInputs.ts`) should process the result: if an `invitee` is returned, it starts a conversation; if a `destination` is returned, it moves the player.
 
3.  **Conversation Lifecycle (`convex/aiTown/conversation.ts`):**
    *   Implement the full state transitions for a conversation.
    *   When an agent receives an invite, its `tick()` method should decide whether to `acceptInvite` or `rejectInvite`.
    *   When an invite is accepted, both agents should use the pathfinding system to walk towards each other.
    *   When they are close enough (`CONVERSATION_DISTANCE`), their state should change to `participating`.
 
Please generate the code for these modules, focusing on the agent's state management and the orchestration of AI-powered decisions.

Related Code Files:

Functional RoleCode FileDescription
Agent “Brain”convex/aiTown/agent.tsContains the Agent class and its core tick() method, which acts as the agent’s state machine.
Agent Operationsconvex/agent/agentOperations.tsContains the backend actions that use LLMs for high-level decision making.
Agent Inputsconvex/aiTown/agentInputs.tsHandles the results of agent operations, updating the game state.
Conversation Managerconvex/aiTown/conversation.tsManages the full lifecycle of a conversation, from invitation to conclusion.