| 
									
										
										
										
											2023-06-09 14:24:59 +08:00
										 |  |  | import axios from "axios"; | 
					
						
							|  |  |  | export const BASE_URL = "https://api.dify.ai/v1"; | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-16 09:22:47 +08:00
										 |  |  | export const routes = { | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |   application: { | 
					
						
							| 
									
										
										
										
											2023-06-09 14:24:59 +08:00
										 |  |  |     method: "GET", | 
					
						
							|  |  |  |     url: () => `/parameters`, | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |   }, | 
					
						
							|  |  |  |   feedback: { | 
					
						
							| 
									
										
										
										
											2023-06-09 14:24:59 +08:00
										 |  |  |     method: "POST", | 
					
						
							|  |  |  |     url: (message_id) => `/messages/${message_id}/feedbacks`, | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |   }, | 
					
						
							|  |  |  |   createCompletionMessage: { | 
					
						
							| 
									
										
										
										
											2023-06-09 14:24:59 +08:00
										 |  |  |     method: "POST", | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     url: () => `/completion-messages`, | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  |   createChatMessage: { | 
					
						
							| 
									
										
										
										
											2023-06-09 14:24:59 +08:00
										 |  |  |     method: "POST", | 
					
						
							| 
									
										
										
										
											2023-05-15 16:57:05 +08:00
										 |  |  |     url: () => `/chat-messages`, | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |   }, | 
					
						
							|  |  |  |   getConversationMessages: { | 
					
						
							| 
									
										
										
										
											2023-06-09 14:24:59 +08:00
										 |  |  |     method: "GET", | 
					
						
							|  |  |  |     url: () => "/messages", | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |   }, | 
					
						
							|  |  |  |   getConversations: { | 
					
						
							| 
									
										
										
										
											2023-06-09 14:24:59 +08:00
										 |  |  |     method: "GET", | 
					
						
							|  |  |  |     url: () => "/conversations", | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |   }, | 
					
						
							|  |  |  |   renameConversation: { | 
					
						
							| 
									
										
										
										
											2023-06-09 14:24:59 +08:00
										 |  |  |     method: "PATCH", | 
					
						
							|  |  |  |     url: (conversation_id) => `/conversations/${conversation_id}`, | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | export class DifyClient { | 
					
						
							|  |  |  |   constructor(apiKey, baseUrl = BASE_URL) { | 
					
						
							| 
									
										
										
										
											2023-06-09 14:24:59 +08:00
										 |  |  |     this.apiKey = apiKey; | 
					
						
							|  |  |  |     this.baseUrl = baseUrl; | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   updateApiKey(apiKey) { | 
					
						
							| 
									
										
										
										
											2023-06-09 14:24:59 +08:00
										 |  |  |     this.apiKey = apiKey; | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-09 14:24:59 +08:00
										 |  |  |   async sendRequest( | 
					
						
							|  |  |  |     method, | 
					
						
							|  |  |  |     endpoint, | 
					
						
							|  |  |  |     data = null, | 
					
						
							|  |  |  |     params = null, | 
					
						
							|  |  |  |     stream = false | 
					
						
							|  |  |  |   ) { | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     const headers = { | 
					
						
							| 
									
										
										
										
											2023-06-09 14:24:59 +08:00
										 |  |  |       Authorization: `Bearer ${this.apiKey}`, | 
					
						
							|  |  |  |       "Content-Type": "application/json", | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-09 14:24:59 +08:00
										 |  |  |     const url = `${this.baseUrl}${endpoint}`; | 
					
						
							|  |  |  |     let response; | 
					
						
							|  |  |  |     if (stream) { | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |       response = await axios({ | 
					
						
							|  |  |  |         method, | 
					
						
							|  |  |  |         url, | 
					
						
							|  |  |  |         data, | 
					
						
							|  |  |  |         params, | 
					
						
							|  |  |  |         headers, | 
					
						
							| 
									
										
										
										
											2023-06-09 14:24:59 +08:00
										 |  |  |         responseType: "stream", | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2023-06-09 14:24:59 +08:00
										 |  |  |       response = await axios({ | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |         method, | 
					
						
							| 
									
										
										
										
											2023-06-09 14:24:59 +08:00
										 |  |  |         url, | 
					
						
							|  |  |  |         data, | 
					
						
							|  |  |  |         params, | 
					
						
							|  |  |  |         headers, | 
					
						
							|  |  |  |         responseType: "json", | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-09 14:24:59 +08:00
										 |  |  |     return response; | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-09 14:24:59 +08:00
										 |  |  |   messageFeedback(message_id, rating, user) { | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     const data = { | 
					
						
							|  |  |  |       rating, | 
					
						
							|  |  |  |       user, | 
					
						
							| 
									
										
										
										
											2023-06-09 14:24:59 +08:00
										 |  |  |     }; | 
					
						
							|  |  |  |     return this.sendRequest( | 
					
						
							|  |  |  |       routes.feedback.method, | 
					
						
							|  |  |  |       routes.feedback.url(message_id), | 
					
						
							|  |  |  |       data | 
					
						
							|  |  |  |     ); | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   getApplicationParameters(user) { | 
					
						
							| 
									
										
										
										
											2023-06-09 14:24:59 +08:00
										 |  |  |     const params = { user }; | 
					
						
							|  |  |  |     return this.sendRequest( | 
					
						
							|  |  |  |       routes.application.method, | 
					
						
							|  |  |  |       routes.application.url(), | 
					
						
							|  |  |  |       null, | 
					
						
							|  |  |  |       params | 
					
						
							|  |  |  |     ); | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export class CompletionClient extends DifyClient { | 
					
						
							| 
									
										
										
										
											2023-06-09 14:24:59 +08:00
										 |  |  |   createCompletionMessage(inputs, query, user, stream = false) { | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     const data = { | 
					
						
							|  |  |  |       inputs, | 
					
						
							|  |  |  |       query, | 
					
						
							|  |  |  |       user, | 
					
						
							| 
									
										
										
										
											2023-06-09 14:24:59 +08:00
										 |  |  |       response_mode: stream ? "streaming" : "blocking", | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     return this.sendRequest( | 
					
						
							|  |  |  |       routes.createCompletionMessage.method, | 
					
						
							|  |  |  |       routes.createCompletionMessage.url(), | 
					
						
							|  |  |  |       data, | 
					
						
							|  |  |  |       null, | 
					
						
							|  |  |  |       stream | 
					
						
							|  |  |  |     ); | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export class ChatClient extends DifyClient { | 
					
						
							| 
									
										
										
										
											2023-06-09 14:24:59 +08:00
										 |  |  |   createChatMessage( | 
					
						
							|  |  |  |     inputs, | 
					
						
							|  |  |  |     query, | 
					
						
							|  |  |  |     user, | 
					
						
							|  |  |  |     stream = false, | 
					
						
							|  |  |  |     conversation_id = null | 
					
						
							|  |  |  |   ) { | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     const data = { | 
					
						
							|  |  |  |       inputs, | 
					
						
							|  |  |  |       query, | 
					
						
							|  |  |  |       user, | 
					
						
							| 
									
										
										
										
											2023-06-09 14:24:59 +08:00
										 |  |  |       response_mode: stream ? "streaming" : "blocking", | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     if (conversation_id) data.conversation_id = conversation_id; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return this.sendRequest( | 
					
						
							|  |  |  |       routes.createChatMessage.method, | 
					
						
							|  |  |  |       routes.createChatMessage.url(), | 
					
						
							|  |  |  |       data, | 
					
						
							|  |  |  |       null, | 
					
						
							|  |  |  |       stream | 
					
						
							|  |  |  |     ); | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-09 14:24:59 +08:00
										 |  |  |   getConversationMessages( | 
					
						
							|  |  |  |     user, | 
					
						
							|  |  |  |     conversation_id = "", | 
					
						
							|  |  |  |     first_id = null, | 
					
						
							|  |  |  |     limit = null | 
					
						
							|  |  |  |   ) { | 
					
						
							|  |  |  |     const params = { user }; | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-09 14:24:59 +08:00
										 |  |  |     if (conversation_id) params.conversation_id = conversation_id; | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-09 14:24:59 +08:00
										 |  |  |     if (first_id) params.first_id = first_id; | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-09 14:24:59 +08:00
										 |  |  |     if (limit) params.limit = limit; | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-09 14:24:59 +08:00
										 |  |  |     return this.sendRequest( | 
					
						
							|  |  |  |       routes.getConversationMessages.method, | 
					
						
							|  |  |  |       routes.getConversationMessages.url(), | 
					
						
							|  |  |  |       null, | 
					
						
							|  |  |  |       params | 
					
						
							|  |  |  |     ); | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-09 14:24:59 +08:00
										 |  |  |   getConversations(user, first_id = null, limit = null, pinned = null) { | 
					
						
							|  |  |  |     const params = { user, first_id: first_id, limit, pinned }; | 
					
						
							|  |  |  |     return this.sendRequest( | 
					
						
							|  |  |  |       routes.getConversations.method, | 
					
						
							|  |  |  |       routes.getConversations.url(), | 
					
						
							|  |  |  |       null, | 
					
						
							|  |  |  |       params | 
					
						
							|  |  |  |     ); | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-09 14:24:59 +08:00
										 |  |  |   renameConversation(conversation_id, name, user) { | 
					
						
							|  |  |  |     const data = { name, user }; | 
					
						
							|  |  |  |     return this.sendRequest( | 
					
						
							|  |  |  |       routes.renameConversation.method, | 
					
						
							|  |  |  |       routes.renameConversation.url(conversation_id), | 
					
						
							|  |  |  |       data | 
					
						
							|  |  |  |     ); | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2023-06-09 14:24:59 +08:00
										 |  |  | } |