End-to-end flow for the in-house agent: admin configuration, vector store handling, system instructions, n8n webhooks, and the external API.
1. Admin Configuration
In-House tab settings: system instructions, RAG documents, images, n8n webhooks, include public RAG toggle, and API key generation.
flowchart TD
A1["System Instructions"] --> B1["in-house/system-prompt"]
B1 --> C1["tenant_configs.inHouseSystemPrompt"]
A2["RAG Documents"] --> B2["in-house/files/upload"]
B2 --> C2["in_house_files + shared vector store"]
A3["Images"] --> B3["in-house/images/upload"]
B3 --> C3["Same vector store as RAG"]
A4["n8n Webhooks"] --> B4["in-house/webhooks"]
B4 --> C4["in_house_webhook_tools"]
A5["Include Public RAG"] --> B5["in-house/include-public-rag"]
B5 --> C5["tenant_configs.inHouseIncludePublicRAG"]
A6["API Keys"] --> B6["in-house/api-keys"]
B6 --> C6["in_house_api_keys"]
2. Vector Store Handling
In-house RAG and images share one vector store. Public stores are optional. OpenAI limits file search to 2 vector stores; we cap accordingly.
flowchart TD
IH1["in_house_files: RAG docs"] --> VS["in-house-tenantId-knowledge-base"]
IH2["in_house_images: metadata JSON"] --> VS
P1["tenant_files (public)"] --> VS2["public vector stores"]
P2["tenant_images (public)"] --> VS3["public image store"]
VS --> L1["Load in-house store ID"]
VS2 --> L2["If inHouseIncludePublicRAG"]
VS3 --> L2
L1 --> CAP["Cap at 2 stores total"]
L2 --> CAP
CAP --> T["fileSearchTool(vectorStoreIds)"]
3. System Message Flow
How inHouseSystemPrompt is resolved at chat time.
flowchart TD
A["Chat / API request"] --> B["prepareInHouseChatCompletion"]
B --> C["getTenantConfig(tenantId)"]
C --> D{"inHouseSystemPrompt set?"}
D -->|Yes| E["Use inHouseSystemPrompt"]
D -->|No| F["Use default: helpful internal AI assistant..."]
E --> G["Agent instructions = system prompt"]
F --> G
G --> H["+ RAG/knowledge base instructions"]
H --> I["+ Webhook tool descriptions if any"]
4. n8n Webhook Flow
Webhooks configured in admin become agent tools. Calls are logged to in_house_webhook_logs.
flowchart TD
W1["Admin: Add webhook (URL, method, params)"] --> W2["in_house_webhook_tools"]
W2 --> A1["prepareInHouseChatCompletion: load webhooks"]
A1 --> A2["buildZodSchema per webhook"]
A2 --> A3["tool(execute: executeWebhook, inHouse: true)"]
A3 --> A4["Attach to Agent"]
A4 --> R1["When agent calls: executeWebhook"]
R1 --> R2["Log to in_house_webhook_logs"]
R2 --> R3["HTTP request to n8n"]
R3 --> R4["Return result to agent"]
5. In-House Agent (prepareInHouseChatCompletion)
How the agent is built: config, vector stores, tools, and conversation.
flowchart TD
A["prepareInHouseChatCompletion(tenantId, messages, conversationId)"] --> B["getTenantConfig"]
B --> C["inHouseSystemPrompt or default"]
C --> D["Load in_house_files vectorStoreId"]
D --> E["Load in_house_images vectorStoreId (same store)"]
E --> F{"inHouseIncludePublicRAG?"}
F -->|Yes| G["Add tenant_files + tenant_images stores"]
F -->|No| H["In-house only"]
G --> I["Cap at 2 vector stores"]
H --> I
I --> J["fileSearchTool(vectorStoreIds)"]
J --> K["Load in_house_webhook_tools"]
K --> L["Attach webhook tools (executeWebhook, inHouse: true)"]
L --> M["Create Agent: gpt-4o + instructions + tools"]
M --> N["Create/reuse conversationId (OpenAI Conversations)"]
N --> O["run(agent, question, stream, conversationId)"]
6. External API Flow
POST /api/in-house/chat/[tenantId] with API key. Stream or non-stream response.
flowchart TD
A["POST /api/in-house/chat/tenantId"] --> B["Extract API key (Bearer or x-api-key)"]
B --> C["verifyInHouseApiKey -> tenantId match"]
C --> D{Valid?}
D -->|No| E["401 Unauthorized"]
D -->|Yes| F["Parse: message/messages, conversationId, stream, steps"]
F --> G{stream=true?}
G -->|Yes| H["streamInHouseChatCompletion"]
G -->|No| I["getInHouseChatCompletion"]
H --> J["Return SSE + X-Conversation-Id"]
I --> K["Return JSON response, conversationId"]