Commit 4b7ed6c0 by DigHuang Committed by GitHub

feat(chat): helper bot ui & file upload support (#7245)

* fix(permission): handle sandbox checks without standard plan

* refactor(chat): unify helper bot with main chat flow

- route helper bot through the main chat schema and API endpoints

- align file upload, image handling, empty-output tips, and interactive payload support

- update related tests and pro submodule pointer

* chore(helperbot): clean unified chat flow leftovers

* feat: align helperbot workflow billing

* feat: optimize helper chat flow

* fix: handle helper chat resume cleanup
parent 9363d7a5
......@@ -130,7 +130,6 @@ x-service-env-config: &x-service-env-config
MAX_LOGIN_SESSION:
ALLOWED_ORIGINS:
AGENT_ENGINE: default
HELPER_BOT_MODEL: qwen-max
CHAT_TITLE_MODEL:
SKIP_FILE_TYPE_CHECK: false
WECHAT_CHANNEL_CONCURRENCY: 1000
......
......@@ -180,7 +180,6 @@ These variables are mainly validated by `packages/service/env.ts` and apply to `
| Variable | Default | Description |
| -------------------------------------- | --------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `AGENT_ENGINE` | `default` | Agent engine. Supported values are `default` and `pi`. |
| `HELPER_BOT_MODEL` | Empty | Helper generation model. The model must be enabled in the system. |
| `SKIP_FILE_TYPE_CHECK` | `false` | Whether upload file type checks are skipped. |
| `WECHAT_CHANNEL_CONCURRENCY` | `1000` | WeChat channel poll worker concurrency. Minimum value is `10`. |
| `PARSE_FILE_WORKERS` | `10` | Resident file parsing worker count. |
......
......@@ -180,7 +180,6 @@ description: projects/app、projects/code-sandbox 与 pro/admin 环境变量说
| 变量 | 默认值 | 说明 |
| -------------------------------------- | --------- | ---------------------------------------------------------------------------------------------- |
| `AGENT_ENGINE` | `default` | Agent 引擎,可选 `default` 或 `pi`。 |
| `HELPER_BOT_MODEL` | 空 | 辅助生成模型,需保证系统中已启用对应模型。 |
| `SKIP_FILE_TYPE_CHECK` | `false` | 是否跳过上传文件类型检查。 |
| `WECHAT_CHANNEL_CONCURRENCY` | `1000` | 微信渠道 poll worker 并发数,最小 `10`。 |
| `PARSE_FILE_WORKERS` | `10` | 文件解析 worker 常驻线程数。 |
......
......@@ -130,7 +130,6 @@ x-service-env-config: &x-service-env-config
MAX_LOGIN_SESSION:
ALLOWED_ORIGINS:
AGENT_ENGINE: default
HELPER_BOT_MODEL: qwen-max
CHAT_TITLE_MODEL:
SKIP_FILE_TYPE_CHECK: false
WECHAT_CHANNEL_CONCURRENCY: 1000
......
......@@ -130,7 +130,6 @@ x-service-env-config: &x-service-env-config
MAX_LOGIN_SESSION:
ALLOWED_ORIGINS:
AGENT_ENGINE: default
HELPER_BOT_MODEL: qwen-max
CHAT_TITLE_MODEL:
SKIP_FILE_TYPE_CHECK: false
WECHAT_CHANNEL_CONCURRENCY: 1000
......
......@@ -130,7 +130,6 @@ x-service-env-config: &x-service-env-config
MAX_LOGIN_SESSION:
ALLOWED_ORIGINS:
AGENT_ENGINE: default
HELPER_BOT_MODEL: qwen-max
CHAT_TITLE_MODEL:
SKIP_FILE_TYPE_CHECK: false
WECHAT_CHANNEL_CONCURRENCY: 1000
......
......@@ -130,7 +130,6 @@ x-service-env-config: &x-service-env-config
MAX_LOGIN_SESSION:
ALLOWED_ORIGINS:
AGENT_ENGINE: default
HELPER_BOT_MODEL: qwen-max
CHAT_TITLE_MODEL:
SKIP_FILE_TYPE_CHECK: false
WECHAT_CHANNEL_CONCURRENCY: 1000
......
......@@ -130,7 +130,6 @@ x-service-env-config: &x-service-env-config
MAX_LOGIN_SESSION:
ALLOWED_ORIGINS:
AGENT_ENGINE: default
HELPER_BOT_MODEL: qwen-max
CHAT_TITLE_MODEL:
SKIP_FILE_TYPE_CHECK: false
WECHAT_CHANNEL_CONCURRENCY: 1000
......
......@@ -130,7 +130,6 @@ x-service-env-config: &x-service-env-config
MAX_LOGIN_SESSION:
ALLOWED_ORIGINS:
AGENT_ENGINE: default
HELPER_BOT_MODEL: qwen-max
CHAT_TITLE_MODEL:
SKIP_FILE_TYPE_CHECK: false
WECHAT_CHANNEL_CONCURRENCY: 1000
......
......@@ -130,7 +130,6 @@ x-service-env-config: &x-service-env-config
MAX_LOGIN_SESSION:
ALLOWED_ORIGINS:
AGENT_ENGINE: default
HELPER_BOT_MODEL: qwen-max
CHAT_TITLE_MODEL:
SKIP_FILE_TYPE_CHECK: false
WECHAT_CHANNEL_CONCURRENCY: 1000
......
......@@ -130,7 +130,6 @@ x-service-env-config: &x-service-env-config
MAX_LOGIN_SESSION:
ALLOWED_ORIGINS:
AGENT_ENGINE: default
HELPER_BOT_MODEL: qwen-max
CHAT_TITLE_MODEL:
SKIP_FILE_TYPE_CHECK: false
WECHAT_CHANNEL_CONCURRENCY: 1000
......
......@@ -130,7 +130,6 @@ x-service-env-config: &x-service-env-config
MAX_LOGIN_SESSION:
ALLOWED_ORIGINS:
AGENT_ENGINE: default
HELPER_BOT_MODEL: qwen-max
CHAT_TITLE_MODEL:
SKIP_FILE_TYPE_CHECK: false
WECHAT_CHANNEL_CONCURRENCY: 1000
......
......@@ -130,7 +130,6 @@ x-service-env-config: &x-service-env-config
MAX_LOGIN_SESSION:
ALLOWED_ORIGINS:
AGENT_ENGINE: default
HELPER_BOT_MODEL: qwen-max
CHAT_TITLE_MODEL:
SKIP_FILE_TYPE_CHECK: false
WECHAT_CHANNEL_CONCURRENCY: 1000
......
......@@ -130,7 +130,6 @@ x-service-env-config: &x-service-env-config
MAX_LOGIN_SESSION:
ALLOWED_ORIGINS:
AGENT_ENGINE: default
HELPER_BOT_MODEL: qwen-max
CHAT_TITLE_MODEL:
SKIP_FILE_TYPE_CHECK: false
WECHAT_CHANNEL_CONCURRENCY: 1000
......
......@@ -130,7 +130,6 @@ x-service-env-config: &x-service-env-config
MAX_LOGIN_SESSION:
ALLOWED_ORIGINS:
AGENT_ENGINE: default
HELPER_BOT_MODEL: qwen-max
CHAT_TITLE_MODEL:
SKIP_FILE_TYPE_CHECK: false
WECHAT_CHANNEL_CONCURRENCY: 1000
......
import {
AppTypeEnum,
defaultAppSelectFileConfig,
defaultQGConfig,
defaultWhisperConfig
} from '../../app/constants';
import type { AppQGConfigType, AppWhisperConfigType } from '../../app/type';
import type { AppFileSelectConfigType } from '../../app/type/config.schema';
export const ChatAgentHelperAppName = 'Chat Agent Helper';
export const ChatAgentHelperAvatar = '/imgs/bot.svg';
export const chatAgentHelperFileSelectConfig: AppFileSelectConfigType = {
...defaultAppSelectFileConfig,
maxFiles: 10,
canSelectFile: true,
canSelectImg: true,
canSelectVideo: true,
canSelectAudio: true,
customPdfParse: false,
canSelectCustomFileExtension: true,
customFileExtensionList: []
};
export const chatAgentHelperQuestionGuideConfig: AppQGConfigType = {
...defaultQGConfig,
open: false
};
export const chatAgentHelperWhisperConfig: AppWhisperConfigType = {
...defaultWhisperConfig,
open: false
};
/** 创建 ChatAgentHelper 专用 Chat 配置,避免调用方共享并误改全局对象。 */
export const createChatAgentHelperChatConfig = () => {
return {
fileSelectConfig: {
...chatAgentHelperFileSelectConfig,
customFileExtensionList: [...(chatAgentHelperFileSelectConfig.customFileExtensionList ?? [])]
},
questionGuide: {
...chatAgentHelperQuestionGuideConfig
},
whisperConfig: {
...chatAgentHelperWhisperConfig
}
};
};
/** 创建 ChatAgentHelper 在通用 ChatBox 中使用的轻量 App 信息。 */
export const createChatAgentHelperAppConfig = () => {
return {
chatConfig: createChatAgentHelperChatConfig(),
chatModels: [],
name: ChatAgentHelperAppName,
avatar: ChatAgentHelperAvatar,
intro: '',
type: AppTypeEnum.simple,
pluginInputs: []
};
};
export enum AuxiliaryGenerationEventEnum {
answer = 'answer',
error = 'error',
status = 'status',
interactive = 'interactive',
chatAgentConfig = 'chatAgentConfig'
}
export enum ChatAgentHelperTypeEnum {
chatAgent = 'chatAgent'
}
import z from 'zod';
import { ChatCompletionMessageParamSchema } from '../llm/type';
import { ChatFileTypeEnum } from '../../chat/constants';
import { SelectedAgentSkillItemTypeSchema } from '../../app/formEdit/type';
import { ObjectIdSchema } from '../../../common/type/mongo';
import { ChatAgentHelperTypeEnum } from './constants';
import { BoolSchema } from '../../../common/zod';
export const AuxiliaryGenerationChatFileSchema = z.object({
type: z.enum(ChatFileTypeEnum),
key: z.string(),
url: z.string().optional(),
name: z.string()
});
export type AuxiliaryGenerationChatFileType = z.infer<typeof AuxiliaryGenerationChatFileSchema>;
export const ChatAgentHelperMetadataSchema = z.object({
role: z.string().nullish(),
taskObject: z.string().nullish(),
systemPrompt: z.string().nullish(),
selectedTools: z.array(z.string()).nullish(),
selectedDatasets: z.array(z.string()).nullish(),
selectedAgentSkills: z.array(SelectedAgentSkillItemTypeSchema).nullish(),
fileUpload: z.boolean().nullish(),
enableSandbox: z.boolean().nullish(),
modelConfig: z
.object({
model: z.string().optional()
})
.optional()
});
export type ChatAgentHelperMetadataType = z.infer<typeof ChatAgentHelperMetadataSchema>;
export const AuxiliaryGenerationSelectedDatasetSchema = z.object({
datasetId: z.string().meta({
description: '可选知识库 ID'
}),
avatar: z.string().meta({
description: '可选知识库头像'
}),
name: z.string().meta({
description: '可选知识库名称'
}),
vectorModel: z.object({
model: z.string().meta({
description: '知识库使用的向量模型'
})
}),
isDeleted: BoolSchema.optional()
});
export type AuxiliaryGenerationSelectedDatasetType = z.infer<
typeof AuxiliaryGenerationSelectedDatasetSchema
>;
export const ChatAgentConfigFormDataSchema = z.object({
systemPrompt: z.string().optional(),
tools: z.array(z.string()).optional().default([]),
datasets: z.array(AuxiliaryGenerationSelectedDatasetSchema).optional().default([]),
selectedAgentSkills: z.array(SelectedAgentSkillItemTypeSchema).optional().default([]),
fileUploadEnabled: z.boolean().optional().default(false),
enableSandboxEnabled: z.boolean().optional().default(false),
executionPlan: z.any().optional()
});
export type ChatAgentConfigFormDataType = z.infer<typeof ChatAgentConfigFormDataSchema>;
export const ChatAgentHelperCompletionsParamsSchema = z
.object({
chatId: z.string(),
responseChatItemId: z.string(),
appId: ObjectIdSchema,
messages: z.array(ChatCompletionMessageParamSchema),
// ChatBox 当前仍复用 workflow interactive 数据结构;辅助生成 API 只透传给 chat round 准备逻辑。
interactive: z.any().optional(),
metadata: z.object({
type: z.literal(ChatAgentHelperTypeEnum.chatAgent),
data: ChatAgentHelperMetadataSchema
})
})
.superRefine(({ messages }, ctx) => {
const lastUserMessage = messages.findLast((message) => message.role === 'user');
if (!lastUserMessage?.dataId) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
path: ['messages'],
message: 'ChatAgentHelper messages requires a user message with dataId'
});
}
});
export type ChatAgentHelperCompletionsParamsType = z.infer<
typeof ChatAgentHelperCompletionsParamsSchema
>;
import type { LLMModelItemType } from '../model.schema';
import { ChatCompletionRequestMessageRoleEnum } from '../constants';
export const removeDatasetCiteText = (text: string, retainDatasetCite: boolean) => {
return retainDatasetCite
......@@ -15,6 +16,44 @@ export const removeDatasetCiteText = (text: string, retainDatasetCite: boolean)
export const normalizeToolResponseContent = (response?: string) =>
response === '' || response === undefined ? 'none' : response;
/**
* 构造 OpenAI Chat Completions 风格的流式 delta 响应片段。
*
* FastGPT 多个 SSE 场景都会向前端输出这种结构,统一放在 LLM 公共层避免各业务重复维护。
*/
export const createChatCompletionDeltaResponse = ({
text,
reasoningContent,
model = '',
finishReason = null,
extraData = {}
}: {
model?: string;
text?: string | null;
reasoningContent?: string | null;
finishReason?: null | 'stop';
extraData?: object;
}) => {
return {
...extraData,
id: '',
object: '',
created: 0,
model,
choices: [
{
delta: {
role: ChatCompletionRequestMessageRoleEnum.Assistant,
content: text,
...(reasoningContent ? { reasoning_content: reasoningContent } : {})
},
index: 0,
finish_reason: finishReason
}
]
};
};
export const getLLMSupportParams = (llm?: LLMModelItemType) => {
return {
vision: !!llm?.vision,
......
......@@ -81,7 +81,6 @@ export const LLMModelItemSchema = PriceTypeSchema.extend(BaseModelItemSchema.sha
isDefaultDatasetTextModel: z.boolean().optional(),
isDefaultDatasetImageModel: z.boolean().optional(),
isDefaultChatTitleModel: z.boolean().optional(),
isDefaultHelperBotModel: z.boolean().optional(),
/** @deprecated */
datasetProcess: z.boolean().optional(), // dataset
......
......@@ -45,7 +45,8 @@ export enum ChatSourceEnum {
*/
export enum ChatSourceTypeEnum {
app = 'app',
skillEdit = 'skillEdit'
skillEdit = 'skillEdit',
chatAgentHelper = 'chatAgentHelper'
}
export const ChatSourceMap = {
......
import { ChatCompletionRequestMessageRoleEnum } from '../../ai/constants';
import type { ChatCompletionContentPart, ChatCompletionMessageParam } from '../../ai/llm/type';
import { ChatFileTypeEnum, ChatRoleEnum } from '../constants';
import type { HelperBotChatItemType } from './type';
import { isFileUrlChatFileType, simpleUserContentPart } from '../adapt';
export const helperChats2GPTMessages = ({
messages
}: {
messages: HelperBotChatItemType[];
}): ChatCompletionMessageParam[] => {
let results: ChatCompletionMessageParam[] = [];
messages.forEach((item) => {
if (item.obj === ChatRoleEnum.System) {
const content = item.value?.[0]?.text?.content;
if (content) {
results.push({
role: ChatCompletionRequestMessageRoleEnum.System,
content
});
}
} else if (item.obj === ChatRoleEnum.Human) {
const value = item.value
.map((item) => {
if (item.text) {
return {
type: 'text',
text: item.text?.content || ''
};
}
if (item.file) {
if (item.file?.type === ChatFileTypeEnum.image) {
return {
type: 'image_url',
key: item.file.key,
image_url: {
url: item.file.url
}
};
} else if (isFileUrlChatFileType(item.file?.type)) {
return {
type: 'file_url',
name: item.file?.name || '',
url: item.file.url,
fileType: item.file.type,
key: item.file.key
};
}
}
})
.filter(Boolean) as ChatCompletionContentPart[];
results.push({
role: ChatCompletionRequestMessageRoleEnum.User,
content: simpleUserContentPart(value)
});
} else {
const aiResults: ChatCompletionMessageParam[] = [];
//AI: 只需要把根节点转化即可
item.value.forEach((value, i) => {
if ('collectionForm' in value) {
const text = JSON.stringify(
value.collectionForm.params.inputForm.map((item) => ({
label: item.label,
type: item.type
}))
);
// Concat text
const lastValue = item.value[i - 1];
const lastResult = aiResults[aiResults.length - 1];
if (lastValue && typeof lastResult?.content === 'string') {
lastResult.content += text;
} else {
aiResults.push({
role: ChatCompletionRequestMessageRoleEnum.Assistant,
content: text
});
}
} else if ('text' in value && typeof value.text?.content === 'string') {
if (!value.text.content && item.value.length > 1) {
return;
}
// Concat text
const lastValue = item.value[i - 1];
const lastResult = aiResults[aiResults.length - 1];
if (lastValue && typeof lastResult?.content === 'string') {
lastResult.content += value.text.content;
} else {
aiResults.push({
role: ChatCompletionRequestMessageRoleEnum.Assistant,
content: value.text.content
});
}
}
});
// Auto add empty assistant message
results = results.concat(
aiResults.length > 0
? aiResults
: [
{
role: ChatCompletionRequestMessageRoleEnum.Assistant,
content: ''
}
]
);
}
});
return results;
};
import z from 'zod';
import { SelectedDatasetSchema } from '../../../workflow/type/io';
import { SelectedAgentSkillItemTypeSchema } from '../../../app/formEdit/type';
// TopAgent 参数配置
export const topAgentParamsSchema = z.object({
role: z.string().nullish(),
taskObject: z.string().nullish(),
systemPrompt: z.string().nullish(),
selectedTools: z.array(z.string()).nullish(),
selectedDatasets: z.array(z.string()).nullish(),
selectedAgentSkills: z.array(SelectedAgentSkillItemTypeSchema).nullish(),
fileUpload: z.boolean().nullish(),
enableSandbox: z.boolean().nullish()
});
export type TopAgentParamsType = z.infer<typeof topAgentParamsSchema>;
export const TopAgentFormDataSchema = z.object({
systemPrompt: z.string().optional(),
tools: z.array(z.string()).optional().default([]),
datasets: z.array(SelectedDatasetSchema).optional().default([]),
selectedAgentSkills: z.array(SelectedAgentSkillItemTypeSchema).optional().default([]),
fileUploadEnabled: z.boolean().optional().default(false),
enableSandboxEnabled: z.boolean().optional().default(false),
executionPlan: z.any().optional()
});
export type TopAgentFormDataType = z.infer<typeof TopAgentFormDataSchema>;
import { ObjectIdSchema } from '../../../common/type/mongo';
import z from 'zod';
import { ChatRoleEnum } from '../constants';
import { UserChatItemSchema, SystemChatItemSchema } from '../type';
import { UserInputInteractiveSchema } from '../../workflow/template/system/interactive/type';
export enum HelperBotTypeEnum {
topAgent = 'topAgent'
}
export const HelperBotTypeEnumSchema = z.enum(Object.values(HelperBotTypeEnum));
export type HelperBotTypeEnumType = z.infer<typeof HelperBotTypeEnumSchema>;
export const HelperBotChatSchema = z.object({
_id: ObjectIdSchema,
chatId: z.string(),
type: HelperBotTypeEnum,
userId: z.string(),
createTime: z.coerce.date(),
updateTime: z.coerce.date(),
metadata: z.record(z.string(), z.any()).optional()
});
export type HelperBotChatType = z.infer<typeof HelperBotChatSchema>;
const AIChatContentValueSchema = z
.object({
text: z
.object({
content: z.string()
})
.optional(),
reasoning: z
.object({
content: z.string()
})
.optional(),
hideReason: z.boolean().optional()
})
.refine((value) => value.text || value.reasoning, {
message: 'HelperBot AI content value requires text or reasoning'
});
// AI schema
export const AIChatItemValueItemSchema = z.union([
AIChatContentValueSchema,
z.object({
collectionForm: UserInputInteractiveSchema
}),
z.object({
planHint: z.object({
type: z.literal('generation')
})
})
]);
export type AIChatItemValueItemType = z.infer<typeof AIChatItemValueItemSchema>;
const AIChatItemSchema = z.object({
obj: z.literal(ChatRoleEnum.AI),
value: z.array(AIChatItemValueItemSchema)
});
export type AIChatItemType = z.infer<typeof AIChatItemSchema>;
const HelperBotChatRoleSchema = z.union([
UserChatItemSchema,
SystemChatItemSchema,
AIChatItemSchema
]);
export const HelperBotChatItemSchema = z.intersection(
z.object({
_id: ObjectIdSchema,
userId: z.string(),
chatId: z.string(),
dataId: z.string(),
createTime: z.coerce.date(),
memories: z.record(z.string(), z.any()).nullish()
}),
HelperBotChatRoleSchema
);
export type HelperBotChatItemType = z.infer<typeof HelperBotChatItemSchema>;
/* 客户端 UI User Interface 展示的类型 */
export const HelperBotChatItemSiteSchema = z.intersection(
z.object({
_id: ObjectIdSchema,
dataId: z.string(),
createTime: z.coerce.date()
}),
HelperBotChatRoleSchema
);
export type HelperBotChatItemSiteType = z.infer<typeof HelperBotChatItemSiteSchema>;
......@@ -24,11 +24,7 @@ export enum SseResponseEventEnum {
// Sandbox lifecycle
sandboxStatus = 'sandboxStatus', // sandbox lifecycle phase notification
skillCall = 'skillCall', // skill invocation announce (when SKILL.md is loaded)
// Helperbot
collectionForm = 'collectionForm', // collection form for HelperBot
topAgentConfig = 'topAgentConfig' // form data for TopAgent
skillCall = 'skillCall' // skill invocation announce (when SKILL.md is loaded)
}
export const StreamResumePhaseEvent = 'resumePhase';
......
......@@ -2,7 +2,7 @@ import json5 from 'json5';
import { ChatRoleEnum } from '../../../core/chat/constants';
import type { ChatItemMiniType } from '../../../core/chat/type';
import type { NodeOutputItemType } from './type';
import { ChatCompletionRequestMessageRoleEnum } from '../../ai/constants';
import { createChatCompletionDeltaResponse } from '../../ai/llm/utils';
import {
NodeInputKeyEnum,
NodeOutputKeyEnum,
......@@ -373,24 +373,13 @@ export const textAdaptGptResponse = ({
finish_reason?: null | 'stop';
extraData?: object;
}) => {
return {
...extraData,
id: '',
object: '',
created: 0,
return createChatCompletionDeltaResponse({
text,
reasoningContent: reasoning_content,
model,
choices: [
{
delta: {
role: ChatCompletionRequestMessageRoleEnum.Assistant,
content: text,
...(reasoning_content && { reasoning_content })
},
index: 0,
finish_reason
}
]
};
finishReason: finish_reason,
extraData
});
};
/* Update runtimeNode's outputs with interactive data from history */
......
......@@ -19,12 +19,18 @@ const ChatTargetInputShape = {
skillId: ObjectIdSchema.optional().meta({
example: '68ad85a7463006c963799a06',
description: 'Skill Edit 调试 ID。appId、skillId 和 outLinkAuthData 必须且只能传一个。'
}),
sourceType: z.enum([ChatSourceTypeEnum.app, ChatSourceTypeEnum.chatAgentHelper]).optional().meta({
example: ChatSourceTypeEnum.chatAgentHelper,
description:
'appId 对应的会话资源类型。缺省为 App 会话;ChatAgentHelper 会话传 chatAgentHelper。'
})
};
type ChatTargetInput = {
appId?: unknown;
skillId?: unknown;
sourceType?: unknown;
outLinkAuthData?: unknown;
[key: string]: unknown;
};
......@@ -43,10 +49,17 @@ export type ChatTargetInputType =
| {
appId: string;
skillId?: never;
sourceType?: ChatSourceTypeEnum.app;
}
| {
appId?: never;
skillId: string;
sourceType?: never;
}
| {
appId: string;
skillId?: never;
sourceType: ChatSourceTypeEnum.chatAgentHelper;
};
export type ChatTargetResponseType = ChatTargetInputType;
......@@ -75,6 +88,13 @@ const getNormalizedOutLinkAuthData = (data: ChatTargetInput): OutLinkChatAuthPro
export const ChatTargetResponseSchema = z.union([
z.object({
appId: ObjectIdSchema.describe('应用 ID,仅 ChatAgentHelper 会话返回'),
skillId: z.undefined().optional(),
sourceType: z
.literal(ChatSourceTypeEnum.chatAgentHelper)
.describe('ChatAgentHelper 会话资源类型')
}),
z.object({
appId: ObjectIdSchema.describe('应用 ID,仅 App 会话返回'),
skillId: z.undefined().optional()
}),
......@@ -107,6 +127,10 @@ export const buildChatTargetResponse = ({
return { skillId: id };
}
if (sourceType === ChatSourceTypeEnum.chatAgentHelper) {
return { appId: id, sourceType: ChatSourceTypeEnum.chatAgentHelper };
}
const exhaustiveCheck: never = sourceType;
throw new Error(`Unsupported chat source type: ${exhaustiveCheck}`);
};
......@@ -119,6 +143,7 @@ export const refineRequiredChatTargetInput = (
data: {
appId?: unknown;
skillId?: unknown;
sourceType?: unknown;
outLinkAuthData?: unknown;
},
ctx: z.RefinementCtx
......@@ -130,6 +155,7 @@ export const refineOptionalChatTargetInput = (
data: {
appId?: unknown;
skillId?: unknown;
sourceType?: unknown;
outLinkAuthData?: unknown;
},
ctx: z.RefinementCtx
......@@ -150,6 +176,7 @@ const refineChatAuthTargetInput = (
) => {
const hasAppTarget = !!data.appId;
const hasSkillTarget = !!data.skillId;
const isChatAgentHelperTarget = data.sourceType === ChatSourceTypeEnum.chatAgentHelper;
const nestedAuthData = getNestedOutLinkAuthData(data);
const shareId = nestedAuthData.shareId;
const outLinkUid = nestedAuthData.outLinkUid;
......@@ -165,6 +192,27 @@ const refineChatAuthTargetInput = (
});
}
if ([hasAppTarget, hasSkillTarget, hasCompleteShareAuth].filter(Boolean).length > 1) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'appId, skillId and share auth cannot be provided at the same time'
});
}
if (isChatAgentHelperTarget && !hasAppTarget) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'sourceType=chatAgentHelper requires appId'
});
}
if (isChatAgentHelperTarget && hasSkillTarget) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'sourceType=chatAgentHelper cannot be used with skillId'
});
}
if (hasShareAuth && !hasCompleteShareAuth) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
......@@ -201,12 +249,18 @@ const refineChatAuthTargetInput = (
* raw schema,避免文档暴露内部 `sourceType/sourceId`。
*/
export const transformChatTargetInput = <T extends ChatTargetInput>(data: T) => {
const { appId, skillId, ...rest } = data;
const { appId, skillId, sourceType: rawSourceType, ...rest } = data;
const sourceId = (appId || skillId) as string;
return {
...rest,
sourceType: appId ? ChatSourceTypeEnum.app : ChatSourceTypeEnum.skillEdit,
sourceType: (() => {
if (skillId) return ChatSourceTypeEnum.skillEdit;
if (rawSourceType === ChatSourceTypeEnum.chatAgentHelper) {
return ChatSourceTypeEnum.chatAgentHelper;
}
return ChatSourceTypeEnum.app;
})(),
sourceId
};
};
......@@ -218,7 +272,7 @@ export const transformChatTargetInput = <T extends ChatTargetInput>(data: T) =>
* 外链上下文统一收敛到 `outLinkAuthData`,再由 `authChatTargetCrud` 解析真实 sourceId。
*/
export const transformChatAuthTargetInput = <T extends ChatTargetInput>(data: T) => {
const { appId, skillId, ...rest } = data;
const { appId, skillId, sourceType: rawSourceType, ...rest } = data;
delete rest.outLinkAuthData;
const sourceId = (appId || skillId) as string | undefined;
const normalizedOutLinkAuthData = getNormalizedOutLinkAuthData(data);
......@@ -238,7 +292,13 @@ export const transformChatAuthTargetInput = <T extends ChatTargetInput>(data: T)
return {
...rest,
...(normalizedOutLinkAuthData ? { outLinkAuthData: normalizedOutLinkAuthData } : {}),
sourceType: appId ? ChatSourceTypeEnum.app : ChatSourceTypeEnum.skillEdit,
sourceType: (() => {
if (skillId) return ChatSourceTypeEnum.skillEdit;
if (rawSourceType === ChatSourceTypeEnum.chatAgentHelper) {
return ChatSourceTypeEnum.chatAgentHelper;
}
return ChatSourceTypeEnum.app;
})(),
sourceId: sourceId!
};
};
......@@ -250,7 +310,7 @@ export const transformChatAuthTargetInput = <T extends ChatTargetInput>(data: T)
* `sourceType/sourceId`。
*/
export const transformOptionalChatTargetInput = <T extends ChatTargetInput>(data: T) => {
const { appId, skillId, ...rest } = data;
const { appId, skillId, sourceType: rawSourceType, ...rest } = data;
delete rest.outLinkAuthData;
const sourceId = (appId || skillId) as string | undefined;
const normalizedOutLinkAuthData = getNormalizedOutLinkAuthData(data);
......@@ -272,7 +332,13 @@ export const transformOptionalChatTargetInput = <T extends ChatTargetInput>(data
...(normalizedOutLinkAuthData ? { outLinkAuthData: normalizedOutLinkAuthData } : {}),
...(appId || skillId
? {
sourceType: appId ? ChatSourceTypeEnum.app : ChatSourceTypeEnum.skillEdit,
sourceType: (() => {
if (skillId) return ChatSourceTypeEnum.skillEdit;
if (rawSourceType === ChatSourceTypeEnum.chatAgentHelper) {
return ChatSourceTypeEnum.chatAgentHelper;
}
return ChatSourceTypeEnum.app;
})(),
sourceId: sourceId!
}
: {
......
export {
AuxiliaryGenerationChatFileSchema as ChatAgentHelperChatFileSchema,
ChatAgentHelperCompletionsParamsSchema,
type AuxiliaryGenerationChatFileType as ChatAgentHelperChatFileType,
type ChatAgentHelperCompletionsParamsType
} from '../../../../core/ai/auxiliaryGeneration/type';
import z from 'zod';
import type { OpenAPIPath } from '../../../type';
import { DevApiTagsMap } from '../../../tag';
import { ChatAgentHelperCompletionsParamsSchema } from './api';
export const ChatAgentHelperPath: OpenAPIPath = {
'/proApi/core/chat/chatAgentHelper/completions': {
post: {
summary: 'Chat Agent 辅助生成对话接口',
description: 'Chat Agent 辅助生成对话接口',
tags: [DevApiTagsMap.chatAgentHelper],
requestBody: {
content: {
'application/json': {
schema: ChatAgentHelperCompletionsParamsSchema
}
}
},
responses: {
200: {
description: '成功返回流式处理结果',
content: {
'application/stream+json': {
schema: z.any()
}
}
}
}
}
}
};
import { z } from 'zod';
import { PaginationResponseSchema, PaginationSchema } from '../../../api';
import { ChatFileTypeEnum } from '../../../../core/chat/constants';
import {
HelperBotChatItemSiteSchema,
HelperBotTypeEnum,
HelperBotTypeEnumSchema
} from '../../../../core/chat/helperBot/type';
import { topAgentParamsSchema } from '../../../../core/chat/helperBot/topAgent/type';
export const HelperBotCompletionsParamsSchema = z.object({
chatId: z.string(),
chatItemId: z.string(),
query: z.string(),
files: z.array(
z.object({
type: z.enum(ChatFileTypeEnum),
key: z.string(),
url: z.string().optional(),
name: z.string()
})
),
metadata: z.object({
type: z.literal(HelperBotTypeEnum.topAgent),
data: topAgentParamsSchema
})
});
export type HelperBotCompletionsParamsType = z.infer<typeof HelperBotCompletionsParamsSchema>;
// 分页获取记录
export const GetHelperBotChatRecordsParamsSchema = PaginationSchema.extend({
type: HelperBotTypeEnumSchema,
chatId: z.string()
});
export type GetHelperBotChatRecordsParamsType = z.infer<typeof GetHelperBotChatRecordsParamsSchema>;
export const GetHelperBotChatRecordsResponseSchema = PaginationResponseSchema(
HelperBotChatItemSiteSchema
);
export type GetHelperBotChatRecordsResponseType = z.infer<
typeof GetHelperBotChatRecordsResponseSchema
>;
// 删除单组对话
export const DeleteHelperBotChatParamsSchema = z.object({
type: HelperBotTypeEnumSchema,
chatId: z.string(),
chatItemId: z.string()
});
export type DeleteHelperBotChatParamsType = z.infer<typeof DeleteHelperBotChatParamsSchema>;
// 获取文件上传签名
export const GetHelperBotFilePresignParamsSchema = z.object({
type: HelperBotTypeEnumSchema,
chatId: z.string(),
filename: z.string()
});
export type GetHelperBotFilePresignParamsType = z.infer<typeof GetHelperBotFilePresignParamsSchema>;
// 获取文件预览链接
export const GetHelperBotFilePreviewParamsSchema = z.object({
key: z.string().min(1),
mode: z.enum(['proxy', 'presigned']).optional()
});
export type GetHelperBotFilePreviewParamsType = z.infer<typeof GetHelperBotFilePreviewParamsSchema>;
export const GetHelperBotFilePreviewResponseSchema = z.string();
import z from 'zod';
import type { OpenAPIPath } from '../../../type';
import { DevApiTagsMap } from '../../../tag';
import {
HelperBotCompletionsParamsSchema,
DeleteHelperBotChatParamsSchema,
GetHelperBotFilePresignParamsSchema,
GetHelperBotFilePreviewParamsSchema,
GetHelperBotFilePreviewResponseSchema,
GetHelperBotChatRecordsParamsSchema,
GetHelperBotChatRecordsResponseSchema
} from './api';
import { CreatePostPresignedUrlResponseSchema } from '../../../../common/file/s3/type';
export const HelperBotPath: OpenAPIPath = {
'/proApi/core/chat/helperBot/completions': {
post: {
summary: '辅助生成统一对话接口',
description: '辅助生成统一对话接口',
tags: [DevApiTagsMap.helperBot],
requestBody: {
content: {
'application/json': {
schema: HelperBotCompletionsParamsSchema
}
}
},
responses: {
200: {
description: '成功返回流式处理结果',
content: {
'application/stream+json': {
schema: z.any()
}
}
}
}
}
},
'/core/chat/helperBot/getRecords': {
get: {
summary: '分页获取记录',
description: '分页获取记录',
tags: [DevApiTagsMap.helperBot],
requestParams: {
query: GetHelperBotChatRecordsParamsSchema
},
responses: {
200: {
description: '成功返回记录列表',
content: {
'application/json': {
schema: GetHelperBotChatRecordsResponseSchema
}
}
}
}
}
},
'/core/chat/helperBot/deleteRecord': {
delete: {
summary: '删除单组对话',
description: '删除单组对话',
tags: [DevApiTagsMap.helperBot],
requestBody: {
content: {
'application/json': {
schema: DeleteHelperBotChatParamsSchema
}
}
},
responses: {
200: {
description: '成功删除记录',
content: {
'application/json': {
schema: z.any()
}
}
}
}
}
},
'/core/chat/helperBot/getFilePresign': {
post: {
summary: '获取辅助助手文件上传签名',
description: '获取辅助助手文件上传签名',
tags: [DevApiTagsMap.helperBot],
requestBody: {
content: {
'application/json': {
schema: GetHelperBotFilePresignParamsSchema
}
}
},
responses: {
200: {
description: '成功返回文件上传签名',
content: {
'application/json': {
schema: CreatePostPresignedUrlResponseSchema
}
}
}
}
}
},
'/core/chat/helperBot/getFilePreviewUrl': {
post: {
summary: '获取辅助助手文件预览链接',
description: '获取辅助助手文件预览链接',
tags: [DevApiTagsMap.helperBot],
requestBody: {
content: {
'application/json': {
schema: GetHelperBotFilePreviewParamsSchema
}
}
},
responses: {
200: {
description: '成功返回文件预览链接',
content: {
'application/json': {
schema: GetHelperBotFilePreviewResponseSchema
}
}
}
}
}
}
};
......@@ -6,7 +6,7 @@ import { ChatHistoryPath } from './history/index';
import { GetRecentlyUsedAppsResponseSchema } from './api';
import { DevApiTagsMap } from '../../tag';
import { ChatControllerPath } from './controler';
import { HelperBotPath } from './helperBot';
import { ChatAgentHelperPath } from './chatAgentHelper';
import { ChatInputGuidePath } from './inputGuide/index';
import { OutLinkChatPath } from './outLink/index';
import { ChatRecordPath } from './record/index';
......@@ -20,7 +20,7 @@ export const ChatPath: OpenAPIPath = {
...ChatFavouriteAppPath,
...ChatHistoryPath,
...ChatControllerPath,
...HelperBotPath,
...ChatAgentHelperPath,
...ChatInputGuidePath,
...OutLinkChatPath,
...ChatRecordPath,
......
......@@ -101,6 +101,6 @@ export const openAPITagGroups = [
},
{
name: '系统接口',
tags: [DevApiTagsMap.helperBot]
tags: [DevApiTagsMap.chatAgentHelper]
}
];
......@@ -58,7 +58,7 @@ export const DevApiTagsMap = {
/* 通用-基础功能 */
apiKey: 'API Key 管理',
customDomain: '自定义域名',
helperBot: '辅助助手',
chatAgentHelper: 'Chat Agent 辅助生成',
/* 辅助-团队管理 */
teamManage: '团队管理',
......
import { describe, expect, it } from 'vitest';
import { helperChats2GPTMessages } from '@fastgpt/global/core/chat/helperBot/adaptor';
import { ChatRoleEnum, ChatFileTypeEnum } from '@fastgpt/global/core/chat/constants';
import { ChatCompletionRequestMessageRoleEnum } from '@fastgpt/global/core/ai/constants';
import type { HelperBotChatItemType } from '@fastgpt/global/core/chat/helperBot/type';
describe('helperChats2GPTMessages', () => {
it('should convert system message', () => {
const messages = [
{
obj: ChatRoleEnum.System,
value: [{ text: { content: 'You are a helpful assistant' } }]
}
] as HelperBotChatItemType[];
const result = helperChats2GPTMessages({ messages });
expect(result).toHaveLength(1);
expect(result[0].role).toBe(ChatCompletionRequestMessageRoleEnum.System);
expect(result[0].content).toBe('You are a helpful assistant');
});
it('should skip system message with empty content', () => {
const messages = [
{
obj: ChatRoleEnum.System,
value: [{ text: { content: '' } }]
}
] as HelperBotChatItemType[];
const result = helperChats2GPTMessages({ messages });
expect(result).toHaveLength(0);
});
it('should convert human message with text', () => {
const messages = [
{
obj: ChatRoleEnum.Human,
value: [{ text: { content: 'Hello' } }]
}
] as HelperBotChatItemType[];
const result = helperChats2GPTMessages({ messages });
expect(result).toHaveLength(1);
expect(result[0].role).toBe(ChatCompletionRequestMessageRoleEnum.User);
expect(result[0].content).toBe('Hello');
});
it('should convert human message with image', () => {
const messages = [
{
obj: ChatRoleEnum.Human,
value: [
{
file: {
type: ChatFileTypeEnum.image,
name: 'test.png',
url: 'http://example.com/test.png',
key: 'test-key'
}
}
]
}
] as HelperBotChatItemType[];
const result = helperChats2GPTMessages({ messages });
expect(result).toHaveLength(1);
expect(result[0].role).toBe(ChatCompletionRequestMessageRoleEnum.User);
expect(Array.isArray(result[0].content)).toBe(true);
const content = result[0].content as any[];
expect(content[0].type).toBe('image_url');
expect(content[0].image_url.url).toBe('http://example.com/test.png');
});
it('should convert human message with file', () => {
const messages = [
{
obj: ChatRoleEnum.Human,
value: [
{
file: {
type: ChatFileTypeEnum.file,
name: 'document.pdf',
url: 'http://example.com/document.pdf',
key: 'doc-key'
}
}
]
}
] as HelperBotChatItemType[];
const result = helperChats2GPTMessages({ messages });
expect(result).toHaveLength(1);
const content = result[0].content as any[];
expect(content[0].type).toBe('file_url');
expect(content[0].name).toBe('document.pdf');
});
it('should convert AI message with text', () => {
const messages = [
{
obj: ChatRoleEnum.AI,
value: [{ text: { content: 'Hello, how can I help?' } }]
}
] as HelperBotChatItemType[];
const result = helperChats2GPTMessages({ messages });
expect(result).toHaveLength(1);
expect(result[0].role).toBe(ChatCompletionRequestMessageRoleEnum.Assistant);
expect(result[0].content).toBe('Hello, how can I help?');
});
it('should convert merged AI reasoning and text value with visible text', () => {
const messages = [
{
obj: ChatRoleEnum.AI,
value: [
{
reasoning: { content: 'Hidden thinking' },
hideReason: true,
text: { content: 'Visible answer' }
}
]
}
] as HelperBotChatItemType[];
const result = helperChats2GPTMessages({ messages });
expect(result).toHaveLength(1);
expect(result[0].role).toBe(ChatCompletionRequestMessageRoleEnum.Assistant);
expect(result[0].content).toBe('Visible answer');
});
it('should concat multiple AI text values', () => {
const messages = [
{
obj: ChatRoleEnum.AI,
value: [{ text: { content: 'Part 1' } }, { text: { content: ' Part 2' } }]
}
] as HelperBotChatItemType[];
const result = helperChats2GPTMessages({ messages });
expect(result).toHaveLength(1);
expect(result[0].content).toBe('Part 1 Part 2');
});
it('should add empty assistant message when AI has no content', () => {
const messages = [
{
obj: ChatRoleEnum.AI,
value: []
}
] as unknown as HelperBotChatItemType[];
const result = helperChats2GPTMessages({ messages });
expect(result).toHaveLength(1);
expect(result[0].role).toBe(ChatCompletionRequestMessageRoleEnum.Assistant);
expect(result[0].content).toBe('');
});
it('should handle mixed message types', () => {
const messages = [
{
obj: ChatRoleEnum.System,
value: [{ text: { content: 'System prompt' } }]
},
{
obj: ChatRoleEnum.Human,
value: [{ text: { content: 'User question' } }]
},
{
obj: ChatRoleEnum.AI,
value: [{ text: { content: 'AI response' } }]
}
] as HelperBotChatItemType[];
const result = helperChats2GPTMessages({ messages });
expect(result).toHaveLength(3);
expect(result[0].role).toBe(ChatCompletionRequestMessageRoleEnum.System);
expect(result[1].role).toBe(ChatCompletionRequestMessageRoleEnum.User);
expect(result[2].role).toBe(ChatCompletionRequestMessageRoleEnum.Assistant);
});
it('should handle collectionForm in AI message', () => {
const messages = [
{
obj: ChatRoleEnum.AI,
value: [
{
collectionForm: {
type: 'userInput',
params: {
description: 'Please fill the form',
inputForm: [
{ label: 'Name', type: 'input', key: 'name', required: true, value: '' }
]
}
}
}
]
}
] as HelperBotChatItemType[];
const result = helperChats2GPTMessages({ messages });
expect(result).toHaveLength(1);
expect(result[0].role).toBe(ChatCompletionRequestMessageRoleEnum.Assistant);
expect(typeof result[0].content).toBe('string');
expect(result[0].content).toContain('Name');
});
});
import { describe, expect, it } from 'vitest';
import { topAgentParamsSchema } from '@fastgpt/global/core/chat/helperBot/topAgent/type';
describe('topAgentParamsSchema', () => {
it('should validate empty object', () => {
const result = topAgentParamsSchema.safeParse({});
expect(result.success).toBe(true);
});
it('should validate full params', () => {
const result = topAgentParamsSchema.safeParse({
role: 'assistant',
taskObject: 'Help user with coding',
systemPrompt: 'You are a helpful assistant',
selectedTools: ['tool1', 'tool2'],
selectedDatasets: ['dataset1'],
selectedAgentSkills: [
{
skillId: 'skill1',
name: 'Research Skill',
description: 'Research workflow',
isDeleted: false
}
],
fileUpload: true
});
expect(result.success).toBe(true);
});
it('should validate partial params', () => {
const result = topAgentParamsSchema.safeParse({
role: 'assistant',
fileUpload: false
});
expect(result.success).toBe(true);
});
it('should allow null values for optional fields', () => {
const result = topAgentParamsSchema.safeParse({
role: null,
taskObject: null,
systemPrompt: null,
selectedTools: null,
selectedDatasets: null,
selectedAgentSkills: null,
fileUpload: null
});
expect(result.success).toBe(true);
});
it('should validate selectedTools as string array', () => {
const result = topAgentParamsSchema.safeParse({
selectedTools: ['tool1', 'tool2', 'tool3']
});
expect(result.success).toBe(true);
if (result.success) {
expect(result.data.selectedTools).toHaveLength(3);
}
});
it('should validate selectedDatasets as string array', () => {
const result = topAgentParamsSchema.safeParse({
selectedDatasets: ['dataset1', 'dataset2']
});
expect(result.success).toBe(true);
if (result.success) {
expect(result.data.selectedDatasets).toHaveLength(2);
}
});
it('should validate selectedAgentSkills as selected skill item array', () => {
const result = topAgentParamsSchema.safeParse({
selectedAgentSkills: [
{
skillId: 'skill1',
name: 'Research Skill',
description: 'Research workflow'
}
]
});
expect(result.success).toBe(true);
if (result.success) {
expect(result.data.selectedAgentSkills).toHaveLength(1);
expect(result.data.selectedAgentSkills?.[0]?.isDeleted).toBe(false);
}
});
it('should reject invalid selectedTools type', () => {
const result = topAgentParamsSchema.safeParse({
selectedTools: 'not-an-array'
});
expect(result.success).toBe(false);
});
it('should reject invalid selectedAgentSkills type', () => {
const result = topAgentParamsSchema.safeParse({
selectedAgentSkills: ['skill1']
});
expect(result.success).toBe(false);
});
it('should reject invalid fileUpload type', () => {
const result = topAgentParamsSchema.safeParse({
fileUpload: 'not-a-boolean'
});
expect(result.success).toBe(false);
});
});
import { describe, expect, it } from 'vitest';
import {
HelperBotTypeEnum,
HelperBotTypeEnumSchema,
HelperBotChatSchema,
HelperBotChatItemSchema,
HelperBotChatItemSiteSchema,
AIChatItemValueItemSchema
} from '@fastgpt/global/core/chat/helperBot/type';
describe('HelperBotTypeEnum', () => {
it('should have correct enum values', () => {
expect(HelperBotTypeEnum.topAgent).toBe('topAgent');
});
it('should have all expected types', () => {
const types = Object.values(HelperBotTypeEnum);
expect(types).toHaveLength(1);
expect(types).toContain('topAgent');
});
});
describe('HelperBotTypeEnumSchema', () => {
it('should validate valid enum values', () => {
const result = HelperBotTypeEnumSchema.safeParse('topAgent');
expect(result.success).toBe(true);
});
it('should reject invalid enum values', () => {
const result = HelperBotTypeEnumSchema.safeParse('invalid');
expect(result.success).toBe(false);
});
});
describe('AIChatItemValueItemSchema', () => {
it('should validate text content', () => {
const result = AIChatItemValueItemSchema.safeParse({
text: { content: 'Hello' }
});
expect(result.success).toBe(true);
});
it('should validate reasoning content', () => {
const result = AIChatItemValueItemSchema.safeParse({
reasoning: { content: 'Let me think...' }
});
expect(result.success).toBe(true);
});
it('should validate merged text and hidden reasoning content', () => {
const result = AIChatItemValueItemSchema.safeParse({
reasoning: { content: 'Hidden thinking' },
hideReason: true,
text: { content: 'Visible answer' }
});
expect(result.success).toBe(true);
});
it('should reject content value without text or reasoning', () => {
const result = AIChatItemValueItemSchema.safeParse({
hideReason: true
});
expect(result.success).toBe(false);
});
it('should validate planHint', () => {
const result = AIChatItemValueItemSchema.safeParse({
planHint: { type: 'generation' }
});
expect(result.success).toBe(true);
});
it('should reject invalid planHint type', () => {
const result = AIChatItemValueItemSchema.safeParse({
planHint: { type: 'invalid' }
});
expect(result.success).toBe(false);
});
});
......@@ -59,6 +59,21 @@ describe('openapi/core/chat target schema', () => {
expect('skillId' in result).toBe(false);
});
it('transforms raw appId with chatAgentHelper sourceType to internal chatAgentHelper source', () => {
const result = StopV2ChatSchema.parse({
appId,
sourceType: ChatSourceTypeEnum.chatAgentHelper,
chatId: 'chat-1'
});
expect(result).toMatchObject({
sourceType: ChatSourceTypeEnum.chatAgentHelper,
sourceId: appId,
chatId: 'chat-1'
});
expect('appId' in result).toBe(false);
});
it('transforms question guide skillId to internal skillEdit source with debug model config', () => {
const result = CreateQuestionGuideV2BodySchema.parse({
skillId,
......@@ -81,7 +96,7 @@ describe('openapi/core/chat target schema', () => {
expect('skillId' in result).toBe(false);
});
it('rejects required target when appId and skillId are both provided', () => {
it('rejects required target when multiple explicit targets are provided', () => {
const payload = {
appId,
skillId,
......@@ -90,6 +105,13 @@ describe('openapi/core/chat target schema', () => {
expect(() => MarkChatReadBodySchema.parse(payload)).toThrow();
expect(() =>
StopV2ChatSchema.parse({
skillId,
sourceType: ChatSourceTypeEnum.chatAgentHelper,
chatId: 'chat-1'
})
).toThrow();
expect(() =>
GetPaginationRecordsBodySchema.parse({
...payload,
pageSize: 10,
......
import type { NextApiResponse } from 'next';
import { responseWrite } from './index';
export type SseStreamWriter = (params: { event?: string; data: string }) => void;
export type SseStreamResumeMirror = {
enqueueRaw?: (chunk: string) => Promise<void> | void;
flush?: () => Promise<void>;
shrinkTTLAfterComplete?: () => Promise<void>;
};
type CreateSseStreamContextParams = {
res?: NextApiResponse;
stream?: boolean;
streamResumeMirror?: SseStreamResumeMirror;
heartbeat?: {
intervalMs?: number;
write: (writer: SseStreamWriter) => void;
};
onCleanup?: () => void;
onError?: () => void;
};
const isResponseClosed = (res: NextApiResponse) =>
!!(res.closed || res.writableEnded || res.destroyed);
/**
* 创建通用 SSE 协议上下文。
*
* 该 helper 只处理 header、原始 chunk 写入、resume mirror、心跳和连接清理;
* 不理解 workflow、chat 或 auxiliary generation 的事件语义。
*/
export const createSseStreamContext = ({
res,
stream = true,
streamResumeMirror,
heartbeat,
onCleanup,
onError
}: CreateSseStreamContextParams) => {
const write: SseStreamWriter = ({ event, data }) => {
if (!stream) return;
const raw = `${event ? `event: ${event}\n` : ''}data: ${data}\n\n`;
void streamResumeMirror?.enqueueRaw?.(raw);
if (!res || isResponseClosed(res)) return;
responseWrite({ res, event, data });
};
if (!stream || !res) {
return {
write,
cleanup: () => undefined,
async flushResume() {
await streamResumeMirror?.flush?.();
await streamResumeMirror?.shrinkTTLAfterComplete?.();
}
};
}
if (!res.headersSent) {
res.setHeader('Connection', 'keep-alive');
res.setHeader('Content-Type', 'text/event-stream;charset=utf-8');
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('X-Accel-Buffering', 'no');
res.setHeader('Cache-Control', 'no-cache, no-transform');
}
let cleaned = false;
const streamCheckTimer = heartbeat
? setInterval(() => {
heartbeat.write(write);
}, heartbeat.intervalMs ?? 10000)
: undefined;
const cleanup = () => {
if (cleaned) return;
cleaned = true;
if (streamCheckTimer) {
clearInterval(streamCheckTimer);
}
onCleanup?.();
};
res.once('finish', cleanup);
res.once('close', cleanup);
res.on('error', () => {
cleanup();
onError?.();
res.end();
});
return {
write,
cleanup,
async flushResume() {
await streamResumeMirror?.flush?.();
await streamResumeMirror?.shrinkTTLAfterComplete?.();
}
};
};
......@@ -26,14 +26,7 @@ export const UploadConstraintsInputSchema = z.object({
});
export type UploadConstraintsInput = z.infer<typeof UploadConstraintsInputSchema>;
export const S3SourcesSchema = z.enum([
'avatar',
'chat',
'dataset',
'temp',
'rawText',
'helperBot'
]);
export const S3SourcesSchema = z.enum(['avatar', 'chat', 'dataset', 'temp', 'rawText']);
export const S3Sources = S3SourcesSchema.enum;
export type S3SourceType = z.infer<typeof S3SourcesSchema>;
......
import type { S3HelperBotSource } from './index';
declare global {
var helperBotBucket: S3HelperBotSource;
}
export {};
import { parseFileExtensionFromUrl } from '@fastgpt/global/common/string/tools';
import { S3PrivateBucket } from '../../buckets/private';
import { S3Sources } from '../../contracts/type';
import {
type CheckHelperBotFileKeys,
type DelChatFileByPrefixParams,
DelChatFileByPrefixSchema,
HelperBotFileUploadSchema
} from './type';
import { differenceInHours } from 'date-fns';
import { S3Buckets } from '../../config/constants';
import path from 'path';
import { getFileS3Key } from '../../utils';
import { parseHelperBotFileS3Key } from './key';
export class S3HelperBotSource extends S3PrivateBucket {
private static instance: S3HelperBotSource;
constructor() {
super();
}
static getInstance() {
return (this.instance ??= new S3HelperBotSource());
}
static parseFileUrl(url: string | URL) {
try {
const parseUrl = new URL(url);
const pathname = decodeURIComponent(parseUrl.pathname);
// 非 S3 key
if (!pathname.startsWith(`/${S3Buckets.private}/${S3Sources.helperBot}/`)) {
return {
filename: '',
extension: '',
imageParsePrefix: ''
};
}
const filename = pathname.split('/').pop() || 'file';
const extension = path.extname(filename);
return {
filename,
extension: extension.replace('.', ''),
imageParsePrefix: `${pathname.replace(`/${S3Buckets.private}/`, '').replace(extension, '')}-parsed`
};
} catch (error) {
return {
filename: '',
extension: '',
imageParsePrefix: ''
};
}
}
parseKey(key: string) {
return parseHelperBotFileS3Key(key);
}
async createGetFileURL(params: {
key: string;
expiredHours?: number;
external: boolean;
mode?: 'proxy' | 'presigned';
}) {
const { key, expiredHours = 1, external = false, mode } = params; // 默认一个小时
if (external) {
return await this.createExternalUrl({ key, expiredHours, mode });
}
return await this.createPreviewUrl({ key, expiredHours });
}
async createUploadFileURL(params: CheckHelperBotFileKeys) {
const { type, chatId, userId, filename, expiredTime } = HelperBotFileUploadSchema.parse(params);
const { fileKey } = getFileS3Key.helperBot({ type, chatId, userId, filename });
return await this.createPresignedPutUrl(
{ rawKey: fileKey, filename },
{ expiredHours: expiredTime ? differenceInHours(expiredTime, new Date()) : 1 }
);
}
deleteFilesByPrefix(params: DelChatFileByPrefixParams) {
const { type, chatId, userId } = DelChatFileByPrefixSchema.parse(params);
const prefix = [S3Sources.helperBot, type, userId, chatId].filter(Boolean).join('/');
return this.addDeleteJob({ prefix });
}
deleteFileByKey(key: string) {
return this.addDeleteJob({ key });
}
}
export function getS3HelperBotSource() {
if (global.helperBotBucket) {
return global.helperBotBucket;
}
global.helperBotBucket = new S3HelperBotSource();
return global.helperBotBucket;
}
import type { HelperBotTypeEnumType } from '@fastgpt/global/core/chat/helperBot/type';
import { HelperBotTypeEnumSchema } from '@fastgpt/global/core/chat/helperBot/type';
import { isS3ObjectKey } from '../../utils';
/**
* 解析 HelperBot 文件 key。
*
* HelperBot 文件 key 的第一段是固定 source,后续才是 type/user/chat 维度。
*/
export function parseHelperBotFileS3Key(key: string): {
type: HelperBotTypeEnumType;
userId: string;
chatId: string;
filename: string;
} | null {
if (!isS3ObjectKey(key, 'helperBot')) return null;
const [, type, userId, chatId, ...filenameParts] = key.split('/');
const filename = filenameParts.join('/');
const parsedType = HelperBotTypeEnumSchema.safeParse(type);
if (!parsedType.success || !userId || !chatId || !filename) return null;
return {
type: parsedType.data,
userId,
chatId,
filename
};
}
/**
* 判断 HelperBot 文件 key 是否属于当前用户。
*/
export function isAuthorizedHelperBotFileS3Key({ key, userId }: { key: string; userId: string }) {
const parsedKey = parseHelperBotFileS3Key(key);
return !!parsedKey && String(parsedKey.userId) === String(userId);
}
import z from 'zod';
import { HelperBotTypeEnumSchema } from '@fastgpt/global/core/chat/helperBot/type';
export const HelperBotFileUploadSchema = z.object({
type: HelperBotTypeEnumSchema,
chatId: z.string().nonempty(),
userId: z.string().nonempty(),
filename: z.string().nonempty(),
expiredTime: z.coerce.date().optional()
});
export type CheckHelperBotFileKeys = z.infer<typeof HelperBotFileUploadSchema>;
export const DelChatFileByPrefixSchema = z.object({
type: HelperBotTypeEnumSchema,
chatId: z.string().nonempty().optional(),
userId: z.string().nonempty().optional()
});
export type DelChatFileByPrefixParams = z.infer<typeof DelChatFileByPrefixSchema>;
......@@ -10,7 +10,6 @@ import { S3PublicBucket } from './buckets/public';
import { getNanoid } from '@fastgpt/global/common/string/tools';
import path from 'node:path';
import type { ParsedFileContentS3KeyParams } from './sources/dataset/type';
import type { HelperBotTypeEnumType } from '@fastgpt/global/core/chat/helperBot/type';
// S3文件名最大长度配置
export const S3_FILENAME_MAX_LENGTH = 50;
......@@ -234,25 +233,6 @@ export const getFileS3Key = {
};
},
helperBot: ({
type,
chatId,
userId,
filename
}: {
type: HelperBotTypeEnumType;
chatId: string;
userId: string;
filename: string;
}) => {
const { formatedFilename, extension } = getFormatedFilename(filename);
const basePrefix = [S3Sources.helperBot, type, userId, chatId].filter(Boolean).join('/');
return {
fileKey: [basePrefix, `${formatedFilename}${extension ? `.${extension}` : ''}`].join('/'),
fileParsedPrefix: [basePrefix, `${formatedFilename}-parsed`].join('/')
};
},
// 上传数据集的文件的解析结果的图片的 Key
dataset: (params: ParsedFileContentS3KeyParams) => {
const { datasetId, filename } = params;
......
import type { ChatCompletionMessageParam } from '@fastgpt/global/core/ai/llm/type';
import { runUnifiedAgentLoop, createUpdatePlanTool } from '../llm/agentLoop';
import type { ChatNodeUsageType } from '@fastgpt/global/support/wallet/bill/type';
import type { AuxiliaryGenerationStreamWriter } from './stream';
import { AuxiliaryGenerationEventEnum } from '@fastgpt/global/core/ai/auxiliaryGeneration/constants';
import { createChatCompletionDeltaResponse } from '@fastgpt/global/core/ai/llm/utils';
type RunAuxiliaryGenerationAgentLoopParams = {
teamId: string;
model: string;
systemPrompt: string;
messages: ChatCompletionMessageParam[];
useVision?: boolean;
useAudio?: boolean;
useVideo?: boolean;
streamWriter?: AuxiliaryGenerationStreamWriter;
checkIsStopping?: () => boolean;
usageSink?: (usages: ChatNodeUsageType[]) => void;
};
/**
* 运行辅助生成的无工具 Agent Loop。
*
* 该入口只封装统一 agent loop 的模型循环和计划维护,不注册 runtime tools,
* 因此不会隐式获得 workflow、Skill 或虚拟机执行能力。
*/
export async function runAuxiliaryGenerationAgentLoop({
teamId,
model,
systemPrompt,
messages,
useVision,
useAudio,
useVideo,
streamWriter,
checkIsStopping,
usageSink
}: RunAuxiliaryGenerationAgentLoopParams) {
const result = await runUnifiedAgentLoop({
runtime: {
teamId,
model,
stream: true,
useVision,
useAudio,
useVideo,
toolCatalog: {
runtimeTools: [],
updatePlanTool: createUpdatePlanTool()
},
executeTool: async () => {
throw new Error('Auxiliary generation does not support runtime tools');
},
checkIsStopping,
emitEvent: (event) => {
if (event.type === 'reasoning_delta') {
streamWriter?.({
event: AuxiliaryGenerationEventEnum.answer,
data: createChatCompletionDeltaResponse({ reasoningContent: event.text })
});
}
},
usageSink
},
input: {
systemPrompt,
messages
}
});
return {
status: result.status,
answerText: result.answerText ?? '',
reasoningText: result.reasoningText
};
}
export * from './type';
export * from './stream';
export * from './usage';
export * from './agentLoop';
export * from './service';
export * from './stop';
import { createAuxiliaryGenerationStream } from './stream';
import { createAuxiliaryGenerationUsage } from './usage';
import { clearAuxiliaryGenerationStop, shouldAuxiliaryGenerationStop } from './stop';
import type { AuxiliaryGenerationRunParams, AuxiliaryGenerationRunResult } from './type';
/**
* 执行一次辅助生成。
*
* 该函数负责鉴权之后的通用生命周期:余额校验、用量记录、SSE 初始化、
* stop 标记、processor 调用和结束事件。业务差异由调用方传入的 processor 承担。
*/
export const runAuxiliaryGeneration = async <T>({
req,
res,
teamId,
tmbId,
userId,
isRoot,
lang,
appName,
sourceType,
sourceId,
chatId,
query,
files,
data,
histories,
usageSource,
processor,
maxFiles,
customPdfParse,
onStreamContextReady
}: AuxiliaryGenerationRunParams<T>): Promise<
AuxiliaryGenerationRunResult & {
streamContext: Awaited<ReturnType<typeof createAuxiliaryGenerationStream>>;
}
> => {
let stopping = false;
const startedAt = Date.now();
const streamContext = await createAuxiliaryGenerationStream({
req,
res,
teamId,
sourceType,
sourceId,
chatId
});
onStreamContextReady?.(streamContext);
const usageContext = await createAuxiliaryGenerationUsage({
teamId,
tmbId,
appName,
sourceType,
sourceId,
usageSource
});
await clearAuxiliaryGenerationStop({ sourceType, sourceId, chatId });
res.once('close', () => {
stopping = true;
});
const stopCheckTimer = setInterval(async () => {
if (stopping) return;
stopping = await shouldAuxiliaryGenerationStop({ sourceType, sourceId, chatId });
}, 100);
try {
const result = await processor({
query,
files,
data,
histories,
requestOrigin: req.headers.origin,
streamWriter: streamContext.write,
checkIsStopping: () => stopping,
usageSink: usageContext.pushUsage,
maxFiles,
customPdfParse,
user: {
teamId,
tmbId,
userId,
isRoot,
lang
}
});
streamContext.writeDone();
return {
...result,
durationSeconds: +((Date.now() - startedAt) / 1000).toFixed(2),
streamContext
};
} finally {
clearInterval(stopCheckTimer);
await clearAuxiliaryGenerationStop({ sourceType, sourceId, chatId });
}
};
import type { ChatSourceTypeEnum } from '@fastgpt/global/core/chat/constants';
import { getGlobalRedisConnection } from '../../../common/redis';
const RUNTIME_STOP_KEY_PREFIX = 'agent_runtime_stopping';
type AuxiliaryGenerationStopParams = {
sourceType: ChatSourceTypeEnum;
sourceId: string;
chatId: string;
};
const getAuxiliaryGenerationStopKey = ({
sourceType,
sourceId,
chatId
}: AuxiliaryGenerationStopParams) =>
`${RUNTIME_STOP_KEY_PREFIX}:${sourceType}:${sourceId}:${chatId}`;
/**
* 读取 `/v2/chat/stop` 写入的运行态停止标记。
*
* 该 key 目前由 workflow status 模块写入;辅助生成不依赖 workflow 执行器,
* 但必须识别同一个停止信号,保证 ChatBox 的停止按钮对 ChatAgentHelper 生效。
*/
export const shouldAuxiliaryGenerationStop = async (params: AuxiliaryGenerationStopParams) => {
const redis = getGlobalRedisConnection();
return redis
.get(getAuxiliaryGenerationStopKey(params))
.then((res) => !!res)
.catch(() => false);
};
/**
* 清理运行态停止标记,让 `/v2/chat/stop` 的等待逻辑能判断生成已经结束。
*/
export const clearAuxiliaryGenerationStop = async (params: AuxiliaryGenerationStopParams) => {
const redis = getGlobalRedisConnection();
await redis.del(getAuxiliaryGenerationStopKey(params)).catch(() => undefined);
};
import type { NextApiRequest, NextApiResponse } from 'next';
import { AuxiliaryGenerationEventEnum } from '@fastgpt/global/core/ai/auxiliaryGeneration/constants';
import type { ChatSourceTypeEnum } from '@fastgpt/global/core/chat/constants';
import { STREAM_RESUME_REQUEST_HEADER } from '@fastgpt/global/core/chat/constants';
import { getSseErrorResponse } from '../../../common/response';
import { createSseStreamContext } from '../../../common/response/sse';
import { clearCookie } from '../../../support/permission/auth/common';
import { getStreamResumeMirror } from '../../chat/resume';
import { createChatCompletionDeltaResponse } from '@fastgpt/global/core/ai/llm/utils';
export type AuxiliaryGenerationStreamWriter = (params: {
event?: `${AuxiliaryGenerationEventEnum}` | string;
data: string | object;
}) => void;
type CreateAuxiliaryGenerationStreamParams = {
req: NextApiRequest;
res: NextApiResponse;
teamId: string;
sourceType: ChatSourceTypeEnum;
sourceId: string;
chatId: string;
};
export type AuxiliaryGenerationStreamContext = {
write: AuxiliaryGenerationStreamWriter;
writeError: (error: unknown) => void;
writeDone: () => void;
flushResume: () => Promise<void>;
};
/**
* 初始化辅助生成 SSE 响应。
*
* 这里只处理通用 SSE 协议、心跳和 chat stream resume mirror,不引入 workflow writer。
* 调用方需要显式写入 answer/interactive/config 等业务事件。
*/
export const createAuxiliaryGenerationStream = async ({
req,
res,
teamId,
sourceType,
sourceId,
chatId
}: CreateAuxiliaryGenerationStreamParams): Promise<AuxiliaryGenerationStreamContext> => {
const mirror = await getStreamResumeMirror({
resumeRequestHeaderValue: req.headers?.[STREAM_RESUME_REQUEST_HEADER],
teamId,
sourceType,
sourceId,
chatId
});
const sseContext = createSseStreamContext({
res,
streamResumeMirror: mirror,
heartbeat: {
write: (writer) => {
writer({
event: AuxiliaryGenerationEventEnum.answer,
data: JSON.stringify(createChatCompletionDeltaResponse({ text: '' }))
});
}
}
});
const write: AuxiliaryGenerationStreamWriter = ({ event, data }) => {
const payload = typeof data === 'string' ? data : JSON.stringify(data);
sseContext.write({ event, data: payload });
};
return {
write,
writeError(error) {
const { data, shouldClearCookie } = getSseErrorResponse(error);
if (shouldClearCookie) {
clearCookie(res);
}
write({
event: AuxiliaryGenerationEventEnum.error,
data
});
},
writeDone() {
write({
event: AuxiliaryGenerationEventEnum.answer,
data: createChatCompletionDeltaResponse({
text: null,
finishReason: 'stop'
})
});
write({
event: AuxiliaryGenerationEventEnum.answer,
data: '[DONE]'
});
},
async flushResume() {
await sseContext.flushResume();
}
};
};
import type { NextApiRequest, NextApiResponse } from 'next';
import type { localeType } from '@fastgpt/global/common/i18n/type';
import type { ChatSourceTypeEnum } from '@fastgpt/global/core/chat/constants';
import type { AIChatItemValueItemType, ChatItemDBSchemaType } from '@fastgpt/global/core/chat/type';
import type { ChatNodeUsageType } from '@fastgpt/global/support/wallet/bill/type';
import type { UsageSourceEnum } from '@fastgpt/global/support/wallet/usage/constants';
import type { AuxiliaryGenerationChatFileType } from '@fastgpt/global/core/ai/auxiliaryGeneration/type';
import type { AuxiliaryGenerationStreamContext, AuxiliaryGenerationStreamWriter } from './stream';
export type AuxiliaryGenerationUser = {
teamId: string;
tmbId: string;
userId: string;
isRoot: boolean;
lang: localeType;
};
export type AuxiliaryGenerationProcessorParams<T = unknown> = {
query: string;
files: AuxiliaryGenerationChatFileType[];
data: T;
histories: ChatItemDBSchemaType[];
streamWriter?: AuxiliaryGenerationStreamWriter;
requestOrigin?: string;
maxFiles?: number;
customPdfParse?: boolean;
checkIsStopping?: () => boolean;
usageSink?: (usages: ChatNodeUsageType[]) => void;
user: AuxiliaryGenerationUser;
};
export type AuxiliaryGenerationProcessorResponse = {
aiResponse: AIChatItemValueItemType[];
usage: {
model: string;
inputTokens: number;
outputTokens: number;
};
};
export type AuxiliaryGenerationRunParams<T = unknown> = {
req: NextApiRequest;
res: NextApiResponse;
teamId: string;
tmbId: string;
userId: string;
isRoot: boolean;
lang: localeType;
appName: string;
sourceType: ChatSourceTypeEnum;
sourceId: string;
chatId: string;
query: string;
files: AuxiliaryGenerationChatFileType[];
data: T;
histories: ChatItemDBSchemaType[];
usageSource: UsageSourceEnum;
processor: (
params: AuxiliaryGenerationProcessorParams<T>
) => Promise<AuxiliaryGenerationProcessorResponse>;
maxFiles?: number;
customPdfParse?: boolean;
/** SSE 创建后立即暴露给路由层,用于失败时写 error 和 flush resume。 */
onStreamContextReady?: (streamContext: AuxiliaryGenerationStreamContext) => void;
};
export type AuxiliaryGenerationRunResult = AuxiliaryGenerationProcessorResponse & {
durationSeconds: number;
};
import { checkTeamAIPoints } from '../../../support/permission/teamLimit';
import { createChatUsageRecord, pushChatItemUsage } from '../../../support/wallet/usage/controller';
import type { ChatNodeUsageType } from '@fastgpt/global/support/wallet/bill/type';
import { ChatSourceTypeEnum } from '@fastgpt/global/core/chat/constants';
import type { UsageSourceEnum } from '@fastgpt/global/support/wallet/usage/constants';
type CreateAuxiliaryGenerationUsageParams = {
teamId: string;
tmbId: string;
appName: string;
sourceType: ChatSourceTypeEnum;
sourceId: string;
usageSource: UsageSourceEnum;
};
/**
* 为辅助生成建立统一的扣费上下文。
*
* 余额校验在生成前执行;后续 processor 只需把各模型/工具用量推入 `pushUsage`。
*/
export const createAuxiliaryGenerationUsage = async ({
teamId,
tmbId,
appName,
sourceType,
sourceId,
usageSource
}: CreateAuxiliaryGenerationUsageParams) => {
await checkTeamAIPoints(teamId);
const usageAppId = (() => {
if ([ChatSourceTypeEnum.app, ChatSourceTypeEnum.chatAgentHelper].includes(sourceType)) {
return sourceId;
}
})();
const usageSkillId = (() => {
if (sourceType === ChatSourceTypeEnum.skillEdit) return sourceId;
})();
const usageId = await createChatUsageRecord({
appName,
appId: usageAppId,
skillId: usageSkillId,
teamId,
tmbId,
source: usageSource
});
return {
usageId,
pushUsage(usages: ChatNodeUsageType[]) {
pushChatItemUsage({
teamId,
usageId,
nodeUsages: usages
});
}
};
};
......@@ -23,7 +23,6 @@ import { refreshVersionKey } from '../../../common/cache';
import { SystemCacheKeyEnum } from '../../../common/cache/type';
import { getLogger, LogCategories } from '../../../common/logger';
import { getRuntimeResolvedPriceTiers } from '@fastgpt/global/core/ai/pricing';
import { serviceEnv } from '../../../env';
export const loadSystemModels = async (init = false, language = 'en') => {
if (!init && global.systemModelList) return;
......@@ -80,9 +79,6 @@ export const loadSystemModels = async (init = false, language = 'en') => {
if (model.isDefaultChatTitleModel) {
_systemDefaultModel.chatTitleLLM = model;
}
if (model.model === serviceEnv.HELPER_BOT_MODEL) {
_systemDefaultModel.helperBotLLM = model;
}
} else if (model.type === ModelTypeEnum.embedding) {
_embeddingModelMap.set(model.model, model);
_embeddingModelMap.set(model.name, model);
......@@ -191,10 +187,6 @@ export const loadSystemModels = async (init = false, language = 'en') => {
(item) => item.vision
);
}
if (!_systemDefaultModel.helperBotLLM) {
// HELPER_BOT_MODEL 未配置或未命中已启用模型时,回退到系统默认 LLM。
_systemDefaultModel.helperBotLLM = _systemDefaultModel.llm;
}
if (!_systemDefaultModel.embedding) {
_systemDefaultModel.embedding = Array.from(_embeddingModelMap.values())[0];
}
......
......@@ -33,9 +33,6 @@ export const getVlmModel = (model?: string) => {
export const getDefaultChatTitleModel = () => global?.systemDefaultModel.chatTitleLLM;
export const getDefaultHelperBotModel = (): LLMModelItemType =>
global?.systemDefaultModel.helperBotLLM || getDefaultLLMModel();
export const getDefaultEmbeddingModel = () => global?.systemDefaultModel.embedding!;
export const getEmbeddingModel = (model?: string | EmbeddingModelItemType) => {
if (!model) return getDefaultEmbeddingModel();
......
......@@ -93,6 +93,9 @@ const buildSandboxResourcesToArchiveQuery = (params: {
const buildSandboxResourceSourceQuery = ({ sourceType, sourceId }: SandboxSourceParams) => {
if (sourceType === ChatSourceTypeEnum.app) return { sourceType, sourceId };
if (sourceType === ChatSourceTypeEnum.skillEdit) return { sourceType, sourceId };
if (sourceType === ChatSourceTypeEnum.chatAgentHelper) {
throw new Error('ChatAgentHelper source does not support sandbox resources');
}
const exhaustiveCheck: never = sourceType;
throw new Error(`Unsupported sandbox source type: ${exhaustiveCheck}`);
......
......@@ -96,6 +96,10 @@ export function buildSandboxClientQueryFromChatSource({
};
}
if (sourceType === ChatSourceTypeEnum.chatAgentHelper) {
throw new Error('ChatAgentHelper source does not support sandbox runtime');
}
const exhaustiveCheck: never = sourceType;
throw new Error(`Unsupported chat source type: ${exhaustiveCheck}`);
}
......@@ -27,6 +27,10 @@ export const getRunningSandboxId = ({
return getEditDebugSandboxId(sourceId);
}
if (sourceType === ChatSourceTypeEnum.chatAgentHelper) {
throw new Error('ChatAgentHelper source does not support sandbox id generation');
}
const exhaustiveCheck: never = sourceType;
throw new Error(`Unsupported chat source type: ${exhaustiveCheck}`);
};
......@@ -34,6 +34,7 @@ import { getDefaultLLMModel } from '../../model';
import { getRunningSkillEditSandbox } from '../../sandbox/interface/skillEdit';
import { dispatchWorkFlow } from '../../../workflow/dispatch';
import { WORKFLOW_MAX_RUN_TIMES } from '../../../workflow/constants';
import type { AppFileSelectConfigType } from '@fastgpt/global/core/app/type/config.schema';
import { getChatItems } from '../../../chat/controller';
import {
failChatRound,
......@@ -51,6 +52,16 @@ import { buildDebugRuntimeNodes } from './runtime';
import type { AgentSandboxPrepareAction } from '../../../workflow/dispatch/ai/agent/sub/sandbox';
const logger = getLogger(LogCategories.MODULE.AGENT_SKILLS);
const skillDebugFileSelectConfig: AppFileSelectConfigType = {
maxFiles: 10,
canSelectFile: true,
canSelectImg: true,
customPdfParse: false,
canSelectVideo: true,
canSelectAudio: true,
canSelectCustomFileExtension: false,
customFileExtensionList: []
};
/**
* 处理 Skill 调试对话的共享主流程。
......@@ -206,7 +217,9 @@ export async function handleSkillDebugChat(
variables: {},
query: removeEmptyUserInput(userQuestion.value),
lastInteractive: interactive,
chatConfig: {},
chatConfig: {
fileSelectConfig: skillDebugFileSelectConfig
},
histories: newHistories,
stream: true,
maxRunTimes: WORKFLOW_MAX_RUN_TIMES,
......
......@@ -102,6 +102,13 @@ export function buildDebugRuntimeNodes(
value: systemPrompt
},
{
key: NodeInputKeyEnum.aiChatVision,
renderTypeList: [FlowNodeInputTypeEnum.hidden],
valueType: WorkflowIOValueTypeEnum.boolean,
label: '',
value: true
},
{
key: NodeInputKeyEnum.editSkillId,
renderTypeList: [FlowNodeInputTypeEnum.hidden],
valueType: WorkflowIOValueTypeEnum.string,
......
......@@ -43,7 +43,7 @@ const mergeMongoAndQuery = (...queries: Record<string, unknown>[]) => {
/**
* 查询当前成员可读的 Agent Skill 列表。
*
* 这里集中维护 Skill 列表 API 和 HelperBot TopAgent 共用的权限过滤规则:
* 这里集中维护 Skill 列表 API 和 ChatAgentHelper 共用的权限过滤规则:
* owner 可读团队内资源;普通成员按成员、用户组、组织授权计算,并支持目录继承权限。
*/
export const listReadableAgentSkills = async ({
......
......@@ -30,7 +30,6 @@ export type SystemDefaultModelType = {
datasetTextLLM?: LLMModelItemType;
datasetImageLLM?: LLMModelItemType;
chatTitleLLM?: LLMModelItemType;
helperBotLLM?: LLMModelItemType;
[ModelTypeEnum.embedding]?: EmbeddingModelItemType;
[ModelTypeEnum.tts]?: TTSModelType;
......
import { ChatRoleEnum } from '@fastgpt/global/core/chat/constants';
import { connectionMongo, getMongoModel } from '../../../common/mongo';
const { Schema } = connectionMongo;
import { helperBotChatItemCollectionName } from './constants';
import { HelperBotTypeEnum } from '@fastgpt/global/core/chat/helperBot/type';
import type { HelperBotChatItemType } from '@fastgpt/global/core/chat/helperBot/type';
const HelperBotChatItemSchema = new Schema({
userId: {
type: String,
require: true
},
chatId: {
type: String,
require: true
},
dataId: {
type: String,
require: true
},
createTime: {
type: Date,
default: () => new Date()
},
obj: {
type: String,
require: true,
enum: Object.values(ChatRoleEnum)
},
value: {
type: Array,
require: true
},
memories: Object
});
HelperBotChatItemSchema.index({ userId: 1, chatId: 1, dataId: 1, obj: 1 }, { unique: true });
export const MongoHelperBotChatItem = getMongoModel<HelperBotChatItemType>(
helperBotChatItemCollectionName,
HelperBotChatItemSchema
);
import { connectionMongo, getMongoModel } from '../../../common/mongo';
const { Schema } = connectionMongo;
import { helperBotChatCollectionName } from './constants';
import { HelperBotTypeEnum } from '@fastgpt/global/core/chat/helperBot/type';
import type { HelperBotChatType } from '../../../../global/core/chat/helperBot/type';
const HelperBotChatSchema = new Schema({
type: {
type: String,
required: true,
enum: Object.values(HelperBotTypeEnum)
},
userId: {
type: String,
require: true
},
chatId: {
type: String,
require: true
},
createTime: {
type: Date,
default: () => new Date()
},
updateTime: {
type: Date,
default: () => new Date()
},
metadata: Object
});
HelperBotChatSchema.index({ type: 1, userId: 1, chatId: 1 }, { unique: true });
export const MongoHelperBotChat = getMongoModel<HelperBotChatType>(
helperBotChatCollectionName,
HelperBotChatSchema
);
export const helperBotChatCollectionName = 'helper_bot_chats';
export const helperBotChatItemCollectionName = 'helper_bot_chat_items';
import type {
AIChatItemValueItemType,
HelperBotTypeEnum
} from '@fastgpt/global/core/chat/helperBot/type';
import type { HelperBotCompletionsParamsType } from '@fastgpt/global/openapi/core/chat/helperBot/api';
import { MongoHelperBotChat } from './chatSchema';
import type { UserChatItemValueItemType } from '@fastgpt/global/core/chat/type';
import { MongoHelperBotChatItem } from './chatItemSchema';
import { ChatRoleEnum } from '@fastgpt/global/core/chat/constants';
import { mongoSessionRun } from '../../../common/mongo/sessionRun';
export const pushChatRecords = async ({
type,
userId,
chatId,
chatItemId,
query,
files,
aiResponse,
memories,
metadata
}: {
type: HelperBotTypeEnum;
userId: string;
chatId: string;
chatItemId: string;
query: string;
files: HelperBotCompletionsParamsType['files'];
aiResponse: AIChatItemValueItemType[];
memories?: Record<string, any>;
metadata?: Record<string, any>;
}) => {
const chat = await MongoHelperBotChat.findOne(
{
type,
userId,
chatId
},
'_id metadata'
).lean();
const metadataUpdate = {
...chat?.metadata,
...metadata
};
const userValue: UserChatItemValueItemType[] = [
...files.map((file) => ({
file: {
type: file.type,
name: file.name,
url: '',
key: file.key || ''
}
})),
...(query
? [
{
text: {
content: query
}
}
]
: [])
];
await mongoSessionRun(async (session) => {
await MongoHelperBotChatItem.create(
[
{
userId,
chatId,
dataId: chatItemId,
obj: ChatRoleEnum.Human,
value: userValue
},
{
userId,
chatId,
dataId: chatItemId,
obj: ChatRoleEnum.AI,
value: aiResponse,
memories
}
],
{ session, ordered: true }
);
await MongoHelperBotChat.updateOne(
{
type,
userId,
chatId
},
{
updateTime: new Date(),
metadata: metadataUpdate
},
{
session
}
);
});
};
import { ChatFileTypeEnum } from '@fastgpt/global/core/chat/constants';
import type { UserChatItemValueItemType } from '@fastgpt/global/core/chat/type';
import { parseUrlToFileType } from './context';
import { getS3RawTextSource } from '../../../common/s3/sources/rawText';
import { isInternalAddress, PRIVATE_URL_TEXT } from '../../../common/system/utils';
import { pickOutboundAxios } from '../../../common/api/axios';
import { S3Buckets } from '../../../common/s3/config/constants';
import { S3Sources } from '../../../common/s3/contracts/type';
import { audioFileType, imageFileType, videoFileType } from '@fastgpt/global/common/file/constants';
import { ChatFileTypeEnum, ChatRoleEnum } from '@fastgpt/global/core/chat/constants';
import type {
ChatItemMiniType,
UserChatItemFileItemType,
UserChatItemValueItemType
} from '@fastgpt/global/core/chat/type';
import { getS3RawTextSource } from '../../common/s3/sources/rawText';
import { isInternalAddress, PRIVATE_URL_TEXT } from '../../common/system/utils';
import { pickOutboundAxios } from '../../common/api/axios';
import { S3Buckets } from '../../common/s3/config/constants';
import { S3Sources } from '../../common/s3/contracts/type';
import {
detectFileEncoding,
parseContentDispositionFilename
} from '@fastgpt/global/common/file/tools';
import path from 'path';
import { getFileS3Key } from '../../../common/s3/utils';
import { S3ChatSource } from '../../../common/s3/sources/chat';
import { readFileContentByBuffer } from '../../../common/file/read/utils';
import { getFileS3Key } from '../../common/s3/utils';
import { S3ChatSource } from '../../common/s3/sources/chat';
import { readFileContentByBuffer } from '../../common/file/read/utils';
import { addDays } from 'date-fns';
import { replaceS3KeyToPreviewUrl } from '../../dataset/utils';
import { replaceS3KeyToPreviewUrl } from '../dataset/utils';
import { getErrText } from '@fastgpt/global/common/error/utils';
import { getUserFilesPrompt, injectUserQueryPrompt } from '../../ai/llm/prompt';
import { getUserFilesPrompt, injectUserQueryPrompt } from '../ai/llm/prompt';
import { getAxiosHeaderValue } from '@fastgpt/global/common/axios/utils';
type GetFileProps = {
......@@ -29,6 +33,98 @@ type GetFileProps = {
usageId?: string;
};
/**
* 将 URL 解析成 ChatBox 文件结构。
*
* `urlTypeMap` 用于 workflow 运行态传入显式文件类型;普通聊天/辅助生成场景则按文件名后缀推断。
*/
export const parseUrlToChatFileType = ({
url,
urlTypeMap = {}
}: {
url: string;
urlTypeMap?: Record<string, ChatFileTypeEnum>;
}): UserChatItemFileItemType | undefined => {
if (typeof url !== 'string') return;
if (url.startsWith('data:')) {
const matches = url.match(/^data:([^;]+);base64,/);
if (!matches) return;
const mimeType = matches[1].toLowerCase();
if (!mimeType.startsWith('image/')) return;
const extension = mimeType.split('/')[1];
return {
type: ChatFileTypeEnum.image,
name: `image.${extension}`,
url
};
}
try {
const parseUrl = new URL(url, 'http://localhost:3000');
const filename = (() => {
if (url.startsWith('chat/')) {
const basename = path.basename(url);
return basename.includes('.') ? basename : '';
}
const fromParam = parseUrl.searchParams.get('filename');
if (fromParam) return fromParam;
const basename = path.basename(parseUrl.pathname);
return basename.includes('.') ? basename : '';
})();
const type = urlTypeMap[url];
if (type) {
return {
type,
name: filename ? decodeURIComponent(filename) : url,
url
};
}
const extension = filename?.split('.').pop()?.toLowerCase() || '';
if (extension && imageFileType.includes(extension)) {
return {
type: ChatFileTypeEnum.image,
name: filename ? decodeURIComponent(filename) : url,
url
};
}
if (extension && audioFileType.includes(extension)) {
return {
type: ChatFileTypeEnum.audio,
name: filename ? decodeURIComponent(filename) : url,
url
};
}
if (extension && videoFileType.includes(extension)) {
return {
type: ChatFileTypeEnum.video,
name: filename ? decodeURIComponent(filename) : url,
url
};
}
return {
type: ChatFileTypeEnum.file,
name: filename ? decodeURIComponent(filename) : url,
url
};
} catch {
return {
type: ChatFileTypeEnum.file,
name: url,
url
};
}
};
export const formatUserQueryWithFiles = async ({
userQuery,
parseFileFn
......@@ -77,6 +173,55 @@ export const formatUserQueryWithFiles = async ({
};
/**
* 在发送给 LLM 前把 Human 消息里的普通文件解析为文本上下文。
*
* 历史 Human 消息是否解析由调用方控制;未解析时会移除历史文件项,避免旧文件继续作为模型文件输入。
*/
export const rewriteChatMessagesWithFileContext = async ({
messages,
parseHistoryFiles,
parseFileFn
}: {
messages: ChatItemMiniType[];
parseHistoryFiles: boolean;
parseFileFn: (urls: string[]) => Promise<
{
id?: string;
name: string;
url: string;
sandboxPath?: string;
content?: string;
}[]
>;
}) => {
return Promise.all(
messages.map(async (message, index): Promise<ChatItemMiniType> => {
if (message.obj !== ChatRoleEnum.Human) {
return message;
}
const isCurrentUserMessage = index === messages.length - 1;
if (!isCurrentUserMessage && !parseHistoryFiles) {
return {
...message,
value: message.value.filter((item) => !item.file)
};
}
const query = await formatUserQueryWithFiles({
userQuery: message.value,
parseFileFn
});
return {
...message,
value: query
};
})
);
};
/**
* 格式化文件 URL,移除请求头部分,只保留文件 URL
*/
export const normalizeReadableFileUrl = ({
......@@ -96,7 +241,7 @@ export const normalizeReadableFileUrl = ({
return '';
}
if (parseUrlToFileType(normalizedUrl)?.type !== ChatFileTypeEnum.file) {
if (parseUrlToChatFileType({ url: normalizedUrl })?.type !== ChatFileTypeEnum.file) {
return '';
}
......
......@@ -69,7 +69,14 @@ const beforeProcess = (props: Props) => {
const isFileValueWithKey = (file: unknown): file is { key: string } =>
!!file && typeof file === 'object' && 'key' in file && typeof file.key === 'string' && !!file.key;
const afterProcess = async ({
/**
* 对话内容落库成功后,移除已持久化文件的 S3 TTL 记录。
*
* ChatBox 上传文件时会先写入临时 TTL,避免用户只上传不发送时文件长期残留。
* 当包含这些 file key 的消息、工作流表单值或文件变量已经进入对话记录后,
* 这里统一删除 TTL,表示这些文件跟随对话生命周期持久化。
*/
export const persistChatFiles = async ({
contents,
variables,
variableList,
......@@ -363,7 +370,7 @@ export const finalizeChatRound = async (props: Props) => {
}
);
await afterProcess({
await persistChatFiles({
contents: processedContent,
variables,
variableList,
......@@ -584,7 +591,8 @@ export const pushChatRecords = async (props: Props) => {
shareId,
outLinkUid,
metadata: metadataUpdate,
updateTime: new Date()
updateTime: new Date(),
chatGenerateStatus: ChatGenerateStatusEnum.done
},
$setOnInsert: {
createTime: new Date()
......@@ -598,7 +606,7 @@ export const pushChatRecords = async (props: Props) => {
}
);
await afterProcess({
await persistChatFiles({
contents: processedContent,
variables,
variableList,
......@@ -890,7 +898,7 @@ export const updateInteractiveChat = async ({
}
);
await afterProcess({
await persistChatFiles({
contents: [userContent, aiContent],
variables,
variableList,
......
......@@ -22,8 +22,8 @@ export function buildChatSourceWriteFields({ sourceType, sourceId }: ChatSourceP
/**
* 构造 chat 三表 source-aware 查询条件。
*
* App 查询默认兼容缺失 `sourceType` 的历史数据;Skill Edit 必须精确匹配
* `sourceType=skillEdit`。
* App 查询默认兼容缺失 `sourceType` 的历史数据;非 App 来源必须精确匹配
* `sourceType`,避免不同资源类型复用同一物理 `appId` 字段时串记录。
*/
export function buildChatSourceQuery({ sourceType, sourceId }: ChatSourceParams) {
if (sourceType === ChatSourceTypeEnum.app) {
......@@ -33,10 +33,13 @@ export function buildChatSourceQuery({ sourceType, sourceId }: ChatSourceParams)
};
}
if (sourceType === ChatSourceTypeEnum.skillEdit) {
if (
sourceType === ChatSourceTypeEnum.skillEdit ||
sourceType === ChatSourceTypeEnum.chatAgentHelper
) {
return {
appId: sourceId,
sourceType: ChatSourceTypeEnum.skillEdit
sourceType
};
}
......
......@@ -119,11 +119,12 @@ export function parseAgentInputFiles({
.map(({ file, url }, index) => {
const parsedFile = parseUrlToFileType(url);
if (!parsedFile) return;
const type = file.type && file.type !== ChatFileTypeEnum.file ? file.type : parsedFile.type;
return {
id: `${prefixId}-${index}`,
name: getSafeSandboxInputFilename(file.name || parsedFile.name || url, index, usedNames),
type: parsedFile.type,
type,
url: parsedFile.url
};
})
......@@ -293,6 +294,15 @@ ${currentTime ? `当前时间: ${currentTime}` : ''}
${currentWorkingDirectory ? `当前 sandbox 工作目录: ${currentWorkingDirectory}` : ''}`;
};
const getAgentMultimodalChatFiles = (files: AgentInputFile[]) =>
files
.filter((file) => file.type !== ChatFileTypeEnum.file)
.map(({ name, type, url }) => ({
name,
type: type as ChatFileTypeEnum,
url
}));
const buildSandboxFileWriteBoundaryPrompt = ({
currentWorkingDirectory
}: {
......@@ -437,6 +447,7 @@ export const useUserContext = async ({
return {
...message,
value: runtimePrompt2ChatsValue({
files: getAgentMultimodalChatFiles(formatFiles),
text: buildAgentUserReminderInput({
query: text,
filesInfo: formatFiles
......@@ -479,6 +490,7 @@ export const useUserContext = async ({
const currentUserMessage: ChatItemMiniType = {
...currentMessage,
value: runtimePrompt2ChatsValue({
files: getAgentMultimodalChatFiles(currentInputFiles),
// 当前 Human 才注入完整 reminder:sandbox、skill、文件、知识库、当前时间和原始问题。
text: buildAgentUserReminderInput({
query: currentUserInput,
......
......@@ -9,6 +9,7 @@ import type {
ChatHistoryItemResType,
ChatItemMiniType
} from '@fastgpt/global/core/chat/type';
import { ChatSourceTypeEnum } from '@fastgpt/global/core/chat/constants';
import { chats2GPTMessages } from '@fastgpt/global/core/chat/adapt';
import { getSystemToolInfo } from '@fastgpt/global/core/workflow/node/agent/constants';
import { SANDBOX_SYSTEM_PROMPT } from '@fastgpt/global/core/ai/sandbox/constants';
......@@ -177,6 +178,7 @@ export const dispatchRunAgent = async (props: DispatchAgentModuleProps): Promise
effectiveUseAgentSandbox && useAgentSandbox && global.feConfigs?.show_agent_sandbox
? sandboxEntrypoint
: undefined;
const skipSandboxInputFiles = runningAppInfo.sourceType === ChatSourceTypeEnum.skillEdit;
// 初始化对话框输入的文件
const fileUrlInput = inputs.find((item) => item.key === NodeInputKeyEnum.fileUrlList);
......@@ -229,7 +231,7 @@ export const dispatchRunAgent = async (props: DispatchAgentModuleProps): Promise
selectedSkills,
editSkillId,
prepareActions: agentSandboxPrepareActions,
currentFiles: userContext.currentFiles
currentFiles: skipSandboxInputFiles ? [] : userContext.currentFiles
});
// 获取请求上下文
const { chatHistories, queryInput, filesMap, fileUrlMap } = userContext;
......
......@@ -2,7 +2,7 @@ import { getErrText } from '@fastgpt/global/common/error/utils';
import { FlowNodeTypeEnum } from '@fastgpt/global/core/workflow/node/constant';
import { i18nT } from '@fastgpt/global/common/i18n/utils';
import type { DispatchSubAppResponse } from '../../type';
import { getFileContentByUrl } from '../../../../../utils/file';
import { getFileContentByUrl } from '../../../../../../chat/fileContext';
type FileReadParams = {
files: { id: string; url: string }[];
......
......@@ -284,11 +284,10 @@ export const getExecuteTool = ({
};
}
const ids = toolParams.data.ids;
const files = ids.map((id) => ({
id,
url: filesMap[id]
}));
const files = ids.flatMap((id) => {
const url = filesMap[id];
return url ? [{ id, url }] : [];
});
const result = await dispatchFileRead({
files,
teamId: runningUserInfo.teamId,
......
......@@ -8,7 +8,7 @@ import {
import type { ChatItemMiniType, UserChatItemFileItemType } from '@fastgpt/global/core/chat/type';
import type { LLMModelItemType } from '@fastgpt/global/core/ai/model.schema';
import type { ChatDispatchProps } from '@fastgpt/global/core/workflow/runtime/type';
import { parseFileContentFromUrls } from '../../../utils/file';
import { parseFileContentFromUrls } from '../../../../chat/fileContext';
import { rewriteChatMessagesWithFiles } from './fileContext';
/**
......
import { ChatRoleEnum } from '@fastgpt/global/core/chat/constants';
import type { ChatItemMiniType, UserChatItemFileItemType } from '@fastgpt/global/core/chat/type';
import { NodeInputKeyEnum } from '@fastgpt/global/core/workflow/constants';
import type { FlowNodeInputItemType } from '@fastgpt/global/core/workflow/type/io';
import { parseUrlToFileType } from '../../../utils/context';
import { formatUserQueryWithFiles } from '../../../utils/file';
import { rewriteChatMessagesWithFileContext } from '../../../../chat/fileContext';
import type { ChatMessageFileParser } from './type';
/**
......@@ -44,31 +43,11 @@ export const rewriteChatMessagesWithFiles = async ({
parseHistoryFiles: boolean;
parseFileFn: ChatMessageFileParser;
}) => {
return Promise.all(
messages.map(async (message, index): Promise<ChatItemMiniType> => {
if (message.obj !== ChatRoleEnum.Human) {
return message;
}
const isCurrentUserMessage = index === messages.length - 1;
if (!isCurrentUserMessage && !parseHistoryFiles) {
return {
...message,
value: message.value.filter((item) => !item.file)
};
}
const query = await formatUserQueryWithFiles({
userQuery: message.value,
parseFileFn
});
return {
...message,
value: query
};
})
);
return rewriteChatMessagesWithFileContext({
messages,
parseHistoryFiles,
parseFileFn
});
};
/**
......
......@@ -5,7 +5,7 @@ import {
} from '@fastgpt/global/core/chat/adapt';
import type { ChatItemMiniType, UserChatItemFileItemType } from '@fastgpt/global/core/chat/type';
import { SANDBOX_USER_FILES_PATH } from '@fastgpt/global/core/ai/sandbox/constants';
import { formatUserQueryWithFiles, parseFileInfoFromUrls } from '../../../../utils/file';
import { formatUserQueryWithFiles, parseFileInfoFromUrls } from '../../../../../chat/fileContext';
import { parseUrlToFileType } from '../../../../utils/context';
import type { DispatchToolModuleProps, FileInputType } from '../type';
......
import type { ChatCompletionTool } from '@fastgpt/global/core/ai/llm/type';
import type { ChatNodeUsageType } from '@fastgpt/global/support/wallet/bill/type';
import { getFileContentByUrl } from '../../../../utils/file';
import { getFileContentByUrl } from '../../../../../chat/fileContext';
import { getErrText } from '@fastgpt/global/common/error/utils';
import { getLogger } from '@fastgpt-sdk/otel/logger';
import { LogCategories } from '../../../../../../common/logger';
......
......@@ -34,8 +34,12 @@ import { dispatchIfElse } from './tools/runIfElse';
import { dispatchUpdateVariable } from './tools/runUpdateVar';
import { dispatchTextEditor } from './tools/textEditor';
import { dispatchRunAgent } from './ai/agent';
import { dispatchInternalRuntimeNode, internalRuntimeNodeType } from './internal/runtimeNode';
export const callbackMap: Record<FlowNodeTypeEnum, (...args: any[]) => unknown> = {
export const callbackMap: Record<
FlowNodeTypeEnum | typeof internalRuntimeNodeType,
(...args: any[]) => unknown
> = {
[FlowNodeTypeEnum.workflowStart]: dispatchWorkflowStart,
// Child
......@@ -74,6 +78,7 @@ export const callbackMap: Record<FlowNodeTypeEnum, (...args: any[]) => unknown>
[FlowNodeTypeEnum.nestedEnd]: dispatchLoopEnd,
[FlowNodeTypeEnum.formInput]: dispatchFormInput,
[FlowNodeTypeEnum.tool]: dispatchRunTool,
[internalRuntimeNodeType]: dispatchInternalRuntimeNode,
// none
[FlowNodeTypeEnum.systemConfig]: dispatchSystemConfig,
......
import { DispatchNodeResponseKeyEnum } from '@fastgpt/global/core/workflow/runtime/constants';
import { NodeOutputKeyEnum } from '@fastgpt/global/core/workflow/constants';
import type { ChatNodeUsageType } from '@fastgpt/global/support/wallet/bill/type';
export const internalRuntimeNodeRunnerKey = '__internalRuntimeNodeRunner';
export const internalRuntimeNodeType = '__internalRuntimeNode';
export type InternalRuntimeNodeRunnerProps = {
usagePush: (usages: ChatNodeUsageType[]) => void;
checkIsStopping: () => boolean;
};
export type InternalRuntimeNodeRunner = (
props: InternalRuntimeNodeRunnerProps
) => Promise<Record<string, any>> | Record<string, any>;
/**
* 执行只存在于服务端内存里的 runtime 节点。
*
* 这类节点不来自用户工作流模板,也不会持久化到应用配置;调用方用它把受控业务逻辑接入
* workflow runtime,从而复用 checkTeamAIPoints、usage record、nodeResponse writer 等通用能力。
*/
export const dispatchInternalRuntimeNode = async (props: Record<string, any>) => {
const runner = props.params?.[internalRuntimeNodeRunnerKey];
if (typeof runner !== 'function') {
return {
error: {
[NodeOutputKeyEnum.errorText]: 'Internal runtime node runner is missing'
},
[DispatchNodeResponseKeyEnum.nodeResponse]: {
error: 'Internal runtime node runner is missing'
}
};
}
return runner(props);
};
......@@ -6,7 +6,7 @@ import { type DispatchNodeResultType } from '@fastgpt/global/core/workflow/runti
import { ChatRoleEnum } from '@fastgpt/global/core/chat/constants';
import { type ChatItemMiniType } from '@fastgpt/global/core/chat/type';
import { getNodeErrResponse } from '../utils';
import { parseFileContentFromUrls } from '../../utils/file';
import { parseFileContentFromUrls } from '../../../chat/fileContext';
import { sliceStrStartEnd } from '@fastgpt/global/common/string/tools';
type Props = ModuleDispatchProps<{
......
import { audioFileType, imageFileType, videoFileType } from '@fastgpt/global/common/file/constants';
import { ChatFileTypeEnum } from '@fastgpt/global/core/chat/constants';
import type { UserChatItemFileItemType } from '@fastgpt/global/core/chat/type';
import type { ChatFileTypeEnum } from '@fastgpt/global/core/chat/constants';
import { parseUrlToChatFileType } from '../../chat/fileContext';
import { AsyncLocalStorage } from 'async_hooks';
import path from 'path';
import type { MCPClient } from '../../app/mcp';
type ContextType = {
......@@ -34,92 +32,9 @@ export const updateWorkflowContextVal = (val: Partial<ContextType>) => {
}
};
// Url => user upload file type
export const parseUrlToFileType = (url: string): UserChatItemFileItemType | undefined => {
if (typeof url !== 'string') return;
// Handle base64 image
if (url.startsWith('data:')) {
const matches = url.match(/^data:([^;]+);base64,/);
if (!matches) return;
const mimeType = matches[1].toLowerCase();
if (!mimeType.startsWith('image/')) return;
const extension = mimeType.split('/')[1];
return {
type: ChatFileTypeEnum.image,
name: `image.${extension}`,
url
};
}
try {
const parseUrl = new URL(url, 'http://localhost:3000');
// Get filename from URL
const filename = (() => {
// Here is a S3 Object Key
if (url.startsWith('chat/')) {
const basename = path.basename(url);
// Return empty if no extension
return basename.includes('.') ? basename : '';
}
const fromParam = parseUrl.searchParams.get('filename');
if (fromParam) {
return fromParam;
}
const basename = path.basename(parseUrl.pathname);
// Return empty if no extension
return basename.includes('.') ? basename : '';
})();
const context = getWorkflowContext();
const type = context?.queryUrlTypeMap[url];
if (type) {
return {
type,
name: filename ? decodeURIComponent(filename) : url,
url
};
}
const extension = filename?.split('.').pop()?.toLowerCase() || '';
if (extension && imageFileType.includes(extension)) {
return {
type: ChatFileTypeEnum.image,
name: filename ? decodeURIComponent(filename) : url,
url
};
}
if (extension && audioFileType.includes(extension)) {
return {
type: ChatFileTypeEnum.audio,
name: filename ? decodeURIComponent(filename) : url,
url
};
}
if (extension && videoFileType.includes(extension)) {
return {
type: ChatFileTypeEnum.video,
name: filename ? decodeURIComponent(filename) : url,
url
};
}
// If it's a document type, return as file, otherwise treat as image
return {
type: ChatFileTypeEnum.file,
name: filename ? decodeURIComponent(filename) : url,
url
};
} catch {
return {
type: ChatFileTypeEnum.file,
name: url,
url
};
}
};
/** 结合 workflow 运行态 URL 类型映射,将 URL 解析成 ChatBox 文件结构。 */
export const parseUrlToFileType = (url: string) =>
parseUrlToChatFileType({
url,
urlTypeMap: getWorkflowContext()?.queryUrlTypeMap
});
......@@ -5,6 +5,7 @@ import { STREAM_RESUME_REQUEST_HEADER } from '@fastgpt/global/core/chat/constant
import type { ChatSourceTypeEnum } from '@fastgpt/global/core/chat/constants';
import { SseResponseEventEnum } from '@fastgpt/global/core/workflow/runtime/constants';
import { textAdaptGptResponse } from '@fastgpt/global/core/workflow/runtime/utils';
import { createSseStreamContext } from '../../../common/response/sse';
import { getStreamResumeMirror } from '../../chat/resume';
import { getWorkflowResponseWrite } from '../dispatch/utils';
......@@ -99,42 +100,30 @@ export const initWorkflowSseResponse = ({
return;
}
if (!res.headersSent) {
res.setHeader('Connection', 'keep-alive');
res.setHeader('Content-Type', 'text/event-stream;charset=utf-8');
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('X-Accel-Buffering', 'no');
res.setHeader('Cache-Control', 'no-cache, no-transform');
}
let cleaned = false;
const cleanup = () => {
if (cleaned) return;
cleaned = true;
clearInterval(streamCheckTimer);
delete workflowRes[workflowSseResponseControllerKey];
};
res.once('finish', cleanup);
res.once('close', cleanup);
res.on('error', () => {
cleanup();
onError?.();
res.end();
const sseContext = createSseStreamContext({
res,
stream,
onError,
onCleanup: () => {
delete workflowRes[workflowSseResponseControllerKey];
},
// 10s 发送一次空 answer,沿用统一 SSE writer,避免浏览器或代理认为长连接已断开。
heartbeat: {
write: () => {
responseWrite?.({
event: SseResponseEventEnum.answer,
data: textAdaptGptResponse({
text: ''
})
});
}
}
});
// 10s 发送一次空 answer,沿用统一 SSE writer,避免浏览器或代理认为长连接已断开。
const streamCheckTimer = setInterval(() => {
responseWrite?.({
event: SseResponseEventEnum.answer,
data: textAdaptGptResponse({
text: ''
})
});
}, 10000);
const controller: WorkflowSseResponseController = {
cleanup
cleanup: () => {
sseContext.cleanup();
}
};
workflowRes[workflowSseResponseControllerKey] = controller;
};
......
......@@ -244,10 +244,6 @@ export const serviceEnv = createEnv({
AGENT_ENGINE: z.enum(['default', 'pi']).default('default').meta({
description: 'Agent 引擎选择:default(unified agent loop)| pi(pi-agent-core 引擎)'
}),
HELPER_BOT_MODEL: z
.string()
.optional()
.meta({ description: '辅助生成模型(暂时只能指定一个,需保证系统中已激活该模型)' }),
SKIP_FILE_TYPE_CHECK: BoolSchema.default(false).meta({
description: '是否跳过文件类型检查'
}),
......
......@@ -175,6 +175,8 @@ export const checkTeamDatasetSyncPermission = async (teamId: string) => {
};
export const checkTeamSandboxPermission = async (teamId: string) => {
if (!global.subPlans?.standard) return;
const { standard } = await getTeamStandPlan({
teamId
});
......
......@@ -160,6 +160,7 @@ export const initTeamFreePlan = async ({
// 获取团队标准套餐
export const getTeamStandPlan = async ({ teamId }: { teamId: string }) => {
const standardPlans = global.subPlans?.standard;
const plans = await MongoTeamSub.find(
{
teamId,
......@@ -172,11 +173,10 @@ export const getTeamStandPlan = async ({ teamId }: { teamId: string }) => {
).lean();
sortStandPlans(plans);
const standardPlans = global.subPlans?.standard;
const standard = plans[0];
const standardConstants =
standard.currentSubLevel && standardPlans
standard?.currentSubLevel && standardPlans
? standardPlans[
standard.currentSubLevel === StandardSubLevelEnum.custom
? StandardSubLevelEnum.advanced
......@@ -185,9 +185,8 @@ export const getTeamStandPlan = async ({ teamId }: { teamId: string }) => {
: undefined;
return {
[SubTypeEnum.standard]: standardConstants
? buildStandardPlan(standard, standardConstants)
: undefined
[SubTypeEnum.standard]:
standard && standardConstants ? buildStandardPlan(standard, standardConstants) : undefined
};
};
......
......@@ -7,10 +7,6 @@ import {
isAuthorizedDatasetFileS3Key,
parseDatasetFileS3Key
} from '@fastgpt/service/common/s3/sources/dataset/key';
import {
isAuthorizedHelperBotFileS3Key,
parseHelperBotFileS3Key
} from '@fastgpt/service/common/s3/sources/helperbot/key';
import { isAuthorizedTempFileS3Key } from '@fastgpt/service/common/s3/sources/temp/key';
describe('authorized S3 object key helpers', () => {
......@@ -125,20 +121,6 @@ describe('authorized S3 object key helpers', () => {
expect(parseDatasetFileS3Key('dataset/dataset-1')).toBeNull();
});
it('parses and authorizes helper bot file keys by userId', () => {
const key = 'helperBot/topAgent/user-1/chat-1/demo.pdf';
expect(parseHelperBotFileS3Key(key)).toEqual({
type: 'topAgent',
userId: 'user-1',
chatId: 'chat-1',
filename: 'demo.pdf'
});
expect(isAuthorizedHelperBotFileS3Key({ key, userId: 'user-1' })).toBe(true);
expect(isAuthorizedHelperBotFileS3Key({ key, userId: 'user-2' })).toBe(false);
expect(parseHelperBotFileS3Key('helperBot/unknown/user-1/chat-1/demo.pdf')).toBeNull();
});
it('authorizes temp file keys by exact team path segment', () => {
expect(isAuthorizedTempFileS3Key({ key: 'temp/team-1/demo.pdf', teamId: 'team-1' })).toBe(true);
expect(isAuthorizedTempFileS3Key({ key: 'temp/team-11/demo.pdf', teamId: 'team-1' })).toBe(
......
......@@ -13,6 +13,8 @@ import { MongoChatItem } from '@fastgpt/service/core/chat/chatItemSchema';
import { MongoChat } from '@fastgpt/service/core/chat/chatSchema';
import { MongoAppChatLog } from '@fastgpt/service/core/app/logs/chatLogsSchema';
import { MongoChatItemResponse } from '@fastgpt/service/core/chat/chatItemResponseSchema';
import { MongoS3TTL } from '@fastgpt/service/common/s3/models/ttl';
import { S3Buckets } from '@fastgpt/service/common/s3/config/constants';
import {
ChatFileTypeEnum,
ChatGenerateStatusEnum,
......@@ -606,6 +608,103 @@ describe('pushChatRecords', () => {
expect(aiItem?.responseData).toBeUndefined();
});
it('should persist skill edit uploaded files by removing S3 TTL after finalization', async () => {
const responseChatItemId = 'skill-edit-file-finalize';
const fileKey = `chat/${ChatSourceTypeEnum.skillEdit}/${testAppId}/${testTmbId}/skill-chat-id/spec.md`;
const props = createMockProps(
{
chatId: 'skill-chat-id',
sourceType: ChatSourceTypeEnum.skillEdit,
sourceId: testAppId,
userContent: {
dataId: responseChatItemId,
obj: ChatRoleEnum.Human,
value: [
{
file: {
type: ChatFileTypeEnum.file,
name: 'spec.md',
url: 'https://example.com/temp-preview',
key: fileKey
}
}
]
},
aiContent: {
dataId: responseChatItemId,
obj: ChatRoleEnum.AI,
value: [
{
text: {
content: 'Skill debug answer'
}
}
]
}
},
{ appId: testAppId, teamId: testTeamId, tmbId: testTmbId }
);
await MongoS3TTL.create({
bucketName: S3Buckets.private,
minioKey: fileKey,
expiredTime: new Date(Date.now() + 60 * 60 * 1000)
});
await MongoChat.create({
appId: testAppId,
chatId: props.chatId,
teamId: testTeamId,
tmbId: testTmbId,
sourceType: ChatSourceTypeEnum.skillEdit,
source: props.source,
chatGenerateStatus: ChatGenerateStatusEnum.generating,
hasBeenRead: false
});
await MongoChatItem.create([
{
teamId: testTeamId,
tmbId: testTmbId,
sourceType: ChatSourceTypeEnum.skillEdit,
appId: testAppId,
chatId: props.chatId,
dataId: responseChatItemId,
obj: ChatRoleEnum.Human,
value: []
},
{
teamId: testTeamId,
tmbId: testTmbId,
sourceType: ChatSourceTypeEnum.skillEdit,
appId: testAppId,
chatId: props.chatId,
dataId: responseChatItemId,
obj: ChatRoleEnum.AI,
value: []
}
]);
await finalizeChatRound(props);
const ttl = await MongoS3TTL.findOne({
bucketName: S3Buckets.private,
minioKey: fileKey
}).lean();
expect(ttl).toBeNull();
const humanItem = await MongoChatItem.findOne({
sourceType: ChatSourceTypeEnum.skillEdit,
appId: testAppId,
chatId: props.chatId,
obj: ChatRoleEnum.Human
}).lean();
expect(humanItem?.value[0].file).toEqual({
type: ChatFileTypeEnum.file,
name: 'spec.md',
url: '',
key: fileKey
});
});
it('should mark prepared round as error and keep ai placeholder', async () => {
const responseChatItemId = 'prepared-ai-error';
const props = createMockProps({}, { appId: testAppId, teamId: testTeamId, tmbId: testTmbId });
......
......@@ -20,6 +20,7 @@ vi.mock('@fastgpt/global/common/time/timezone', () => ({
getSystemTime: vi.fn(() => '2026-05-14 10:00:00 Thursday')
}));
vi.mock('@fastgpt/service/core/dataset/schema', () => ({
DatasetCollectionName: 'datasets',
MongoDataset: {
find: vi.fn(() => ({
lean: vi.fn(async () => [])
......@@ -211,6 +212,28 @@ describe('parseAgentInputFiles', () => {
]);
expect(files.every((file) => !file.name.includes('/'))).toBe(true);
});
it('keeps explicit image type when preview url has no file extension', () => {
const files = parseAgentInputFiles({
files: [
{
name: '20260629-233136_wT851P.jpg',
type: ChatFileTypeEnum.image,
url: 'https://fastgpt.example.com/api/core/chat/file/preview?token=abc'
}
],
prefixId: 'current',
maxFiles: 10
});
expect(files).toEqual([
expect.objectContaining({
name: '20260629-233136_wT851P.jpg',
type: ChatFileTypeEnum.image,
url: 'https://fastgpt.example.com/api/core/chat/file/preview?token=abc'
})
]);
});
});
describe('buildAgentUserReminderInput', () => {
......@@ -412,6 +435,7 @@ describe('useUserContext', () => {
{
queryUrlTypeMap: {
'/old.pdf': ChatFileTypeEnum.file,
'/old.png': ChatFileTypeEnum.image,
'/current.pdf': ChatFileTypeEnum.file,
'/current.png': ChatFileTypeEnum.image
},
......@@ -421,7 +445,10 @@ describe('useUserContext', () => {
const history = createHumanMessage({
dataId: 'history_1',
text: '历史问题',
files: [{ name: 'old.pdf', url: '/old.pdf' }]
files: [
{ name: 'old.pdf', url: '/old.pdf' },
{ name: 'old.png', url: '/old.png', type: ChatFileTypeEnum.image }
]
});
const result = await getUserContextMessagesForTest({
history: 6,
......@@ -452,19 +479,36 @@ describe('useUserContext', () => {
});
expect(result.fileUrlMap).toEqual({
'history_1-0': '/old.pdf',
'history_1-1': '/old.png',
'current_chat_item-0': '/current.pdf',
'current_chat_item-1': '/current.png'
});
const { text: historyText } = chatValue2RuntimePrompt(result.rewrittenHistories[0].value);
const { text: historyText, files: historyFiles } = chatValue2RuntimePrompt(
result.rewrittenHistories[0].value
);
const { text: currentText, files: currentFiles } = chatValue2RuntimePrompt(
result.currentUserMessage.value
);
expect(historyFiles).toEqual([
{
name: 'old.png',
type: ChatFileTypeEnum.image,
url: '/old.png'
}
]);
expect(historyText).toContain('<id>history_1-0</id>');
expect(historyText).toContain('<id>history_1-1</id>');
expect(historyText).not.toContain('当前 sandbox 工作目录');
expect(historyText).not.toContain('当前时间');
expect(currentFiles).toEqual([]);
expect(currentFiles).toEqual([
{
name: 'current.png',
type: ChatFileTypeEnum.image,
url: '/current.png'
}
]);
expect(currentText).toContain('## 背景信息');
expect(currentText).toContain('当前 sandbox 工作目录: /workspace');
expect(currentText).toContain('<id>current_chat_item-0</id>');
......@@ -724,6 +768,47 @@ describe('useUserContext', () => {
);
});
it('keeps uploaded image as multimodal content when preview url has no extension', async () => {
await runWithContextAsync(
{
queryUrlTypeMap: {},
mcpClientMemory: {}
},
async () => {
const previewUrl = 'https://fastgpt.example.com/api/core/chat/file/preview?token=abc';
const result = await getUserContextMessagesForTest({
history: 0,
histories: [],
currentUserInput: '看一下这张图',
currentDataId: 'current_chat_item',
currentQuery: runtimePrompt2ChatsValue({
text: '前端原始问题',
files: [
{
name: '20260629-233136_wT851P.jpg',
url: previewUrl,
type: ChatFileTypeEnum.image
}
]
}),
tmbId: 'tmb_1',
timezone: 'Asia/Shanghai',
maxFiles: 20
});
const { files, text } = chatValue2RuntimePrompt(result.currentUserMessage.value);
expect(files).toEqual([
expect.objectContaining({
type: ChatFileTypeEnum.image,
name: '20260629-233136_wT851P.jpg',
url: previewUrl
})
]);
expect(text).toContain('<type>image</type>');
}
);
});
it('uses message index when historical human has no human dataId', async () => {
await runWithContextAsync(
{
......
......@@ -509,6 +509,48 @@ describe('dispatchRunAgent user context', () => {
expect(loopInput.messages.at(-1)?.content).toContain('<path>./SKILL.md</path>');
});
it('does not inject current files into sandbox for skill edit preview', async () => {
const { dispatchRunAgent } = await import('@fastgpt/service/core/workflow/dispatch/ai/agent');
const props = createProps();
props.runningAppInfo.sourceType = ChatSourceTypeEnum.skillEdit;
props.runningAppInfo.sourceId = 'edit_skill_1';
props.params.editSkillId = 'edit_skill_1';
ensureAgentSandboxRuntimeMock.mockResolvedValueOnce({
sandboxClient: {
provider: {
writeFiles: vi.fn()
},
exec: sandboxClientExecMock,
getSandboxId: () => 'sandbox_prepared'
},
currentWorkingDirectory: '/workspace',
skillInfos: []
});
let result: any;
runWithContext(
{
queryUrlTypeMap: {
'/current.pdf': ChatFileTypeEnum.file
},
mcpClientMemory: {}
},
() => {
result = dispatchRunAgent(props);
}
);
await result;
expect(ensureAgentSandboxRuntimeMock).toHaveBeenCalledWith(
expect.objectContaining({
sourceType: ChatSourceTypeEnum.skillEdit,
sourceId: 'edit_skill_1',
editSkillId: 'edit_skill_1',
currentFiles: []
})
);
});
it('returns the final answer as assistant response', async () => {
const { dispatchRunAgent } = await import('@fastgpt/service/core/workflow/dispatch/ai/agent');
......
......@@ -23,7 +23,6 @@ const {
ensureAgentSandboxRuntimeMock,
streamAgentSandboxInitStatusMock,
sandboxClientExecMock,
axiosGetMock,
getAgentRuntimeToolsMock
} = vi.hoisted(() => ({
agentPromptMock: vi.fn(),
......@@ -37,7 +36,6 @@ const {
ensureAgentSandboxRuntimeMock: vi.fn(),
streamAgentSandboxInitStatusMock: vi.fn(),
sandboxClientExecMock: vi.fn(),
axiosGetMock: vi.fn(),
getAgentRuntimeToolsMock: vi.fn(async () => [])
}));
......@@ -87,18 +85,6 @@ vi.mock('@fastgpt/service/core/workflow/dispatch/ai/agent/sub/sandbox', async (i
};
});
vi.mock('@fastgpt/service/common/api/axios', async (importOriginal) => {
const original = await importOriginal<typeof import('@fastgpt/service/common/api/axios')>();
const mockClient = {
get: axiosGetMock
};
return {
...original,
pickOutboundAxios: () => mockClient
};
});
vi.mock('@fastgpt/service/core/dataset/schema', async (importOriginal) => {
const original = await importOriginal<typeof import('@fastgpt/service/core/dataset/schema')>();
return {
......@@ -256,9 +242,6 @@ describe('dispatchPiAgent user context', () => {
stdout: '/workspace\n',
stderr: ''
});
axiosGetMock.mockResolvedValue({
data: new ArrayBuffer(1)
});
ensureAgentSandboxRuntimeMock.mockResolvedValue({
sandboxClient: {
provider: {
......
......@@ -5,8 +5,8 @@ const { getFileContentByUrlMock } = vi.hoisted(() => ({
getFileContentByUrlMock: vi.fn()
}));
vi.mock('@fastgpt/service/core/workflow/utils/file', async (importOriginal) => {
const mod = await importOriginal<typeof import('@fastgpt/service/core/workflow/utils/file')>();
vi.mock('@fastgpt/service/core/chat/fileContext', async (importOriginal) => {
const mod = await importOriginal<typeof import('@fastgpt/service/core/chat/fileContext')>();
return {
...mod,
getFileContentByUrl: getFileContentByUrlMock
......
......@@ -30,21 +30,22 @@ vi.mock('@fastgpt/service/core/app/mcp', () => ({
}))
}));
vi.mock('@fastgpt/service/common/logger', () => ({
LogCategories: {
MODULE: {
APP: {
TOOL: 'tool'
},
AI: {
LLM: 'llm'
}
}
},
getLogger: vi.fn(() => ({
error: vi.fn()
}))
}));
vi.mock('@fastgpt/service/common/logger', async () => {
const actual = await vi.importActual<typeof import('@fastgpt/service/common/logger')>(
'@fastgpt/service/common/logger'
);
return {
...actual,
getLogger: vi.fn(() => ({
log: vi.fn(),
debug: vi.fn(),
info: vi.fn(),
warn: vi.fn(),
error: vi.fn()
}))
};
});
vi.mock('@fastgpt/service/common/middle/tracks/utils', () => ({
pushTrack: {
......
......@@ -163,6 +163,59 @@ describe('Agent read_files tool protocol', () => {
);
});
it('ignores read_files ids that are not registered document files', async () => {
const executeTool = getExecuteTool({
checkIsStopping: vi.fn(),
chatConfig: {},
runningUserInfo: {
teamId: 'team_1',
tmbId: 'tmb_1'
},
runningAppInfo: {
id: 'app_1'
},
chatId: 'chat_1',
uid: 'user_1',
variableState: {} as any,
externalProvider: {
openaiAccount: undefined
} as any,
lang: 'zh-CN',
requestOrigin: '',
mode: 'chat',
timezone: 'Asia/Shanghai',
retainDatasetCite: false,
maxRunTimes: 10,
workflowDispatchDeep: 0,
params: {
model: 'gpt-4'
},
stream: false,
getSubAppInfo: () => ({
name: '文件解析',
avatar: '',
toolDescription: ''
}),
getSubApp: () => undefined,
completionTools: [readFileTool],
filesMap: {}
} as any);
const result = await executeTool({
callId: 'call_read_files',
toolId: SubAppIds.readFiles,
args: '{"ids":["current-0"]}'
});
expect(dispatchFileReadMock).toHaveBeenCalledTimes(1);
expect(dispatchFileReadMock).toHaveBeenCalledWith(
expect.objectContaining({
files: []
})
);
expect(result.response).toBe('file content');
});
it('replaces agent file ids before dispatching user tools', async () => {
dispatchToolMock.mockResolvedValue({
response: 'tool response',
......
......@@ -10,7 +10,7 @@ const { formatUserQueryWithFilesMock, parseFileInfoFromUrlsMock, parseUrlToFileT
parseUrlToFileTypeMock: vi.fn()
}));
vi.mock('@fastgpt/service/core/workflow/utils/file', () => ({
vi.mock('@fastgpt/service/core/chat/fileContext', () => ({
formatUserQueryWithFiles: formatUserQueryWithFilesMock,
parseFileInfoFromUrls: parseFileInfoFromUrlsMock
}));
......
......@@ -5,7 +5,7 @@ const { getFileContentByUrlMock } = vi.hoisted(() => ({
getFileContentByUrlMock: vi.fn()
}));
vi.mock('@fastgpt/service/core/workflow/utils/file', () => ({
vi.mock('@fastgpt/service/core/chat/fileContext', () => ({
getFileContentByUrl: getFileContentByUrlMock
}));
......
......@@ -23,6 +23,7 @@ vi.mock('@fastgpt/service/core/dataset/search', () => ({
}));
vi.mock('@fastgpt/service/core/dataset/schema', () => ({
DatasetCollectionName: 'datasets',
MongoDataset: {
findById: findDatasetByIdMock
}
......
......@@ -6,7 +6,7 @@ import { DispatchNodeResponseKeyEnum } from '@fastgpt/global/core/workflow/runti
const mockparseFileContentFromUrls = vi.hoisted(() => vi.fn());
vi.mock('@fastgpt/service/core/workflow/utils/file', () => ({
vi.mock('@fastgpt/service/core/chat/fileContext', () => ({
parseFileContentFromUrls: mockparseFileContentFromUrls
}));
......
......@@ -84,7 +84,7 @@ import {
parseFileInfoFromUrls,
normalizeReadableFileUrl,
formatUserQueryWithFiles
} from '@fastgpt/service/core/workflow/utils/file';
} from '@fastgpt/service/core/chat/fileContext';
const createHumanMessage = (value: UserChatItemValueItemType[]): ChatItemMiniType => ({
obj: ChatRoleEnum.Human,
......
......@@ -836,9 +836,22 @@ describe('checkTeamDatasetSyncPermission', () => {
describe('checkTeamSandboxPermission', () => {
beforeEach(() => {
vi.clearAllMocks();
delete (global as any).subPlans;
});
it('当 global.subPlans.standard 不存在时直接返回', async () => {
const getTeamStandPlanSpy = vi.spyOn(walletUtils, 'getTeamStandPlan');
await expect(checkTeamSandboxPermission(mockTeamId)).resolves.toBeUndefined();
expect(getTeamStandPlanSpy).not.toHaveBeenCalled();
});
it('当 enableSandbox 为 false 时抛出错误', async () => {
(global as any).subPlans = {
standard: {
[StandardSubLevelEnum.basic]: {}
}
};
const mockStandard = {
standard: {
enableSandbox: false
......@@ -852,6 +865,11 @@ describe('checkTeamSandboxPermission', () => {
});
it('当 standard 不存在时不抛出错误', async () => {
(global as any).subPlans = {
standard: {
[StandardSubLevelEnum.basic]: {}
}
};
const mockStandard = {
standard: undefined
};
......@@ -861,6 +879,11 @@ describe('checkTeamSandboxPermission', () => {
});
it('当 enableSandbox 为 true 时正常通过', async () => {
(global as any).subPlans = {
standard: {
[StandardSubLevelEnum.basic]: {}
}
};
const mockStandard = {
standard: {
enableSandbox: true
......
......@@ -678,6 +678,16 @@ describe('getTeamStandPlan', () => {
delete (global as any).subPlans;
});
it('未开启标准套餐配置且没有套餐记录时返回 undefined', async () => {
vi.spyOn(MongoTeamSub, 'find').mockReturnValue({
lean: vi.fn().mockResolvedValue([])
} as any);
const result = await getTeamStandPlan({ teamId: mockTeamId });
expect(result[SubTypeEnum.standard]).toBeUndefined();
});
it('返回团队标准套餐', async () => {
const teamId = mockTeamId;
const mockPlan = {
......@@ -722,6 +732,32 @@ describe('getTeamStandPlan', () => {
expect(result[SubTypeEnum.standard]).toBeDefined();
expect(result[SubTypeEnum.standard]?.name).toBe('Basic Plan');
});
it('开启标准套餐配置但没有套餐记录时返回 undefined', async () => {
vi.spyOn(MongoTeamSub, 'find').mockReturnValue({
lean: vi.fn().mockResolvedValue([])
} as any);
(global as any).subPlans = {
standard: {
[StandardSubLevelEnum.free]: {
name: 'Free Plan',
price: 0,
totalPoints: 100,
maxTeamMember: 1,
maxAppAmount: 3,
maxDatasetAmount: 1,
maxDatasetSize: 10,
chatHistoryStoreDuration: 7,
enableSandbox: true
}
}
};
const result = await getTeamStandPlan({ teamId: mockTeamId });
expect(result[SubTypeEnum.standard]).toBeUndefined();
});
});
describe('getTeamPlanStatus', () => {
......
This diff is collapsed. Click to expand it.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment