From 917e4e926293421e57f21f1651e9b046236c7673 Mon Sep 17 00:00:00 2001 From: Archer <545436317@qq.com> Date: Wed, 8 May 2024 22:18:22 +0800 Subject: [PATCH] 4.8 test fix (#1397) * adapt v1 chat init * adapt v1 chat init * adapt v1 chat init * perf: message input line; fix: http request un stream * perf: message input line; fix: http request un stream * perf: message input line; fix: http request un stream * perf: error tip --- packages/global/common/error/code/app.ts | 2 +- packages/global/common/error/code/openapi.ts | 4 +- packages/global/common/error/code/outLink.ts | 2 +- packages/global/common/error/code/plugin.ts | 4 +- .../template/system/variableUpdate/index.tsx | 2 +- .../service/core/workflow/dispatch/index.ts | 2 +- .../core/workflow/dispatch/tools/http468.ts | 3 +- .../data/pluginTemplates/customFeedback.json | 24 +++- .../src/components/ChatBox/MessageInput.tsx | 9 +- .../src/components/core/workflow/context.tsx | 1 - projects/app/src/pages/api/core/chat/init.ts | 128 +++++++++--------- .../app/src/pages/api/core/workflow/debug.ts | 125 ++++++++--------- .../api/plugins/customFeedback/v2/index.ts | 7 +- 13 files changed, 167 insertions(+), 146 deletions(-) diff --git a/packages/global/common/error/code/app.ts b/packages/global/common/error/code/app.ts index 70573f836..e3dfdcf3a 100644 --- a/packages/global/common/error/code/app.ts +++ b/packages/global/common/error/code/app.ts @@ -2,7 +2,7 @@ import { ErrType } from '../errorCode'; /* dataset: 502000 */ export enum AppErrEnum { - unExist = 'unExist', + unExist = 'appUnExist', unAuthApp = 'unAuthApp' } const appErrList = [ diff --git a/packages/global/common/error/code/openapi.ts b/packages/global/common/error/code/openapi.ts index 5737af63e..c64f51b32 100644 --- a/packages/global/common/error/code/openapi.ts +++ b/packages/global/common/error/code/openapi.ts @@ -2,8 +2,8 @@ import { ErrType } from '../errorCode'; /* dataset: 506000 */ export enum OpenApiErrEnum { - unExist = 'unExist', - unAuth = 'unAuth' + unExist = 'openapiUnExist', + unAuth = 'openapiUnAuth' } const errList = [ { diff --git a/packages/global/common/error/code/outLink.ts b/packages/global/common/error/code/outLink.ts index 81130e10f..25ee6fab8 100644 --- a/packages/global/common/error/code/outLink.ts +++ b/packages/global/common/error/code/outLink.ts @@ -2,7 +2,7 @@ import { ErrType } from '../errorCode'; /* dataset: 505000 */ export enum OutLinkErrEnum { - unExist = 'unExist', + unExist = 'outlinkUnExist', unAuthLink = 'unAuthLink', linkUnInvalid = 'linkUnInvalid', diff --git a/packages/global/common/error/code/plugin.ts b/packages/global/common/error/code/plugin.ts index d3ae672a8..430f0d2a7 100644 --- a/packages/global/common/error/code/plugin.ts +++ b/packages/global/common/error/code/plugin.ts @@ -2,8 +2,8 @@ import { ErrType } from '../errorCode'; /* dataset: 507000 */ export enum PluginErrEnum { - unExist = 'unExist', - unAuth = 'unAuth' + unExist = 'pluginUnExist', + unAuth = 'pluginUnAuth' } const errList = [ { diff --git a/packages/global/core/workflow/template/system/variableUpdate/index.tsx b/packages/global/core/workflow/template/system/variableUpdate/index.tsx index 34ddd2a06..6867d98bb 100644 --- a/packages/global/core/workflow/template/system/variableUpdate/index.tsx +++ b/packages/global/core/workflow/template/system/variableUpdate/index.tsx @@ -15,7 +15,7 @@ export const VariableUpdateNode: FlowNodeTemplateType = { targetHandle: getHandleConfig(true, true, true, true), avatar: '/imgs/workflow/variable.png', name: '变量更新', - intro: '可以更新指定节点的输出值和全局变量', + intro: '可以更新指定节点的输出值或更新全局变量', showStatus: true, isTool: false, inputs: [ diff --git a/packages/service/core/workflow/dispatch/index.ts b/packages/service/core/workflow/dispatch/index.ts index 3dc63cf59..bb4a78385 100644 --- a/packages/service/core/workflow/dispatch/index.ts +++ b/packages/service/core/workflow/dispatch/index.ts @@ -290,7 +290,7 @@ export async function dispatchWorkFlow(data: Props): Promise { if (runtimeNodeStatus[node.nodeId] !== 'wait') { - console.log(node.name); onChangeNode({ nodeId: node.nodeId, type: 'attr', diff --git a/projects/app/src/pages/api/core/chat/init.ts b/projects/app/src/pages/api/core/chat/init.ts index 902c9fb8e..08e3ff796 100644 --- a/projects/app/src/pages/api/core/chat/init.ts +++ b/projects/app/src/pages/api/core/chat/init.ts @@ -1,6 +1,5 @@ import type { NextApiRequest, NextApiResponse } from 'next'; import { jsonRes } from '@fastgpt/service/common/response'; -import { connectToDatabase } from '@/service/mongo'; import { authApp } from '@fastgpt/service/support/permission/auth/app'; import { getGuideModule, replaceAppChatConfig } from '@fastgpt/global/core/workflow/utils'; import { getChatModelNameListByModules } from '@/service/core/app/workflow'; @@ -10,78 +9,73 @@ import { getChatItems } from '@fastgpt/service/core/chat/controller'; import { ChatErrEnum } from '@fastgpt/global/common/error/code/chat'; import { DispatchNodeResponseKeyEnum } from '@fastgpt/global/core/workflow/runtime/constants'; import { getAppLatestVersion } from '@fastgpt/service/core/app/controller'; +import { NextAPI } from '@/service/middle/entry'; -export default async function handler(req: NextApiRequest, res: NextApiResponse) { - try { - await connectToDatabase(); +async function handler( + req: NextApiRequest, + res: NextApiResponse +): Promise { + let { appId, chatId, loadCustomFeedbacks } = req.query as InitChatProps; - let { appId, chatId, loadCustomFeedbacks } = req.query as InitChatProps; - - if (!appId) { - return jsonRes(res, { - code: 501, - message: "You don't have an app yet" - }); - } - - // auth app permission - const [{ app, tmbId }, chat] = await Promise.all([ - authApp({ - req, - authToken: true, - appId, - per: 'r' - }), - chatId ? MongoChat.findOne({ appId, chatId }) : undefined - ]); - - // auth chat permission - if (chat && !app.canWrite && String(tmbId) !== String(chat?.tmbId)) { - throw new Error(ChatErrEnum.unAuthChat); - } - - // get app and history - const [{ history }, { nodes }] = await Promise.all([ - getChatItems({ - appId, - chatId, - limit: 30, - field: `dataId obj value adminFeedback userBadFeedback userGoodFeedback ${ - DispatchNodeResponseKeyEnum.nodeResponse - } ${loadCustomFeedbacks ? 'customFeedbacks' : ''}` - }), - getAppLatestVersion(app._id, app) - ]); - - jsonRes(res, { - data: { - chatId, - appId, - title: chat?.title || '新对话', - userAvatar: undefined, - variables: chat?.variables || {}, - history, - app: { - userGuideModule: replaceAppChatConfig({ - node: getGuideModule(nodes), - variableList: chat?.variableList, - welcomeText: chat?.welcomeText - }), - chatModels: getChatModelNameListByModules(nodes), - name: app.name, - avatar: app.avatar, - intro: app.intro - } - } - }); - } catch (err) { - jsonRes(res, { - code: 500, - error: err + if (!appId) { + return jsonRes(res, { + code: 501, + message: "You don't have an app yet" }); } + + // auth app permission + const [{ app, tmbId }, chat] = await Promise.all([ + authApp({ + req, + authToken: true, + appId, + per: 'r' + }), + chatId ? MongoChat.findOne({ appId, chatId }) : undefined + ]); + + // auth chat permission + if (chat && !app.canWrite && String(tmbId) !== String(chat?.tmbId)) { + throw new Error(ChatErrEnum.unAuthChat); + } + + // get app and history + const [{ history }, { nodes }] = await Promise.all([ + getChatItems({ + appId, + chatId, + limit: 30, + field: `dataId obj value adminFeedback userBadFeedback userGoodFeedback ${ + DispatchNodeResponseKeyEnum.nodeResponse + } ${loadCustomFeedbacks ? 'customFeedbacks' : ''}` + }), + getAppLatestVersion(app._id, app) + ]); + + return { + chatId, + appId, + title: chat?.title || '新对话', + userAvatar: undefined, + variables: chat?.variables || {}, + history, + app: { + userGuideModule: replaceAppChatConfig({ + node: getGuideModule(nodes), + variableList: chat?.variableList, + welcomeText: chat?.welcomeText + }), + chatModels: getChatModelNameListByModules(nodes), + name: app.name, + avatar: app.avatar, + intro: app.intro + } + }; } +export default NextAPI(handler); + export const config = { api: { responseLimit: '10mb' diff --git a/projects/app/src/pages/api/core/workflow/debug.ts b/projects/app/src/pages/api/core/workflow/debug.ts index f0fef3ba3..e4749d8b5 100644 --- a/projects/app/src/pages/api/core/workflow/debug.ts +++ b/projects/app/src/pages/api/core/workflow/debug.ts @@ -1,6 +1,4 @@ import type { NextApiRequest, NextApiResponse } from 'next'; -import { connectToDatabase } from '@/service/mongo'; -import { jsonRes } from '@fastgpt/service/common/response'; import { pushChatUsage } from '@/service/support/wallet/usage/push'; import { UsageSourceEnum } from '@fastgpt/global/support/wallet/usage/constants'; import { authApp } from '@fastgpt/service/support/permission/auth/app'; @@ -9,8 +7,12 @@ import { authCert } from '@fastgpt/service/support/permission/auth/common'; import { getUserChatInfoAndAuthTeamPoints } from '@/service/support/permission/auth/team'; import { PostWorkflowDebugProps, PostWorkflowDebugResponse } from '@/global/core/workflow/api'; import { authPluginCrud } from '@fastgpt/service/support/permission/auth/plugin'; +import { NextAPI } from '@/service/middle/entry'; -export default async function handler(req: NextApiRequest, res: NextApiResponse) { +async function handler( + req: NextApiRequest, + res: NextApiResponse +): Promise { const { nodes = [], edges = [], @@ -18,72 +20,65 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) appId, pluginId } = req.body as PostWorkflowDebugProps; - try { - await connectToDatabase(); - if (!nodes) { - throw new Error('Prams Error'); - } - if (!Array.isArray(nodes)) { - throw new Error('Nodes is not array'); - } - if (!Array.isArray(edges)) { - throw new Error('Edges is not array'); - } - /* user auth */ - const [{ teamId, tmbId }] = await Promise.all([ - authCert({ - req, - authToken: true - }), - appId && authApp({ req, authToken: true, appId, per: 'r' }), - pluginId && authPluginCrud({ req, authToken: true, pluginId, per: 'r' }) - ]); - - // auth balance - const { user } = await getUserChatInfoAndAuthTeamPoints(tmbId); - - /* start process */ - const { flowUsages, flowResponses, debugResponse } = await dispatchWorkFlow({ - res, - mode: 'debug', - teamId, - tmbId, - user, - appId, - runtimeNodes: nodes, - runtimeEdges: edges, - variables, - query: [], - histories: [], - stream: false, - detail: true, - maxRunTimes: 200 - }); - - pushChatUsage({ - appName: '工作流Debug', - appId, - teamId, - tmbId, - source: UsageSourceEnum.fastgpt, - flowUsages - }); - - jsonRes(res, { - data: { - ...debugResponse, - flowResponses - } - }); - } catch (err: any) { - jsonRes(res, { - code: 500, - error: err - }); + if (!nodes) { + throw new Error('Prams Error'); } + if (!Array.isArray(nodes)) { + throw new Error('Nodes is not array'); + } + if (!Array.isArray(edges)) { + throw new Error('Edges is not array'); + } + + /* user auth */ + const [{ teamId, tmbId }] = await Promise.all([ + authCert({ + req, + authToken: true + }), + appId && authApp({ req, authToken: true, appId, per: 'r' }), + pluginId && authPluginCrud({ req, authToken: true, pluginId, per: 'r' }) + ]); + + // auth balance + const { user } = await getUserChatInfoAndAuthTeamPoints(tmbId); + + /* start process */ + const { flowUsages, flowResponses, debugResponse } = await dispatchWorkFlow({ + res, + mode: 'debug', + teamId, + tmbId, + user, + appId, + runtimeNodes: nodes, + runtimeEdges: edges, + variables, + query: [], + histories: [], + stream: false, + detail: true, + maxRunTimes: 200 + }); + + pushChatUsage({ + appName: '工作流Debug', + appId, + teamId, + tmbId, + source: UsageSourceEnum.fastgpt, + flowUsages + }); + + return { + ...debugResponse, + flowResponses + }; } +export default NextAPI(handler); + export const config = { api: { bodyParser: { diff --git a/projects/app/src/pages/api/plugins/customFeedback/v2/index.ts b/projects/app/src/pages/api/plugins/customFeedback/v2/index.ts index c04c73b86..afb97e6eb 100644 --- a/projects/app/src/pages/api/plugins/customFeedback/v2/index.ts +++ b/projects/app/src/pages/api/plugins/customFeedback/v2/index.ts @@ -34,11 +34,14 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse< if (!chatId || !chatItemId) { return res.json({ - [NodeOutputKeyEnum.answerText]: `\\n\\n**自动反馈调试**: "${customFeedback}"\\n\\n` + [NodeOutputKeyEnum.answerText]: `\\n\\n**自动反馈调试**: "${customFeedback}"\\n\\n`, + text: customFeedback }); } - return res.json({}); + res.json({ + text: customFeedback + }); } catch (err) { console.log(err); res.status(500).send(getErrText(err));