push both
Browse files- src/docs/swagger.ts +59 -0
- src/routes/chatRoutes.ts +211 -2
src/docs/swagger.ts
CHANGED
|
@@ -139,6 +139,65 @@ export const swaggerSpec = swaggerJSDoc({
|
|
| 139 |
},
|
| 140 |
},
|
| 141 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
},
|
| 143 |
},
|
| 144 |
security: [{ bearerAuth: [] }],
|
|
|
|
| 139 |
},
|
| 140 |
},
|
| 141 |
},
|
| 142 |
+
ChatMessage: {
|
| 143 |
+
type: 'object',
|
| 144 |
+
properties: {
|
| 145 |
+
id: {
|
| 146 |
+
type: 'string',
|
| 147 |
+
format: 'uuid',
|
| 148 |
+
},
|
| 149 |
+
role: {
|
| 150 |
+
type: 'string',
|
| 151 |
+
enum: ['user', 'assistant', 'system'],
|
| 152 |
+
description: 'Message role (user = user message, assistant = agent response, system = system message)',
|
| 153 |
+
},
|
| 154 |
+
content: {
|
| 155 |
+
type: 'string',
|
| 156 |
+
description: 'Message content',
|
| 157 |
+
},
|
| 158 |
+
agentId: {
|
| 159 |
+
type: 'string',
|
| 160 |
+
format: 'uuid',
|
| 161 |
+
},
|
| 162 |
+
userId: {
|
| 163 |
+
type: 'string',
|
| 164 |
+
format: 'uuid',
|
| 165 |
+
nullable: true,
|
| 166 |
+
},
|
| 167 |
+
conversationId: {
|
| 168 |
+
type: 'string',
|
| 169 |
+
nullable: true,
|
| 170 |
+
description: 'Conversation ID this message belongs to',
|
| 171 |
+
},
|
| 172 |
+
metadata: {
|
| 173 |
+
type: 'object',
|
| 174 |
+
description: 'Additional message metadata (files, IPFS hashes, etc.)',
|
| 175 |
+
additionalProperties: true,
|
| 176 |
+
},
|
| 177 |
+
createdAt: {
|
| 178 |
+
type: 'string',
|
| 179 |
+
format: 'date-time',
|
| 180 |
+
},
|
| 181 |
+
},
|
| 182 |
+
},
|
| 183 |
+
ChatResponse: {
|
| 184 |
+
type: 'object',
|
| 185 |
+
properties: {
|
| 186 |
+
response: {
|
| 187 |
+
type: 'string',
|
| 188 |
+
description: 'Agent\'s response message',
|
| 189 |
+
},
|
| 190 |
+
conversationId: {
|
| 191 |
+
type: 'string',
|
| 192 |
+
description: 'Conversation ID for this chat session',
|
| 193 |
+
},
|
| 194 |
+
metadata: {
|
| 195 |
+
type: 'object',
|
| 196 |
+
description: 'Additional response metadata from agent',
|
| 197 |
+
additionalProperties: true,
|
| 198 |
+
},
|
| 199 |
+
},
|
| 200 |
+
},
|
| 201 |
},
|
| 202 |
},
|
| 203 |
security: [{ bearerAuth: [] }],
|
src/routes/chatRoutes.ts
CHANGED
|
@@ -6,6 +6,13 @@ import { authenticate, optionalAuth } from '../middlewares/auth';
|
|
| 6 |
const router = Router();
|
| 7 |
const chatController = new ChatController();
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
// Configure multer for file uploads
|
| 10 |
// Accept all file types (documents, images, code files, etc.)
|
| 11 |
const upload = multer({
|
|
@@ -25,8 +32,131 @@ const upload = multer({
|
|
| 25 |
},
|
| 26 |
});
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
router.get(
|
| 38 |
'/:id/history',
|
| 39 |
authenticate,
|
|
|
|
| 6 |
const router = Router();
|
| 7 |
const chatController = new ChatController();
|
| 8 |
|
| 9 |
+
/**
|
| 10 |
+
* @swagger
|
| 11 |
+
* tags:
|
| 12 |
+
* name: Chat
|
| 13 |
+
* description: Chat protocol endpoints for communicating with agents
|
| 14 |
+
*/
|
| 15 |
+
|
| 16 |
// Configure multer for file uploads
|
| 17 |
// Accept all file types (documents, images, code files, etc.)
|
| 18 |
const upload = multer({
|
|
|
|
| 32 |
},
|
| 33 |
});
|
| 34 |
|
| 35 |
+
/**
|
| 36 |
+
* @swagger
|
| 37 |
+
* /chat/{id}/message:
|
| 38 |
+
* post:
|
| 39 |
+
* summary: Send a message to an agent
|
| 40 |
+
* tags: [Chat]
|
| 41 |
+
* description: |
|
| 42 |
+
* Send a text message and/or files to an agent. The system handles communication with any agent endpoint format.
|
| 43 |
+
*
|
| 44 |
+
* **Agent Communication Protocol:**
|
| 45 |
+
* - Files are uploaded to IPFS first, then URLs are sent to the agent endpoint
|
| 46 |
+
* - For text-only messages: Sends JSON payload to agent endpoint
|
| 47 |
+
* - For messages with files: Sends multipart/form-data with file URLs (IPFS hashes)
|
| 48 |
+
* - Supports custom headers via agent metadata (agent.metadata.headers)
|
| 49 |
+
* - Works with any agent endpoint format (REST, GraphQL, custom APIs)
|
| 50 |
+
*
|
| 51 |
+
* **Payment:**
|
| 52 |
+
* - First 2 tasks per user are free
|
| 53 |
+
* - Admins can test unapproved agents without payment
|
| 54 |
+
* - Regular users need sufficient wallet balance for approved agents
|
| 55 |
+
* security:
|
| 56 |
+
* - bearerAuth: []
|
| 57 |
+
* parameters:
|
| 58 |
+
* - in: path
|
| 59 |
+
* name: id
|
| 60 |
+
* required: true
|
| 61 |
+
* schema:
|
| 62 |
+
* type: string
|
| 63 |
+
* format: uuid
|
| 64 |
+
* description: Agent ID
|
| 65 |
+
* requestBody:
|
| 66 |
+
* required: true
|
| 67 |
+
* content:
|
| 68 |
+
* multipart/form-data:
|
| 69 |
+
* schema:
|
| 70 |
+
* type: object
|
| 71 |
+
* properties:
|
| 72 |
+
* message:
|
| 73 |
+
* type: string
|
| 74 |
+
* description: Text message (optional if files provided)
|
| 75 |
+
* example: "Hello, can you help me analyze this code?"
|
| 76 |
+
* conversationId:
|
| 77 |
+
* type: string
|
| 78 |
+
* description: Conversation ID for continuing a conversation (optional)
|
| 79 |
+
* example: "conv_1234567890_abc123"
|
| 80 |
+
* metadata:
|
| 81 |
+
* type: string
|
| 82 |
+
* description: JSON string of additional metadata (optional)
|
| 83 |
+
* example: '{"context": "code review", "language": "typescript"}'
|
| 84 |
+
* files:
|
| 85 |
+
* type: array
|
| 86 |
+
* items:
|
| 87 |
+
* type: string
|
| 88 |
+
* format: binary
|
| 89 |
+
* description: Files to upload (images, documents, code files, etc.). Max 10 files, 10MB each.
|
| 90 |
+
* responses:
|
| 91 |
+
* 200:
|
| 92 |
+
* description: Message sent successfully, agent response returned
|
| 93 |
+
* content:
|
| 94 |
+
* application/json:
|
| 95 |
+
* schema:
|
| 96 |
+
* type: object
|
| 97 |
+
* properties:
|
| 98 |
+
* response:
|
| 99 |
+
* type: string
|
| 100 |
+
* description: Agent's response message
|
| 101 |
+
* example: "I'd be happy to help you analyze the code!"
|
| 102 |
+
* conversationId:
|
| 103 |
+
* type: string
|
| 104 |
+
* description: Conversation ID for this chat session
|
| 105 |
+
* example: "conv_1234567890_abc123"
|
| 106 |
+
* metadata:
|
| 107 |
+
* type: object
|
| 108 |
+
* description: Additional response metadata from agent
|
| 109 |
+
* additionalProperties: true
|
| 110 |
+
* 400:
|
| 111 |
+
* description: Invalid request (missing message and files)
|
| 112 |
+
* content:
|
| 113 |
+
* application/json:
|
| 114 |
+
* schema:
|
| 115 |
+
* type: object
|
| 116 |
+
* properties:
|
| 117 |
+
* error:
|
| 118 |
+
* type: string
|
| 119 |
+
* example: "Message or files are required"
|
| 120 |
+
* 401:
|
| 121 |
+
* description: Unauthorized (for agents requiring authentication)
|
| 122 |
+
* 402:
|
| 123 |
+
* description: Insufficient wallet balance
|
| 124 |
+
* content:
|
| 125 |
+
* application/json:
|
| 126 |
+
* schema:
|
| 127 |
+
* type: object
|
| 128 |
+
* properties:
|
| 129 |
+
* error:
|
| 130 |
+
* type: string
|
| 131 |
+
* example: "Insufficient wallet balance"
|
| 132 |
+
* details:
|
| 133 |
+
* type: object
|
| 134 |
+
* properties:
|
| 135 |
+
* balance:
|
| 136 |
+
* type: number
|
| 137 |
+
* description: Current wallet balance in points
|
| 138 |
+
* required:
|
| 139 |
+
* type: number
|
| 140 |
+
* description: Required points for this task
|
| 141 |
+
* freeTasksRemaining:
|
| 142 |
+
* type: number
|
| 143 |
+
* description: Number of free tasks remaining
|
| 144 |
+
* message:
|
| 145 |
+
* type: string
|
| 146 |
+
* description: Human-readable message
|
| 147 |
+
* 404:
|
| 148 |
+
* description: Agent not found
|
| 149 |
+
* 500:
|
| 150 |
+
* description: Server error or agent endpoint error
|
| 151 |
+
* content:
|
| 152 |
+
* application/json:
|
| 153 |
+
* schema:
|
| 154 |
+
* type: object
|
| 155 |
+
* properties:
|
| 156 |
+
* error:
|
| 157 |
+
* type: string
|
| 158 |
+
* example: "Failed to send message to agent"
|
| 159 |
+
*/
|
| 160 |
router.post(
|
| 161 |
'/:id/message',
|
| 162 |
optionalAuth,
|
|
|
|
| 164 |
chatController.sendMessage.bind(chatController)
|
| 165 |
);
|
| 166 |
|
| 167 |
+
/**
|
| 168 |
+
* @swagger
|
| 169 |
+
* /chat/{id}/history:
|
| 170 |
+
* get:
|
| 171 |
+
* summary: Get conversation history with an agent
|
| 172 |
+
* tags: [Chat]
|
| 173 |
+
* description: |
|
| 174 |
+
* Retrieve conversation history between the authenticated user and an agent.
|
| 175 |
+
* Admins can view history for any agent (including pending/rejected), while regular users can only view history for approved agents.
|
| 176 |
+
* security:
|
| 177 |
+
* - bearerAuth: []
|
| 178 |
+
* parameters:
|
| 179 |
+
* - in: path
|
| 180 |
+
* name: id
|
| 181 |
+
* required: true
|
| 182 |
+
* schema:
|
| 183 |
+
* type: string
|
| 184 |
+
* format: uuid
|
| 185 |
+
* description: Agent ID
|
| 186 |
+
* - in: query
|
| 187 |
+
* name: conversationId
|
| 188 |
+
* schema:
|
| 189 |
+
* type: string
|
| 190 |
+
* description: Filter by specific conversation ID
|
| 191 |
+
* example: "conv_1234567890_abc123"
|
| 192 |
+
* - in: query
|
| 193 |
+
* name: limit
|
| 194 |
+
* schema:
|
| 195 |
+
* type: integer
|
| 196 |
+
* default: 50
|
| 197 |
+
* minimum: 1
|
| 198 |
+
* maximum: 100
|
| 199 |
+
* description: Maximum number of messages to return
|
| 200 |
+
* responses:
|
| 201 |
+
* 200:
|
| 202 |
+
* description: Conversation history retrieved successfully
|
| 203 |
+
* content:
|
| 204 |
+
* application/json:
|
| 205 |
+
* schema:
|
| 206 |
+
* type: object
|
| 207 |
+
* properties:
|
| 208 |
+
* messages:
|
| 209 |
+
* type: array
|
| 210 |
+
* items:
|
| 211 |
+
* type: object
|
| 212 |
+
* properties:
|
| 213 |
+
* id:
|
| 214 |
+
* type: string
|
| 215 |
+
* format: uuid
|
| 216 |
+
* role:
|
| 217 |
+
* type: string
|
| 218 |
+
* enum: [user, assistant, system]
|
| 219 |
+
* description: Message role (user = user message, assistant = agent response)
|
| 220 |
+
* content:
|
| 221 |
+
* type: string
|
| 222 |
+
* description: Message content
|
| 223 |
+
* agentId:
|
| 224 |
+
* type: string
|
| 225 |
+
* format: uuid
|
| 226 |
+
* userId:
|
| 227 |
+
* type: string
|
| 228 |
+
* format: uuid
|
| 229 |
+
* conversationId:
|
| 230 |
+
* type: string
|
| 231 |
+
* description: Conversation ID this message belongs to
|
| 232 |
+
* metadata:
|
| 233 |
+
* type: object
|
| 234 |
+
* description: Additional message metadata (files, etc.)
|
| 235 |
+
* additionalProperties: true
|
| 236 |
+
* createdAt:
|
| 237 |
+
* type: string
|
| 238 |
+
* format: date-time
|
| 239 |
+
* 401:
|
| 240 |
+
* description: Unauthorized - authentication required
|
| 241 |
+
* 404:
|
| 242 |
+
* description: Agent not found
|
| 243 |
+
* 500:
|
| 244 |
+
* description: Server error
|
| 245 |
+
*/
|
| 246 |
router.get(
|
| 247 |
'/:id/history',
|
| 248 |
authenticate,
|