|
| 1 | +# AgentState |
| 2 | + |
| 3 | +Conversation history database-as-a-service for AI agents. |
| 4 | + |
| 5 | +## Project Structure |
| 6 | + |
| 7 | +``` |
| 8 | +packages/ |
| 9 | + api/ Hono API on Cloudflare Workers + D1 |
| 10 | + shared/ Shared TypeScript types (public API contract) |
| 11 | + dashboard/ Next.js + Clerk + shadcn/ui (served as static assets by API Worker) |
| 12 | +docs/ Integration guides |
| 13 | +scripts/ Setup and deployment scripts |
| 14 | +``` |
| 15 | + |
| 16 | +See `docs/knowledge/core-memory.md` for durable maintenance notes. Do not create dated AI review reports; fold recurring findings into core memory and keep `docs/INDEX.md` updated. |
| 17 | + |
| 18 | +**Architecture**: Single Cloudflare Worker serves both the REST API (`/api/v1/*`) and dashboard static assets. |
| 19 | + |
| 20 | +## Dev Commands |
| 21 | + |
| 22 | +```bash |
| 23 | +# Install |
| 24 | +bun install |
| 25 | + |
| 26 | +# Local dev — API (port 8787) |
| 27 | +cd packages/api |
| 28 | +bunx wrangler dev |
| 29 | + |
| 30 | +# Local dev — Dashboard (port 3000, separate dev server) |
| 31 | +cd packages/dashboard |
| 32 | +bun run dev |
| 33 | + |
| 34 | +# Database |
| 35 | +bunx drizzle-kit generate # Generate migrations |
| 36 | +bunx wrangler d1 migrations apply agentstate-db --local # Apply locally |
| 37 | +bunx wrangler d1 execute agentstate-db --local --file=scripts/seed.sql # Seed |
| 38 | + |
| 39 | +# Lint + Format |
| 40 | +bunx biome check packages/api/src/ # Check |
| 41 | +bunx biome check --write packages/api/src/ # Auto-fix |
| 42 | + |
| 43 | +# Type check |
| 44 | +bunx tsc --noEmit -p packages/api/tsconfig.json |
| 45 | + |
| 46 | +# Test |
| 47 | +cd packages/api && bunx vitest run |
| 48 | + |
| 49 | +# Deploy |
| 50 | +bunx wrangler deploy -c packages/api/wrangler.jsonc |
| 51 | +``` |
| 52 | + |
| 53 | +## Architecture |
| 54 | + |
| 55 | +- **Package manager**: Bun |
| 56 | +- **Linter/Formatter**: Biome |
| 57 | +- **API Framework**: Hono (ultrafast, Workers-native) |
| 58 | +- **ORM**: Drizzle ORM (type-safe, D1/SQLite) |
| 59 | +- **Database**: Cloudflare D1 (SQLite at edge) |
| 60 | +- **Auth (API)**: Bearer token with SHA-256 hashed API keys |
| 61 | +- **Auth (Dashboard)**: Clerk (client-side keyless mode for dev; prod keys in `.env.production`) |
| 62 | +- **AI**: Workers AI for title generation and follow-up questions |
| 63 | +- **Deployment**: Single Cloudflare Worker serves both API and dashboard static assets |
| 64 | + |
| 65 | +## Conventions |
| 66 | + |
| 67 | +- **IDs**: nanoid (21 chars), auto-generated via Drizzle `$defaultFn` |
| 68 | +- **Timestamps**: Unix milliseconds (Date.now()), stored as INTEGER in SQLite |
| 69 | +- **API responses**: snake_case field names |
| 70 | +- **API keys**: Format `as_live_` + 40 base62 chars. Only SHA-256 hash stored. |
| 71 | +- **Metadata**: JSON serialized as TEXT column, parsed on read |
| 72 | +- **Pagination**: Cursor-based (never offset-based) |
| 73 | +- **Error format**: `{ error: { code: "MACHINE_CODE", message: "Human message" } }` |
| 74 | + |
| 75 | +## Key Files |
| 76 | + |
| 77 | +- `packages/api/src/db/schema.ts` — Single source of truth for DB schema |
| 78 | +- `packages/api/src/middleware/auth.ts` — API key auth (critical security path) |
| 79 | +- `packages/api/src/routes/conversations/` — Core CRUD operations |
| 80 | +- `packages/api/src/routes/projects.ts` — Project and key management routes |
| 81 | +- `packages/api/drizzle/` — Generated SQL migrations (committed to git) |
| 82 | + |
| 83 | +## Testing |
| 84 | + |
| 85 | +```bash |
| 86 | +# Run all tests |
| 87 | +cd packages/api && bunx vitest run |
| 88 | + |
| 89 | +# Local test API key (from seed.sql): |
| 90 | +as_live_TEST_KEY_FOR_LOCAL_DEV_ONLY_1234567890ab |
| 91 | + |
| 92 | +# Health check |
| 93 | +curl http://localhost:8787/ |
| 94 | + |
| 95 | +# Create conversation |
| 96 | +curl -X POST http://localhost:8787/v1/conversations \ |
| 97 | + -H "Authorization: Bearer as_live_TEST_KEY_FOR_LOCAL_DEV_ONLY_1234567890ab" \ |
| 98 | + -H "Content-Type: application/json" \ |
| 99 | + -d '{"messages":[{"role":"user","content":"Hello"}]}' |
| 100 | +``` |
| 101 | + |
| 102 | +## Git Conventions |
| 103 | + |
| 104 | +- **Semantic commits**: `feat:`, `fix:`, `chore:`, `docs:`, `refactor:`, `test:` |
| 105 | +- **Co-authors** (always include both): |
| 106 | + |
| 107 | +``` |
| 108 | +Co-Authored-By: Duyet Le <me@duyet.net> |
| 109 | +Co-Authored-By: duyetbot <bot@duyet.net> |
| 110 | +``` |
| 111 | + |
| 112 | +## Autonomous Maintenance |
| 113 | + |
| 114 | +See **[PLAN.md](PLAN.md)** for the full maintenance playbook, quality benchmarks, scenario tables, and backlog. Keep PLAN.md up to date after every iteration. |
| 115 | + |
| 116 | +### Continuous Improvement Loop (every 15 min via cron) |
| 117 | + |
| 118 | +When the cron fires, run this full cycle: |
| 119 | + |
| 120 | +#### Phase 0 — Benchmark |
| 121 | +Run all quality checks in parallel. If any regress, fix before proceeding. |
| 122 | +```bash |
| 123 | +bunx biome check packages/api/src/ |
| 124 | +bunx tsc --noEmit -p packages/api/tsconfig.json |
| 125 | +cd packages/api && bunx vitest run |
| 126 | +cd packages/dashboard && bunx tsc --noEmit |
| 127 | +git status --short |
| 128 | +``` |
| 129 | + |
| 130 | +#### Phase 1 — Plan & Brainstorm |
| 131 | +1. **Read current state**: Check PLAN.md backlog, memory files, recent git log |
| 132 | +2. **Brainstorm**: What has the biggest impact right now? |
| 133 | + - Next unchecked backlog item ready to build |
| 134 | + - Quality gaps (test coverage, type safety, accessibility) |
| 135 | + - Architectural improvements that unlock future work |
| 136 | + - Dashboard UX gaps users would expect |
| 137 | + - Developer experience — docs accuracy, SDK completeness |
| 138 | +3. **Plan**: For non-trivial items, use Agent (Plan) to design approach. Write plan to memory if it spans multiple iterations. |
| 139 | +4. **Prioritize**: Pick 2-4 concrete tasks for parallel execution without file conflicts. Prefer high-impact items. |
| 140 | + |
| 141 | +#### Phase 2 — Execute (batch parallel agents) |
| 142 | +Spawn 2-4 parallel agents in a single message (`run_in_background: true`): |
| 143 | +- Use PLAN.md scenario tables as menu |
| 144 | +- Mix categories: feature + quality + dashboard + docs |
| 145 | +- Agents touch **different files** — no conflicts |
| 146 | +- senior-engineer for features/refactors, junior-engineer for lint/format/dead-code, code-reviewer for audits |
| 147 | + |
| 148 | +#### Phase 3 — Collect & Verify |
| 149 | +1. Review agent outputs — reject anything that breaks existing behavior |
| 150 | +2. Full quality suite: lint → typecheck → test |
| 151 | +3. If anything fails, fix before continuing |
| 152 | + |
| 153 | +#### Phase 4 — PR, Monitor, Merge & Deploy |
| 154 | +1. Create feature branch: `git checkout -b claude/improvement-<timestamp>` |
| 155 | +2. Commit each logical change separately with semantic messages + co-authors |
| 156 | +3. Push branch and create PR: `gh pr create --title "..." --body "..."` |
| 157 | +4. Monitor CI: poll `gh pr checks <pr-number>` until all checks pass or fail |
| 158 | +5. If CI passes → merge: `gh pr merge <pr-number> --squash --delete-branch` |
| 159 | +6. If CI fails → fix issues, push again, re-check |
| 160 | +7. After merge → deploy: `bunx wrangler deploy -c packages/api/wrangler.jsonc` |
| 161 | +8. Verify: `curl -s -o /dev/null -w '%{http_code}' https://agentstate.app/api` |
| 162 | +9. Update memory benchmark scores + PLAN.md |
| 163 | + |
| 164 | +#### Phase 5 — Reflect |
| 165 | +- What was accomplished? |
| 166 | +- What should the NEXT iteration focus on? |
| 167 | +- Any blockers or decisions needing user input? |
| 168 | +- Save insights to memory for continuity |
| 169 | + |
| 170 | +#### Rules |
| 171 | +- Always use PR workflow — never push directly to main |
| 172 | +- One commit per logical change |
| 173 | +- Skip iteration if working tree is dirty from user work |
| 174 | +- Save progress to memory after each run |
| 175 | +- Don't repeat work from previous iterations — check memory first |
| 176 | +- If a brainstormed idea is too large, break into phases and save plan to memory |
| 177 | + |
| 178 | +## Deployment |
| 179 | + |
| 180 | +```bash |
| 181 | +# 1. Fill in credentials |
| 182 | +cp .env.example .env.local |
| 183 | +# Edit .env.local with your Cloudflare credentials |
| 184 | + |
| 185 | +# 2. Set GitHub secrets |
| 186 | +./scripts/setup-secrets.sh |
| 187 | + |
| 188 | +# 3. Create D1 database |
| 189 | +bunx wrangler d1 create agentstate-db |
| 190 | +# Update database_id in packages/api/wrangler.jsonc |
| 191 | + |
| 192 | +# 4. Push to main → auto-deploys via GitHub Actions |
| 193 | +git push origin main |
| 194 | +``` |
0 commit comments