From 43264db8f562b732f32aa835de953f66314fc2d1 Mon Sep 17 00:00:00 2001 From: ivamp Date: Sun, 25 Aug 2024 06:15:18 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20=E5=9B=BE=E7=89=87?= =?UTF-8?q?=E4=B8=8A=E4=BC=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/swagger.yaml | 59 +++++++++++ src/api/api.ts | 181 +++++++++++++++++++++++++------- src/pages/public_chat/index.vue | 57 +++++++++- 3 files changed, 257 insertions(+), 40 deletions(-) diff --git a/api/swagger.yaml b/api/swagger.yaml index ed3ee38..b45d1ea 100644 --- a/api/swagger.yaml +++ b/api/swagger.yaml @@ -927,6 +927,65 @@ paths: summary: 清空公开对话的聊天记录 tags: - chat_public + /api/v1/chat_public/{chat_id}/images: + post: + consumes: + - application/json + description: 将一个图片添加到聊天记录中 + parameters: + - in: path + name: chat_id + required: true + type: integer + - in: formData + name: assistant_token + required: true + type: string + - in: formData + name: guest_id + required: true + type: string + - description: 图片 + in: formData + name: image + required: true + type: file + produces: + - application/json + responses: + "200": + description: OK + schema: + allOf: + - $ref: '#/definitions/rag-new_internal_schema.ResponseBody' + - properties: + data: + $ref: '#/definitions/rag-new_internal_schema.ChatMessageResponse' + type: object + "400": + description: Bad Request + schema: + $ref: '#/definitions/rag-new_internal_schema.ResponseBody' + "404": + description: Not Found + schema: + $ref: '#/definitions/rag-new_internal_schema.ResponseBody' + "409": + description: Conflict + schema: + allOf: + - $ref: '#/definitions/rag-new_internal_schema.ResponseBody' + - properties: + data: + $ref: '#/definitions/rag-new_internal_schema.ChatMessageResponse' + type: object + "500": + description: Internal Server Error + schema: + $ref: '#/definitions/rag-new_internal_schema.ResponseBody' + summary: 添加图片 + tags: + - chat_public /api/v1/chat_public/{chat_id}/messages: get: consumes: diff --git a/src/api/api.ts b/src/api/api.ts index decb3cd..c1d322d 100644 --- a/src/api/api.ts +++ b/src/api/api.ts @@ -209,6 +209,62 @@ export interface ApiV1AssistantsPost200Response { */ 'success'?: boolean; } +/** + * + * @export + * @interface ApiV1ChatPublicChatIdImagesPost200Response + */ +export interface ApiV1ChatPublicChatIdImagesPost200Response { + /** + * + * @type {RagNewInternalSchemaChatMessageResponse} + * @memberof ApiV1ChatPublicChatIdImagesPost200Response + */ + 'data'?: RagNewInternalSchemaChatMessageResponse; + /** + * + * @type {string} + * @memberof ApiV1ChatPublicChatIdImagesPost200Response + */ + 'error'?: string; + /** + * + * @type {string} + * @memberof ApiV1ChatPublicChatIdImagesPost200Response + */ + 'message'?: string; + /** + * + * @type {boolean} + * @memberof ApiV1ChatPublicChatIdImagesPost200Response + */ + 'success'?: boolean; +} +/** + * + * @export + * @interface ApiV1ChatPublicChatIdImagesPostRequest + */ +export interface ApiV1ChatPublicChatIdImagesPostRequest { + /** + * + * @type {string} + * @memberof ApiV1ChatPublicChatIdImagesPostRequest + */ + 'assistant_token': string; + /** + * + * @type {string} + * @memberof ApiV1ChatPublicChatIdImagesPostRequest + */ + 'guest_id': string; + /** + * 图片 + * @type {File} + * @memberof ApiV1ChatPublicChatIdImagesPostRequest + */ + 'image': File; +} /** * * @export @@ -240,37 +296,6 @@ export interface ApiV1ChatPublicChatIdMessagesGet200Response { */ 'success'?: boolean; } -/** - * - * @export - * @interface ApiV1ChatPublicChatIdMessagesPost200Response - */ -export interface ApiV1ChatPublicChatIdMessagesPost200Response { - /** - * - * @type {RagNewInternalSchemaChatMessageResponse} - * @memberof ApiV1ChatPublicChatIdMessagesPost200Response - */ - 'data'?: RagNewInternalSchemaChatMessageResponse; - /** - * - * @type {string} - * @memberof ApiV1ChatPublicChatIdMessagesPost200Response - */ - 'error'?: string; - /** - * - * @type {string} - * @memberof ApiV1ChatPublicChatIdMessagesPost200Response - */ - 'message'?: string; - /** - * - * @type {boolean} - * @memberof ApiV1ChatPublicChatIdMessagesPost200Response - */ - 'success'?: boolean; -} /** * * @export @@ -2891,7 +2916,7 @@ export const ChatMessageApiFp = function(configuration?: Configuration) { * @param {*} [options] Override http request option. * @throws {RequiredError} */ - async apiV1ChatsIdImagesPost(id: number, apiV1ChatsIdImagesPostRequest: ApiV1ChatsIdImagesPostRequest, options?: RawAxiosRequestConfig): Promise<(axios?: AxiosInstance, basePath?: string) => AxiosPromise> { + async apiV1ChatsIdImagesPost(id: number, apiV1ChatsIdImagesPostRequest: ApiV1ChatsIdImagesPostRequest, options?: RawAxiosRequestConfig): Promise<(axios?: AxiosInstance, basePath?: string) => AxiosPromise> { const localVarAxiosArgs = await localVarAxiosParamCreator.apiV1ChatsIdImagesPost(id, apiV1ChatsIdImagesPostRequest, options); const localVarOperationServerIndex = configuration?.serverIndex ?? 0; const localVarOperationServerBasePath = operationServerMap['ChatMessageApi.apiV1ChatsIdImagesPost']?.[localVarOperationServerIndex]?.url; @@ -2918,7 +2943,7 @@ export const ChatMessageApiFp = function(configuration?: Configuration) { * @param {*} [options] Override http request option. * @throws {RequiredError} */ - async apiV1ChatsIdMessagesPost(id: number, message: RagNewInternalSchemaChatMessageAddRequest, options?: RawAxiosRequestConfig): Promise<(axios?: AxiosInstance, basePath?: string) => AxiosPromise> { + async apiV1ChatsIdMessagesPost(id: number, message: RagNewInternalSchemaChatMessageAddRequest, options?: RawAxiosRequestConfig): Promise<(axios?: AxiosInstance, basePath?: string) => AxiosPromise> { const localVarAxiosArgs = await localVarAxiosParamCreator.apiV1ChatsIdMessagesPost(id, message, options); const localVarOperationServerIndex = configuration?.serverIndex ?? 0; const localVarOperationServerBasePath = operationServerMap['ChatMessageApi.apiV1ChatsIdMessagesPost']?.[localVarOperationServerIndex]?.url; @@ -2932,7 +2957,7 @@ export const ChatMessageApiFp = function(configuration?: Configuration) { * @param {*} [options] Override http request option. * @throws {RequiredError} */ - async apiV1StreamStreamIdGet(streamId: string, xUserIP?: string, options?: RawAxiosRequestConfig): Promise<(axios?: AxiosInstance, basePath?: string) => AxiosPromise> { + async apiV1StreamStreamIdGet(streamId: string, xUserIP?: string, options?: RawAxiosRequestConfig): Promise<(axios?: AxiosInstance, basePath?: string) => AxiosPromise> { const localVarAxiosArgs = await localVarAxiosParamCreator.apiV1StreamStreamIdGet(streamId, xUserIP, options); const localVarOperationServerIndex = configuration?.serverIndex ?? 0; const localVarOperationServerBasePath = operationServerMap['ChatMessageApi.apiV1StreamStreamIdGet']?.[localVarOperationServerIndex]?.url; @@ -2966,7 +2991,7 @@ export const ChatMessageApiFactory = function (configuration?: Configuration, ba * @param {*} [options] Override http request option. * @throws {RequiredError} */ - apiV1ChatsIdImagesPost(id: number, apiV1ChatsIdImagesPostRequest: ApiV1ChatsIdImagesPostRequest, options?: any): AxiosPromise { + apiV1ChatsIdImagesPost(id: number, apiV1ChatsIdImagesPostRequest: ApiV1ChatsIdImagesPostRequest, options?: any): AxiosPromise { return localVarFp.apiV1ChatsIdImagesPost(id, apiV1ChatsIdImagesPostRequest, options).then((request) => request(axios, basePath)); }, /** @@ -2987,7 +3012,7 @@ export const ChatMessageApiFactory = function (configuration?: Configuration, ba * @param {*} [options] Override http request option. * @throws {RequiredError} */ - apiV1ChatsIdMessagesPost(id: number, message: RagNewInternalSchemaChatMessageAddRequest, options?: any): AxiosPromise { + apiV1ChatsIdMessagesPost(id: number, message: RagNewInternalSchemaChatMessageAddRequest, options?: any): AxiosPromise { return localVarFp.apiV1ChatsIdMessagesPost(id, message, options).then((request) => request(axios, basePath)); }, /** @@ -2998,7 +3023,7 @@ export const ChatMessageApiFactory = function (configuration?: Configuration, ba * @param {*} [options] Override http request option. * @throws {RequiredError} */ - apiV1StreamStreamIdGet(streamId: string, xUserIP?: string, options?: any): AxiosPromise { + apiV1StreamStreamIdGet(streamId: string, xUserIP?: string, options?: any): AxiosPromise { return localVarFp.apiV1StreamStreamIdGet(streamId, xUserIP, options).then((request) => request(axios, basePath)); }, }; @@ -3123,6 +3148,46 @@ export const ChatPublicApiAxiosParamCreator = function (configuration?: Configur options: localVarRequestOptions, }; }, + /** + * 将一个图片添加到聊天记录中 + * @summary 添加图片 + * @param {number} chatId + * @param {ApiV1ChatPublicChatIdImagesPostRequest} apiV1ChatPublicChatIdImagesPostRequest + * @param {*} [options] Override http request option. + * @throws {RequiredError} + */ + apiV1ChatPublicChatIdImagesPost: async (chatId: number, apiV1ChatPublicChatIdImagesPostRequest: ApiV1ChatPublicChatIdImagesPostRequest, options: RawAxiosRequestConfig = {}): Promise => { + // verify required parameter 'chatId' is not null or undefined + assertParamExists('apiV1ChatPublicChatIdImagesPost', 'chatId', chatId) + // verify required parameter 'apiV1ChatPublicChatIdImagesPostRequest' is not null or undefined + assertParamExists('apiV1ChatPublicChatIdImagesPost', 'apiV1ChatPublicChatIdImagesPostRequest', apiV1ChatPublicChatIdImagesPostRequest) + const localVarPath = `/api/v1/chat_public/{chat_id}/images` + .replace(`{${"chat_id"}}`, encodeURIComponent(String(chatId))); + // use dummy base URL string because the URL constructor only accepts absolute URLs. + const localVarUrlObj = new URL(localVarPath, DUMMY_BASE_URL); + let baseOptions; + if (configuration) { + baseOptions = configuration.baseOptions; + } + + const localVarRequestOptions = { method: 'POST', ...baseOptions, ...options}; + const localVarHeaderParameter = {} as any; + const localVarQueryParameter = {} as any; + + + + localVarHeaderParameter['Content-Type'] = 'application/json'; + + setSearchParams(localVarUrlObj, localVarQueryParameter); + let headersFromBaseOptions = baseOptions && baseOptions.headers ? baseOptions.headers : {}; + localVarRequestOptions.headers = {...localVarHeaderParameter, ...headersFromBaseOptions, ...options.headers}; + localVarRequestOptions.data = serializeDataIfNeeded(apiV1ChatPublicChatIdImagesPostRequest, localVarRequestOptions, configuration) + + return { + url: toPathString(localVarUrlObj), + options: localVarRequestOptions, + }; + }, /** * * @summary 获取公开对话的聊天记录 @@ -3307,6 +3372,20 @@ export const ChatPublicApiFp = function(configuration?: Configuration) { const localVarOperationServerBasePath = operationServerMap['ChatPublicApi.apiV1ChatPublicChatIdClearPost']?.[localVarOperationServerIndex]?.url; return (axios, basePath) => createRequestFunction(localVarAxiosArgs, globalAxios, BASE_PATH, configuration)(axios, localVarOperationServerBasePath || basePath); }, + /** + * 将一个图片添加到聊天记录中 + * @summary 添加图片 + * @param {number} chatId + * @param {ApiV1ChatPublicChatIdImagesPostRequest} apiV1ChatPublicChatIdImagesPostRequest + * @param {*} [options] Override http request option. + * @throws {RequiredError} + */ + async apiV1ChatPublicChatIdImagesPost(chatId: number, apiV1ChatPublicChatIdImagesPostRequest: ApiV1ChatPublicChatIdImagesPostRequest, options?: RawAxiosRequestConfig): Promise<(axios?: AxiosInstance, basePath?: string) => AxiosPromise> { + const localVarAxiosArgs = await localVarAxiosParamCreator.apiV1ChatPublicChatIdImagesPost(chatId, apiV1ChatPublicChatIdImagesPostRequest, options); + const localVarOperationServerIndex = configuration?.serverIndex ?? 0; + const localVarOperationServerBasePath = operationServerMap['ChatPublicApi.apiV1ChatPublicChatIdImagesPost']?.[localVarOperationServerIndex]?.url; + return (axios, basePath) => createRequestFunction(localVarAxiosArgs, globalAxios, BASE_PATH, configuration)(axios, localVarOperationServerBasePath || basePath); + }, /** * * @summary 获取公开对话的聊天记录 @@ -3330,7 +3409,7 @@ export const ChatPublicApiFp = function(configuration?: Configuration) { * @param {*} [options] Override http request option. * @throws {RequiredError} */ - async apiV1ChatPublicChatIdMessagesPost(chatId: number, schemaAddPublicChatMessageRequest: RagNewInternalSchemaAddPublicChatMessageRequest, options?: RawAxiosRequestConfig): Promise<(axios?: AxiosInstance, basePath?: string) => AxiosPromise> { + async apiV1ChatPublicChatIdMessagesPost(chatId: number, schemaAddPublicChatMessageRequest: RagNewInternalSchemaAddPublicChatMessageRequest, options?: RawAxiosRequestConfig): Promise<(axios?: AxiosInstance, basePath?: string) => AxiosPromise> { const localVarAxiosArgs = await localVarAxiosParamCreator.apiV1ChatPublicChatIdMessagesPost(chatId, schemaAddPublicChatMessageRequest, options); const localVarOperationServerIndex = configuration?.serverIndex ?? 0; const localVarOperationServerBasePath = operationServerMap['ChatPublicApi.apiV1ChatPublicChatIdMessagesPost']?.[localVarOperationServerIndex]?.url; @@ -3383,6 +3462,17 @@ export const ChatPublicApiFactory = function (configuration?: Configuration, bas apiV1ChatPublicChatIdClearPost(chatId: number, schemaGetPublicChatMessageRequest: RagNewInternalSchemaGetPublicChatMessageRequest, options?: any): AxiosPromise { return localVarFp.apiV1ChatPublicChatIdClearPost(chatId, schemaGetPublicChatMessageRequest, options).then((request) => request(axios, basePath)); }, + /** + * 将一个图片添加到聊天记录中 + * @summary 添加图片 + * @param {number} chatId + * @param {ApiV1ChatPublicChatIdImagesPostRequest} apiV1ChatPublicChatIdImagesPostRequest + * @param {*} [options] Override http request option. + * @throws {RequiredError} + */ + apiV1ChatPublicChatIdImagesPost(chatId: number, apiV1ChatPublicChatIdImagesPostRequest: ApiV1ChatPublicChatIdImagesPostRequest, options?: any): AxiosPromise { + return localVarFp.apiV1ChatPublicChatIdImagesPost(chatId, apiV1ChatPublicChatIdImagesPostRequest, options).then((request) => request(axios, basePath)); + }, /** * * @summary 获取公开对话的聊天记录 @@ -3403,7 +3493,7 @@ export const ChatPublicApiFactory = function (configuration?: Configuration, bas * @param {*} [options] Override http request option. * @throws {RequiredError} */ - apiV1ChatPublicChatIdMessagesPost(chatId: number, schemaAddPublicChatMessageRequest: RagNewInternalSchemaAddPublicChatMessageRequest, options?: any): AxiosPromise { + apiV1ChatPublicChatIdMessagesPost(chatId: number, schemaAddPublicChatMessageRequest: RagNewInternalSchemaAddPublicChatMessageRequest, options?: any): AxiosPromise { return localVarFp.apiV1ChatPublicChatIdMessagesPost(chatId, schemaAddPublicChatMessageRequest, options).then((request) => request(axios, basePath)); }, /** @@ -3449,6 +3539,19 @@ export class ChatPublicApi extends BaseAPI { return ChatPublicApiFp(this.configuration).apiV1ChatPublicChatIdClearPost(chatId, schemaGetPublicChatMessageRequest, options).then((request) => request(this.axios, this.basePath)); } + /** + * 将一个图片添加到聊天记录中 + * @summary 添加图片 + * @param {number} chatId + * @param {ApiV1ChatPublicChatIdImagesPostRequest} apiV1ChatPublicChatIdImagesPostRequest + * @param {*} [options] Override http request option. + * @throws {RequiredError} + * @memberof ChatPublicApi + */ + public apiV1ChatPublicChatIdImagesPost(chatId: number, apiV1ChatPublicChatIdImagesPostRequest: ApiV1ChatPublicChatIdImagesPostRequest, options?: RawAxiosRequestConfig) { + return ChatPublicApiFp(this.configuration).apiV1ChatPublicChatIdImagesPost(chatId, apiV1ChatPublicChatIdImagesPostRequest, options).then((request) => request(this.axios, this.basePath)); + } + /** * * @summary 获取公开对话的聊天记录 diff --git a/src/pages/public_chat/index.vue b/src/pages/public_chat/index.vue index 0e6aefc..f0e0024 100644 --- a/src/pages/public_chat/index.vue +++ b/src/pages/public_chat/index.vue @@ -12,7 +12,7 @@
AI
系统
- +
图片
用户
@@ -32,6 +32,12 @@
+
+ +
{{ message.content }}
@@ -61,7 +67,17 @@ label="输入消息" @keyup.enter="sendMessage" > + + 发送 + 上传图片 + 清空
@@ -78,6 +94,7 @@ import { RagNewInternalSchemaGetPublicChatMessageRequest, } from "@/api"; import { usePublicChatStore } from "@/stores/public_chat"; +import config from "@/config/config"; const publicChatStore = usePublicChatStore(); @@ -87,6 +104,9 @@ if (publicChatStore.guest_id === "") { const chatId = ref(); +const uploading = ref(false); +const imageBaseUrl = config.backend + "/api/v1/files"; + // 获取查询参数 const queries = new URLSearchParams(window.location.search); const requiredQuries = ["assistant_token"]; @@ -119,6 +139,7 @@ const input = ref(""); const toolName = ref(""); const toolError = ref(false); const toolCalling = ref(false); +const fileUpload = ref(); function sendMessage() { if (input.value !== "") { @@ -264,5 +285,39 @@ const createChat = () => { }); }; +const uploadImage = () => { + if (!fileUpload.value) { + return; + } + + uploading.value = true; + api.ChatPublic.apiV1ChatPublicChatIdImagesPost( + chatId.value, + { + image: fileUpload.value, + assistant_token: publicChatInfo.value.assistant_token, + guest_id: publicChatInfo.value.guest_id, + }, + { + headers: { + "Content-Type": "multipart/form-data", + }, + }, + ) + .then((r) => { + // 如果成功 + if (r.status === 200 || r.status === 201) { + fileUpload.value = null; + getMessages(); + } + }) + .catch((err) => { + alert(err.response.data.message); + }) + .finally(() => { + uploading.value = false; + }); +}; + createChat();