Overview
Gorkie follows a modular architecture organized by feature and layer. The main application code lives in theserver/ directory.
Entry Point
server/index.ts
The application entry point sets up:- OpenTelemetry for observability (using Langfuse)
- Error handlers for unhandled rejections and exceptions
- Slack Bolt app initialization
- Background services (sandbox janitor, scheduled task runner)
server/index.ts
Configuration
server/env.ts
Environment variables are validated using@t3-oss/env-core with Zod schemas:
server/env.ts
server/config.ts
Application constants and configuration:server/config.ts
Library Layer (server/lib/)
Core application logic organized by feature.AI Layer (server/lib/ai/)
AI-related functionality using Vercel AI SDK:Prompts
Prompts are modular and composable:- chat/core.ts - Core system instructions
- chat/personality.ts - Bot personality and tone
- chat/tools.ts - How to use tools effectively
- chat/examples.ts - Example interactions for few-shot learning
Tools
AI tools follow two patterns (see Adding Tools):- Context-aware tools - Use factory pattern, receive
contextandstream - Stateless tools - Simple exports, no context needed
Providers
AI model configuration using Vercel AI SDK:server/lib/ai/providers.ts
Slack Layer (server/slack/)
Slack integration using Bolt SDK:Database Layer (server/db/)
Database schema and queries using Drizzle ORM:Schema Definition
server/db/schema.ts
Other Libraries
- server/lib/allowed-users.ts - User permission caching
- server/lib/kv.ts - Redis client and rate limiting
- server/lib/logger.ts - Pino logger configuration
- server/lib/sandbox/ - E2B code sandbox management
- server/lib/tasks/ - Scheduled task runner
- server/lib/validators/ - Input validation schemas
Types (server/types/)
TypeScript type definitions organized by domain:Common Types
Utils (server/utils/)
Utility functions for common operations:- context.ts - Context ID generation and parsing
- error.ts - Error handling utilities
- messages.ts - Message formatting
- users.ts - User info fetching
- triggers.ts - Message trigger detection
- slack.ts - Slack API helpers
- images.ts - Image handling
- text.ts - Text processing
Key Design Patterns
Dependency Injection
Context and dependencies are passed explicitly:Factory Pattern
Tools and agents use factories to inject dependencies:Structured Logging
All logs include structured context:Type Safety
Strict TypeScript configuration with runtime validation:- Compile-time type checking
- Runtime schema validation (Zod)
- Type-safe database queries (Drizzle)
Configuration Files
Root Level
- package.json - Dependencies and scripts
- tsconfig.json - TypeScript configuration
- biome.jsonc - Biome formatter/linter config
- drizzle.config.ts - Drizzle ORM configuration
- lefthook.yml - Git hooks configuration
- .env.example - Example environment variables
Development
- .editorconfig - Editor configuration
- .cspell.jsonc - Spell checker configuration
- commitlint.config.ts - Commit message linting
Understanding the Flow
Message Processing Flow
- Slack event received →
server/slack/events/message-create/ - Event validated → Check permissions, triggers
- Context created →
SlackMessageContextwith event data - AI orchestrator called →
server/lib/ai/agents/orchestrator.ts - Tools registered → Context-aware tools created
- AI generates response → Uses tools to interact with Slack
- Stream updates → Real-time status updates to user
- Response complete → Final message sent to Slack
Tool Execution Flow
- AI decides to use tool → Based on user message and available tools
- Input validated → Zod schema validation
- Task created → Status tracking started
- Tool executes → Performs operation (API call, database query, etc.)
- Task updated → Progress and results tracked
- Result returned → Structured response to AI
- AI continues → Uses result to formulate next action
Next Steps
- Learn how to add new AI tools
- Review code style guidelines
- Set up your development environment