Lightweight chat service implementing a multi-agent system with tool calling using Workers AI. Provides chat session management, message history via D1, and an agentic loop that can search products, query interactions, and retrieve patterns. Also exposes A2A (Agent-to-Agent) protocol endpoints and an agent card.
Worker name: crow-core-chat-service
Domain (prod): internal.chat.crowai.dev
Domain (dev): dev.internal.chat.crowai.dev
Schema
chat_session
| Column | Type | Notes |
|---|
| id | text PK | |
| organization_id | text | |
| user_id | text | |
| created_at | integer | epoch ms |
| updated_at | integer | epoch ms |
chat_message
| Column | Type | Notes |
|---|
| id | text PK | |
| session_id | text FK | references chat_session.id |
| role | text | user, assistant, tool |
| content | text | |
| created_at | integer | epoch ms |
Routes
| Method | Path | Description |
|---|
| POST | /api/v1/chat/sessions | Create a new chat session |
| POST | /api/v1/chat/sessions/{sessionId}/messages | Send a message (triggers agentic loop) |
| GET | /api/v1/chat/sessions/{sessionId}/messages | Get message history for session |
| GET | /api/v1/chat/sessions/organization/{orgId} | List sessions for org |
| DELETE | /api/v1/chat/sessions/{sessionId} | Delete a session and its messages |
| GET | /.well-known/agent.json | A2A agent card |
| POST | /a2a | A2A protocol endpoint |
Agentic Loop
The service runs an agentic loop (max 5 iterations) using @cf/meta/llama-3.3-70b-instruct-fp8-fast with three tools:
| Tool | Description | Calls |
|---|
search_products | Semantic product search | Gateway /api/v1/products/search |
get_interactions | Query interactions by org | Gateway /api/v1/interactions/organization/{orgId} |
get_patterns | Retrieve pattern analysis | Gateway /api/v1/patterns/organization/{orgId} |
Each iteration: send message + tool definitions to LLM, check if LLM wants to call a tool, execute the tool, feed result back. Loop terminates when the LLM produces a final response without tool calls or after 5 iterations.
Environment Variables
| Variable | Example |
|---|
| ENVIRONMENT | dev |
| API_GATEWAY_URL | https://dev.api.crowai.dev |
Secrets
| Secret | Purpose |
|---|
| INTERNAL_GATEWAY_KEY | Gateway trust validation |
Bindings
| Binding | Type | Name |
|---|
| DB | D1 | crow-core-chat-service-db |
| AI | Workers AI | LLM inference (@cf/meta/llama-3.3-70b-instruct-fp8-fast) |
Dependencies
- Inbound: gateway (dashboard chat interface)
- Outbound: gateway API (products, interactions, patterns via tool calls)
Key Behaviors
- INTERNAL_GATEWAY_KEY guard: All
/api/v1/* routes require the shared internal key
- BOLA: Session operations check
X-Organization-Id against organization_id
- A2A protocol: Implements the Agent-to-Agent protocol for inter-agent communication
- Tool calling: LLM decides which tools to invoke based on the user's question context